CLEiM
Cross Lingual Education in Medicine
 All Classes Namespaces Files Functions Variables
CallNCBO.java
Go to the documentation of this file.
00001 package com.uem.gsi.cleim.nlp;
00002 
00003 import java.io.File;
00004 
00005 import org.apache.commons.httpclient.HttpClient;
00006 import org.apache.commons.httpclient.methods.PostMethod;
00007 import org.apache.commons.io.FileUtils;
00008 
00009 import com.uem.gsi.cleim.util.Constants;
00010 import com.uem.gsi.cleim.util.MyDOMParser;
00011 
00012 import org.w3c.dom.Document;
00013 import org.w3c.dom.NodeList;
00014 import org.w3c.dom.Node;
00015 
00016 public class CallNCBO {
00017   
00018         //HTTP REST Web service parameter defaults
00019   private String text = "";
00020   //ICD10(44103),Medlineplus(40397),MeSH(42142),Human disease(44172)
00021   private String ontologiesToKeepInResult="40397";
00022   private String levelMax="0";
00023   private String longestOnly="false";
00024   private String wholeWordOnly="true";
00025   private String filterNumber="false";
00026   private String stopWords="null";
00027   private String withDefaultStopWords="true";
00028   private String withSynonyms="true";
00029   private String scored="true";
00030   private String ontologiesToExpand="";
00031   private String semanticTypes="";
00032   private String mappingTypes="all";
00033   private String format="xml"; //"text","tabDelimited"
00034   
00035         public CallNCBO(){
00036         }
00037         public String doRESTQuery() throws Exception{
00038                 String sSource="";
00039                 //try {
00040           HttpClient client = new HttpClient();
00041       PostMethod method = new PostMethod(Constants.NCBO_BASE+Constants.NCBO_ANNOT);
00042 
00043       // Configure the form parameters
00044       method.addParameter("apikey", Constants.NCBO_APIKEY);
00045       method.addParameter("email", Constants.NCBO_EMAIL);
00046       method.addParameter("longestOnly", longestOnly);
00047       method.addParameter("wholeWordOnly", wholeWordOnly);
00048       method.addParameter("filterNumber", filterNumber);
00049       method.addParameter("stopWords", stopWords);
00050       method.addParameter("withDefaultStopWords", withDefaultStopWords);
00051       method.addParameter("withSynonyms", withSynonyms);
00052       method.addParameter("scored", scored);
00053       method.addParameter("ontologiesToExpand", ontologiesToExpand);
00054       method.addParameter("ontologiesToKeepInResult", ontologiesToKeepInResult);
00055       method.addParameter("semanticTypes", semanticTypes);
00056       method.addParameter("levelMax", levelMax);
00057       method.addParameter("mappingTypes", mappingTypes);
00058       method.addParameter("textToAnnotate", text);
00059       //method.addParameter("format", "asText");
00060       //method.addParameter("format", "tabDelimited");
00061       method.addParameter("format", format);
00062       
00063       // Execute the POST method
00064        int statusCode = client.executeMethod(method);
00065         if( statusCode != -1 ) {
00066           sSource = method.getResponseBodyAsString();
00067           method.releaseConnection();
00068           //System.out.println(contents);
00069         }else{
00070                 sSource="<statusCode>"+statusCode+"</statusCode>";
00071                 //System.out.println("status: "+statusCode);
00072         }
00073     //} catch( Exception e ) {
00074     //  e.printStackTrace();
00075     //}
00076                 return sSource;
00077         }
00078         public void setText(String ptext){
00079                 this.text=ptext;
00080         }
00081         public void setOntInRes(String pontologiesToKeepInResult){
00082                 this.ontologiesToKeepInResult=pontologiesToKeepInResult;
00083         }
00084         public void setLevelMax(String plevelMax){
00085                 this.levelMax=plevelMax;
00086         }
00087         public void setLongestOnly(String plongestOnly){
00088                 this.longestOnly=plongestOnly;
00089         }
00090         public void setWholeWordOnly(String pwholeWordOnly){
00091                 this.wholeWordOnly=pwholeWordOnly;
00092         }
00093         public void setFilterNumber(String pfilterNumber){
00094                 this.filterNumber=pfilterNumber;
00095         }
00096         public void setStopWords(String pstopWords){
00097                 this.stopWords=pstopWords;
00098         }
00099         public void setWithDefaultStopWords(String pwithDefaultStopWords){
00100                 this.withDefaultStopWords=pwithDefaultStopWords;
00101         }
00102         public void setWithSynonyms(String pwithSynonyms){
00103                 this.withSynonyms=pwithSynonyms;
00104         }
00105         public void setScored(String pscored){
00106                 this.scored=pscored;
00107         }
00108         public void setOntologiesToExpand(String pontologiesToExpand){
00109                 this.ontologiesToExpand=pontologiesToExpand;
00110         }
00111         public void setSemanticTypes(String psemanticTypes){
00112                 this.semanticTypes=psemanticTypes;
00113         }
00114         public void setMappingTypes(String pmappingTypes){
00115                 this.mappingTypes=pmappingTypes;
00116         }
00117         public void setFormat(String pformat){
00118                 this.format=pformat;
00119         }
00120         
00121   public static void main( String[] args ) {
00122          //System.out.println("********************* NCBO ANNOTATOR CLIENT TEST ************************* \n");
00123          try {
00124                  CallNCBO oNCBO=new CallNCBO();
00125                  File fClinicalCase=new File("D:/UEM/MedicalMiner/ClinicalCases/CasoClinicoNCBO.txt");
00126                  //File fClinicalCase=new File("D:/UEM/MedicalMiner/ClinicalCases/CasoClinico20.txt");
00127                  // Read the entire contents of sample.txt
00128                  String text=FileUtils.readFileToString(fClinicalCase);
00129                  oNCBO.setText("diabetes");
00130                  //System.out.print(oNCBO.doRESTQuery());
00131                  //System.exit(0);
00132                  Document doc = MyDOMParser.getDocument(oNCBO.doRESTQuery());
00133                  //String elementName = doc.getNodeName();
00134                  //String textInXml=doc.getElementsByTagName("textToAnnotate").item(0).getFirstChild().getNodeValue();
00135                  NodeList listAnnot=doc.getElementsByTagName("annotationBean");
00136                  
00137                  for (int i=0;i<listAnnot.getLength();i++){
00138                          //Initialize data to show
00139                          String localOntologyId="",fullId="",preferredName="";
00140                          String contextName="",isDirect="",from="",to="",nameInText="";
00141                          Node annotNode=listAnnot.item(i);
00142                          //System.out.println(listAnnot.item(i).getNodeName());
00143                          NodeList listChildAnnot=annotNode.getChildNodes();
00144                          for (int j=0;j<listChildAnnot.getLength();j++){
00145                                  Node nodeTemp1=listChildAnnot.item(j);
00146                                  //System.out.println(nodeTemp1.getNodeName());
00147                                  //Concept
00148                          if (nodeTemp1.getNodeName().equals("concept")){
00149                                  //System.out.println(nodeTemp1.getNodeName());
00150                                  NodeList listChildRes=nodeTemp1.getChildNodes();
00151                                  for (int k=0;k<listChildRes.getLength();k++){
00152                                          Node node1=listChildRes.item(k);
00153                                          String node1name=node1.getNodeName();
00154                                          //Elements selection
00155                                          if (node1name.equals("localOntologyId"))
00156                                                  localOntologyId=node1.getFirstChild().getNodeValue().trim();
00157                                          else if (node1name.equals("fullId"))
00158                                                  fullId=node1.getFirstChild().getNodeValue().trim();
00159                                          else if (node1name.equals("preferredName"))
00160                                                  preferredName=node1.getFirstChild().getNodeValue().trim();
00161                  }
00162                                         
00163                          }//Context
00164                          else if (nodeTemp1.getNodeName().equals("context")){
00165                                  //System.out.println(nodeTemp1.getNodeName());
00166                                  NodeList listChildRes=nodeTemp1.getChildNodes();
00167                                  for (int k=0;k<listChildRes.getLength();k++){
00168                                          Node node1=listChildRes.item(k);
00169                                          String node1name=node1.getNodeName();
00170                                          //System.out.println(node1.getNodeName()+"="+node1.getNodeValue());
00171                                          //Seleccionar elementos
00172                                          if (node1name.equals("contextName"))
00173                                                  contextName=node1.getFirstChild().getNodeValue().trim();
00174                                          else if (node1name.equals("isDirect"))
00175                                                  isDirect=node1.getFirstChild().getNodeValue().trim();
00176                                          else if (node1name.equals("from"))
00177                                                  from=node1.getFirstChild().getNodeValue().trim();
00178                                          else if (node1name.equals("to"))
00179                                                  to=node1.getFirstChild().getNodeValue().trim();
00180                                                  
00181                                                  }/*else if (node1name.equals("term")){
00182                                                          if (node1.getFirstChild()!=null && node1.getFirstChild().getFirstChild()!=null)
00183                                                                  System.out.println("***"+node1.getFirstChild().getNodeName()+"="+node1.getFirstChild().getFirstChild().getNodeValue().trim());
00184                                  }else if (node1name.equals("concept")){
00185                                                          
00186                                                  }*/
00187                                  }
00188                          }
00189                                  nameInText=text.substring(new Integer(from)-1,new Integer(to));
00190                                  //nameInText=textInXml.substring(new Integer(from)-1,new Integer(to));
00191                          
00192                                  System.out.println(localOntologyId+","+fullId+","+preferredName+","+
00193                                                  contextName+","+isDirect+","+from+","+to+","+nameInText);
00194                          }
00195          }
00196          catch( Exception e ) {
00197            e.printStackTrace();
00198          }
00199   }
00200 
00201 }