Approaches to Web Development for Bioinformatics
Here is a more interesting example. It is the web version of
the class we will look at later when we investigate the BioJava project later in this
article. The BioJava libraries are explained there. The
source code for this example is in file TranslationServlet.java.
1 package net.medicalcomputing.servletExample;
2
3 importjava.io.IOException;
4 import java.io.PrintWriter;
5
6 import javax.servlet.Servlet;
7 import javax.servlet.ServletException;
8 import javax.servlet.http.HttpServlet;
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11
12 import org.biojava.bio.seq.DNATools;
13 import org.biojava.bio.seq.RNATools;
14 import org.biojava.bio.symbol.IllegalAlphabetException;
15 import org.biojava.bio.symbol.IllegalSymbolException;
16 import org.biojava.bio.symbol.SymbolList;
17
18 public class
TranslationServlet extends HttpServlet
implements Servlet {
19
20 protected void
doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
21 String dnaInput =
request.getParameter("dna");
22 SymbolList aminoAcidSequence
= null;
23 String errorOutput = null;
24 try{
25 // create a DNA Sequence
26 SymbolList
dna = DNATools.createDNA(dnaInput);
27
28 // transcribe DNA to RNA
29
SymbolList rna = RNATools.transcribe(dna);
30
31
// translate RNA to amino acid sequence
32
aminoAcidSequence = RNATools.translate(rna);
33
34 } catch(IllegalSymbolException e) {
35
errorOutput = e.getMessage();
36
e.printStackTrace();
37 } catch (IllegalAlphabetException e) {
38
errorOutput = e.getMessage();
39 e.printStackTrace();
40 }
41 PrintWriter writer = response.getWriter();
42 writer.println(
43
"<html>" +
44
"<body>"+
45
" <h3>Results:
Translation of a DNA nucleotide sequence</h3>" +
46
" <p>Input:
" + dnaInput + "</p>" +
47
" <p>Output:
" + aminoAcidSequence.seqString() + "</p>" +
48
" <p>Errors:"
+ ((errorOutput == null)? "No errors were
encountered" : errorOutput) + "</p>" +
49
" <p><a
href='index.html'>Back to the main page</a></p>" +
50
" </body>" +
51
"</html>"
52 );
53 }
54 }
Line 1 is the package declaration of the Servlet class. Lines
3 and 4 are import statements for standard Java libraries. Lines
6 through 10 are import statements for Servlet API's. The BioJava
import statements are on lines 12 through 16. Eclipse generated
all of these import statements for me so I didn't have to spend time
looking them up in the API documentation.
The TranslationServlet class is declared on line 18 to extend
HttpServlet, a base class that initializes the extending class to be
ready to process the HTTP request. On line 20 the doPost() method
is declared. This Servlet only processes POST requests so we need
a HTML page with a form to invoke it. Line 21 gets the DNA input
string from the web form, which is listed below, using the request
object. Lines 22 through 40 do the DNA translation and are
explained in a later section. On line 40 a PrintWriter is
obtained from the response object. Finally, the PrintWriter is
used to output the HTML document containing the translated amino acid
sequence to the browser.
The HTML page used to invoke this Servlet is
1 <html>
2 <body>
3 <h3>Servlet
Example: Translation of a DNA nucleotide sequence</h3>
4 <p>Add
the DNA string to the input field or use the default and click
submit.</p>
5 <form
action="TranslationServlet" method="post">
6 DNA String:<br/>
7 <input
name="dna" type="text"
value="atggcacaggcactgttggtacccccaggacctgaaagcttccgcctttttactaga"
size="80"/><br/>
8 <input
name="submit" type="submit" value="Submit"/>
9 </form>
10 </body>
11 </html>
You may download the file at index.html. On line 5
the action of the form is set to post the data from the form to the URL
TranslationServlet. This URL is specified in the URL
mapping section of the web application file, which describes to the
application server how the application should be deployed. The
file is web.xml:
1 <?xml
version="1.0" encoding="UTF-8"?>
2 <web-app
id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
->
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
3 <display-name>ServletExample</display-name>
4 <servlet>
5 <description>
6 Servlet to
demonstrate translation of a DNA nucleotide sequence into an amino acid
sequence</description>
7 <display-name>
8
TranslationServlet</display-name>
9 <servlet-name>TranslationServlet</servlet-name>
10 <servlet-class>
11
net.medicalcomputing.servletExample.TranslationServlet</servlet-class>
13 </servlet>
14 <servlet-mapping>
15 <servlet-name>TranslationServlet</servlet-name>
16 <url-pattern>/TranslationServlet</url-pattern>
17 </servlet-mapping>
18 <welcome-file-list>
19 <welcome-file>index.html</welcome-file>
20 </welcome-file-list>
21 </web-app>
Eclipse generated this file for me so I didn't have to go looking up
the syntax in the J2EE specification. However, you need to
understand some of the details to tie the Servlet together with your
HTML pages. Lines 4 through 13 tell the application server that
we have a Servlet and what the name of the class is. Lines 14
through 17 tell the application server what URL it should use to make
the Servlet available. That is the relative URL we need in the
HTML form.
Finally, we need to pack the files up in the right structure. A zip
file containing the files in the correct structure is ServletExample.zip. It also
includes the BioJava library jar file that contains the imports in the
Servlet.
You may deploy it in Tomcat by copying the exploded directory into
the Tomcat webapps folder. Start Tomcat by typing startup go to
URL http://localhost:8080/ServletExample/index.html.
Here is what it looks like
Input form for Translation Servlet
After clicking the submit button the output page is shown:
Translation Servlet Output
There are no user comments.
Please send ideas and opinions by email at alexamies@gmail.com.
© 2006-2007 Alex Amies