Coverage Report - uk.ac.ebi.ep.biomart.adapter.BiomartAdapter
 
Classes in this File Line Coverage Branch Coverage Complexity
BiomartAdapter
70%
63/90
75%
9/12
3.571
 
 1  
 package uk.ac.ebi.ep.biomart.adapter;
 2  
 
 3  
 import java.io.BufferedReader;
 4  
 import java.io.IOException;
 5  
 import java.io.InputStreamReader;
 6  
 import java.io.UnsupportedEncodingException;
 7  
 import java.net.MalformedURLException;
 8  
 import java.net.URL;
 9  
 import java.net.URLConnection;
 10  
 import java.util.ArrayList;
 11  
 import java.util.List;
 12  
 
 13  
 import org.apache.log4j.Logger;
 14  
 
 15  
 import uk.ac.ebi.ep.enzyme.model.EnzymeReaction;
 16  
 import uk.ac.ebi.ep.enzyme.model.ReactionPathway;
 17  
 import uk.ac.ebi.util.result.DataTypeConverter;
 18  
 
 19  
 /**
 20  
  *
 21  
  * @since   1.0
 22  
  * @version $LastChangedRevision: 1081 $ <br/>
 23  
  *          $LastChangedDate: 2012-08-17 13:45:00 +0100 (Fri, 17 Aug 2012) $ <br/>
 24  
  *          $Author: ralcantara $
 25  
  * @author  $Author: ralcantara $
 26  
  */
 27  3
 public class BiomartAdapter {
 28  
 
 29  1
         private static final Logger LOGGER = Logger.getLogger(BiomartAdapter.class);
 30  
 
 31  
         /**
 32  
          * Limits a list of results to only two.
 33  
          * @param results
 34  
          *            a list
 35  
          * @return Only the first two elements in the list.
 36  
          */
 37  
         private static List<String> limitResults(List<String> results) {
 38  1
                 List<String> subResults = null;
 39  1
                 if (results.size() > 0) {
 40  1
                         subResults = results.subList(0, 2);
 41  
                 } else {
 42  0
                         subResults = results;
 43  
                 }
 44  1
                 return subResults;
 45  
         }
 46  
 
 47  
         /**
 48  
          * Retrieves Reactome pathway stable IDs for a given Reactome reaction
 49  
          * stable ID.
 50  
          * @param reactionStableId a Reactome reaction stable ID
 51  
          * @return a list of Reactome pathway stable IDs
 52  
          * @throws BiomartFetchDataException
 53  
          */
 54  
         public List<String> getPathwaysByReactionId(String reactionStableId)
 55  
         throws BiomartFetchDataException {
 56  0
                 String idWithoutVersion = reactionStableId.split("\\.")[0];
 57  0
                 Object[] reactionIdArg = { idWithoutVersion };
 58  0
                 String getPathwaysByReactionIdQuery = Transformer.getMessageTemplate(
 59  
                                 "getPathwaysByReactionIdQuery", reactionIdArg);
 60  0
                 Object[] getPathwaysByReactionIdQueryArg =
 61  
                                 { getPathwaysByReactionIdQuery };
 62  0
                 String query = Transformer.getMessageTemplate("queryTpl",
 63  
                                 getPathwaysByReactionIdQueryArg);
 64  0
                 String baseUrl = Transformer.getMessageTemplate("baseUrl");
 65  0
                 URLConnection uCon = sendRequest(baseUrl, query);
 66  0
                 List<String> results = parsePathwaysResponse(uCon);
 67  
                 // Test Reactome is too slow to handle more than 2
 68  
                 // return results;
 69  0
                 return limitResults(results);
 70  
         }
 71  
 
 72  
         /**
 73  
          * Retrieves reactions from Reactome for a given UniProt accession.
 74  
          * @param accession a UniProt accession
 75  
          * @return a list of ReactionPathway objects with only reactions, whose
 76  
          *                 only xrefs will be plain Reactome pathway stable IDs.
 77  
          * @throws BiomartFetchDataException
 78  
          */
 79  
         public List<ReactionPathway> getReactionsByUniprotAccession(String accession)
 80  
         throws BiomartFetchDataException {
 81  1
                 Object[] reactionIdArg = { accession };
 82  1
                 String getReactionsByUniprotIdQuery = Transformer.getMessageTemplate(
 83  
                                 "getReactionsByUniprotIdQuery", reactionIdArg);
 84  1
                 Object[] getReactionsByUniprotIdQueryArg =
 85  
                                 { getReactionsByUniprotIdQuery };
 86  1
                 String query = Transformer.getMessageTemplate("queryTpl",
 87  
                                 getReactionsByUniprotIdQueryArg);
 88  1
                 String baseUrl = Transformer.getMessageTemplate("baseUrl");
 89  1
                 URLConnection uCon = sendRequest(baseUrl, query);
 90  1
                 List<ReactionPathway> results = parseReactionsResponse(uCon);
 91  1
                 return results;
 92  
 
 93  
         }
 94  
 
 95  
         /**
 96  
          * Retrieves Reactome pathway stable IDs for a given UniProt accession.
 97  
          * @param accession a UniProt accession
 98  
          * @return a list of Reactome pathway stable IDs
 99  
          * @throws BiomartFetchDataException
 100  
          */
 101  
         public List<String> getPathwaysByUniprotAccession(String accession)
 102  
         throws BiomartFetchDataException {
 103  1
                 Object[] uniprotArg = { accession };
 104  1
                 String getPathwaysByUniprotIdQuery = Transformer.getMessageTemplate(
 105  
                                 "getPathwaysByUniprotIdQuery", uniprotArg);
 106  1
                 Object[] getPathwaysByUniprotIdQueryArg =
 107  
                                 { getPathwaysByUniprotIdQuery };
 108  1
                 String query = Transformer.getMessageTemplate("queryTpl",
 109  
                                 getPathwaysByUniprotIdQueryArg);
 110  1
                 String baseUrl = Transformer.getMessageTemplate("baseUrl");
 111  1
                 URLConnection uCon = sendRequest(baseUrl, query);
 112  1
                 List<String> results = parsePathwaysResponse(uCon);
 113  1
                 return limitResults(results);
 114  
         }
 115  
 
 116  
         /**
 117  
          * Prepares the URL and sends a request to BioMart.
 118  
          * 
 119  
          * @param baseUrl the BioMart base URL.
 120  
          * @param query the query (XML) sent to BioMart.
 121  
          * @return a URLConnection.
 122  
          * @throws BiomartFetchDataException
 123  
          *             in case of problem with the URL, its encoding or while
 124  
          *             opening the connection.
 125  
          */
 126  
         protected URLConnection sendRequest(String baseUrl, String query)
 127  
         throws BiomartFetchDataException {
 128  3
                 URL url = null;
 129  
                 try {
 130  3
                         url = DataTypeConverter.createEncodedUrl(baseUrl, query);
 131  0
                 } catch (UnsupportedEncodingException ex) {
 132  0
                         throw new BiomartFetchDataException(
 133  
                                         "Failed to encode the url for Biomart request " + baseUrl
 134  
                                         + " " + query, ex);
 135  0
                 } catch (MalformedURLException ex) {
 136  0
                         throw new BiomartFetchDataException(
 137  
                                         "Failed to create the url for Biomart request " + baseUrl
 138  
                                         + " " + query, ex);
 139  3
                 }
 140  3
                 URLConnection uCon = null;
 141  
                 try {
 142  3
                         uCon = url.openConnection();
 143  0
                 } catch (IOException ex) {
 144  0
                         throw new BiomartFetchDataException(
 145  
                                         "Unable to connect to Biomart service to process request "
 146  
                                         + url.toString(), ex);
 147  3
                 }
 148  3
                 return uCon;
 149  
         }
 150  
 
 151  
    /**
 152  
     * Parses the response from BioMart containing pathways info.
 153  
     * @param uCon
 154  
     * @return a list of Reactome pathway stable IDs.
 155  
     * @throws BiomartFetchDataException in case of error reading from the
 156  
     *                 connection, or if BioMart returns an error message.
 157  
     */
 158  
    private static List<String> parsePathwaysResponse(URLConnection uCon) 
 159  
    throws BiomartFetchDataException {
 160  1
        List<String> results = new ArrayList<String>();
 161  1
         BufferedReader in = null;
 162  
         try {
 163  1
             in = new BufferedReader(new InputStreamReader(uCon.getInputStream()));
 164  1
             String inputLine, errorMsg = "";
 165  14
             while ((inputLine = in.readLine()) != null) {
 166  13
                     if (inputLine.indexOf("ERROR") > -1){
 167  0
                             errorMsg += inputLine;
 168  
                     } else {
 169  13
                     final String pathwayId = inputLine.trim();
 170  13
                     if (!results.contains(pathwayId)){
 171  9
                                             results.add(pathwayId);
 172  
                     }
 173  13
                     }
 174  
             }
 175  1
             if (errorMsg.length() > 0){
 176  0
                     LOGGER.error(errorMsg);
 177  0
                         throw new BiomartFetchDataException(errorMsg);
 178  
                 }
 179  0
         } catch (IOException ex) {
 180  0
             throw new BiomartFetchDataException("Failed to read the response of the Url connection"
 181  
                             + uCon.toString(), ex);
 182  
         } finally {
 183  0
             try {
 184  1
                                 in.close();
 185  0
                         } catch (IOException e) {
 186  0
                                 LOGGER.error("Unable to close BufferedReader", e);
 187  1
                         }
 188  0
         }
 189  1
         LOGGER.debug("Pathways parsed: " + results);
 190  1
        return results;
 191  
    }
 192  
 
 193  
         /**
 194  
         * Parses the response from BioMart containing reaction info.
 195  
         * @param uCon a connection to BioMart.
 196  
         * @return a list of ReactionPathway objects containing information only
 197  
         *                 about reactions, whose xrefs will be Reactome reaction IDs (not
 198  
         *                 whole URLs).
 199  
         * @throws BiomartFetchDataException in case of problem reading from the
 200  
         *                 connection.
 201  
         */
 202  
    private static List<ReactionPathway> parseReactionsResponse(URLConnection uCon)
 203  
    throws BiomartFetchDataException {
 204  1
        List<ReactionPathway> reactionPathways = new ArrayList<ReactionPathway>();
 205  1
         BufferedReader in = null;
 206  
         try {
 207  1
             in = new BufferedReader(new InputStreamReader(uCon.getInputStream()));
 208  
             String inputLine;
 209  3
             while ((inputLine = in.readLine()) != null) {
 210  2
                 String[]fieldValues = inputLine.split("\t");
 211  2
                 ReactionPathway reactionPathway = new ReactionPathway();
 212  2
                 EnzymeReaction enzymeReaction = new EnzymeReaction();
 213  2
                 String reactionId = fieldValues[0].trim();
 214  2
                 List<Object> reactomeReactionId = new ArrayList<Object>();
 215  2
                 reactomeReactionId.add(reactionId);
 216  2
                 enzymeReaction.setXrefs(reactomeReactionId);
 217  2
                 String reactionName = fieldValues[1].trim();                
 218  2
                 enzymeReaction.setName(reactionName);
 219  2
                 reactionPathway.setReaction(enzymeReaction);
 220  2
                 reactionPathways.add(reactionPathway);
 221  2
             }
 222  1
             in.close();
 223  0
         } catch (IOException ex) {
 224  0
             throw new BiomartFetchDataException("Failed to read the response of the Url connection" +uCon.toString(), ex);
 225  1
         }
 226  1
        return reactionPathways;
 227  
    }
 228  
 }