Java-for-beginners - code 1 explanation

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

 

Code Explanation:-

The first step is to declare that this is a JSP file and that we are going to import and use java API functions for our program below.

//declare that this is a java program

<%@ page language = "java"%>

//import all the java api libraries

<%@ 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.*" %>

<%

//java program begins.

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

//create an http session

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

try //the try catch block starts here

{

//this is the base URL of the String EMBL db. Store it in a variable

String URLString = http://string-db.org/api/psi-mi-tab/interactionsList?identifiers=+ request.getParameter(“txtSearchWords”) + “& required_score=” + request.getParameter(“txtReqScore”);

//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);

URL yahoo = new URL(URLString); // store the URL in the a URL class object

//create the file name for storing the output from the String db.

Random rn = new Random();

int rnval = rn.nextInt() ;

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

File file = new File(fname);

String tempstr = "";

//create the handle to the stream that sends the input URL to the db using the stream created and then starts reading the output data from the db.

BufferedReader in = new BufferedReader( new InputStreamReader(yahoo.openStream())); //read the file

//define the variables for reading and writing the data

String inputLine;

String temp = "";

String taxonId = "";

String arrVal[] ;

String arrVal2[] ;

String arrVal3[] ;

int reccnt = 0;

String tscore = "";

String score = "";

String escore = "";

String dscore = "";

String temp2 = "";

//Print to the browser that the output display starts.

out.println("<b> List of Protein Interactors for your query </b><br>");

out.println("<b>--------------------------------------------------------</b> <br>");

//this loop reads the content of the output line by line

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

{

//Here we just parse the data received and present it in a readable format.

out.println("<b>Record No:-"+ ( reccnt+ 1) + " </b> <br>");

tempstr = tempstr + inputLine ; //store the line data in another variable.

temp = inputLine ;

arrVal = temp.split("\t"); //split the line content at each tab space in the line. Here we assume that we know the format of the output coming from the db, which is in a continuous set of words, each separated by tab space.

for( int i=0;i< arrVal.length; i++) //loop to find all the column values.

{

if (i == 0) //if this is the first column of the line i.e. the start of the output line

{

//the array has stored the data in each variable. Thus, we now just present the data to the user on the browser.

out.println("String ID:"+ arrVal[i] +"<br>"); //data at array position 0

out.println("NCBI ID:"+ arrVal[i+1] +"<br>");//data at array position 1

out.println("Preferred Name:"+ arrVal[i+2] +"<br>");//data at array position 2

//data at array position 3

out.println("String DB Name:"+ arrVal[i+3] +"<br>"); //data at array position 9

out.println("String Db Taxonomy ID:"+ arrVal[i+9] +"<br>");

//data at array position 10

out.println("NCBI taxonomy ID:"+ arrVal[i+10] +"<br>");

//data at array position 14

out.println("Score String:"+ arrVal[i+14] +"<br>");

//sometimes the value in an array position is again a string of data. Thus, we need to split the data in the same manner and use it to

arrVal3 = arrVal[i+14].split("\\|"); //split the array at each ‘\’ character

for( int j=0; j < arrVal3.length; j++) //loop through all the elements

{

out.println( "" + arrVal3[j] +" <br>"); //print data at the index position

}

}

}

//loop ends here

reccnt++; // variable to count no. Of records.

out.println("<b>--------------------------------------------------------</b> <br>");

}

in.close(); //close the stream

//store the URL String

hs.setAttribute("urlstring",URLString);

System.out.println( "urlstring="+URLString); //print the file name to tomcat log

} //try block ends

catch(Exception ex) //catch block to handle any error or exception that may occur

{

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

//get the handle to the write the error to the browser.

PrintWriter pw = response.getWriter();

ex.printStackTrace(pw); //print the details of the error to the browser

} //code ends

%> //this means that the java program ends

So the complete code is shown to you with the logic. Notice how easy it becomes to actually write the code if the logic is clear from the start.



You are here: Tutorials Languages Java