<%
  use strict;
  use LWP::UserAgent;
  use HTML::TreeBuilder;
  use Bio::DB::XRT;
  use Bio::DB::GFF;
  use DBI;
%>

<%

  my $source = $Request->QueryString("source");
  my $keyword = $Request->QueryString("keyword");
  #remove leading and trailing spaces - btg
  $keyword =~ s/^\s+|\s+$//g;
  my $exactMatch = $Request->QueryString("exactMatch");
  $exactMatch = $exactMatch eq 'yes' ? 1: 0;

  unless (length($keyword) > 1) {
	  print "<h2>Please specify at least one keyword with length no less than two characters! [<a href='/variation'>Home</a>]</h2>\n";
	  $Response->End();
	  exit;
  }
  #xrt and gff database conenctions
  my $xrtDsn = "dbi:mysql:variation_xrt_hg17:localhost:3306";
  $xrtDsn = "dbi:mysql:variation_xrt_hg18:localhost:3306" if $source eq "hg18";
  $xrtDsn = "dbi:mysql:variation_xrt_hg19:localhost:3306" if $source eq "hg19";
  my $gffDsn = "dbi:mysql:duplication_hg17_Aug08:localhost:3306";
  $gffDsn = "dbi:mysql:duplication_hg18_Aug08:localhost:3306" if $source eq "hg18";
  $gffDsn = "dbi:mysql:duplication_hg19_Mar10:localhost:3306" if $source eq "hg19";
  
  #types and highlight features
  my $url_types = "enable=cytoband-RefGene";
  
  #read conf files
  my $conf_file = "./conf/gbrowse.conf/2.hg17.conf";
  $conf_file = "./conf/gbrowse.conf/1.hg18.conf" if $source eq "hg18";
  $conf_file = "./conf/gbrowse.conf/3.hg19.conf" if $source eq "hg19";
  
  open (INFILE, "<$conf_file") || die "Can't open $conf_file\n";
  my %stanzas;
  my %aggregators;
  $aggregators{"exon"}=1;
  my $stanza_name = "";
  while (<INFILE>){
    chomp;
    if (/^\[/) {  # name of the stanza
      s/^\[\s*//; s/\]\s+//; s/\s+$//;
      $stanza_name = $_;
      #print "$stanza_name\n";
    } else {
    if (/^feature/) {
      my @fields = split(/\s+=\s+/);
      my @s_m = split(/\s/,$fields[1]);
      foreach my $t (@s_m){
        s/^\s+//; s/\s+$//;
        #print "$t\n";
        $stanzas{$t}=$stanza_name;
      }
      #reset
      $stanza_name="";
    }
    if (/^aggregator/) {
      s/^aggregators\s+=\s+//;
      
      my @fields = split(/\s+/);
      foreach my $t (@fields){
        $t =~ s/^.*\{//;
        $t =~ s/\/.*\}$//;
        my @f = split(/,/, $t);
        foreach my $t1 (@f){
          $aggregators{$t1}=1;
        }
      }
    }else {
      next;
    }
  }
  }
  close(INFILE);
  #search bioxrt
  my $xrt = Bio::DB::XRT->new( -dsn=>$xrtDsn , -user => 'www', -pass => 'www');
  my @results;
  if ($exactMatch) {
      @results = $xrt->query_main_gref_by_keyword( -mclass => 'variation', -keyword => $keyword, -exact => 1);
  } else {
      @results = $xrt->query_main_gref_by_keyword( -mclass => 'variation', -keyword => $keyword);
  }
  my $numResults = @results;
  my $landmarkAid = $xrt->attribute2aid("Landmark");
  my $variationTypeAid = $xrt->attribute2aid("VariationType");
  my $cytobandAid = $xrt->attribute2aid("Cytoband");
  my @a2show = ( ["C1", 0, $landmarkAid],["C2", 0, $variationTypeAid],["C3", 0, $cytobandAid]);
  my %resultDetails;
  my $resultViewAllUrl = "";
  if ($numResults) {
      %resultDetails = $xrt->relational_query( -mclass => 'variation', -mrefids => \@results, -a2show => \@a2show);
      $resultViewAllUrl = "/cgi-bin/variation/tbrowse?source=$source&table=Locus&show=table&keyword=&rnum=0&flop=AND";
      $resultViewAllUrl .= "&fcol=_C2&fcomp=in&fkwd=";
      for (my $i = 0; $i < $numResults; $i++) {
##          $resultViewAllUrl .= "&fcol=_C2&fcomp==&fkwd=" . $results[$i];
          $resultViewAllUrl .= $results[$i] . ",";
      }
      $resultViewAllUrl =~ s/,$//;
      $resultViewAllUrl .= "&cols=";
  }

  #search GBrowse
  # We require that the fgroup table has full-text indexing, so let's do it
  my $gbrowseDbMaxMatch = 1000;  ## added by Junjun on Feb 23, 2007
  my $tooManyMatches = 0;
  my $dbh = DBI->connect($gffDsn,  "www", "www");
  
  ##modified by btg Aug 2008
  my $sql = "select fid from fdata f,(select gid, gname from fgroup where match (gname) against ('$keyword*' in boolean mode) > 0 having gname like '$keyword%' limit 1000) as temp where f.gid=temp.gid;";
  my $sth = $dbh->prepare($sql);
  $sth->execute();
  #my @gids;
  my @fids;

  while ( my @row = $sth->fetchrow_array ) {
      #push(@gids, $row[0]);
      push(@fids, $row[0]);
  }
  $sth->finish;
  $dbh->disconnect;

  $tooManyMatches = 1 if @fids == $gbrowseDbMaxMatch;
  my @gffEntries;
  my %url_to_add;
  my $db = Bio::DB::GFF->new( -aggregator => ['transcript', 'clone', 'alignment'],
                              -adaptor => 'dbi::mysql',
                              -dsn => $gffDsn,
                              -user => 'www',
                              -pass => 'www'
                            );

   for (my $i = 0; $i < @fids; $i++) {
     my $feature = $db->get_feature_by_id($fids[$i]);
     #next unless ($feature);  ### added by Junjun on May 19, 2006
     if (exists($aggregators{$feature->method()})){
	    next;
     }else{
	   if (($exactMatch && $feature->name() eq $keyword) || not ($exactMatch)) {
		  $url_to_add{$feature->method().":".$feature->source()} = 1;
         push(@gffEntries, $feature);
       }
     }
   }
   
  while ( my ($key, $value) = each(%url_to_add) ) {
	  #print "key: $key*****\n";
        if (exists($stanzas{$key})){
        	$url_types .= "-".$stanzas{$key};
      }
    }

  my $numGffEntries = @gffEntries;
  my $alternateChr = "";
  if ($numResults == 0 and $numGffEntries > 0) {

      for (my $i = 0; $i < @gffEntries; $i++) {
          my $thisChr = $gffEntries[$i]->location()->seq_id();
	  if ($alternateChr) {
	      if ($alternateChr eq $thisChr) {
	      } else {
	          $alternateChr = "";
		  last;
	      }
	  } else {
	      $alternateChr = $thisChr;
	  }
      }
  }

### Added by Junjun on Apr. 29, 2008.
## if there is no matches from the previous GBrowse search, then search for SNPs, eg: rsxxxxxxx, in the GFF attribute fields
  my @snpMatches = ();
  unless ($numGffEntries ) {
    if ($keyword =~ m/^rs/)
	{
      my $sql = "select distinct b.fref,b.fstart,b.fstop,c.gclass,c.gname,a.fattribute_value from fattribute_to_feature a left join fdata b on a.fid = b.fid left join fgroup c on b.gid = c.gid where match(fattribute_value) against ('$keyword' in boolean mode) > 0 limit $gbrowseDbMaxMatch;";
      my $sth = $dbh->prepare($sql);
      $sth->execute();
      while ( my $ref = $sth->fetchrow_arrayref() ) {
  	    next unless ($ref->[0] && $ref->[1] && $ref->[2] && $ref->[3] && $ref->[4]);
        push(@snpMatches, [$ref->[0],$ref->[1],$ref->[2],$ref->[3],$ref->[4],,$ref->[5]]);
      }
      $url_types .= "-snp";
      $sth->finish;
    }
  }
  $dbh->disconnect;
%>

<html>
<head>
  <title>Database of Genomic Variants: Search Results</title>
  <LINK REL=StyleSheet HREF="variation.css" TYPE="text/css">
</head>

<body bgcolor="#FFFFFF">
  <table class="master" align="center">
  <tr>
    <td><hr class="basic" align="center"></td>
  </tr>
  <tr> <td>
  <table class="organiser" align="center">
    <tr align="center">
      <td><a href="/variation"><img border="0" src="images/DGV_logo.gif" align="center"></a></td>
      
    </tr>
    <tr>
      <td>
        <br>
        <p class="header_c">Search results for keyword: <font color="red"><i><% print $keyword; %></i></font></p>
        <br>
		<p align="left">
        <!-- XRT Database results -->
        <font size="4" color="#000088">Results from Database of Genomic Variants
        (<%
	      print $numResults, " variation";
	      if ($numResults != 1) { print "s"; }
	      if ($numResults > 0) {
	        print " : ";
	        print "<a href=\"$resultViewAllUrl\">";
	        print "view them";
	        print "</a>";
	      }
	    %>)
        </font>
        </p>
        <ul class="left">
        <%
	      if ($numResults > 0) {
	        for (my $i = 0; $i < $numResults; $i++) {
	          print "<li>";
	          my $key = $results[$i] . "~-~0~-~" . $landmarkAid;
	          print "<a href='/cgi-bin/variation/xview?source=$source&view=variation&id=", $results[$i], "'>";
	          print $resultDetails{$results[$i]}{$key}[0], "</a>&nbsp;&nbsp;|&nbsp;&nbsp;";
	          my $key = $results[$i] . "~-~0~-~" . $variationTypeAid;
	          print $resultDetails{$results[$i]}{$key}[0], "&nbsp;&nbsp;|&nbsp;&nbsp;";
	          my $key = $results[$i] . "~-~0~-~" . $cytobandAid;
	          print $resultDetails{$results[$i]}{$key}[0];
	          print "</li>\n";
	        }
	      } else {
	        print "<li>";
	        print "The region that you have searched for does not contain a variation known to this database.";
	        if ($alternateChr) {
	          print " However, the region that you are looking for seem to be located on $alternateChr.";
		      print " You may click ";
		      print "<a href=\"/cgi-bin/variation/tbrowse?source=$source&table=Locus&show=table&keyword=&flop=AND&fcol=_C19&fcomp==&fkwd=$alternateChr&cols=\">";
		      print "here</a> to view all the entries on $alternateChr. ";
	        } elsif ($numGffEntries > 0 || @snpMatches > 0) {
	          print " However, there seem to be multiple matches found in the Genome Browser. Please see the section below for details. You may wish to use one of the the terms listed below to search the database again.";
	        }
	        print "</li>";
	      }
	    %>
        </ul>
        <br>
        <p align=left>
        <!-- GBrowse Result -->
        <font size="4" color="#000088">Results from Genome Browser
          (<% print $numGffEntries || @snpMatches + 0; %> record<%
		  if ($numGffEntries > 1 || @snpMatches > 1) { print "s"; } ;
		  print ", not all matches are shown" if $tooManyMatches; %>)
        </font>
        <ul class="left">
          <%
	      if ($numGffEntries > 0) {
	        for (my $i = 0; $i < @gffEntries; $i++) {
	          my $feature = $gffEntries[$i];
	          #added by BTG to change clone names to locations
	          my $l_temp_string = $feature->location()->seq_id().":";
	          $l_temp_string .= $feature->start."..";
	          $l_temp_string .= $feature->end;
	          #my $temp_string = $feature->name();
	          print "<li>";
	          print $feature->name()."|";
		      print $feature->class()."|";
	          print "<a href='/cgi-bin/variation/gbrowse/$source?name=", $l_temp_string , ";", $url_types, ";h_feat=",$keyword,"'>";
	          print "$l_temp_string </a>";
	          print "</li>\n";
	        }
	      } elsif (@snpMatches > 0) {
		    foreach (@snpMatches) {
			  my $temp_string = "$_->[0]:$_->[1]..$_->[2]";
			  print "<li>";
			  print "$_->[0]|".uc($_->[3])."|";
			  print "<a href='/cgi-bin/variation/gbrowse/$source?name=", $temp_string , ";", $url_types, ";h_feat=",$keyword, "'>";
			  print "$temp_string </a>";
			  print "</li>\n";
		    }
	      } else {
	        print "<li>Unfortunately, no matches found. ";
	        print "Please try using another equivalent search term or ";
	        print "<a href=\"mailto:lfeuk\@sickkids.ca\">email us </a>";
	        print "and we will try our best to help.";
	        print "</li>\n";
	      }
	      %>
        </ul>
        </p>
      </td>
    </tr>
  </table>
  <tr>
    <td align="center">
      <br><a href="/variation/">Home</a>
      <br><hr class="basic" align="center"><br>
    </td>
  </tr>
  </td></tr>
</table>
</body>
</html>

