#!/usr/local/bin/perl -w
# Script to perform a systematic review of OMIM.
# Rudolf Cardinal, June 2008.


# ---------------------------------------------------------------------------
# Subroutine to prompt user for variables in the next section

sub ask_user {
  print "$_[0] [$_[1]]: ";
  my $rc = <>;
  chomp $rc;
  if($rc eq "") { $rc = $_[1]; }
  return $rc;
}

use LWP::Simple;

my $utils = "http://www.ncbi.nlm.nih.gov/entrez/eutils";

my $db = "omim";
my $query  = ask_user("Query",    "(psychosis) OR (psychoses) OR (psychot*) OR (hallucinat*) OR (\"thought disorder\") OR (delusion*) OR (schizophren*)"); # query (example)
my $idtype = "uilist";
my $summarytype = "docsum";
my $fulltype = "detailed";
my $directory = ask_user("Directory for results", "d:/working_psychosis/XX_pubmed_perl/omim_results");
my $summaryfile = "summary.txt"; # tab-separated
my $indexfile = "index.html";

# ---------------------------------------------------------------------------
# Find date/time.

my $current_date = localtime();

# ---------------------------------------------------------------------------
# Search.

print "Searching remote database...\n";
my $esearch = "$utils/esearch.fcgi?" .
              "db=$db&retmax=1&usehistory=y&term=";

my $esearch_result = get($esearch . $query);

$esearch_result =~ 
  m|<Count>(\d+)</Count>.*<QueryKey>(\d+)</QueryKey>.*<WebEnv>(\S+)</WebEnv>|s;

my $Count    = $1;
my $QueryKey = $2;
my $WebEnv   = $3;

print "Number of records found: $Count\n";

# NOTE: NO ERROR CHECKING ON MAX (WHICH IS 10,000)

# ---------------------------------------------------------------------------
# Fetch each one.
# You can bulk fetch, or fetch individually (using retstart and retmax).
# But individual fetching is very slow.
# If you use it, retstart should run from 0 to (count-1).
# HTML format fetch brings little benefit - only topped and tailed, not HTML-formatted for each one.

# See http://www.ncbi.nlm.nih.gov/entrez/query/static/efetchlit_help.html

print "Fetching from remote database...\n";
my $efetch_id = "$utils/efetch.fcgi?" .
                 "rettype=$idtype&retmode=text" .
                 "&db=$db&query_key=$QueryKey&WebEnv=$WebEnv";
my $efetch_result_id = get($efetch_id);
my $efetch_full = "$utils/efetch.fcgi?" .
                  "rettype=$fulltype&retmode=text" .
                  "&db=$db&query_key=$QueryKey&WebEnv=$WebEnv";
my $efetch_result_full = get($efetch_full);
my $efetch_docsum = "$utils/efetch.fcgi?" .
                  "rettype=$summarytype&retmode=text" .
                  "&db=$db&query_key=$QueryKey&WebEnv=$WebEnv";
my $efetch_result_docsum = get($efetch_docsum);


# ---------------------------------------------------------------------------
# Chop into pieces.

print "Processing...\n";

my @uid = split(/\n/, $efetch_result_id);

my @docsum = split(/^\d+:\s+\W\d+\s*/m, $efetch_result_docsum);

# This works (with a redundant entry at the start):
#
# my @description = split(/^\d+:\s+\W\d+\s*/m, $efetch_result_full);
#
# beginning of line
# number
# colon
# whitespace
# symbol of some sort
# number
# possible whitespace
# end of line
# [and to do beginning/end of line matching need the "/m" modifier]
# the 'm' modifier treats it as multiline, so '^' applies to lines within the string, not the whole string
# + = 1 or more
# * = 0 or more
# ^ = beginning of line
# $ = end of line
# \d = digit
# \s = whitespace
# \W = non-word = non-(alphanumeric plus "_")

# This also works, with a redundant entry at the end:
my @description = split(/^Copyright \(c\) 1966-2008 Johns Hopkins University$/m, $efetch_result_full);

print "Number of UIDs: " . @uid . "\n";
print "Number of titles: " . (@docsum-1) . "\n";
print "Number of descriptions: " . (@description-1) . "\n";

# ---------------------------------------------------------------------------
# Squirt to files.

chdir($directory);
open(SUMMARYFILE, ">$summaryfile");
print SUMMARYFILE "Reviewed\tNumber\tOMIM_ID\tDescription\n";
open(INDEXFILE, ">$indexfile");

print INDEXFILE "<html>\n";
print INDEXFILE "<head>\n";
print INDEXFILE "<title>Search of database $db</title>\n";
print INDEXFILE "<body>\n";
print INDEXFILE "<b>\n";
print INDEXFILE "Search of database: $db<br>\n";
print INDEXFILE "On: $current_date<br>\n";
print INDEXFILE "Query: $query<br>\n";
print INDEXFILE "</b><br><br>\n";
print INDEXFILE "<table border=1 cellspacing=0 cellpadding=5>\n";
print INDEXFILE "  <tr>\n"
              . "    <td><b>Result</b></td>\n"
              . "    <td><b>OMIM ID</b></td>\n"
              . "    <td><b>Description</b></td>\n"
              . "  </tr>\n";

for (my $loop = 0; $loop < @uid; $loop++) {
  my $onelinedocsum = $docsum[$loop+1];
  $onelinedocsum =~ s/\n/\; /g; # replace all \n with "; "
  print SUMMARYFILE "\t" . ($loop+1) . "\t" . $uid[$loop] . "\t" . $onelinedocsum . "\n";
  
  print INDEXFILE "  <tr>\n"
                . "    <td>" . "<a href=\"" . ($loop+1) . ".html\">" . ($loop+1) . "</a></td>\n"
                . "    <td>" . "<a href=\"" . ($loop+1) . ".html\">" . $uid[$loop] . "</a></td>\n"
                . "    <td>" . "<a href=\"" . ($loop+1) . ".html\">" . $onelinedocsum . "</a></td>\n"
                . "  </tr>\n";
  
  open(HTMLFILE, ">" . ($loop+1) . ".html");
  print HTMLFILE "<html>\n";
  print HTMLFILE "<head>\n";
  print HTMLFILE "<title>Item " . ($loop+1) . ": OMIM $uid[$loop]</title>\n";
  print HTMLFILE "</head>\n";
  print HTMLFILE "<body>\n";
  if ($loop > 0) {
    print HTMLFILE "<a href=\"" . $loop . ".html\">Previous</a>\n";
  }
  if ($loop > 0 && $loop < @uid - 1) {
    print HTMLFILE " | ";
  }
  if ($loop < @uid - 1) {
    print HTMLFILE  "<a href=\"" . ($loop+2) . ".html\">Next</a>\n";
  }
  print HTMLFILE " | <a href=\"index.html\">Index</a>\n";
  print HTMLFILE "<br><br>\n";
  $docsum[$loop+1] =~ s/\n\n/\n/g;
  $docsum[$loop+1] =~ s/\n/\<br\>\n/g;
  print HTMLFILE "<b>Result " . ($loop+1) . " of " . @uid . ": <a href=http://www.ncbi.nlm.nih.gov/entrez/dispomim.cgi?id=" . $uid[$loop] . ">OMIM " . $uid[$loop] . "</a><br>" . $docsum[$loop+1] . "</b><br><br>\n";
  $description[$loop] =~ s/\n\n/\<br\>\<br\>\n/g; # replace all \n\n with <br><br>\n
  print HTMLFILE $description[$loop];
  print HTMLFILE "</body>\n";
  print HTMLFILE "</html>\n";
  close(HTMLFILE);  
}

print INDEXFILE "</table>\n";
print INDEXFILE "</body>\n";
print INDEXFILE "</html>\n";

print "Done.\n";
