View Javadoc
1   package ubic.basecode.ontology.ncbo;
2   
3   import java.util.Collection;
4   import java.util.HashSet;
5   import java.util.Map;
6   
7   import javax.xml.parsers.DocumentBuilder;
8   import javax.xml.parsers.DocumentBuilderFactory;
9   
10  import org.apache.commons.lang3.StringUtils;
11  import org.apache.commons.lang3.exception.ExceptionUtils;
12  import org.apache.http.HttpResponse;
13  import org.apache.http.client.HttpClient;
14  import org.apache.http.client.methods.HttpGet;
15  import org.apache.http.impl.client.HttpClientBuilder;
16  import org.slf4j.Logger;
17  import org.slf4j.LoggerFactory;
18  import org.w3c.dom.Document;
19  import org.w3c.dom.Element;
20  import org.w3c.dom.Node;
21  import org.w3c.dom.NodeList;
22  
23  import ubic.basecode.util.Configuration;
24  
25  /**
26   * to use the OMIM api
27   * TODO Document Me
28   * 
29   * @author Nicolas
30   */
31  public class OmimAnnotatorClient {
32  
33      private static String OMIM_API_URL = "http://api.omim.org/api/entry";
34  
35      // this OMIM_API_KEY needs to be added to basecode.properties
36      private static String OMIM_API_KEY = Configuration.getString( "omim.api.key" );
37  
38      private static Logger log = LoggerFactory.getLogger( OmimAnnotatorClient.class );
39  
40      /**
41       * Giving a set of OMIM id return for each id its list of related publication
42       * 
43       * @param omimIds      the omimIds we want to find the publication
44       * @param mimToPubmeds the results, pass by reference for multiple calls, limit of size 10
45       */
46      public static Map<Long, Collection<Long>> findLinkedPublications( Collection<Long> omimIds,
47              Map<Long, Collection<Long>> mimToPubmeds ) throws InterruptedException {
48  
49          if ( omimIds.size() > 10 || omimIds.isEmpty() ) {
50              throw new IllegalArgumentException( "Size of the Omim ids must be between 1 and 10,  Size Found: "
51                      + omimIds.size() );
52          }
53  
54          // From OMIM documentation : The rate of requests is currently limited to 4 requests per second.
55          // if too many request are made, the ip gets ban, sleep makes this not possible
56          Thread.sleep( 700 );
57  
58          String omimIdsAsString = StringUtils.removeEnd( StringUtils.join( omimIds, "," ), "," );
59  
60          try {
61  
62              String url = OMIM_API_URL + "?mimNumber=" + omimIdsAsString + "&format=xml&include=referenceList&apiKey="
63                      + OMIM_API_KEY;
64  
65              log.info( "request url: " + url );
66  
67              HttpClient httpclient = HttpClientBuilder.create().build();
68              HttpGet httpGet = new HttpGet( url );
69              HttpResponse response = httpclient.execute( httpGet );
70  
71              // term was not found
72              if ( response.getStatusLine().getStatusCode() == 404 ) {
73                  throw new Exception( "404 returned" );
74              }
75  
76              return findPublications( response, mimToPubmeds );
77          } catch ( Exception e ) {
78              log.error( ExceptionUtils.getStackTrace( e ) );
79              return null;
80          }
81      }
82  
83      private static Map<Long, Collection<Long>> findPublications( HttpResponse response,
84              Map<Long, Collection<Long>> mimToPubmeds ) throws Exception {
85  
86          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
87          DocumentBuilder builder = factory.newDocumentBuilder();
88          Document document = builder.parse( response.getEntity().getContent() );
89          NodeList nodes = document.getElementsByTagName( "reference" );
90  
91          // for each response receive, populate the return objects
92          for ( int temp = 0; temp < nodes.getLength(); temp++ ) {
93  
94              Node nNode = nodes.item( temp );
95              Element eElement = ( Element ) nNode;
96  
97              if ( eElement.getElementsByTagName( "pubmedID" ).item( 0 ) != null
98                      && eElement.getElementsByTagName( "mimNumber" ).item( 0 ) != null ) {
99  
100                 Long pubmedID = new Long( eElement.getElementsByTagName( "pubmedID" ).item( 0 ).getTextContent() );
101                 Long mimNumber = new Long( eElement.getElementsByTagName( "mimNumber" ).item( 0 ).getTextContent() );
102 
103                 Collection<Long> pubmeds = new HashSet<>();
104 
105                 if ( mimToPubmeds.get( mimNumber ) != null ) {
106                     pubmeds = mimToPubmeds.get( mimNumber );
107                 }
108                 pubmeds.add( pubmedID );
109 
110                 mimToPubmeds.put( mimNumber, pubmeds );
111             }
112         }
113         return mimToPubmeds;
114     }
115 
116 }