Approaches to Web Development for Bioinformatics

Previous  Contents  Next
References

Processing HTML Forms

Although CGI can invoke any program, the CGI Perl module18 can be used to simplify parsing of the query string containing user input parameters and sending the HTTP request back to the browser.  (Rather than the Apache module that I mentioned above this is a Perl module to make CGI programming easier.) One of the central aspects of this web application is displaying and processing forms.  Here is an example of a Perl script that displays a group of three radio buttons and processes the result of the user's selection.  You can try the script out at radio_example.pl.


#!/usr/bin/perl
# A script to demonstrate a simple form with radio buttons
use CGI;

my $query = new CGI;

print $query->header,
$query->start_html('Simple Form Example with Radio Buttons'),
$query->h1('Simple Form Example with Radio Buttons');

# Display the form
%labels = (
'one'=>'The first choice',
'two'=>'The second choice',
'three'=>'The third choice');
print
$query->start_multipart_form,
"Please choose your option", $query->br,
$query->radio_group(-name=>'choices',
-values=>['one', 'two', 'three'],
-default=>'two',
-linebreak=>'true',
-labels=>\%labels),
$query->p,
$query->submit('Submit'),
$query->end_form,
$query->p;

# Process the form
if ($query->param('choices')) {

# retrieve and print the value of the radio button
my $radio_value = $query->param( 'choices');
print "You chose $radio_value, whose label is '$labels{$radio_value}'.";

}

print $query->end_html;

Here is an example of the RNA translation script described above converted into a CGI program to display a HTML interface to translate RNA to an amino acid sequence.  The page can be tried out at translate_form.pl.


#!/usr/bin/perl

use CGI;

# Hash table for codon to amino acid translation
my %rna_to_amino_acid = (
uuu => F, uuc => F, uua => L, uug => L,
ucu => S, ucc => S, uca => S, ucg => S,
uau => Y, uac => Y, uaa => "--STOP--", uag => "--STOP--",
ugu => C, ugc => C, uga => "--STOP--", ugg => W,
cuu => L, cuc => L, cua => L, cug => L,
ccu => P, ccc => P, cca => P, ccg => P,
cau => H, cac => H, caa => Q, cag => Q,
cgu => R, cgc => R, cga => R, cgg => R,
auu => I, auc => I, aua => I, aug => M,
acu => T, acc => T, aca => T, acg => T,
aau => N, aac => N, aaa => K, aag => K,
agu => S, agc => S, aga => R, agg => R,
guu => V, guc => V, gua => V, gug => V,
gcu => A, gcc => A, gca => A, gcg => A,
gau => D, gac => D, gaa => E, gag => E,
ggu => G, ggc => G, gga => G, ggg => G

my $query = new CGI;

# Starts the HTTP request and HTML page
print $query->header, $query->start_html;

# Check for the presence of the rna input parameter in the HTTP request
unless ($query->param('rna')) {

# Generate the form with a title, text area for entering an RNA sequence, and a submit button
print $query->start_form,
"Translate an RNA Sequence to an Amino Acid Sequence<br>\n", $query->textarea(-name=>'rna',
-default=>'auggcacaggcacuguugguacccccaggaccugaaagcuuccgccuuuuuacuaga',
-rows=>4,
-columns=>100),
"<br>",
$query->submit,
"<br>",
$query->end_form;
} else {

# RNA string submitted
my $rna = $query->param('rna');

# Echo the input string
print "Input RNA sequence is $rna<br>\n";

# Amino acid string
my $amino_acid;

# Iterate over RNA string translating codon to amino acid
my $i = 0;
while ($i < length $rna) {
$amino_acid .= $rna_to_amino_acid{substr($rna, $i, 3)};
$i += 3;
}

print "Amino acid string is $amino_acid <br>\n",
"<a href='translate_form.pl'>Start again</a>\n";

}

print $query->end_html;

The web page Translation of DNA to an Amino Acid Sequence on this site was constructed using these principles.

The use CGI; line tells the Perl interpreter that the script will be using the CGI module.  The $query object is used to output the HTTP header, test for the existence of the input parameter 'rna' ($query->param('rna')), and create the text area for entering the RNA string.  In the else stanza the algorithm for translating the RNA sequence to amino acids is executed, as described in the example in the Perl section of this article.

The script demonstrated use of a text area. Another common form element is a textfield, which is generated with script in the form


$query->textfield(-name=>'starting_pos',
-default=>'1',
-size=>4,
-maxlength=>4),

In this method the first parameter is the name of the textfield, the second is the default value, the third is the length of the textfield as shown on the HTML form, and the forth parameter is the maximum number of characters accepted by the form.

In the code that repeats the input string back to the user there is no checking for validity, including escaping of HTML. This is only acceptable in this example because I am repeating the user's input data to him or herself. If the input data was stored in a database and output as HTML to someone else this is vulnerable to a cross-site scripting attack. In that case the user entering the data could include <script> tags to attack the user viewing the data. The script in between the <script> would be executed in the browser of the user viewing the data.


Previous  Contents  Next
References

Contributed Comments and NotesAdd a comment.

There are no user comments.

Google

Please send ideas and opinions by email at alexamies@gmail.com.

© 2006-2007 Alex Amies