package SGN::View::Mason::Commands;
use strict;
use vars qw($m $c);
HTML::Mason::Component::FileBased->new(
'code' => sub {
use utf8; local $SGN::View::Mason::Commands::m = $HTML::Mason::Commands::m;
HTML::Mason::Exception::Params->throw
    ( error =>
      "Odd number of parameters passed to component expecting name/value pairs"
    ) if @_ % 2;
my ( $preselected_database, $preselected_category, $dataset_groups, $databases, $programs, @input_options, @parse_options, $preload_seq, $preload_id, $preload_type, $blast_db_icons );
{
    my %pos;
    for ( my $x = 0; $x < @_; $x += 2 )
    {
        $pos{ $_[$x] } = $x + 1;
    }

    foreach my $arg ( qw( programs blast_db_icons ) )
    {
        HTML::Mason::Exception::Params->throw
            ( error => "no value sent for required parameter '$arg'" )
                unless exists $pos{$arg};
    }
#line 6 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
     $preselected_database = exists $pos{'preselected_database'} ? $_[ $pos{'preselected_database'} ] :  '';
#line 7 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
     $preselected_category = exists $pos{'preselected_category'} ? $_[ $pos{'preselected_category'} ] :  '';
#line 8 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
     $dataset_groups = exists $pos{'dataset_groups'} ? $_[ $pos{'dataset_groups'} ] :  [];
#line 9 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
     $databases = exists $pos{'databases'} ? $_[ $pos{'databases'} ] :  {};
#line 10 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
    $programs = $_[ $pos{'programs'} ];
#line 11 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
     @input_options = exists $pos{'input_options'} ? HTML::Mason::Tools::coerce_to_array( $_[ $pos{'input_options'} ], '@input_options') :  ();
#line 12 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
     @parse_options = exists $pos{'parse_options'} ? HTML::Mason::Tools::coerce_to_array( $_[ $pos{'parse_options'} ], '@parse_options') :  ();
#line 13 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
     $preload_seq = exists $pos{'preload_seq'} ? $_[ $pos{'preload_seq'} ] :  '';
#line 14 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
     $preload_id = exists $pos{'preload_id'} ? $_[ $pos{'preload_id'} ] :  '';
#line 15 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
     $preload_type = exists $pos{'preload_type'} ? $_[ $pos{'preload_type'} ] :  '';
#line 16 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
    $blast_db_icons = $_[ $pos{'blast_db_icons'} ];
}
$m->debug_hook( $m->current_comp->path ) if ( HTML::Mason::Compiler::IN_PERL_DB() );

#line 1 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->print( '
' );
#line 4 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->print( '
' );
#line 18 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->print( '
' );
#line 19 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"

  use JSON::Any;

  my $menus = {};
  foreach my $d (keys(%$databases)) {
     $menus->{$d} = $m->scomp('/page/html_select.mas', name=>"database", params=>"class\=\"form-control input-sm blast_select_box\"", id=>"database", choices => [ @{$databases->{$d}} ], selected=> $preselected_database );
  }

  my $substitution_matrix_select = $m->scomp('/page/html_select.mas',
     name=>"matrix",
     params=>"class\=\"form-control input-sm blast_select_box\"",
     id=>"matrix",
     choices => [  [ 'BLOSUM62', 'BLOSUM62 (default)' ],
                   [ 'BLOSUM80', 'BLOSUM80 (recent divergence)' ],
  		 [ 'BLOSUM45', 'BLOSUM45 (ancient divergence)' ],
  		 'PAM30',
  		 'PAM70',
  ]);

  #print STDERR "INPUT OPTIONS: ".Data::Dumper::Dumper(\@input_options);
  my $input_options_select = $m->scomp('/page/html_select.mas', name=>'input_options', params=>"class\=\"form-control input-sm blast_select_box\"", id=>'input_options',
    choices => \@input_options, selected=>$preload_type,
  );

  # array with the preselected output formats
  my @parse_options_selected = ();
  # foreach my $po (@parse_options) {
    # if ($po->[0] eq "SGN") {
      # push @parse_options_selected, $po;
    # }
    # if (($po->[0] ne "Basic") && ($po->[0] ne "Table") && ($po->[0] ne "Coverage") && ($po->[0] ne "SGN")) {
      # push @parse_options_selected, $po;
    # }
  # }

  # output formats alphabetically sorted
  my $parse_options_select = $m->scomp('/page/html_select.mas', name=>'parse_options', id=>'parse_options', params=>"size = \"5\" class\=\"form-control input-sm blast_select_box\"", choices => [sort (map { $_->[0] } @parse_options) ], multiple=>1, selected=> [ sort ( map { $_->[0] } @parse_options_selected) ] );

  # print STDERR "OUTPUT OPTIONS: ".(join ", ", (sort (map { $_->[0] } @parse_options) ))."\n";
  # print STDERR "OUTPUT SELECTED: ".(join ", ", (sort (map { $_->[0] } @parse_options_selected) ))."\n";

  my $db_type = {};

  foreach my $g (keys %$databases) {
    foreach my $d (@{$databases->{$g}}) {
       $db_type->{$d->[0]} = $d->[2];
    }
  }
  my $database_type_json = JSON::Any->encode($db_type);

  #print STDERR Data::Dumper::Dumper($database_type_json);

  my $q_type = {};
  my $q_example = {};

  foreach my $t (@input_options) {
    $q_type->{$t->[1]} = $t->[2];
    $q_example->{$t->[1]} = $t->[3];
  }

  #print STDERR Data::Dumper::Dumper($q_type);
  my $input_option_json = JSON::Any->encode($q_type);
  my $input_example_json = JSON::Any->encode($q_example);

  my $preload = "";
  if ($preload_id && $preload_type) {
    $preload = $preload_id;
  }

  if ($preload_seq) {
    $preload = $preload_seq;
  }

  #print STDERR "MENUS: ".Data::Dumper::Dumper($menus);

#line 95 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->print( '
<!-- script src="/js/kinetic-v5.1.0/kinetic-v5.1.0.min.js"></script -->
' );
#line 97 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->comp(   '/util/import_javascript.mas', classes => [ 'tools.blast_canvas_graph', 'CXGN.Blast' , 'jqueryui', 'kinetics.kinetic']   
); #line 97 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->print( '


' );
#line 100 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->comp(   '/page/page_title.mas', title=>"BLAST"   
); #line 100 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->print( '

<div style="border-style:dotted;border-width:1px;margin-bottom:20px;"><div style="margin-top:10px;margin-right:10px;margin-bottom:10px;margin-left:10px;" ><b>Note</b>: To match shorter sequences:
<div>
<ul>
<li>Consider setting the word_size to a lower value.</li>
<li>You may have to set the evalue to a higher value.</li>
</ul>
</div>
The default word_size for blastn is 11 and for protein-based blast programs it is 3. The correct default will be set automatically when changing the program.</div></div>

' );
#line 111 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->comp( { content => sub {
#line 111 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->print( '
  <input type="hidden" name="outformat" value="0" />

  <div class="form-group form-inline">
        <label class="blast_select_label" for="dataset_select">Categories &nbsp; </label>

    ' );
#line 117 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->comp(   '/page/html_select.mas', name=>'Datasets', choices=> $dataset_groups, id=>"dataset_select", name=>"dataset_select", params=>"class\=\"form-control input-sm blast_select_box\"", selected=>"$preselected_category"   
); #line 117 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->print( '

' );
#line 119 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
 if ($blast_db_icons) {
$m->print( '<!-- images to get direct access to the model plant dbs. db_id is the blast_db_id on the SGN database -->
<img class="fruit_icon" height="25" src="/documents/img/blast_icons/tomato.png" db_id="34" title="Tomato current DB">
<img class="fruit_icon" height="18" src="/documents/img/blast_icons/potato.png" db_id="10" title="Potato current DB">
<img class="fruit_icon" height="25" src="/documents/img/blast_icons/pepper.png" db_id="21" title="Pepper current DB">
<img class="fruit_icon" height="25" src="/documents/img/blast_icons/eggplant.png" db_id="29" title="Eggplant current DB">
<img class="fruit_icon" height="25" src="/documents/img/blast_icons/benth.png" db_id="9" title="N.benthamiana current DB">
' );
#line 126 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
 }
$m->print( '
<br>

    <label class="blast_select_label" for="database_select">Database &nbsp; </label>

    <span id=\'database_select\' ></span><a style="font-size: 80%" title="View details of each database" href="/tools/blast/dbinfo">db details</a>
    <br>

    <label class="blast_select_label" for="program_select">Program &nbsp; </label>
    ' );
#line 136 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->comp(   '/page/html_select.mas', name=>'program_select', id=>'program_select', params=>"class\=\"form-control input-sm blast_select_box\"", choices=>$programs   
); #line 136 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->print( '
    <br>

    <label class="blast_select_label" for="input_example">Query &nbsp; </label>
    ' );
#line 140 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->print(  $input_options_select  );
#line 140 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->print( '<a id="input_example" style="font-size: 80%;cursor:pointer;">Show example</a>
  </div>

    <textarea name="sequence" id="sequence" class="form-control" rows="8" cols="80">' );
#line 143 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->print(  $preload  );
#line 143 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->print( '</textarea></td></tr>
    <br>

        <div style="text-align: center">
          <button class="btn btn-primary" id="submit_blast_button" type="submit" name="search" value="Search">BLAST</button>
        </div>
        <br />

' );
#line 151 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->comp( { content => sub {
#line 151 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->print( '

	' );
#line 153 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->comp(   '/page/info_table.mas', title=>'', data => [
	  'Expect (e-value) Threshold' => '<input id="evalue" class="blast_select_box form-control" type="text" size="6" value="1e-10" name="expect" />',
	  'Word Size (default 11, use lower value for shorter sequences)' => '<input id="word_size" class="blast_select_box form-control" type="text" size="6" value="11" name="word_size" />',
	  'Substitution Matrix' => $substitution_matrix_select,
		'Output Options' => $parse_options_select,
	  'Max. hits to show' => '<input id="maxhits" class="blast_select_box form-control" type="text" name="maxhits" size="6" value="20" />',
	  'Filters' => '<div class="checkbox"><label><input type="checkbox" name="filterq" checked="1" /> Filter Low complexity</label>',
	  __multicol => 2,
	  __border => 0,
	  __tableattrs => 'width="80%"',
	]
	  
); #line 164 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->print( '

' );
#line 166 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
} }, '/page/info_section.mas', title=>"Advanced Options", collapsible=>1, collapsed =>1, is_subsection=>"1"
 );
#line 166 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->print( '

' );
#line 168 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
} }, '/page/info_section.mas', id=>"input_parameter_section", title=>"Input parameters", collapsible=>1, collapsed=>0, subtitle=>'<a class="btn btn-link pull-right" href="/help/tools/blast" target="_blank">Help <span class="glyphicon glyphicon-question-sign"></span></a>'
 );
#line 168 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->print( '

' );
#line 170 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->comp( { content => sub {
#line 170 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->print( '
<br>
<div class="form-inline form-group" style="width:100%">
  <input class="form-control" value="putrescine" id="blast_desc"/>
  <button id="search_by_desc" class="btn btn-primary">Search</button>
  </div>
  <div class="well well-sm">Select a BLAST database and paste a gene name or description. Eg: Solyc07g063040 or ribulose</div>
' );
#line 177 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
} }, '/page/info_section.mas', title=>"Extract sequences from BLAST databases", collapsible=>1, collapsed =>1
 );
#line 177 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->print( '

' );
#line 179 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->comp( { content => sub {
#line 179 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->print( '

  <div id="prereqs"></div>

  <center>
    <div id="sgn_blast_graph" style="display:none">
        <div id="myCanvas">
          Your browser does not support the HTML5 canvas
        </div>
    </div>
  </center>
  <br>
  <div id="SGN_output"></div>
  
  <div id="Overview_output"></div>
  <div id="Coverage_output"></div>
  <div id="Table_output" style="min-width:900px;"></div>
  <div id="Bioperl_output"></div>
  <div id="Basic_output"></div>

' );
#line 199 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
} }, '/page/info_section.mas', title=>"Results", collapsible=>1, collapsed=>0, subtitle=>'<a id="download_table" class="download_tag" target="_blank" href="javascript:download_table();" title="Download results in tabular format">Table&#8675;</a>&nbsp;&nbsp;<a id="download_basic" class="download_tag" target="_blank" href="javascript:download();" title="Download results in basic format">Basic&#8675;</a>'
 );
#line 199 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->print( '



<div id="jobid" style="display:none;"></div>

<div class="modal fade" id="myModal" role="dialog">
  <div class="modal-dialog modal-sm">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title">BLAST status</h4>
      </div>
      <div class="modal-body">
        <center>
          <img src="/img/wheel.gif" />
          <p id="bs_working_msg"></p>
        </center>
      </div>
    </div>

  </div>
</div>


<div class="modal fade" id="search_modal" role="dialog">
  <div class="modal-dialog modal-lg">

    <!-- Modal content-->
  <div class="modal-content">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal">&times;</button>
      <h4 class="modal-title">Search Result</h4>
    </div>

    <div class="modal-body">
      <p id="search_output" style="max-height:300px;overflow:scroll;"></p>
    </div>

    <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    </div>
  </div>

  </div>
</div>



<script>


jQuery(document).ready(function() {

	//function to select category and database when click on fruit images
	jQuery(\'.fruit_icon\').click( function() {
		var db_id = jQuery(this).attr(\'db_id\');

		jQuery(\'#dataset_select\').val(db_id);
		swap_menu(db_id);
	});

	swap_menu(jQuery(\'#dataset_select\').find(\':selected\').attr(\'value\'));

	jQuery(\'#sequence\').attr(\'spellcheck\',false);

	jQuery(\'#dataset_select\').change( function() {
	  var selectedId = jQuery(\'#dataset_select\').find(\':selected\').attr(\'value\');
	  swap_menu(selectedId);
	});

	jQuery(\'#program_select\').change( function() {
	  var selected_program = jQuery(\'#program_select\').find(\':selected\').attr(\'value\');
	  if (selected_program === \'blastp\' || selected_program === \'blastx\' || selected_program === \'tblastn\' || selected_program === \'tblastx\') {
	     jQuery(\'#word_size\').val("3");
	  }
	  if (selected_program === \'blastn\') { jQuery(\'#word_size\').val("11"); }
	});

	var input_examples = eval(' );
#line 279 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->print(  $input_example_json  );
#line 279 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->print( ');
	var database_types = eval (' );
#line 280 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->print(  $database_type_json  );
#line 280 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->print( ');
	var input_option_types = eval(' );
#line 281 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->print(  $input_option_json  );
#line 281 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->print( ');

	jQuery(\'#input_example\').click( function() {
	  jQuery(\'#sequence\').html( input_examples[jQuery(\'#input_options\').val() ] );
	});

	jQuery(\'#submit_blast_button\').click( function() {
		var pasted_seq = jQuery(\'#sequence\').val();

		if (pasted_seq.match(/>/)) {
			var seq_num = pasted_seq.match(/>/g);

			if (seq_num.length <= 20 ) {
			  run_blast(database_types, input_option_types);
			} else {
				alert("Please, do not run more than 20 sequences");
			}
		} else {
			run_blast(database_types, input_option_types);
		}
	});

	//search blastdb by description
	jQuery(\'#search_by_desc\').click( function() {
		var database = jQuery(\'#database\').val();
		var blast_desc = jQuery(\'#blast_desc\').val();
		// alert("db: "+database+", blast_desc: "+blast_desc);
    disable_ui();

		jQuery.ajax( {
			url: \'/tools/blast/desc_search/\',
			method: \'POST\',
			data: {\'blast_desc\':blast_desc, \'database\': database},
			success: function(response) {
        enable_ui();
        $(\'#search_output\').html(response.output_seq);
        $(\'#search_modal\').modal("show");

			},
			error: function(response) {
				alert("An error occurred. The service may not be available right now.");
				enable_ui();
				return;
			}
		});

	});

	function swap_menu(group_id) {
	  var menu_html = eval( ' );
#line 330 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->print(  JSON::Any->encode($menus)  );
#line 330 "/home/production/cxgn/sgn/mason/tools/blast/index.mas"
$m->print( ');
	  jQuery(\'#database_select\').html(menu_html[group_id]);
	}

});

</script>


<!-- STYLE -->
<style>

h1 {
  display:none;
}

.seq_map {
	color: #777777;
	width: 700px;
	position:relative;
	overflow: auto;
	align: left;
}

.blast_select_box {
  width:300px;
  margin-right:10px;
}

.blast_select_label {
  width:100px;
  margin-right:10px;
  line-height: 32px;
}

.ui-dialog {
	position:relative;
}

#region_square {
	position:absolute;
	vertical-align:middle;
}
.help_dialog {
	color:blue;
	cursor:pointer
}
#desc_dialog {
	overflow: auto;
	position: relative;
}
.help_box {
	background-color:#EEEEFE;
	border-color:#AAA;
	border-width:2px;
	border-style:solid;
	border-radius:5px;
	padding-left: 10px;
	padding-right: 10px;
}

#sequence {
  min-height: 80px;
  max-height: 300px;
/*  min-width: 700px;*/
  max-width: 98%;
}

.download_tag {
  display:none;
}

/* BLAST canvas Graph */

.width-1000 {
  width: 1000px;
  text-align: center;
}

#sgn_blast_graph {
  overflow:hidden;
}

#myCanvas {
/*  border-style: solid;*/
/*  border-width: 1px;*/
/*  border-color: #ddd;*/
/*  border-width:0px 1px 1px 1px;*/
  height:450px;
  width:1020px;
  overflow:scroll;
  overflow-x: hidden;
}


</style>
' );
;return;
},
'declared_args' => {
  '$blast_db_icons' => { default => undef },
  '$databases' => { default => ' {}' },
  '$dataset_groups' => { default => ' []' },
  '$preload_id' => { default => ' \'\'' },
  '$preload_seq' => { default => ' \'\'' },
  '$preload_type' => { default => ' \'\'' },
  '$preselected_category' => { default => ' \'\'' },
  '$preselected_database' => { default => ' \'\'' },
  '$programs' => { default => undef },
  '@input_options' => { default => ' ()' },
  '@parse_options' => { default => ' ()' }
},
'load_time' => 1763121823,

)
;