#!/bin/tcsh -f
#
# Author: Ram Samudrala (me@ram.org)
#
# September 15, 2005.
#
# csv2html interaction-file [function-list-file]
#
# This program takes two comma-separated value (csv) files as input
# and generates an HTML page that can be used with the Bioverse
# viewer. The first file specifies the list of interactions in this
# form:
#
# molecule1,molecule2,confidence
# molecule2,molecule3,confidence
# molecule3,molecule1,confidence
#
# where confidence is a value ranging from 0 to 1 indicating the
# strength of the relationship between the two molecules denoted by
# a single-word identifier. 
#
# The second file, which is optional, is a list of descriptions (such
# as functional descriptions) for the interacting molecules from the
# first file. This is specified in this form:
#
# molecule1,description1
# molecule2,description2
# molecule3,description3
#
# where description may be an arbitrary string containing any of the
# 26 letters in the English alphabet.
#
#
##########################################################################

set arg0 = `echo $0 | awk -F'/' '{print $NF}'`

if ($#argv < 1) then
  echo "Usage: $arg0 interaction-file [function-list-file]"
  exit
endif

set interaction_file = $1

set function_list_file = ""
if ($#argv == 2) then
  set function_list_file = $2
endif

##########################################################################

awk -F',' '{print $1}' $interaction_file > /tmp/csv2html.$$
awk -F',' '{print $2}' $interaction_file >> /tmp/csv2html.$$
set list = `sort /tmp/csv2html.$$ | uniq`
rm /tmp/csv2html.$$

##########################################################################

cat <<EOF
<title> Bioverse viewer </title>
<h1> Bioverse viewer </h1> <hr>

<table>
<tr>
<td width=800></td><td width=10></td><td width=150></td>
</tr>
<tr><td>
<applet codebase="src" code="org.compbio.bioverse.viewer.ViewerApplet.class" width=1000 height=580 name="viewer">
EOF

##########################################################################

echo -n '<param name=functionlist value="'
foreach x ($list)
  echo -n $x'#'$x,
end
echo '">'

echo -n '<param name=interaction value="'
awk -F',' '{printf "%s-%s#%s,", $1, $2, $3}' $interaction_file
echo '">'

if ($function_list_file != "") then
  echo -n '<param name=functionkey value="u#insert-default-url-here,'
  foreach x ($list)
     grep ^"$x," $function_list_file >& /dev/null
     if ($status == 1) then
        set function = "unknown"
     else
       set num_lines =  `grep ^"$x," $function_list_file | wc -l | awk '{print $1}'`
       set i = 1
       while ($i <= $num_lines)
         set function = `grep ^"$x," $function_list_file | awk -F',' '{print $2}' | head -$i | tail -1`
         echo -n $x'#'$function,
         @ i++
       end
     endif
  end
  echo '">'
else
  echo '<param name=functionkey value="u#insert-default-url-here,">'
endif

##########################################################################

cat <<EOF
<center><br><hr><h2>Your browser does not appear to support
Java. <br>Please <a href="http://java.sun.com/downloads/">click
here</a> for the latest Java and JRE releases for your platform or
enable Java in your browser's preferences.<br> The Bioverse Network
Browser works best with JRE 1.4.2 or better<hr></h2></center>
</applet>

<br>
<font size=-2 color=gray>
<center>
Please <a href="http://java.sun.com/downloads/">click here</a> for the
latest Java and JRE releases for your platform or enable Java in your
browser's preferences.
<br> The Bioverse viewer works best with JRE 1.4.2 or better.
</center>
</font>
</td>

<td></td>

<td>
<center>
<img src="images/colorKey.jpg">
<br><br>
<img src="images/mouseKey.jpg">
</center>
</td>
</tr>

<tr><td colspan=3><iframe width=100% height=500 src="http://bioverse.compbio.washington.edu/?esPick=1" name="bioverse"></td></tr></table>

<hr>
EOF

cat .signature
echo '<hr>'

##########################################################################
