| 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 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | 3 | public class BiomartAdapter { |
| 28 | |
|
| 29 | 1 | private static final Logger LOGGER = Logger.getLogger(BiomartAdapter.class); |
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 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 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 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 | |
|
| 68 | |
|
| 69 | 0 | return limitResults(results); |
| 70 | |
} |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 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 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 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 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 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 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 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 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 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 | |
} |