Beautiful Code [186]
The table can be sorted a number of ways. In this case, it is sorted by proximity to the selected gene, SYP. SYP is a gene involved with the release of neurotransmitters.
The Design of the Gene Sorte > Maintaining a Dialog with the User over the Web
13.2. Maintaining a Dialog with the User over the Web
The Gene Sorter is a CGI script. When the user points her web browser to the Gene Sorter's URL (http://genome.ucsc.edu/cgi-bin/hgNear), the web server runs the script and sends the output over the network. The script's output is an HTML form. When the user hits a button on the form, the web browser sends a URL to the web server that includes the values in the drop-down menus and other controls encoded as a series of variable=value pairs. The web server runs the script once more, passing the variable=value pairs as input. The script then generates a new HTML form in response.
CGI scripts can be written in any language. The Gene Sorter script is actually a moderately large program written in C.
A CGI script has both advantages and disadvantages compared to other programs that interact with users on the desktop. CGI scripts are quite portable and do not need different versions to support users on Windows, Macintosh, and Linux desktops. On the other hand, their interactivity is only modest. Unless one resorts to JavaScript (which introduces serious portability issues of its own), the display will be updated only when the user presses a button and waits a second or two for a new web page. However, for most genomic purposes, CGI provides an acceptably interactive and very standard user interface.
The lifetime of a CGI script is very limited. It starts in response to the user clicking on something and finishes when it generates a web page. As a consequence, the script can't keep long-term information in program variables. For very simple CGI scripts, all of the necessary information is stored in the variable=value pairs (also known as CGI variables).
However, for more complex scripts such as the Gene Sorter, this is not sufficient because the script might need to remember the result of a control that the user set several screens back, but the web server sends only the CGI variables corresponding to the controls in the most recently submitted page. Our CGI scripts therefore need a way to store data for the long term.
There are two mechanisms CGI provides for storing data not visible in a form's controls: hidden CGI variables and cookies. In a hidden CGI variable, the data is stored in the HTML in the form of tags of type hidden. With cookies, the data is stored by the web browser and sent in the HTTP header.
Cookies were somewhat controversial when they were first released, and some users would disable them. However, cookies can persist for years, while hidden variables disappear as soon as a web page is closed. Neither cookies nor hidden variable can store truly large amounts of data. The exact amount varies between web browsers, but generally it's not safe to try to save more than 4 KB of data via these mechanisms.
To exploit both cookies and hidden variables, the Gene Sorter team developed a "cart" object that integrates the cookie and hidden variable mechanisms with an SQL database. The cart maintains two tables, one associated with a user and one associated with a web session. The tables are of the same format, consisting of a key column, a blob field containing all of the variable=valuepairs in the same format they are passed in the URL, and fields that track the usage times and access counts.
A key into the user table is kept in a persistent cookie, and a key into the session table is kept in a hidden CGI variable. On startup, the script looks for the user key in a cookie. If it finds it, it loads the associated variable=value pairs into a hash table. If it doesn't find the cookie, it generates a new user key, and the hash table remains empty.
Next, the script looks for the session key and loads the variables from it, replacing any variables that are already in the