Java-for-beginners - Java program for NCBI fetch reference

E-mail Print PDF
Article Index
Java-for-beginners
first code
code 1 explanation
Executing the Program
Fetch Data from NCBI
NCBI fetch reference
All Pages

 

Here is the Java program for our reference:-

File Name:- ImportFromNCBI3.jsp

// comments are entered in java code in this format.

//define the language for this page as Java

<%@ page language = "java"%>

//import the libraries for java

<%@ page import = "java.util.*"%>

<%@ page import = "java.io.*"%>

<%@ page import="java.lang.*"%>

<%@ page import="java.net.*"%>

<%@ page import="java.nio.*"%>

<%@ page import= "javax.xml.parsers.*" %>

<%@ page import= "org.w3c.dom.*" %>

<%

//print the message to the log file of Tomcat engine.

System.out.println("ImportFromNCBI3.jsp");

//create the session variable

javax.servlet.http.HttpSession hs = request.getSession();

try //start the try-catch block.

{

String URLString = ""; //variable to hold the URL

//store the authentication settings for connecting to the internet

Properties systemSettings = System.getProperties();

systemSettings.put("http.proxyHost", "proxy.it.iitb.ac.in"); //proxy host

systemSettings.put("http.proxyPort", "80"); //proxy port

//time out settings for connecting to the internet.

systemSettings.put("sun.net.client.defaultConnectTimeout", "10000");

systemSettings.put("sun.net.client.defaultReadTimeout", "10000");

//the user id and password to connect to the internet via the home network.

Authenticator.setDefault(new Authenticator()

{

protected PasswordAuthentication getPasswordAuthentication()

{

return new PasswordAuthentication("aaqua", "aaqua123".toCharArray()); // specify ur user name password of iitb login

}

});

System.setProperties(systemSettings); //set the user id and settings

System.out.println(" System Properties Set");

System.out.println(" URL variables set="+URLString);//press the URL

String retstart = "";//variable to set the page number

String retmax = "";//variable to set the maximum records per page

/*Is the request coming from the client or from the output file. Is it a different page being requested for the same search request or is it a new request coming from the client. If the latter is true, then the variable loopflag = no, else it will be false. This variable loopflag is set in the client input form and the value received from there to this program.

*/

if (request.getParameter("loopflag").toString().equals("no") )

{//this is a page from the client input.

//the URL data is sent from the previous input. The javascript converts the

//client input into NCBI format URL and then sends the details via a text

//field named “txtURLString”. Only the parameters retmax and retstart are

//added here

URLString = request.getParameter("txtURLString").toString();

System.out.println("loopflag=no");//print message:- the loopflag value is no

retstart = request.getParameter("retstart") ; //get the value of retstart from client

retmax = request.getParameter("retmax");//get the value of retmax from client

//print the values of the parameters to the apache tomcat log file.

System.out.println("loopflag=else part");

System.out.println("retstart="+retstart);

System.out.println("retmax="+retmax);

System.out.println("url_IMPORTFROM="+request.getParameter("txtURLString"));

//contruct the final URL and store in the variable

//set the value of page number in session variable

URLString = (String)request.getParameter("txtURLString")+ "&retstart=" + retstart + "&retmax=" + retmax;

//set the value of no. of records in session variable

//value of retstart and retmax stored in the variable

hs.setAttribute("retstart",retstart);

hs.setAttribute("retmax",retmax);

}

else if ( request.getParameter("loopflag").toString().equals("next") )//loop

{

System.out.println("next loopflag="+hs.getAttribute("next"));

URLString = (String)hs.getAttribute("next");

//database name stored in the session variable.

hs.setAttribute("dbname", hs.getAttribute("dbname") ); //database

System.out.println("loopflag=next"); //next page is being clicked.

}

else if ( request.getParameter("loopflag").toString().equals("previous") )

//loop input if loopflag is from the ouput page in which the previous page

//has been requester i.e. the user has clicked on the “previous” page of the

//output page.

{

//previous page is being clicked.

System.out.println("previous loopflag="+hs.getAttribute("previous"));

//database name is being set in to the session variable.

hs.setAttribute("dbname", hs.getAttribute("dbname") );

//previous page value being set into the session variable.

URLString = (String)hs.getAttribute("previous");

System.out.println("loopflag=previous");

}

else

{ //none of the above cases were executed.

retstart = request.getParameter("retstart") ;

retmax = request.getParameter("retmax");

System.out.println("loopflag=else part");

System.out.println("retstart="+retstart);

System.out.println("retmax="+retmax);

System.out.println("url_IMPORTFROM="+request.getParameter("txtURLString")); //print the URL string

//store the variables in session..

hs.setAttribute("retstart",retstart);

hs.setAttribute("retmax",retmax);

hs.setAttribute("dbname", hs.getAttribute("dbname") );//database name

}

System.out.println(" URL variables set="+URLString);

URL url = new URL(URLString); //url string taken from user input.

//define the HTTPURLConnection object

HttpURLConnection connection = null;

//open the connection to the following URL.

connection = (HttpURLConnection) url.openConnection();

//set the connection type to POST method.

connection.setRequestMethod("POST");

connection.setDoInput(true);//allow the user to give input

connection.setDoOutput(true);//allow the connection to give output

connection.setUseCaches(false);//don’t use cache to store temporary results.

connection.setAllowUserInteraction(false); //don’t allow user interaction for this //connection

//set the content type of output to XML/text format. This means that the output will //be of text or XML format.

connection.setRequestProperty ("Content-Type","text/xml; charset=\"utf-8\"");

//print message. This will help in tracking the errors in tomcat log file.

System.out.println(" connection Set");

//set the channel to get the input. This is called the input stream. We use the //buffered reader for getting the input. Open the input stream using the buffered //reader.

BufferedReader in = new BufferedReader( new InputStreamReader( connection.getInputStream()));

String decodedString;//variable to hold the decoded string

String tempstr = ""; //variable to store the single output

System.out.println("Reader Set");//message – stream set

Random rn = new Random(); //random object to get a random integer

int rnval = rn.nextInt() ; //get a random integer.

//create the XML file name

String fname = "file_ncbi_"+ String.valueOf(rnval) + ".xml" ;

//a channel to handle the output from the NCBI and then write it to the XML file specified above.

BufferedWriter bw = new BufferedWriter(new FileWriter(fname));

//loop through all the records line by line

while ((decodedString = in.readLine()) != null)

{

tempstr = tempstr + decodedString;// append the output line to the existing o/p

//out.println(tempstr);

bw.write(decodedString); //write the output line to the XML file.

if ( tempstr.indexOf("/") == -1 ) //find if there is a new line started

{

bw.newLine(); //if yes, then start on a new line

}

}

System.out.println(" output given.Set"); //message – output set.

bw.close(); //close the streams

in.close();

//store the file name and URL in session variable. This value will be passed onto //the next program in Step-3)

hs.setAttribute("NCBIfile",fname.toString()); //store the file name in session //variable

hs.setAttribute("urlstring",URLString);

//print the values of the variables.

System.out.println("Session variable Set");

System.out.println( "fname="+fname.toString() );

System.out.println( "urlstring="+URLString);

//move the control to the next page.

response.sendRedirect("/ProteomDb/FetchDataFromNCBI.jsp");

}

catch(Exception ex) //catch any exceptions

{

out.println("Exception->"+ex); //print the exception

PrintWriter pw = response.getWriter(); //get the handle to the output stream for //this JSP page

ex.printStackTrace(pw); //print the error stack details to the JSP page

}//try catch block ends.

%>

3) Parse the output response from NCBI

So now you have the output data from the previous program. The problem is that this data is in XML format. In an XML file, data is present in the form of tags. The value will be present between the tags. We need to extract this information between each tag. This is what the current Java program does.

Please note that the name of the XML file is needed here. We assume that we are having the XML file at present. I have attached a snapshot besides each code shunk to explain how it looks

<%//@ page contentType="text/xml; charset=utf-8" pageEncoding="UTF-8"%>

<%@ page language = "java" // set the page language to java %>

//import the java libraries

<%@ page import = "java.util.*"%>

<%@ page import = "java.io.*"%>

<%@ page import="java.lang.*"%>

<%@ page import="java.net.*"%>

<%@ page import="java.nio.*"%>

<%@ page import = "java.sql.*" %>

<%@ include file = "header.jsp" %>

<%@ page import = "javax.xml.parsers.*" %>

<%@ page import = "org.w3c.dom.*" %>

<%

try

{

//create the object for session.

javax.servlet.http.HttpSession hs = request.getSession();

//get the name of the file that was stored in the session variable.

String fname = (String) hs.getAttribute("NCBIfile");

System.out.println("fname = " + fname.toString() ); //print the file name.

File file = new File(fname); //create the file name

//create the DocumentBuilderFactory class object. This will be the handle to //the XML file. Create a document object and map it to the XML file.

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();

Document doc = db.parse(file); doc.getDocumentElement().normalize();

//get the root element and print its value to the tomcat node.

System.out.println("Root element " + .getDocumentElement().getNodeName());

//total no. of records retrieved in the search

//get the NodeList Object and get the handle to the tag – Count .

NodeList nodeLst_cnt = doc.getElementsByTagName("Count");

//get the first item for the tag Count.

Element fstNmElmnt_cnt = (Element) nodeLst_cnt.item(0);

//get all the child nodes for the tag Count.

NodeList fstNm_cnt = fstNmElmnt_cnt.getChildNodes();

System.out.println("Document Count : " + ((Node) fstNm_cnt.item(0)).getNodeValue());

//get the node value at first child node and print the value to the Tomcat log file.

int rec_cnt = Integer.valueOf( ((Node) fstNm_cnt.item(0)).getNodeValue() );

if (rec_cnt != 0 ) //check if the no. Of records is > 0 or not.

{

System.out.println("rec_cnt not 0");//print the message to tomcat log.

//Ret Max variable. Return no. of records to be shown per page. here it is 20.

//Now we try to get the value for the tag RetMax in the same way.

NodeList nodeLst_retmax = doc.getElementsByTagName("RetMax");

Element fstNmElmnt_retmax = (Element) nodeLst_retmax.item(0);

NodeList fstNm_retmax = fstNmElmnt_retmax.getChildNodes();

System.out.println("Document RetMax : " + ((Node) fstNm_retmax.item(0)).getNodeValue());

int rec_max = Integer.valueOf( ((Node) fstNm_retmax.item(0)).getNodeValue() );

//Now we try to get the value for the tag RetStart in the same way.

NodeList nodeLst_retstart = doc.getElementsByTagName("RetStart");

Element fstNmElmnt_retstart = (Element) nodeLst_retstart.item(0);

NodeList fstNm_retstart = fstNmElmnt_retstart.getChildNodes();

System.out.println("Document RetStart : " + ((Node) fstNm_retstart.item(0)).getNodeValue());

int rec_start = Integer.valueOf( ((Node) fstNm_retstart.item(0)).getNodeValue() );

//The node below is IdList. This tag contains a list of all the record id tags for the search results. Thus, this will be a loop and have multiple child nodes.

NodeList nodeLst = doc.getElementsByTagName("IdList");//get the handle to tag

System.out.println("Information of all ids WITH IdList Length=" + nodeLst.getLength() ); //print the number of tags present for IdList.

//the tag IdList has several child nodes called Id. We need to first count the number of tags and then parse each of the tags.

for (int s = 0; s < nodeLst.getLength(); s++) //loop through all the “Id” node.

{

Node fstNode = nodeLst.item(s); //get the handle to the node at position s.

System.out.println("in first for loop<br>"); //print the for loop.

//check if the node is of the type ELEMENT

if (fstNode.getNodeType() == Node.ELEMENT_NODE)

{

System.out.println("in first if condition<br>");

Element fstElmnt = (Element) fstNode; //Assign the node

//get the element to the node Id.

NodeList fstNmElmntLst = stElmnt.getElementsByTagName("Id");

//GET The string array to store the Ids.

String[] pubmedids = new String[fstNmElmntLst.getLength()];

for (int h = 0; h < fstNmElmntLst.getLength(); h++) //gets all the Id //tag nodes

{

System.out.println("in second for loop<br>");

//get the handle to each element of the node at position h.

Element fstNmElmnt = (Element) fstNmElmntLst.item(h);

NodeList fstNm = fstNmElmnt.getChildNodes();//get the child //nodes.

pubmedids[h] = (String) fstNm.item(0).getNodeValue() ;//store //the value in array.

//print the node value.

System.out.println("Id : " + ( (Node) fstNm.item(0)).getNodeValue() + "<br>");

System.out.println("pubmedids@h=:" + h + "=" + pubmedids[h] + "<br>");

}

//store the variables in the session variable.

hs.setAttribute("pubmedids", pubmedids);

hs.setAttribute("dbname","genome");

hs.setAttribute("retstart", String.valueOf(rec_start) );

hs.setAttribute("retmax", String.valueOf(rec_max) );

hs.setAttribute("retcnt",String.valueOf(rec_cnt));

//print the values of the variables. Next close the loop.

System.out.println("retstart="+rec_start);

System.out.println("retmax="+rec_max);

System.out.println("rec_cnt="+rec_cnt);

}

}

//redirect to the next page to show the output. If there were no records //returned, then redirect to the first client page and ask him to enter the //details again.

response.sendRedirect("/ProteomDb/ShowDataFromNCBI.jsp");

}

else

{

System.out.println("rec_cnt=0");

//if the error is found then redirect to the client page with //message.

response.sendRedirect("/ProteomDb/ImportFromNCBIDb.jsp?noresultsflag=true&db");

}

//close the try-catch block. If there is any error present, then the description of the //same should be shown.

}

catch (Exception e) //catch the exception.

{

e.printStackTrace(); //print the stack trace

}

finally

{}

%>

4) Display the formatted output to the user

Create a simple HTML page using any HTML editor or write the code yourself. Screenshots below.

Then you can

Right, now we have got the output in the text formatted. This output is in the form of a String array. Now, we need to just parse the loop through each of the elements of the array and display it to the user in a tabular format.

This is a very simple code and should be self-explanatory. Here we just get the values from the session variable and use them to present the code for each database. Please remember that this program allows you to browse through all the NCBI databases and thus many categories for each case will be present.

I suggest you go through the tutorials and references mentioned above in the manual before reading the code.

<%@ page language = "java" %>

<%//@ page contentType="text/xml; charset=utf-8" pageEncoding="UTF-8"%>

<%@ page language = "java"%>

<%@ page import = "java.util.*"%>

<%@ page import = "java.io.*"%>

<%@ page import="java.lang.*"%>

<%@ page import="java.net.*"%>

<%@ page import="java.nio.*"%>

<%@ page import = "java.sql.*" %>

<%@ include file = "header.jsp" %>

<%@ page import = "javax.xml.parsers.*" %>

<%@ page import = "org.w3c.dom.*" %>

<%

javax.servlet.http.HttpSession hs = request.getSession();

String pubmedids[] = (String[]) hs.getAttribute("pubmedids");

String next = "";

String previous = "" ;

System.out.println("ShowDataFromNCBI.jsp");

String loopflag = "yes";

String operation = "";

String recordspage = (String) hs.getAttribute("dbname");

String pmid_url_base = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=protein&rettype=gp&retmode=xml&id=";

String url = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=protein&rettype=gp&retmode=xml&id=" ;

String pagename = "";

if ( recordspage.equals("genome") )

{

url = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=genome&rettype=gp&retmode=xml&id=";

pagename = "ImportGenomeDetails3.jsp";

out.println("db=genome&pagename="+pagename+"<br>");

hs.setAttribute("linkurl",url);

}

//http://www.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=taxonomy&id=2&rettype=native&retmode=xml

if ( recordspage.equals("taxonomy") )

{

url = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=taxonomy&rettype=gp&retmode=xml&id=";

pagename = "ImportTaxonomyDetails3.jsp";

hs.setAttribute("linkurl",url);

}

// http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=unists&id=254085,254086&retmode=xml

if ( recordspage.equals("unists") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=unists&retmode=xml&id=";

pagename = "ImportUnistsDetails3.jsp";

hs.setAttribute("linkurl",url);

}

// http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=unists&id=254085,254086&retmode=xml

if ( recordspage.equals("structure") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=structure&retmode=xml&id=";

pagename = "ImportStructureDetails3.jsp";

hs.setAttribute("linkurl",url);

}

//-------

if ( recordspage.equals("biosystems") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=biosystems&retmode=xml&id=";

pagename = "ImportBioSystemsDetails3.jsp";

hs.setAttribute("linkurl",url);

}

//books

if ( recordspage.equals("books") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=books&retmode=xml&id=";

pagename = "ImportBooksDetails3.jsp";

hs.setAttribute("linkurl",url);

}

//cancerchromosomes

if ( recordspage.equals("cancerchromosomes") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=cancerchromosomes&retmode=xml&id=";

pagename = "ImportCancerChromosomesDetails3.jsp";

hs.setAttribute("linkurl",url);

}

//cdd

if ( recordspage.equals("cdd ") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=cdd&retmode=xml&id=";

pagename = "ImportCddDetails3.jsp";

hs.setAttribute("linkurl",url);

}

//gap

if ( recordspage.equals("gap") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=gap&retmode=xml&id=";

pagename = "ImportCancerChromosomesDetails3.jsp";

hs.setAttribute("linkurl",url);

}

//domains

if ( recordspage.equals("domains") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=domains&retmode=xml&id=";

pagename = "ImportDomainsDetails3.jsp";

hs.setAttribute("linkurl",url);

}

//gene

if ( recordspage.equals("gene") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=gene&retmode=xml&id=";

pagename = "ImportGeneDetails3.jsp";

hs.setAttribute("linkurl",url);

}

//genomeprj

if ( recordspage.equals("genomeprj") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=genomeprj&retmode=xml&id=";

pagename = "ImportGenomeprjDetails3.jsp";

hs.setAttribute("linkurl",url);

}

// gensat

if ( recordspage.equals("gensat") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=gensat&retmode=xml&id=";

pagename = "ImportGensatDetails3.jsp";

hs.setAttribute("linkurl",url);

}

//geo

if ( recordspage.equals("geo") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=geo&retmode=xml&id=";

pagename = "ImportGeoDetails3.jsp";

hs.setAttribute("linkurl",url);

}

//gds

if ( recordspage.equals("gds") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=gds&retmode=xml&id=";

pagename = "ImportGdsDetails3.jsp";

hs.setAttribute("linkurl",url);

}

//homologene

if ( recordspage.equals("homologene") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=homologene&retmode=xml&id=";

pagename = "ImportHomologeneDetails3.jsp";

hs.setAttribute("linkurl",url);

}

// journals

if ( recordspage.equals("journals") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=journals&retmode=xml&id=";

pagename = "ImportJournalsDetails3.jsp";

hs.setAttribute("linkurl",url);

}

//mesh

if ( recordspage.equals("mesh") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=mesh&retmode=xml&id=";

pagename = "ImportMeshDetails3.jsp";

hs.setAttribute("linkurl",url);

}

//ncbisearch

if ( recordspage.equals("ncbisearch") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=ncbisearch&retmode=xml&id=";

pagename = "ImportNcbisearchDetails3.jsp";

hs.setAttribute("linkurl",url);

}

//nlmcatalog

if ( recordspage.equals("nlmcatalog") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=nlmcatalog&retmode=xml&id=";

pagename = "ImportNlmcatalogDetails3.jsp";

hs.setAttribute("linkurl",url);

}

//code starts

// omia

if ( recordspage.equals("omia") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=omia&retmode=xml&id=";

pagename = "ImportOmiaDetails3.jsp";

hs.setAttribute("linkurl",url);

}

//omim

if ( recordspage.equals("omim") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=omim&retmode=xml&id=";

pagename = "ImportOmimDetails3.jsp";

hs.setAttribute("linkurl",url);

}

//pepdome

if ( recordspage.equals("pepdome") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pepdome&retmode=xml&id=";

pagename = "ImportPepdomeDetails3.jsp";

hs.setAttribute("linkurl",url);

}

//pmc

if ( recordspage.equals("pmc") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pmc&retmode=xml&id=";

pagename = "ImportPmcDetails3.jsp";

hs.setAttribute("linkurl",url);

}

// popset

if ( recordspage.equals("popset") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=popset&retmode=xml&id=";

pagename = "ImportPopsetDetails3.jsp";

hs.setAttribute("linkurl",url);

}

// probe

if ( recordspage.equals("probe") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=probe&retmode=xml&id=";

pagename = "ImportprobeDetails3.jsp";

hs.setAttribute("linkurl",url);

}

//proteinclusters

if ( recordspage.equals("proteinclusters") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=proteinclusters&retmode=xml&id=";

pagename = "ImportProteinclustersDetails3.jsp";

hs.setAttribute("linkurl",url);

}

//pcassay

if ( recordspage.equals("pcassay") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pcassay&retmode=xml&id=";

pagename = "ImportPcassayDetails3.jsp";

hs.setAttribute("linkurl",url);

}

//code starts

//pccompound

if ( recordspage.equals("pccompound") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pccompound&retmode=xml&id=";

pagename = "ImportPccompoundDetails3.jsp";

hs.setAttribute("linkurl",url);

}

//pcsubstance

if ( recordspage.equals("pcsubstance") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pcsubstance&retmode=xml&id=";

pagename = "ImportPcsubstanceDetails3.jsp";

hs.setAttribute("linkurl",url);

}

//snp

if ( recordspage.equals("snp") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=snp&retmode=xml&id=";

pagename = "ImportSnpdomeDetails3.jsp";

hs.setAttribute("linkurl",url);

}

//sra

if ( recordspage.equals("sra") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=sra&retmode=xml&id=";

pagename = "ImportSraDetails3.jsp";

hs.setAttribute("linkurl",url);

}

// toolkit

if ( recordspage.equals("toolkit") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=toolkit&retmode=xml&id=";

pagename = "ImportToolkitDetails3.jsp";

hs.setAttribute("linkurl",url);

}

// toolkitall

if ( recordspage.equals("toolkitall") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=toolkitall&retmode=xml&id=";

pagename = "ImportToolkitallDetails3.jsp";

hs.setAttribute("linkurl",url);

}

//unigene

if ( recordspage.equals("unigene") )

{

url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=unigene&retmode=xml&id=";

pagename = "ImportUnigeneDetails3.jsp";

hs.setAttribute("linkurl",url);

}

//code ends

//----------

String retstart = hs.getAttribute("retstart").toString();

String retmax = hs.getAttribute("retmax").toString() ;

String retcnt = hs.getAttribute("retcnt").toString() ;

System.out.println("url_string="+ hs.getAttribute("urlstring"));

String urlstring=null;

urlstring = (String)hs.getAttribute("urlstring");

System.out.println("url_sheesecond="+ urlstring);

next = previous = urlstring ;

out.println("retstart="+retstart);

out.println("retmax="+retmax);

out.println("retcnt="+retcnt);

out.println("urstring="+urlstring);

String tempvar = "";

tempvar = urlstring.substring(0, urlstring.indexOf("&retstart")) ;

System.out.println("tempvar="+tempvar);

urlstring= tempvar ;

//String hrefvalue = "txtURLString="+urlstring ;

int maxpage = 0 ;

if ( ( retcnt != null ) && (retmax != null ) )

{

maxpage = Integer.valueOf(retcnt) % Integer.valueOf(retmax) ; //no of pages to be shown

System.out.println("maxpage="+maxpage);

}

if ( Integer.valueOf(retstart) < maxpage ) // if curpage is less than lastpage

{

next = tempvar + "&retstart=" + (Integer.valueOf(retstart)+1) + "&retmax=" + retmax;

//find the value for previous

if ( Integer.valueOf(retstart) > 0 )

{

//previous = hrefvalue + "&retstart=" + (Integer.valueOf(retstart)-1) + "&retmax=" + retmax;

previous = urlstring + "&retstart=" + (Integer.valueOf(retstart)-1) + "&retmax=" + retmax;

System.out.println("previous="+previous);

}

else//first page

{

previous = "NA";

}

}

else

{

next = "NA";

//find the value for previous

if ( Integer.valueOf(retstart) > 0 )

{

//previous = hrefvalue + "&retstart=" + (Integer.valueOf(retstart)-1) + "&retmax=" + retmax;

previous = urlstring + "&retstart=" + (Integer.valueOf(retstart)-1) + "&retmax=" + retmax;

}

else//first page

{

previous = "NA";

}

}

%>

<html>

<head>

<title>New Page 1</title>

<style type="text/css">

.style1 {

border: 1px solid #3399FF;

background-color: #99CCFF;

font-size: small;

}

.style2 {

border: 1px solid #3399FF;

font-weight: bold;

background-color: #99CCFF;

font-size: small;

}

.style3 {

border-collapse: collapse;

border: 1px solid #3399FF;

background-color: #FFFFFF;

}

.style4 {

border: 1px solid #3399FF;

}

.style5 {

text-align: right;

}

.style7 {

font-size: large;

font-weight: bold;

}

.style8 {

font-size: large;

}

.style9 {

text-align: center;

}

</style>

</head>

<form type=POST action="cancer_des.jsp">

<body>

<p align="center" class="style7">&nbsp;</p>

<div align="center">

<center>

<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="68%" id="AutoNumber2" height="58">

<tr>

<td width="150%" height="19">

<p align="center"><span class="style8">Search Results Page</span>

<span class="style8">

for <%=(recordspage)%>Database</span></td>

</tr>

<tr>

<td width="150%" height="19" class="style9">

<%=("Select your choice and click your operation button")%>

</td>

</tr>

</table>

</center>

</div>

<p align="center">The following are the search results of your query. Please

click on the link to view the paper.</p>

<div align="center">

<center>

<table cellpadding="0" cellspacing="0" style="padding: 2 4; width: 71%; height: 82px;" id="AutoNumber1" class="style3">

<tr>

<td align="center" style="width: 13%" class="style2">Serial No</td>

<td align="center" style="width: 23%" class="style2">Record ID</td>

<td align="center" style="width: 73%" class="style1"><strong>URL</strong></td>

</tr>

</font><br>

<%

for (int i=0;i < pubmedids.length ; i++ )

{

%>

<tr>

<td bgcolor="#FFFFFF" align="center" style="width: 13%" class="style4"><%=(i+1)%></td>

<td bgcolor="#FFFFFF" align="center" style="width: 23%" class="style4"><%=( pubmedids[i] )%></td>

<td bgcolor="#FFFFFF" align="center" style="width: 73%" class="style4">

<a href="/<%=( pagename + "?loopflag=no&id=" + pubmedids[i] )%>"> View Record</a>

</td>

</tr>

<% }

%>

<tr>

<td bgcolor="#FFFFFF" align="center" style="width: 13%" class="style4">&nbsp;</td>

<td bgcolor="#FFFFFF" align="center" style="width: 23%" class="style4">&nbsp;</td>

<td bgcolor="#FFFFFF" align="center" style="width: 73%" class="style4">

<p align="right">

<%

System.out.println("next="+next);

System.out.println("previous="+previous);

hs.setAttribute("next",next);

hs.setAttribute("previous",previous);

if (next != "NA" )

{

%>

<a href="/<%=( "ImportFromNCBI3.jsp?" + "loopflag=next")%>">Next</a>

<% } %>

<%

if (previous != "NA" )

{

%>

<a href="/<%=( "ImportFromNCBI3.jsp?" + "loopflag=previous")%>">Previous</a>

<% } %>

</td>

</tr>

</table>

</center>

</div>

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; <a href="/mainpage.jsp">Back</a></p>

</body>

</form>

</html>

Screenshot of the execution of the above file (assuming it has received the output from the previous step of parsing the XML file).

So now we have displayed the list of records. Now the client will click on a particular record. Thus, this request is sent to the NCBI server in the same manner. We follow the same 4 steps (Step- 1 being the output file of last request. The final output will be a complete set of details for the selected record as shown in this file)

So the first step is the output file having the list of records from the eSearch utility. Now the request goes to the java program that sends it to the NCBI server. WE use the same program but the URL will be different. We use the eFetch utility to get the details in the XML format.

The Java program to send request and receive response. The code should be self-explanatory now after the previous java program explained in the same fashion.

File Name:- ImportGenomeDetails3.jsp

<%//@ page contentType="text/xml; charset=utf-8" pageEncoding="UTF-8"%>

<%@ page language = "java"%>

<%@ page import = "java.util.*"%>

<%@ page import = "java.io.*"%>

<%@ page import="java.lang.*"%>

<%@ page import="java.net.*"%>

<%@ page import="java.nio.*"%>

<%@ page import= "javax.xml.parsers.*" %>

<%@ page import= "org.w3c.dom.*" %>

<%

System.out.println("ImportGenomeDetails3.jsp");

javax.servlet.http.HttpSession hs = request.getSession();

try

{

String URLString = "";

Properties systemSettings = System.getProperties();

systemSettings.put("http.proxyHost", "proxy.it.iitb.ac.in");

systemSettings.put("http.proxyPort", "80");

systemSettings.put("sun.net.client.defaultConnectTimeout", "10000");

systemSettings.put("sun.net.client.defaultReadTimeout", "10000");

Authenticator.setDefault(new Authenticator()

{

protected PasswordAuthentication getPasswordAuthentication()

{

return new PasswordAuthentication("aaqua", "aaqua123".toCharArray()); // specify ur user name password of iitb login

}

});

System.setProperties(systemSettings);

System.out.println(" System Properties Set");

String retstart = "";

String retmax = "";

URLString = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=genome&rettype=gp&retmode=xml&id=" + request.getParameter("id").toString();

System.out.println(" URL variables set="+URLString);

URL url = new URL(URLString); //url string taken from user input.

HttpURLConnection connection = null;

connection = (HttpURLConnection) url.openConnection();

connection.setRequestMethod("POST");

connection.setDoInput(true);

connection.setDoOutput(true);

connection.setUseCaches(false);

connection.setAllowUserInteraction(false);

connection.setRequestProperty ("Content-Type","text/xml; charset=\"utf-8\"");

System.out.println(" connection Set");

BufferedReader in = new BufferedReader( new InputStreamReader( connection.getInputStream()));

String decodedString;

String tempstr = "";

System.out.println("Reader Set");

Random rn = new Random();

int rnval = rn.nextInt() ;

String fname = "genomefile_ncbi_"+ String.valueOf(rnval) + ".xml" ;

//File file = new File(fname);

BufferedWriter bw = new BufferedWriter(new FileWriter(fname));

while ((decodedString = in.readLine()) != null)

{

tempstr = tempstr + decodedString;

//out.println(tempstr);

bw.write(decodedString);

if ( tempstr.indexOf("/") == -1 )

{

bw.newLine();

}

}

System.out.println(" output given.Set");

bw.close();

in.close();

hs.setAttribute("genomencbifile",fname.toString());

hs.setAttribute("urlstring",URLString);

System.out.println("Session variable Set");

System.out.println( "fname="+ fname.toString() );

System.out.println( "urlstring=" + URLString );

response.sendRedirect("/ProteomDb/FetchGenomeDetailsDataFromNCBI.jsp");

}

catch(Exception ex)

{

out.println("Exception->"+ex);

PrintWriter pw = response.getWriter();

ex.printStackTrace(pw);

}

%>

I hope the code is self-explanatory. It is similar to the step -2 of fetching records using the eSearch utility.

The next step is actually parsing the XML file that is generated. We assume here that you have access to the XML file on your machine.

This file is very complicated to parse and contains nested loops. If you have understood the previous file, then we can easily follow the flow and parse through each tag. You will need to keep the XML file open in another window so that you can understand the file we discuss here.

File -> FetchGenomeDetailsFromNCBI.jsp

//set the language to java and the encoding type to XML/Text

<%@ page language = "java" %>

<%//@ page contentType="text/xml; charset=utf-8" pageEncoding="UTF-8"%>

//import the java libraries.

<%@ page import = "java.util.*"%>

<%@ page import = "java.io.*"%>

<%@ page import="java.lang.*"%>

<%@ page import="java.net.*"%>

<%@ page import="java.nio.*"%>

<%@ page import = "java.sql.*" %>

<% //@ include file = "header.jsp" %>

<%@ page import = "javax.xml.parsers.*" %>

<%@ page import = "org.w3c.dom.*" %>

<%

//get the base URL

String pmid_url_base = "http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?cmd=Retrieve&db=pubmed&dopt=Abstract&list_uids=";

out.println("FetchGenomeDetailsFromNCBI.jsp");

try

{

//create the session object

javax.servlet.http.HttpSession hs = request.getSession();

//get the file name from the session variable

String fname = (String) hs.getAttribute("genomencbifile");

//print the file name

out.println("fname = " + fname.toString() );

//get the handle of the file name

File file = new File(fname);

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();

Document doc = db.parse(file);

doc.getDocumentElement().normalize();

//get the value for the parent element GbSet(an element without any child //nodes)

NodeList nodeLst_gbset = doc.getElementsByTagName("GBSet");

Element fstNmElmnt_gbset = (Element) nodeLst_gbset.item(0);

NodeList fstNm_gbset = fstNmElmnt_gbset.getChildNodes();

//out.println("Document GBSet : " + ((Node) fstNm_gbset.item(0)).getNodeValue());

out.println("GBSet node ");

//get the value of the node GBSeq

NodeList nodeLst = doc.getElementsByTagName("GBSeq");

//out.println("<br>Information of all ids.Length="+nodeLst.getLength() + "<br>");

Node fstNode = nodeLst.item(0);

// get the value of the node GbSeq_locus

NodeList nodeLst_gbseqlocus = doc.getElementsByTagName("GBSeq_locus");

Element fstNmElmnt_gbseqlocus = (Element) nodeLst_gbseqlocus.item(0);

NodeList fstNm_gbseqlocus = fstNmElmnt_gbseqlocus.getChildNodes();

out.println("<b> Document GBSeqlocus :</b> " + ((Node) fstNm_gbseqlocus.item(0)).getNodeValue() + "<br>");

// GbSeq_locus.

// get the value of the node GbSeq_length

NodeList nodeLst_gbseqlen = doc.getElementsByTagName("GBSeq_length");

Element fstNmElmnt_gbseqlen = (Element) nodeLst_gbseqlen.item(0);

NodeList fstNm_gbseqlen = fstNmElmnt_gbseqlen.getChildNodes();

out.println("<b>Document GBSeqlen : </b>" + ((Node) fstNm_gbseqlen.item(0)).getNodeValue() + "<br>");

// GbSeq_length code ends..

// get the value of the node GBSeq_definition

NodeList nodeLst_gbseqdefinition = doc.getElementsByTagName("GBSeq_definition");

Element fstNmElmnt_gbseqdefinition = (Element) nodeLst_gbseqdefinition.item(0);

NodeList fstNm_gbseqdefinition = fstNmElmnt_gbseqdefinition.getChildNodes();

out.println("<b> Document GBSeq_definition : </b>" + ((Node) fstNm_gbseqdefinition.item(0)).getNodeValue() + "<br>");

//GBSeq_definition ends

// get the value of the node GBSeq_strandedness

NodeList nodeLst_gbseqstrandedness = doc.getElementsByTagName("GBSeq_strandedness");

Element fstNmElmnt_gbseqstrandedness = (Element) nodeLst_gbseqstrandedness.item(0);

NodeList fstNm_gbseqstrandedness = fstNmElmnt_gbseqstrandedness.getChildNodes();

out.println("<b> Document GBSeq_strandedness : </b>" + ((Node) fstNm_gbseqstrandedness.item(0)).getNodeValue() + "<br>");

//GBSeq_strandedness ends

// get the value of the node GBSeq_moltype

NodeList nodeLst_gbseqmoltype = doc.getElementsByTagName("GBSeq_moltype");

Element fstNmElmnt_gbseqmoltype = (Element) nodeLst_gbseqmoltype.item(0);

NodeList fstNm_gbseqmoltype = fstNmElmnt_gbseqmoltype.getChildNodes();

out.println("<b> Document GBSeq_moltype : </b>" + ((Node) fstNm_gbseqmoltype.item(0)).getNodeValue() + "<br>");

//GBSeq_moltype

// get the value of the node GBSeq_topology

NodeList nodeLst_gbseqtopology = doc.getElementsByTagName("GBSeq_topology");

Element fstNmElmnt_gbseqtopology = (Element) nodeLst_gbseqtopology.item(0);

NodeList fstNm_gbseqtopology = fstNmElmnt_gbseqtopology.getChildNodes();

out.println("<b> Document GBSeq_topology : </b>" + ((Node) fstNm_gbseqtopology.item(0)).getNodeValue() + "<br>");

//GBSeq_topology code ends

// get the value of the node GBSeq_division

NodeList nodeLst_gbseqdivision = doc.getElementsByTagName("GBSeq_division");

Element fstNmElmnt_gbseqdivision = (Element) nodeLst_gbseqdivision.item(0);

NodeList fstNm_gbseqdivision = fstNmElmnt_gbseqdivision.getChildNodes();

out.println("<b> Document GBSeq_division : </b>" + ((Node) fstNm_gbseqdivision.item(0)).getNodeValue() + "<br>");

//GBSeq_division code ends

// get the value of the node GBSeq_update-date

NodeList nodeLst_gbsequpdatedate = doc.getElementsByTagName("GBSeq_update-date");

Element fstNmElmnt_gbsequpdatedate = (Element) nodeLst_gbsequpdatedate.item(0);

NodeList fstNm_gbsequpdatedate = fstNmElmnt_gbsequpdatedate.getChildNodes();

out.println("<b> Document GBSeq_updatedate : </b>" + ((Node) fstNm_gbsequpdatedate.item(0)).getNodeValue() + "<br>");

//GBSeq_update-date code ends

// get the value of the node GBSeq_create-date

NodeList nodeLst_gbseqcreatedate = doc.getElementsByTagName("GBSeq_create-date");

Element fstNmElmnt_gbseqcreatedate = (Element) nodeLst_gbseqcreatedate.item(0);

NodeList fstNm_gbseqcreatedate = fstNmElmnt_gbseqcreatedate.getChildNodes();

out.println("<b> Document GBSeq_createdate : </b>" + ((Node) fstNm_gbseqcreatedate.item(0)).getNodeValue() + "<br>");

//GBSeq_create-date code ends

// get the value of the node GBSeq_primary-accession

NodeList nodeLst_gbseqpriacc = doc.getElementsByTagName("GBSeq_primary-accession");

Element fstNmElmnt_gbseqpriacc = (Element) nodeLst_gbseqpriacc.item(0);

NodeList fstNm_gbseqpriacc = fstNmElmnt_gbseqpriacc.getChildNodes();

out.println("<b>Document GBSeq_primary-accession : </b>" + ((Node) fstNm_gbseqpriacc.item(0)).getNodeValue() + "<br>");

//GBSeq_primary-accession ends..

// get the value of the node GBSeq_accession-version

NodeList nodeLst_gbseqpriaccver = doc.getElementsByTagName("GBSeq_primary-accession");

Element fstNmElmnt_gbseqpriaccver = (Element) nodeLst_gbseqpriaccver.item(0);

NodeList fstNm_gbseqpriaccver = fstNmElmnt_gbseqpriaccver.getChildNodes();

out.println("<b> Document GBSeq_primary-accession version: </b>" + ((Node) fstNm_gbseqpriaccver.item(0)).getNodeValue() + "<br>");

//GBSeq_accession-version ends..

// get the value of the node GBSeq_source

NodeList nodeLst_gbseqsource = doc.getElementsByTagName("GBSeq_source");

Element fstNmElmnt_gbseqsource = (Element) nodeLst_gbseqsource.item(0);

NodeList fstNm_gbseqsource = fstNmElmnt_gbseqsource.getChildNodes();

out.println("<b> Document GBSeq_source:</b> " + ((Node) fstNm_gbseqsource.item(0)).getNodeValue() + "<br>");

//GBSeq_source ends.

// get the value of the node GBSeq_organism

NodeList nodeLst_gbseqorg = doc.getElementsByTagName("GBSeq_organism");

Element fstNmElmnt_gbseqorg = (Element) nodeLst_gbseqorg.item(0);

NodeList fstNm_gbseqorg = fstNmElmnt_gbseqorg.getChildNodes();

out.println("<b> Document GBSeq_organism: </b>" + ((Node) fstNm_gbseqorg.item(0)).getNodeValue() + "<br>");

//GBSeq_organism ends

// get the value of the node GBSeq_taxonomy

NodeList nodeLst_gbseqtax = doc.getElementsByTagName("GBSeq_taxonomy");

Element fstNmElmnt_gbseqtax = (Element) nodeLst_gbseqtax.item(0);

NodeList fstNm_gbseqtax = fstNmElmnt_gbseqtax.getChildNodes();

out.println("<b> Document GBSeq_taxonomy: </b>" + ((Node) fstNm_gbseqtax.item(0)).getNodeValue() + "<br>");

//GBSeq_taxonomy

// get the value of the node GBSeq_references

NodeList nodeLst_GbSeqRefs = doc.getElementsByTagName("GBSeq_references");

//out.println("<b> Node GBSeq_references ..Length=</b>"+nodeLst_GbSeqRefs.getLength() + "<br>");

//out.println("Node GBSeq_references <br>");

//loop through all the records of this tag - GBSeq_references.

for (int s = 0; s < nodeLst_GbSeqRefs.getLength(); s++)

{

//get the value for the node item at position s.

Node fstNode_GbSeqRefNode = nodeLst_GbSeqRefs.item(s);

// out.println(" fst_Node_GbSeqRefNode first for loop<br>");

if (fstNode_GbSeqRefNode.getNodeType() == Node.ELEMENT_NODE) //if condition for idlist nodes

{

//out.println(" second if condition <br>");

//out.println("fstNode_GbSeqRefNode in first if condition<br>");

//Element fstElmnt_gbref = (Element) fstNode;

//get the node for GBReference.

NodeList fstNmElmntLst_gbref = doc.getElementsByTagName("GBReference");

//String[] pubmedids = new String[fstNmElmntLst.getLength()];

//loop through all the Gbreference

for (int h = 0; h < fstNmElmntLst_gbref.getLength(); h++) //gets all the gbreference tag nodes

{

//get the node of the item at position s

Node fstNode_GbSeqRefNode2 = fstNmElmntLst_gbref.item(s);

if (fstNode_GbSeqRefNode2.getNodeType() == Node.ELEMENT_NODE)

//if condition -gets all the gbreference tag nodes

{

/ out.println(" fst_Node_GbSeqRefNode2 second for loop <br>");

// out.println(" fstNmElmntLst_gbref loop <br> ");

Element fstNmElmnt_gbref_ref = (Element) fstNode_GbSeqRefNode2;

//get node GBReference_reference

NodeList nodeLst_gbref_ref = fstNmElmnt_gbref_ref.getElementsByTagName("GBReference_reference");

Element nodeLst_gbref_refElement = (Element)nodeLst_gbref_ref.item(0);

NodeList textnodeLst_gbref_ref = nodeLst_gbref_refElement.getChildNodes();

out.println("<b> Document GBReference_reference: </b>" + ((Node) textnodeLst_gbref_ref.item(0)).getNodeValue() + "<br>");

//code ends for GBReference_reference

//get node GBReference_position

NodeList nodeLst_gbref_pos = fstNmElmnt_gbref_ref.getElementsByTagName("GBReference_position");

Element nodeLst_gbref_posElement = (Element)nodeLst_gbref_pos.item(0);

NodeList textnodeLst_gbref_pos = nodeLst_gbref_posElement.getChildNodes();

out.println("<b> Document GBReference_position: </b>" + ((Node) textnodeLst_gbref_pos.item(0)).getNodeValue() + "<br>");

//code ends for GBReference_reference

//gbauth loop for authors fields starts

//Element fstElmnt_gbref_auth = (Element) fstNodeauth;

NodeList fstNmElmntLst_gbauth = fstNmElmnt_gbref_ref.getElementsByTagName("GBReference_authors");

for (int k = 0; k < fstNmElmntLst_gbauth.getLength(); k++) //gets all the //gbreference tag nodes

{

Node fstNode_GbSeqRefNode3 = fstNmElmntLst_gbauth.item(k);

if (fstNode_GbSeqRefNode3.getNodeType() == Node.ELEMENT_NODE)

{

//out.println(" fst_Node_GbSeqRefNode3 authors second for loop <br>");

//out.println(" fstNmElmntLst_gbauth loop <br> ");

Element fstNmElmnt_gbref_auth = (Element) fstNode_GbSeqRefNode3;

//get node GBAuthor

NodeList nodeLst_gbref_gbauth = fstNmElmnt_gbref_auth.getElementsByTagName("GBAuthor");

//loop for all records.

//out.println("<br><b> No of gbAuthors =</b>" + nodeLst_gbref_gbauth.getLength() + "<br>");

for (int l = 0; l < nodeLst_gbref_gbauth.getLength(); l++) //gets all the / /gbreference tag nodes

{

out.println("<b> Author No=" + l + "=</b>" + nodeLst_gbref_gbauth.item(l).getChildNodes().item(0).getNodeValue() + "<br>");

}//code ends

}

}//gbauth loop ends

//get node GBReference_title

NodeList nodeLst_gbref_title = fstNmElmnt_gbref_ref.getElementsByTagName("GBReference_title");

Element nodeLst_gbref_titleElement = (Element)nodeLst_gbref_title.item(0);

NodeList textnodeLst_gbref_title = nodeLst_gbref_titleElement.getChildNodes();

out.println("<b> Document GBReference_title: </b>" + ((Node) textnodeLst_gbref_title.item(0)).getNodeValue() + "<br>");

//code ends for GBReference_reference

//get node GBReference_journal

NodeList nodeLst_gbref_journal = fstNmElmnt_gbref_ref.getElementsByTagName("GBReference_journal");

Element nodeLst_gbref_journalElement = (Element)nodeLst_gbref_journal.item(0);

NodeList textnodeLst_gbref_journal = nodeLst_gbref_journalElement.getChildNodes();

out.println("<b> Document GBReference_journal: </b>" + ((Node) textnodeLst_gbref_journal.item(0)).getNodeValue() + "<br>");

//code ends for GBReference_journal

//out.println("Features - Location/Qualifiers");

//features code starts GBSeq_feature-table

//Element fstElmnt_gbref = (Element) fstNode;

NodeList fstNmElmntLst_gbfeattab = doc.getElementsByTagName("GBSeq_feature-table");

for (int z = 0; z < fstNmElmntLst_gbfeattab.getLength(); z++) //gets all the GBSeq_feature-table tag nodes

{

Node fstNode_GbSeqRefNode7 = fstNmElmntLst_gbfeattab.item(z);

if (fstNode_GbSeqRefNode7.getNodeType() == Node.ELEMENT_NODE) //if condition for GBSeq_feature-table

{

//out.println(" fst_Node_GbSeqRefNode7 features table second for loop <br>");

//out.println(" fstNmElmntLst_gbfeattab loop <br> ");

Element fstNmElmnt_gbfeat = (Element) fstNode_GbSeqRefNode7;

//get node GB feat tab

NodeList nodeLst_gbfeat = fstNmElmnt_gbfeat.getElementsByTagName("GBFeature");

//loop for all records.

//out.println("<br> <b> No of GBFeature =</b>" + nodeLst_gbfeat.getLength() + "<br>");

for (int y = 0; y < nodeLst_gbfeat.getLength(); y++) //gets all the gbfeature tag nodes

{

Node fstNode_GbSeqRefNode8 = nodeLst_gbfeat.item(y);

out.println("<b> GBFeature No=" + y + "=</b>" + nodeLst_gbfeat.item(y).getChildNodes().item(0).getNodeValue() + "<br>");

if (fstNode_GbSeqRefNode8.getNodeType() == Node.ELEMENT_NODE)

{

//out.println(" fst_Node_GbSeqRefNode8 features table second for loop <br>");

Element fstNmElmnt_gbfeat2 = (Element) fstNode_GbSeqRefNode8;

NodeList textnodeLst_gbfeatkey = fstNmElmnt_gbfeat2.getElementsByTagName("GBFeature_key");

out.println("<b>Document GBFeature_key: </b>" + ((Node) textnodeLst_gbfeatkey.item(0)).getChildNodes().item(0).getNodeValue() + "<br>");

out.println("<br>"); //GBFeature_location

NodeList textnodeLst_gbfeatloc = fstNmElmnt_gbfeat2.getElementsByTagName("GBFeature_location");

out.println("<b>Document GBFeature_location: </b>" + ((Node) textnodeLst_gbfeatloc.item(0)).getChildNodes().item(0).getNodeValue() + "<br>");

//code ends

out.println("<br>");

//GBFeature_intervals

NodeList textnodeLst_gbfeatint = fstNmElmnt_gbfeat2.getElementsByTagName("GBFeature_intervals");

//gets all the gbreference tag nodes

for (int x = 0; x < textnodeLst_gbfeatint.getLength(); x++) {

Node fstNode_GbSeqRefNode9 = textnodeLst_gbfeatint.item(x);

if (fstNode_GbSeqRefNode9.getNodeType() == Node.ELEMENT_NODE)

{

Element fstNmElmnt_gbfeatint2 = (Element) fstNode_GbSeqRefNode9;

NodeList textnodeLst_gbintfrom = fstNmElmnt_gbfeatint2.getElementsByTagName("GBInterval_from");

out.println("Document GBFeature_Interval From: " + ((Node) textnodeLst_gbintfrom.item(0)).getChildNodes().item(0).getNodeValue() + "<br>");

//GBInterval_to

NodeList textnodeLst_gbintto = fstNmElmnt_gbfeatint2.getElementsByTagName("GBInterval_to");

out.println("Document GBFeature_Interval To: " + ((Node) textnodeLst_gbintto.item(0)).getChildNodes().item(0).getNodeValue() + "<br>");

//GBInterval_accession

//GBInterval_to

NodeList textnodeLst_gbintAcc = fstNmElmnt_gbfeatint2.getElementsByTagName("GBInterval_accession");

out.println("Document GBFeature_ GBInterval_accession: " + ((Node) textnodeLst_gbintAcc.item(0)).getChildNodes().item(0).getNodeValue() + "<br>");

}

}

out.println("<br>");

//GBFeature_intervals code ends

//GBFeature_quals

NodeList textnodeLst_gbfeatquals = fstNmElmnt_gbfeat2.getElementsByTagName("GBFeature_quals");

//out.println("Length="+textnodeLst_gbfeatquals.getLength() + "<br>");

for ( int x = 0; x < textnodeLst_gbfeatquals.getLength(); x++) //gets all the gbreference tag nodes

{

Node fstNode_GbSeqRefNode10 = textnodeLst_gbfeatquals.item(x);

if (fstNode_GbSeqRefNode10.getNodeType() ==

Node.ELEMENT_NODE)

{

Element fstNmElmnt_gbfeatint2 = (Element)

fstNode_GbSeqRefNode10;

//GBQualifier_name NodeList textnodeLst_gbintfrom =

fstNmElmnt_gbfeatint2.getElementsByTagName("GBQualifier_name");

out.println("GBQualifier_name Length="+textnodeLst_gbintfrom.getLength() + "<br>");

//GBQualifier_value

NodeList textnodeLst_gbqualval = fstNmElmnt_gbfeatint2.getElementsByTagName("GBQualifier_value");

//out.println("GBQualifier_name Length="+textnodeLst_gbqualval.getLength() + "<br>");

out.println("<br>");

//gets all the gbreference tag nodes

for ( int d = 0; d < textnodeLst_gbintfrom.getLength(); d++) { out.println("Document GBSeqRefNode10: " + ((Node)

textnodeLst_gbintfrom.item(x)).getChildNodes().item(d).getNodeValue() + "<br>");

out.println("<b>GBQualifier_name No= " + d + "=</b>" + textnodeLst_gbintfrom.item(d).getChildNodes().item(0).getNodeValue() + "<br>");

out.println("<b>GBQualifier_value No= " + d + "=</b>" + textnodeLst_gbqualval.item(d).getChildNodes().item(0).getNodeValue() + "<br>");

}

}

}

//GBFeature_quals code ends

}

}//gbfeature tag nodes code ends

}//if condition for GBSeq_feature-table ends here..

}//GBSeq_feature-table code ends here@ for loop

}////if condition -gets all the gbreference tag nodes ends

}////condition -gets all the gbreference tag nodes for ends

//NodeList fstNmElmntLst_gbsequence = doc.getElementsByTagName("GBSeq_sequence");

NodeList nodeLst_gbsequence = doc.getElementsByTagName("GBSeq_sequence");

Element fstNmElmnt_gbsequence = (Element) nodeLst_gbsequence.item(0);

NodeList fstNm_gbsequence = fstNmElmnt_gbsequence.getChildNodes();

out.println("<b> Document GBsequence :</b> " + ((Node) fstNm_gbsequence.item(0)).getNodeValue() + "<br>");

out.println("<br>");

//GBSeq_sequence code goes here.

}////if condition for idlist nodes

}//for ends@idlist nodes .

//GBSeq_references ends

}

catch (Exception e)

{

e.printStackTrace();

}

finally

{}

%>



You are here: Tutorials Languages Java