1 package ubic.basecode.ontology.ncbo;
2
3 import org.apache.commons.lang3.StringUtils;
4 import org.apache.commons.lang3.exception.ExceptionUtils;
5 import org.slf4j.Logger;
6 import org.slf4j.LoggerFactory;
7 import org.w3c.dom.Document;
8 import org.w3c.dom.Element;
9 import org.w3c.dom.Node;
10 import org.w3c.dom.NodeList;
11 import ubic.basecode.util.Configuration;
12
13 import javax.xml.parsers.DocumentBuilder;
14 import javax.xml.parsers.DocumentBuilderFactory;
15 import java.io.InputStream;
16 import java.net.URL;
17 import java.util.Collection;
18 import java.util.HashSet;
19 import java.util.Map;
20
21
22
23
24
25
26
27 public class OmimAnnotatorClient {
28
29 private static String OMIM_API_URL = "http://api.omim.org/api/entry";
30
31
32 private static String OMIM_API_KEY = Configuration.getString( "omim.api.key" );
33
34 private static Logger log = LoggerFactory.getLogger( OmimAnnotatorClient.class );
35
36
37
38
39
40
41
42 public static Map<Long, Collection<Long>> findLinkedPublications( Collection<Long> omimIds,
43 Map<Long, Collection<Long>> mimToPubmeds ) throws InterruptedException {
44
45 if ( omimIds.size() > 10 || omimIds.isEmpty() ) {
46 throw new IllegalArgumentException( "Size of the Omim ids must be between 1 and 10, Size Found: "
47 + omimIds.size() );
48 }
49
50
51
52 Thread.sleep( 700 );
53
54 String omimIdsAsString = StringUtils.removeEnd( StringUtils.join( omimIds, "," ), "," );
55
56 String url = OMIM_API_URL + "?mimNumber=" + omimIdsAsString + "&format=xml&include=referenceList&apiKey="
57 + OMIM_API_KEY;
58
59 log.info( "request url: " + url );
60
61 try ( InputStream response = new URL( url ).openConnection().getInputStream() ) {
62 return findPublications( response, mimToPubmeds );
63 } catch ( Exception e ) {
64 log.error( ExceptionUtils.getStackTrace( e ) );
65 return null;
66 }
67 }
68
69 private static Map<Long, Collection<Long>> findPublications( InputStream response,
70 Map<Long, Collection<Long>> mimToPubmeds ) throws Exception {
71
72 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
73 DocumentBuilder builder = factory.newDocumentBuilder();
74 Document document = builder.parse( response );
75 NodeList nodes = document.getElementsByTagName( "reference" );
76
77
78 for ( int temp = 0; temp < nodes.getLength(); temp++ ) {
79
80 Node nNode = nodes.item( temp );
81 Element eElement = ( Element ) nNode;
82
83 if ( eElement.getElementsByTagName( "pubmedID" ).item( 0 ) != null
84 && eElement.getElementsByTagName( "mimNumber" ).item( 0 ) != null ) {
85
86 Long pubmedID = new Long( eElement.getElementsByTagName( "pubmedID" ).item( 0 ).getTextContent() );
87 Long mimNumber = new Long( eElement.getElementsByTagName( "mimNumber" ).item( 0 ).getTextContent() );
88
89 Collection<Long> pubmeds = new HashSet<>();
90
91 if ( mimToPubmeds.get( mimNumber ) != null ) {
92 pubmeds = mimToPubmeds.get( mimNumber );
93 }
94 pubmeds.add( pubmedID );
95
96 mimToPubmeds.put( mimNumber, pubmeds );
97 }
98 }
99 return mimToPubmeds;
100 }
101
102 }