22
33import org .apache .commons .lang3 .StringUtils ;
44import org .apache .commons .lang3 .exception .ExceptionUtils ;
5- import org .apache .http .HttpResponse ;
6- import org .apache .http .client .HttpClient ;
7- import org .apache .http .client .methods .HttpGet ;
8- import org .apache .http .impl .client .HttpClientBuilder ;
95import org .slf4j .Logger ;
106import org .slf4j .LoggerFactory ;
117import org .w3c .dom .Document ;
2117import java .io .IOException ;
2218import java .io .InputStream ;
2319import java .net .ConnectException ;
20+ import java .net .URL ;
2421import java .util .Collection ;
2522import java .util .TreeSet ;
2623import java .util .regex .Matcher ;
2724import java .util .regex .Pattern ;
2825
2926/**
3027 * Use the NCBO annotator to find ontology terms matching strings.
31- *
28+ *
3229 */
3330public class AnnotatorClient {
3431
@@ -49,7 +46,7 @@ public class AnnotatorClient {
4946 private static String ontologies = HP_ONTOLOGY + "," + DOID_ONTOLOGY ;
5047
5148 /**
52- *
49+ *
5350 * @param term
5451 * @return
5552 * @throws ParserConfigurationException
@@ -58,7 +55,7 @@ public class AnnotatorClient {
5855 * @throws Exception
5956 */
6057 public static Collection <AnnotatorResponse > findTerm ( String term )
61- throws IOException , ParserConfigurationException , IllegalStateException , SAXException {
58+ throws IOException , ParserConfigurationException , IllegalStateException , SAXException {
6259 if ( StringUtils .isBlank ( API_KEY ) ) {
6360 throw new IllegalStateException ( "NCBO ncbo.api.key needs to be configured" );
6461 }
@@ -70,18 +67,16 @@ public static Collection<AnnotatorResponse> findTerm( String term )
7067 if ( StringUtils .isBlank ( termClean ) ) return responsesFound ;
7168
7269 String url = ANNOTATOR_URL + "apikey=" + API_KEY + "&max_level=0&ontologies=" + ontologies
73- + "&format=xml&text=" + termClean ;
70+ + "&format=xml&text=" + termClean ;
7471
7572 if ( log .isDebugEnabled () ) log .debug ( "request url: " + url );
7673
7774 int tries = 0 ;
7875
79- HttpClient httpclient = HttpClientBuilder .create ().build ();
80- HttpResponse response = null ;
76+ InputStream response = null ;
8177 while ( response == null && tries < MAX_TRIES ) {
8278 try {
83- HttpGet httpGet = new HttpGet ( url );
84- response = httpclient .execute ( httpGet );
79+ response = new URL ( url ).openStream ();
8580 } catch ( IOException e ) {
8681 try {
8782 Thread .sleep ( 10000 ); // long wait...
@@ -99,7 +94,7 @@ public static Collection<AnnotatorResponse> findTerm( String term )
9994
10095 DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
10196 DocumentBuilder builder = factory .newDocumentBuilder ();
102- Document document = builder .parse ( response . getEntity (). getContent () );
97+ Document document = builder .parse ( response );
10398 NodeList nodes = document .getElementsByTagName ( "annotation" );
10499
105100 // for each response receive, populate the return objects
@@ -124,7 +119,7 @@ public static Collection<AnnotatorResponse> findTerm( String term )
124119 Integer to = new Integer ( infoE .getElementsByTagName ( "to" ).item ( 0 ).getTextContent () );
125120
126121 AnnotatorResponse annotatorResponse = new AnnotatorResponse ( valueUri , matchType , txtMatched , from , to ,
127- ontologyUsed , termClean );
122+ ontologyUsed , termClean );
128123
129124 responsesFound .add ( annotatorResponse );
130125 }
@@ -135,7 +130,7 @@ public static Collection<AnnotatorResponse> findTerm( String term )
135130
136131 /**
137132 * FIXME only knows about HP and DOID
138- *
133+ *
139134 * @param url
140135 * @return
141136 */
@@ -153,10 +148,10 @@ private static String findOntologyUsed( String url ) {
153148
154149 /**
155150 * return the label associated with an conceptid. FIXME why are we doing things this way, it must be terribly slow
156- *
151+ *
157152 * @param ontologyId what virtual ontology to use
158153 * @param identifier the identifier, knows about: OMIM, DOID, MESH
159- * @return the label for that term, example : ABCD syndrome
154+ * @return the label for that term, example : ABCD syndrome
160155 */
161156 public static String findLabelForIdentifier ( String ontologyId , String identifier ) {
162157
@@ -173,43 +168,32 @@ public static String findLabelForIdentifier( String ontologyId, String identifie
173168
174169 // http://data.bioontology.org/ontologies/OMIM
175170
176- // http://data.bioontology.org/ontologies/OMIM/classes/http%3A%2F%2Fpurl.bioontology.org%2Fontology%2FOMIM%2F600374
177-
178- for ( int i = 0 ; i < MAX_TRIES ; i ++ ) {
179- try {
180- String url ;
181-
182- // These Ontology identifiers could potentially be retrieved, using OntologyLookup
183- switch ( ontologyId ) {
184- case "OMIM" :
185- case "MESH" :
186- url = "http://data.bioontology.org/ontologies/" + ontologyId
187- + "/classes/http%3A%2F%2Fpurl.bioontology.org%2Fontology%2FMESH%2F"
188- + identifier
189- + "/?apikey=" + API_KEY + "&format=xml" ;
190- break ;
191- case "DOID" :
192- url = "http://data.bioontology.org/ontologies/" + ontologyId + "/classes/http%3A%2F%2Fpurl.obolibrary.org%2Fobo%2F"
193- + identifier
194- + "/?apikey=" + API_KEY + "&format=xml" ;
195- break ;
196- default :
197- throw new IllegalArgumentException ( "Don't know how to deal with " + ontologyId );
198- }
199-
200- log .debug ( url );
201-
202- HttpClient httpclient = HttpClientBuilder .create ().build ();
171+ // http://data.bioontology.org/ontologies/OMIM/classes/http%3A%2F%2Fpurl.bioontology.org%2Fontology%2FOMIM%2F600374
172+
173+ String url ;
174+
175+ // These Ontology identifiers could potentially be retrieved, using OntologyLookup
176+ switch ( ontologyId ) {
177+ case "OMIM" :
178+ case "MESH" :
179+ url = "http://data.bioontology.org/ontologies/" + ontologyId
180+ + "/classes/http%3A%2F%2Fpurl.bioontology.org%2Fontology%2FMESH%2F"
181+ + identifier
182+ + "/?apikey=" + API_KEY + "&format=xml" ;
183+ break ;
184+ case "DOID" :
185+ url = "http://data.bioontology.org/ontologies/" + ontologyId + "/classes/http%3A%2F%2Fpurl.obolibrary.org%2Fobo%2F"
186+ + identifier
187+ + "/?apikey=" + API_KEY + "&format=xml" ;
188+ break ;
189+ default :
190+ throw new IllegalArgumentException ( "Don't know how to deal with " + ontologyId );
191+ }
203192
204- HttpGet httpGet = new HttpGet ( url );
205- HttpResponse response = httpclient .execute ( httpGet );
206-
207- // term was not found
208- if ( response .getStatusLine ().getStatusCode () == 404 ) {
209- log .debug ( "404 returned, the term: " + identifier + " was not found" );
210- return null ;
211- }
193+ log .debug ( url );
212194
195+ for ( int i = 0 ; i < MAX_TRIES ; i ++ ) {
196+ try ( InputStream response = new URL ( url ).openStream () ) {
213197 return findLabel ( response );
214198 } catch ( ConnectException ce ) {
215199 try {
@@ -227,10 +211,10 @@ public static String findLabelForIdentifier( String ontologyId, String identifie
227211 /**
228212 * using the response return the label associated with the request
229213 */
230- private static String findLabel ( HttpResponse response ) throws Exception {
214+ private static String findLabel ( InputStream response ) throws Exception {
231215 DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
232216 DocumentBuilder builder = factory .newDocumentBuilder ();
233- try ( InputStream content = response . getEntity (). getContent () ) {
217+ try ( InputStream content = response ) {
234218 Document document = builder .parse ( content );
235219 NodeList nodes = document .getElementsByTagName ( "prefLabel" );
236220 if ( nodes == null ) {
0 commit comments