Changeset 161
- Timestamp:
- 08/19/09 05:53:32 (2 years ago)
- Location:
- trunk/corelib
- Files:
-
- 1 added
- 3 modified
- 1 copied
- 7 moved
-
build.xml (modified) (1 diff)
-
src/org/bridgedb/webservice/biomart/BiomartStub.java (modified) (2 diffs)
-
src/org/bridgedb/webservice/biomart/IDMapperBiomart.java (moved) (moved from trunk/corelib/src/org/bridgedb/webservice/IDMapperBiomart.java) (18 diffs)
-
src/org/bridgedb/webservice/biomart/package.html (moved) (moved from trunk/corelib/src/org/bridgedb/webservice/package.html) (1 diff)
-
src/org/bridgedb/webservice/biomart/util (added)
-
src/org/bridgedb/webservice/biomart/util/Attribute.java (moved) (moved from trunk/corelib/src/org/bridgedb/webservice/biomart/Attribute.java) (1 diff)
-
src/org/bridgedb/webservice/biomart/util/BiomartClient.java (copied) (copied from trunk/corelib/src/org/bridgedb/webservice/biomart/BiomartStub.java) (14 diffs)
-
src/org/bridgedb/webservice/biomart/util/Database.java (moved) (moved from trunk/corelib/src/org/bridgedb/webservice/biomart/Database.java) (1 diff)
-
src/org/bridgedb/webservice/biomart/util/Dataset.java (moved) (moved from trunk/corelib/src/org/bridgedb/webservice/biomart/Dataset.java) (3 diffs)
-
src/org/bridgedb/webservice/biomart/util/Filter.java (moved) (moved from trunk/corelib/src/org/bridgedb/webservice/biomart/Filter.java) (1 diff)
-
src/org/bridgedb/webservice/biomart/util/XMLQueryBuilder.java (moved) (moved from trunk/corelib/src/org/bridgedb/webservice/biomart/XMLQueryBuilder.java) (1 diff)
-
test/org/bridgedb/TestBiomart.java (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/corelib/build.xml
r140 r161 34 34 </javac> 35 35 <copy file="../resources/filterconversion.txt" 36 todir="build/org/bridgedb/webservice/biomart " />36 todir="build/org/bridgedb/webservice/biomart/util" /> 37 37 </target> 38 38 -
trunk/corelib/src/org/bridgedb/webservice/biomart/BiomartStub.java
r156 r161 19 19 20 20 import java.io.BufferedReader; 21 import java.io.InputStream;22 import java.io.InputStreamReader;23 21 import java.io.IOException; 24 import java.io.OutputStream;25 import java.io.PrintStream;26 27 import java.net.URL;28 import java.net.URLConnection;29 22 30 23 import java.util.HashMap; 24 import java.util.HashSet; 25 import java.util.Iterator; 31 26 import java.util.Map; 27 import java.util.Set; 32 28 import java.util.Vector; 33 29 34 import javax.xml.parsers.DocumentBuilder;35 import javax.xml.parsers.DocumentBuilderFactory;36 30 import javax.xml.parsers.ParserConfigurationException; 37 31 38 import org.w3c.dom.Document; 39 import org.w3c.dom.NamedNodeMap; 40 import org.w3c.dom.NodeList; 32 import org.bridgedb.DataSource; 33 import org.bridgedb.IDMapperException; 34 import org.bridgedb.Xref; 35 import org.bridgedb.file.IDMappingReaderFromDelimitedReader; 36 import org.bridgedb.webservice.biomart.util.Attribute; 37 import org.bridgedb.webservice.biomart.util.BiomartClient; 38 import org.bridgedb.webservice.biomart.util.Database; 39 import org.bridgedb.webservice.biomart.util.Dataset; 40 import org.bridgedb.webservice.biomart.util.Filter; 41 import org.bridgedb.webservice.biomart.util.XMLQueryBuilder; 41 42 42 43 import org.xml.sax.SAXException; 43 44 44 45 /** 45 * BioMart service class, adapted from BioMart client in Cytoscape. 46 * Cache for SynergizerClient 47 * @author gjj 46 48 */ 47 public final class BiomartStub { 48 public static final String defaultBaseURL = "http://www.biomart.org/biomart/martservice"; 49 50 private final String baseURL; 51 private static final String RESOURCE = "/org/bridgedb/webservice/biomart/filterconversion.txt"; 52 53 //private Map<String, Map<String, String>> databases = null; 54 private Map<String, Database> databases = null; 55 56 private Map<String, Dataset> datasets = new HashMap(); 57 private Map<String, Vector<Dataset>> mapDbDss = new HashMap(); 58 private Map<String, Map<String, Filter>> mapDsFilters = new HashMap(); 59 private Map<String, Map<String, Attribute>> mapDsAttrs = new HashMap(); 60 61 private Map<String, Map<String, String>> filterConversionMap; 62 63 private static final int BUFFER_SIZE = 81920; 64 65 // one instance per base url 49 public class BiomartStub { 50 public static final String defaultBaseURL 51 = BiomartClient.defaultBaseURL; 52 53 // cache data 54 // private Map<String,Map<String,Map<String,Set<String>>>> 55 // mapAuthSpeciesDomainRange = null; 56 57 // one instance per url 66 58 private static Map<String, BiomartStub> instances = new HashMap(); 67 59 68 60 /** 69 * Get a BioMartStub with the default base URL.70 * @return BiomartStub71 * @throws IOException if failed to read local resource61 * 62 * @return SynergizerStub with the default server url 63 * @throws IOException if failed to connect 72 64 */ 73 65 public static BiomartStub getInstance() throws IOException { … … 76 68 77 69 /** 78 * Get a BioMartStub with the base URL.79 * @param baseU RL base URL of BioMart80 * @return BioMartStub81 * @throws IOException if failed to read local resource82 */ 83 public static BiomartStub getInstance(String baseU RL) throws IOException {84 if (baseU RL==null) {70 * 71 * @param baseUrl server url 72 * @return SynergizerStub from the server 73 * @throws IOException if failed to connect 74 */ 75 public static BiomartStub getInstance(String baseUrl) throws IOException { 76 if (baseUrl==null) { 85 77 throw new IllegalArgumentException("base url cannot be null"); 86 78 } 87 79 88 BiomartStub instance = instances.get(baseU RL);80 BiomartStub instance = instances.get(baseUrl); 89 81 if (instance==null) { 90 instance = new BiomartStub(baseU RL);91 instances.put(baseU RL, instance);82 instance = new BiomartStub(baseUrl); 83 instances.put(baseUrl, instance); 92 84 } 93 85 94 86 return instance; 95 } 96 97 98 /** 99 * Creates a new BiomartStub object from given URL. 100 * 101 * @param baseURL DOCUMENT ME! 102 * @throws IOException if failed to read local resource 103 */ 104 private BiomartStub(String baseURL) throws IOException { 105 this.baseURL = baseURL + "?"; 106 loadConversionFile(); 107 } 108 109 /** 110 * Conversion map from filter to attribute. 111 * @throws IOException if failed to read local resource 112 */ 113 private void loadConversionFile() throws IOException { 114 filterConversionMap = new HashMap<String, Map<String, String>>(); 115 116 InputStreamReader inFile = new InputStreamReader(this.getClass().getResource(RESOURCE).openStream()); 117 118 BufferedReader inBuffer = new BufferedReader(inFile); 119 120 String line; 121 String trimed; 122 String oldName = null; 123 Map<String, String> oneEntry = new HashMap<String, String>(); 124 125 String[] dbparts; 126 127 while ((line = inBuffer.readLine()) != null) { 128 trimed = line.trim(); 129 dbparts = trimed.split("\\t"); 130 131 if (dbparts[0].equals(oldName) == false) { 132 oneEntry = new HashMap<String, String>(); 133 oldName = dbparts[0]; 134 filterConversionMap.put(oldName, oneEntry); 87 } 88 89 private BiomartClient client; 90 91 /** 92 * 93 * @param baseUrl server url. 94 * @throws IOException if failed to connect. 95 */ 96 private BiomartStub(String baseUrl) throws IOException { 97 client = new BiomartClient(baseUrl); 98 } 99 100 /** 101 * 102 * @return available marts 103 * @throws IDMapperException if failed 104 */ 105 public Set<String> availableMarts() throws IDMapperException { 106 Map<String, Database> marts; 107 try { 108 marts = client.getRegistry(); 109 } catch (IOException e) { 110 throw new IDMapperException(e); 111 } catch (ParserConfigurationException e) { 112 throw new IDMapperException(e); 113 } catch (SAXException e) { 114 throw new IDMapperException(e); 115 } 116 117 Set<String> visibleMarts = new HashSet(); 118 for (Database db : marts.values()) { 119 if (db.visible()) { 120 visibleMarts.add(db.getName()); 135 121 } 136 137 oneEntry.put(dbparts[1], dbparts[2]); 138 } 139 140 inFile.close(); 141 inBuffer.close(); 142 } 143 144 /** 145 * Convert filter to attribute according to the conversion file. 146 * @param dsName dataset name. 147 * @param dbName database name 148 * @param filterID filter ID 149 * @return converted attribute 150 */ 151 public Attribute filterToAttributeName(String dsName, String dbName, 152 String filterID) { 153 if (filterConversionMap.get(dbName) == null) { 122 } 123 124 return visibleMarts; 125 } 126 127 /** 128 * 129 * @param mart mart name 130 * @return mart display name or null if not exist 131 * @throws IDMapperException if failed to connect 132 */ 133 public String martDisplayName(String mart) { 134 if (mart==null) { 154 135 return null; 155 } else { 156 String attrName = filterConversionMap.get(dbName).get(filterID); 157 return this.getAttribute(dsName, attrName); 158 } 159 } 160 161 /** 162 * Get the registry information from the base URL. 163 * 164 * @return Map of registry information. Key value is "name" field. 165 * @throws ParserConfigurationException if failed new document builder 166 * @throws SAXException if failed to parse registry 167 * @throws IOException if failed to read from URL 168 */ 169 public Map<String, Database> getRegistry() 170 throws IOException, ParserConfigurationException, SAXException { 171 // If already loaded, just return it. 172 if (databases != null) 173 return databases; 174 175 // Initialize database map. 176 databases = new HashMap<String, Database>(); 177 178 // Prepare URL for the registry status 179 final String reg = "type=registry"; 180 final URL targetURL = new URL(baseURL + reg); 181 182 // Get the result as XML document. 183 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 184 final DocumentBuilder builder = factory.newDocumentBuilder(); 185 186 InputStream is = getInputStream(targetURL); 187 188 final Document registry = builder.parse(is); 189 190 // Extract each datasource 191 NodeList locations = registry.getElementsByTagName("MartURLLocation"); 192 int locSize = locations.getLength(); 193 NamedNodeMap attrList; 194 int attrLen; 195 String dbID; 196 197 for (int i = 0; i < locSize; i++) { 198 attrList = locations.item(i).getAttributes(); 199 attrLen = attrList.getLength(); 200 201 // First, get the key value 202 dbID = attrList.getNamedItem("name").getNodeValue(); 203 204 Map<String, String> entry = new HashMap<String, String>(); 205 206 for (int j = 0; j < attrLen; j++) { 207 entry.put(attrList.item(j).getNodeName(), attrList.item(j).getNodeValue()); 208 } 209 210 databases.put(dbID, new Database(dbID,entry)); 211 } 212 213 is.close(); 214 is = null; 215 216 return databases; 217 } 218 219 /** 220 * Get available datasets of a mart/database. 221 * @param martName mart name 222 * @return {@link Vector} of available datasets 223 * @throws IOException if failed to read 224 */ 225 public Vector<Dataset> getAvailableDatasets(final String martName) 226 throws IOException { 227 Vector<Dataset> result = mapDbDss.get(martName); 228 if (result!=null) { 229 return result; 230 } 231 232 try { 233 getRegistry(); 234 } catch (ParserConfigurationException e) { 235 e.printStackTrace(); 236 } catch (SAXException e) { 237 e.printStackTrace(); 238 } 239 240 //final Map<String, String> datasources = new HashMap<String, String>(); 241 result = new Vector(); 242 243 Database database = databases.get(martName); 244 245 if (database==null) { 136 } 137 138 Database db = client.getMart(mart); 139 return db==null?null:db.displayName(); 140 } 141 142 /** 143 * 144 * @param authority mart name 145 * @return available datasets from this mart 146 * @throws IDMapperException if failed 147 */ 148 public Set<String> availableDatasets(String mart) 149 throws IDMapperException { 150 if (mart==null) { 151 return new HashSet(0); 152 } 153 154 if (!availableMarts().contains(mart)) { 155 return new HashSet(0); 156 } 157 158 Map<String,Dataset> datasets; 159 try { 160 datasets = client.getAvailableDatasets(mart); 161 } catch (IOException e) { 162 throw new IDMapperException(e); 163 } 164 165 return datasets.keySet(); 166 } 167 168 /** 169 * 170 * @param dataset dataset name 171 * @return dataset display name or null if not exist 172 * @throws IDMapperException if failed to connect 173 */ 174 public String datasetDisplayName(String dataset) { 175 if (dataset==null) { 246 176 return null; 247 177 } 248 178 249 Map<String, String> detail = database.getParam(); 250 251 String urlStr = "http://" + detail.get("host") + ":" + detail.get("port") 252 + detail.get("path") + "?type=datasets&mart=" + detail.get("name"); 253 //System.out.println("DB name = " + martName + ", Target URL = " + urlStr + "\n"); 254 255 URL url = new URL(urlStr); 256 InputStream is = getInputStream(url); 257 258 BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 259 String s; 260 261 String[] parts; 262 263 while ((s = reader.readLine()) != null) { 264 parts = s.split("\\t"); 265 266 if ((parts.length > 4) && parts[3].equals("1")) { 267 Dataset dataset = new Dataset(parts[1], parts[2], database); 268 result.add(dataset); 269 //datasourceMap.put(parts[1], martName); 270 datasets.put(parts[1], dataset); 271 } 272 } 273 274 is.close(); 275 reader.close(); 276 reader = null; 277 is = null; 278 279 mapDbDss.put(martName, result); 280 281 return result; 282 } 283 284 /** 285 * Get filters for a dataset. 286 * @param datasetName name of data set 287 * @return filters 288 * @throws IOException if failed to read 289 */ 290 public Map<String, Filter> getFilters(String datasetName) 291 throws IOException { 292 if (datasetName==null) { 293 throw new IllegalArgumentException("Dataset name cannot be null"); 294 } 295 296 if (mapDsFilters.get(datasetName)!=null) { 297 return mapDsFilters.get(datasetName); 298 } 299 300 Map<String, Filter> filters = new HashMap(); 301 302 Dataset dataset = getDataset(datasetName); 303 if (dataset==null) { 304 return filters; 305 } 306 307 Database database = dataset.getDatabase(); 308 309 Map<String, String> detail = database.getParam(); 310 311 String urlStr = "http://" 312 + detail.get("host") + ":" 313 + detail.get("port") 314 + detail.get("path") 315 + "?virtualschema=" 316 + detail.get("serverVirtualSchema") 317 + "&type=filters&dataset=" 318 + datasetName; 319 320 //System.out.println("Dataset name = " + datasetName + ", Target URL = " 321 // + urlStr + "\n"); 322 URL url = new URL(urlStr); 323 InputStream is = getInputStream(url); 324 325 BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 326 String s; 327 328 String[] parts; 329 330 while ((s = reader.readLine()) != null) { 331 parts = s.split("\\t"); 332 333 if ((parts.length > 1)) { 334 if ((parts[1].contains("ID(s)") 335 || parts[1].contains("Accession(s)") 336 || parts[1].contains("IDs")) 337 && (parts[0].startsWith("with_") == false) 338 && (parts[0].endsWith("-2") == false) 339 || parts.length>6 340 && parts[5].equals("id_list")) { 341 //filters.put(parts[1], parts[0]); 342 filters.put(parts[0], new Filter(parts[0], parts[1])); 343 // System.out.println("### Filter Entry = " + parts[1] + " = " + parts[0]); 179 Dataset ds = client.getDataset(dataset); 180 return ds==null?null:ds.displayName(); 181 } 182 183 /** 184 * 185 * @param mart mart name 186 * @param dataset dataset name 187 * @return available filters / source id types of the dataset from this 188 * mart 189 * @throws IDMapperException if failed 190 */ 191 public Set<String> availableFilters(final String mart, 192 final String dataset) throws IDMapperException { 193 if (mart==null || dataset==null) { 194 return new HashSet(0); 195 } 196 197 if (!availableDatasets(mart).contains(dataset)) { 198 return new HashSet(0); 199 } 200 201 Map<String,Filter> filters; 202 try { 203 filters = client.getFilters(dataset); 204 } catch (IOException e) { 205 throw new IDMapperException(e); 206 } 207 208 return filters.keySet(); 209 } 210 211 /** 212 * 213 * @param mart mart name 214 * @param dataset dataset name 215 * @return attribute names / target id types of the dataset from this 216 * mart 217 * @throws IDMapperException if failed. 218 */ 219 public Set<String> availableAttributes(final String mart, 220 final String dataset, boolean idOnly) throws IDMapperException { 221 222 if (mart==null || dataset==null) { 223 return new HashSet(0); 224 } 225 226 if (!availableDatasets(mart).contains(dataset)) { 227 return new HashSet(0); 228 } 229 230 Map<String,Attribute> attributes; 231 try { 232 attributes = client.getAttributes(dataset); 233 } catch (IOException e) { 234 throw new IDMapperException(e); 235 } 236 237 Set<String> result; 238 if (idOnly) { 239 result = new HashSet(); 240 for (String name : attributes.keySet()) { 241 String displayName = client.getAttribute(dataset, name).getDisplayName(); 242 if (displayName.endsWith("ID") 243 || displayName.endsWith("Accession") 244 || name.endsWith("id") 245 || name.endsWith("accession")) { 246 result.add(name); 344 247 } 345 248 } 346 } 347 348 is.close(); 349 reader.close(); 350 reader = null; 351 is = null; 352 353 mapDsFilters.put(datasetName, filters); 354 355 return filters; 356 } 357 358 /** 359 * Get attributes. 360 * @param datasetName dataset name 361 * @return Map of attribute name to attributes 362 * @throws IOException if failed to read 363 */ 364 public Map<String, Attribute> getAttributes(String datasetName) throws IOException { 365 if (datasetName==null) { 366 throw new java.lang.IllegalArgumentException("Dataset name cannot be null"); 367 } 368 369 if (mapDsAttrs.get(datasetName)!=null) { 370 return mapDsAttrs.get(datasetName); 371 } 372 373 Map<String, Attribute> attributes = new HashMap<String, Attribute>(); 374 375 Dataset dataset = getDataset(datasetName); 376 if (dataset==null) { 377 return attributes; 378 } 379 380 Database database = dataset.getDatabase(); 381 382 Map<String, String> detail = database.getParam(); 383 384 String urlStr = "http://" + detail.get("host") + ":" + detail.get("port") 385 + detail.get("path") + "?virtualschema=" 386 + detail.get("serverVirtualSchema") + "&type=attributes&dataset=" 387 + datasetName; 388 389 //System.out.println("Dataset name = " + datasetName + ", Target URL = " + urlStr + "\n"); 390 URL url = new URL(urlStr); 391 InputStream is = getInputStream(url); 392 393 BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 394 String s; 395 396 String displayName; 397 398 String[] parts; 399 400 while ((s = reader.readLine()) != null) { 401 parts = s.split("\\t"); 402 403 if (parts.length == 0) 404 continue; 405 406 if (parts.length == 4) { 407 displayName = parts[3] + ": " + parts[1]; 408 } else if (parts.length > 1) { 409 displayName = parts[1]; 410 } else { 411 displayName = ""; 249 } else { 250 result = new HashSet(attributes.keySet()); 251 } 252 253 return result; 254 } 255 256 /** 257 * 258 * @param mart mart name 259 * @param dataset dataset name 260 * @param filter filter name / source id type 261 * @param attributes attribute names / target id types 262 * @param ids source ids to be translated 263 * @return map from source id to target ids 264 * key: source id 265 * value: corresponding target ids 266 * @throws IDMapperException 267 */ 268 public Map<String,Set<String>[]> translate(final String mart, 269 final String dataset, final String filter, 270 final String[] attributes, final Set<String> ids) 271 throws IDMapperException { 272 int nAttr = attributes.length; 273 Attribute[] attrs = new Attribute[nAttr+1]; 274 275 // prepare attributes 276 int iattr = 0; 277 for (String attr : attributes) { 278 attrs[iattr++] = client.getAttribute(dataset, attr); 279 } 280 attrs[nAttr] = client.filterToAttribute(dataset, filter); 281 282 // prepare filters 283 StringBuilder sb = new StringBuilder(); 284 for (String str : ids) { 285 sb.append(str); 286 sb.append(","); 287 } 288 289 int len = sb.length(); 290 if (len>0) { 291 sb.deleteCharAt(len-1); 292 } 293 294 Map<String, String> queryFilter = new HashMap(1); 295 queryFilter.put(filter, sb.toString()); 296 297 // build query string 298 String query = XMLQueryBuilder.getQueryString(dataset, attrs, queryFilter); 299 300 // query 301 BufferedReader bfr = null; 302 try { 303 bfr = client.sendQuery(query); 304 if (!bfr.ready()) 305 throw new IDMapperException("Query failed"); 306 } catch (IOException e) { 307 throw new IDMapperException(e); 308 } 309 310 if (bfr==null) { 311 return new HashMap(0); 312 } 313 314 // read id mapping 315 Map<String,Set<String>[]> result = new HashMap(); 316 try { 317 bfr.readLine(); 318 String line; 319 while ((line = bfr.readLine())!=null) { 320 String[] strs = line.split("\t"); 321 if (strs.length!=nAttr+1) 322 continue; // because the last one is the src id 323 String src = strs[nAttr]; 324 Set<String>[] tgt = result.get(src); 325 if (tgt==null) { 326 tgt = new Set[nAttr]; 327 for (int i=0; i<nAttr; i++) { 328 tgt[i] = new HashSet(); 329 } 330 result.put(src, tgt); 331 } 332 333 for (int i=0; i<nAttr; i++) { 334 String str = strs[i]; 335 if (str.length()>0) { 336 tgt[i].add(str); 337 } 338 } 339 412 340 } 413 414 attributes.put(parts[0], new Attribute(parts[0],displayName)); 415 } 416 417 is.close(); 418 reader.close(); 419 reader = null; 420 is = null; 421 422 this.mapDsAttrs.put(datasetName, attributes); 423 424 return attributes; 425 } 426 427 /** 428 * Get filter. 429 * @param datasetName dataset name 430 * @param filterName filter name 431 * @return filter 432 */ 433 public Filter getFilter(String datasetName, String filterName) { 434 if (datasetName==null || filterName==null) { 435 throw new java.lang.IllegalArgumentException("datasetName and filterName cannot be null"); 436 } 437 438 Map<String, Filter> map = this.mapDsFilters.get(datasetName); 439 if (map==null) { 440 return null; 441 } 442 443 return map.get(filterName); 444 } 445 446 /** 447 * get Attribute. 448 * @param datasetName dataset name 449 * @param attrName attribute name 450 * @return attribute 451 */ 452 public Attribute getAttribute(String datasetName, String attrName) { 453 if (datasetName==null || attrName==null) { 454 throw new java.lang.IllegalArgumentException("datasetName and attrName cannot be null"); 455 } 456 457 Map<String, Attribute> map = this.mapDsAttrs.get(datasetName); 458 if (map==null) { 459 return null; 460 } 461 462 return map.get(attrName); 463 } 464 465 /** 466 * Send the XML query to Biomart, and get the result as table. 467 * @param xmlQuery query xml 468 * @return result {@link BufferedReader} 469 * @throws IOException if failed to read 470 */ 471 public BufferedReader sendQuery(String xmlQuery) throws IOException { 472 473 //System.out.println("=======Query = " + xmlQuery); 474 475 URL url = new URL(baseURL); 476 URLConnection uc = url.openConnection(); 477 uc.setDoOutput(true); 478 uc.setRequestProperty("User-Agent", "Java URLConnection"); 479 480 OutputStream os = uc.getOutputStream(); 481 482 final String postStr = "query=" + xmlQuery; 483 PrintStream ps = new PrintStream(os); 484 485 // Post the data 486 ps.print(postStr); 487 os.close(); 488 ps.close(); 489 ps = null; 490 os = null; 491 492 return new BufferedReader(new InputStreamReader(uc.getInputStream()), BUFFER_SIZE); 493 } 494 495 /** 496 * get Database/mart. 497 * @param dbname database name 498 * @return database 499 */ 500 public Database getDatabase(final String dbname) { 501 return databases.get(dbname); 502 } 503 504 /** 505 * get Dataset. 506 * @param dsname dataset name 507 * @return dataset 508 */ 509 public Dataset getDataset(final String dsname) { 510 return datasets.get(dsname); 511 } 512 513 private static final int msConnectionTimeout = 2000; 514 //TODO: test when IOException is throwed 515 protected static InputStream getInputStream(URL source) throws IOException { 516 InputStream stream = null; 517 int expCount = 0; 518 int timeOut = msConnectionTimeout; 519 while (true) { // multiple chances 520 try { 521 URLConnection uc = source.openConnection(); 522 uc.setUseCaches(false); // don't use a cached page 523 uc.setConnectTimeout(timeOut); // set timeout for connection 524 stream = uc.getInputStream(); 525 break; 526 } catch (IOException e) { 527 if (expCount++==4) { 528 throw(e); 529 } else { 530 timeOut *= 2; 531 } 532 } 533 } 534 535 return stream; 341 } catch (IOException e) { 342 throw new IDMapperException(e); 343 } 344 345 return result; 536 346 } 537 347 } -
trunk/corelib/src/org/bridgedb/webservice/biomart/IDMapperBiomart.java
r156 r161 16 16 // 17 17 18 package org.bridgedb.webservice; 19 20 import java.io.BufferedReader; 18 package org.bridgedb.webservice.biomart; 19 21 20 import java.io.IOException; 22 21 23 22 import java.util.HashMap; 24 23 import java.util.HashSet; 25 import java.util.Iterator;26 24 import java.util.Map; 27 25 import java.util.Set; 28 import java.util.Vector;29 30 import javax.xml.parsers.ParserConfigurationException;31 26 32 27 import org.bridgedb.AbstractIDMapperCapabilities; … … 37 32 import org.bridgedb.IDMapperException; 38 33 import org.bridgedb.Xref; 39 import org.bridgedb.file.IDMappingReaderFromDelimitedReader; 40 import org.bridgedb.webservice.biomart.Attribute; 41 import org.bridgedb.webservice.biomart.BiomartStub; 42 import org.bridgedb.webservice.biomart.Filter; 43 import org.bridgedb.webservice.biomart.XMLQueryBuilder; 44 45 import org.xml.sax.SAXException; 34 import org.bridgedb.webservice.IDMapperWebservice; 35 import org.bridgedb.webservice.biomart.util.BiomartClient; 46 36 47 37 /** … … 61 51 /** {@inheritDoc} */ 62 52 public IDMapper connect(String location) throws IDMapperException { 63 // e.g.: transitivity=false,id-type-filter=true@dataset=oanatinus_gene_ensembl53 // e.g.: id-type-filter=true@dataset=oanatinus_gene_ensembl 64 54 // e.g.: http://www.biomart.org/biomart/martservice?dataset=oanatinus_gene_ensembl 65 String baseURL = BiomartStub.defaultBaseURL; 66 boolean transitivity = false; 55 String baseURL = BiomartClient.defaultBaseURL; 67 56 boolean idTypeFilter = true; 68 57 … … 78 67 79 68 if (config!=null) { 80 String transitivityTag = "transitivity=";81 idx = config.indexOf(transitivityTag);82 if (idx!=-1) {83 String tran = config.substring(idx+transitivityTag.length());84 if (tran.toLowerCase().startsWith("true")) {85 transitivity = true;86 } else if (tran.toLowerCase().startsWith("false")) {87 transitivity = false;88 } else {89 throw new IDMapperException(90 "transivity can only be true or false");91 }92 }93 94 95 69 String idTypeFilterTag = "id-type-filter="; 96 70 idx = config.indexOf(idTypeFilterTag); … … 117 91 String martTag = "mart="; 118 92 idx = param.indexOf(martTag); 119 String mart Name= param.substring(idx+martTag.length());120 121 idx = mart Name.indexOf("&");93 String mart = param.substring(idx+martTag.length()); 94 95 idx = mart.indexOf("&"); 122 96 if (idx>-1) { 123 mart Name = martName.substring(0,idx);97 mart = mart.substring(0,idx); 124 98 } 125 99 126 100 String datasetTag = "dataset="; 127 101 idx = param.indexOf(datasetTag); 128 String dataset Name= param.substring(idx+datasetTag.length());129 130 idx = dataset Name.indexOf("&");102 String dataset = param.substring(idx+datasetTag.length()); 103 104 idx = dataset.indexOf("&"); 131 105 if (idx>-1) { 132 dataset Name = datasetName.substring(0,idx);133 } 134 135 return new IDMapperBiomart(mart Name, datasetName, baseURL,136 idTypeFilter , transitivity);137 } 138 } 139 140 private String mart Name;141 private String dataset Name;106 dataset = dataset.substring(0,idx); 107 } 108 109 return new IDMapperBiomart(mart, dataset, baseURL, 110 idTypeFilter); 111 } 112 } 113 114 private String mart; 115 private String dataset; 142 116 private BiomartStub stub; 143 private boolean transitivity;144 117 private boolean idOnlyForTgtDataSource; 145 118 146 119 private String baseURL; 147 120 148 private Map<DataSource, Filter> mapSrcDSFilter;149 private Map<DataSource, Attribute> mapSrcDSAttr;121 private Set<DataSource> supportedSrcDs; 122 private Set<DataSource> supportedTgtDs; 150 123 151 124 /** 152 125 * Transitivity is unsupported.ID only. ID only for target data sources. 153 126 * Use default url of BiMart. 154 * @param mart Namename of mart155 * @param dataset Namename of dataset127 * @param mart name of mart 128 * @param dataset name of dataset 156 129 * @throws IDMapperException if failed to link to the dataset 157 130 */ 158 public IDMapperBiomart(String martName, String datasetName) throws IDMapperException { 159 this(martName, datasetName, null); 131 public IDMapperBiomart(String mart, String dataset) 132 throws IDMapperException { 133 this(mart, dataset, null); 160 134 } 161 135 162 136 /** 163 137 * Use default url of BiMart. 164 * @param mart Namename of mart165 * @param dataset Namename of dataset138 * @param mart name of mart 139 * @param dataset name of dataset 166 140 * @param idOnlyForTgtDataSource id-only option, filter data source ends 167 141 * with 'ID' or 'Accession'. … … 169 143 * @throws IDMapperException if failed to link to the dataset 170 144 */ 171 public IDMapperBiomart(String mart Name, String datasetName, boolean idOnlyForTgtDataSource,172 boolean transitivity) throws IDMapperException {173 this(mart Name, datasetName, null, idOnlyForTgtDataSource, transitivity);145 public IDMapperBiomart(String mart, String dataset, 146 boolean idOnlyForTgtDataSource) throws IDMapperException { 147 this(mart, dataset, null, idOnlyForTgtDataSource); 174 148 } 175 149 176 150 /** 177 151 * Transitivity is unsupported.ID only. ID only for target data sources. 178 * @param mart Namename of mart179 * @param dataset Namename of dataset152 * @param mart name of mart 153 * @param dataset name of dataset 180 154 * @param baseURL base url of BioMart 181 155 * @throws IDMapperException if failed to link to the dataset 182 156 */ 183 public IDMapperBiomart(String mart Name, String datasetName, String baseURL)157 public IDMapperBiomart(String mart, String dataset, String baseURL) 184 158 throws IDMapperException { 185 this(martName, datasetName, baseURL, true); 186 } 187 188 /** 189 * Transitivity is unsupported. 190 * @param martName name of mart 191 * @param datasetName name of dataset 192 * @param baseURL base url of BioMart 193 * @param idOnlyForTgtDataSource id-only option, filter data source ends 194 * with 'ID' or 'Accession'. 195 * @throws IDMapperException if failed to link to the dataset 196 */ 197 public IDMapperBiomart(String martName, String datasetName, String baseURL, 198 boolean idOnlyForTgtDataSource) throws IDMapperException { 199 this(martName, datasetName, baseURL, idOnlyForTgtDataSource, false); 159 this(mart, dataset, baseURL, true); 200 160 } 201 161 … … 203 163 * Construct from a dataset, a database, id-only option and transitivity 204 164 * option. 205 * @param mart Namename of mart206 * @param dataset Namename of dataset165 * @param mart name of mart 166 * @param dataset name of dataset 207 167 * @param baseURL base url of BioMart 208 168 * @param idOnlyForTgtDataSource id-only option, filter data source ends … … 211 171 * @throws IDMapperException if failed to link to the dataset 212 172 */ 213 public IDMapperBiomart(String mart Name, String datasetName, String baseURL,214 boolean idOnlyForTgtDataSource , boolean transitivity) throws IDMapperException {215 this.mart Name = martName;216 this.dataset Name = datasetName;173 public IDMapperBiomart(String mart, String dataset, String baseURL, 174 boolean idOnlyForTgtDataSource) throws IDMapperException { 175 this.mart = mart; 176 this.dataset = dataset; 217 177 if (baseURL!=null) { 218 178 this.baseURL = baseURL; 219 179 } else { 220 this.baseURL = Biomart Stub.defaultBaseURL;180 this.baseURL = BiomartClient.defaultBaseURL; 221 181 } 222 182 … … 227 187 } 228 188 229 try { 230 if (!stub.getRegistry().containsKey(martName)) { 231 throw new IDMapperException("Mart not exist."); 232 } 233 234 if (!stub.getAvailableDatasets(martName).contains(stub.getDataset(datasetName))) { 235 throw new IDMapperException("dataset not exist."); 236 } 237 } catch (IOException e) { 238 throw new IDMapperException(e); 239 } catch (ParserConfigurationException e) { 240 throw new IDMapperException(e); 241 } catch (SAXException e) { 242 throw new IDMapperException(e); 243 } 244 245 setIDOnlyForTgtDataSource(idOnlyForTgtDataSource); 246 setTransitivity(transitivity); 247 248 mapSrcDSFilter = new HashMap<DataSource, Filter>(); 249 mapSrcDSAttr = new HashMap<DataSource, Attribute>(); 189 if (!stub.availableMarts().contains(mart)) { 190 throw new IDMapperException("Mart not exist."); 191 } 192 193 if (!stub.availableDatasets(mart).contains(dataset)) { 194 throw new IDMapperException("dataset not exist."); 195 } 196 197 this.idOnlyForTgtDataSource = idOnlyForTgtDataSource; 198 199 supportedSrcDs = this.getSupportedSrcDataSources(); 200 supportedTgtDs = this.getSupportedTgtDataSources(); 250 201 251 202 cap = new BiomartCapabilities(); … … 253 204 254 205 /** 255 * Filter target datasource ending with "ID" or "Accession".256 * @param idOnlyForTgtDataSource ID-only if true257 */258 public void setIDOnlyForTgtDataSource(boolean idOnlyForTgtDataSource) {259 this.idOnlyForTgtDataSource = idOnlyForTgtDataSource;260 }261 262 /**263 206 * 264 207 * @return true if ID-only for target data sources. … … 269 212 270 213 /** 271 * Set transitivity support.272 * @param transitivity support transitivity if true.273 */274 public void setTransitivity(final boolean transitivity) {275 this.transitivity = transitivity;276 }277 278 /**279 * Get transitivity support.280 * @return true if support transitivity; false otherwise.281 */282 public boolean getTransitivity() {283 return transitivity;284 }285 286 /**287 * Set base url of BioMart.288 * @param baseURL URL of BioMart.289 * @throws IDMapperException if failed to read local resources.290 */291 public void setBaseURL(final String baseURL) throws IDMapperException {292 try {293 stub = BiomartStub.getInstance(baseURL);294 } catch (IOException e) {295 throw new IDMapperException(e);296 }297 this.baseURL = baseURL;298 }299 300 /**301 214 * 302 215 * @return base URL of BioMart. … … 308 221 /** 309 222 * 310 * @param mart mart name311 */312 public void setMart(final String mart) {313 this.martName = mart;314 }315 316 /**317 *318 223 * @return mart name 319 224 */ 320 225 public String getMart() { 321 return martName; 322 } 323 324 /** 325 * Set dataset. 326 * @param dataset dataset from BioMart 327 */ 328 public void setDataset(final String dataset) { 329 this.datasetName = dataset; 226 return mart; 330 227 } 331 228 … … 335 232 */ 336 233 public String getDataset() { 337 return dataset Name;234 return dataset; 338 235 } 339 236 … … 353 250 if (srcXrefs==null) { 354 251 throw new java.lang.IllegalArgumentException( 355 "srcXrefs or tgtDataSources cannot be null"); 356 } 357 358 Map<Xref, Set<Xref>> result = new HashMap(); 359 360 // remove unsupported source datasources 361 Set<DataSource> supportedSrcDatasources 362 = cap.getSupportedSrcDataSources(); 363 Map<DataSource, String> queryFilters = getQueryFilters(srcXrefs); 364 Iterator<DataSource> it = queryFilters.keySet().iterator(); 365 while (it.hasNext()) { 366 DataSource ds = it.next(); 367 if (!supportedSrcDatasources.contains(ds)) { 368 it.remove(); 369 } 370 } 371 if (queryFilters.isEmpty()) { 372 return result; 373 } 374 375 // remove unsupported target datasources 376 Set<DataSource> supportedTgtDatasources = cap.getSupportedTgtDataSources(); 377 Vector<DataSource> tgtDss; 378 if (tgtDataSources == null) 379 tgtDss = new Vector(supportedSrcDatasources); 380 else 381 { 382 tgtDss = new Vector(tgtDataSources); 383 tgtDss.retainAll(supportedTgtDatasources); 384 } 385 if (tgtDss.isEmpty()) { 386 return result; 387 } 388 389 for (Map.Entry<DataSource, String> filter : queryFilters.entrySet()) { 390 DataSource srcDs = filter.getKey(); 391 392 String srcAttr = mapSrcDSFilter.get(srcDs).getName(); 393 Attribute[] attrs = getAttributes(tgtDss, srcAttr); 394 395 Map<String, String> queryFilter = new HashMap(1); 396 queryFilter.put(srcAttr, filter.getValue()); 397 398 String query = XMLQueryBuilder.getQueryString(datasetName, attrs, queryFilter); 399 400 BufferedReader bfr = null; 401 try { 402 bfr = stub.sendQuery(query); 403 if (!bfr.ready()) 404 throw new IDMapperException("Query failed"); 405 } catch (IOException e) { 406 throw new IDMapperException(e); 407 } 408 409 if (bfr==null) { 410 return result; 411 } 412 413 IDMappingReaderFromDelimitedReader reader 414 = new IDMappingReaderFromDelimitedReader(bfr, 415 "\\t", null, transitivity); 416 417 Vector<DataSource> dss = new Vector(tgtDss.size()+1); 418 dss.addAll(tgtDss); 419 dss.add(srcDs); 420 reader.setDataSources(dss); 421 422 Map<Xref,Set<Xref>> mapXrefs = reader.getIDMappings(); 423 if (mapXrefs==null) { 424 return result; 425 } 426 427 for (Xref srcXref : srcXrefs) { 428 Set<Xref> refs = mapXrefs.get(srcXref); 429 if (refs==null) continue; 430 431 Set<Xref> tgtRefs = result.get(srcXref); 432 if (tgtRefs==null) { 433 tgtRefs = new HashSet(); 434 result.put(srcXref, tgtRefs); 252 "srcXrefs or tgtDataSources cannot be null."); 253 } 254 255 Map<Xref, Set<Xref>> result = new HashMap<Xref, Set<Xref>>(); 256 257 // source datasources 258 Map<String, Map<String, Xref>> mapSrcTypeIDXrefs = new HashMap(); 259 for (Xref xref : srcXrefs) { 260 DataSource ds = xref.getDataSource(); 261 if (!supportedSrcDs.contains(ds)) continue; 262 263 String src = ds.getFullName(); 264 Map<String, Xref> ids = mapSrcTypeIDXrefs.get(src); 265 if (ids==null) { 266 ids = new HashMap(); 267 mapSrcTypeIDXrefs.put(src, ids); 268 } 269 ids.put(xref.getId(), xref); 270 } 271 272 // supported tgt datasources 273 Set<String> tgtTypes = new HashSet(); 274 for (DataSource ds : tgtDataSources) { 275 if (supportedTgtDs.contains(ds)) { 276 tgtTypes.add(ds.getFullName()); 277 } 278 } 279 String[] tgts = tgtTypes.toArray(new String[0]); 280 281 for (Map.Entry<String, Map<String, Xref>> entry : 282 mapSrcTypeIDXrefs.entrySet()) { 283 String src = entry.getKey(); 284 Set<String> ids = entry.getValue().keySet(); 285 Map<String,Set<String>[]> res = 286 stub.translate(mart, dataset , src, tgts, ids); 287 288 289 for (Map.Entry<String,Set<String>[]> entryRes : res.entrySet()) { 290 String srcId = entryRes.getKey(); 291 Set<String>[] tgtIds = entryRes.getValue(); 292 if (tgtIds==null) { // source xref not exist 293 continue; 435 294 } 436 295 437 for (Xref tgtXref : refs) { 438 if (tgtDataSources.contains(tgtXref.getDataSource())) { 439 tgtRefs.add(tgtXref); 296 Xref srcXref = mapSrcTypeIDXrefs.get(src).get(srcId); 297 298 Set<Xref> tgtXrefs = new HashSet(); 299 for (int itgt=0; itgt<tgts.length; itgt++) { 300 for (String tgtId : tgtIds[itgt]) { 301 Xref tgtXref = new Xref(tgtId, 302 DataSource.getByFullName(tgts[itgt])); 303 tgtXrefs.add(tgtXref); 440 304 } 441 305 } 442 } 443 } 306 307 result.put(srcXref, tgtXrefs); 308 } 309 } 444 310 445 311 return result; 446 }447 448 /**449 * Create filters from the source xrefs.450 * @param srcXrefs source xrefs451 * @return map from data source to IDs452 */453 protected Map<DataSource,String> getQueryFilters(Set<Xref> srcXrefs) {454 Map<DataSource, Set<String>> mapNameValue = new HashMap();455 for (Xref xref : srcXrefs) {456 DataSource ds = xref.getDataSource();457 Set<String> ids = mapNameValue.get(ds);458 if (ids==null) {459 ids = new HashSet();460 mapNameValue.put(ds, ids);461 }462 ids.add(xref.getId());463 }464 465 Map<DataSource,String> filters = new HashMap();466 for (Map.Entry<DataSource, Set<String>> entry : mapNameValue.entrySet()) {467 DataSource ds = entry.getKey();468 StringBuilder value = new StringBuilder();469 for (String str : entry.getValue()) {470 value.append(str);471 value.append(",");472 }473 474 int len = value.length();475 if (len>0) {476 value.deleteCharAt(len-1);477 }478 479 filters.put(ds, value.toString());480 }481 482 return filters;483 }484 485 /**486 * This code is bollowed from IDMapperClient from Cytoscape.487 * @param tgtDataSources target data sources488 * @param filterName filter name489 * @return attributes490 */491 protected Attribute[] getAttributes(Vector<DataSource> tgtDataSources,492 String filterName) {493 int n = tgtDataSources.size();494 Attribute[] attrs = new Attribute[n+1];495 496 int iattr = 0;497 for (DataSource ds : tgtDataSources) {498 //attrs[iattr++] = new Attribute(ds.getFullName());499 attrs[iattr++] = this.mapSrcDSAttr.get(ds);500 }501 502 // Database-specific modification.503 // This is not the best way, but cannot provide universal solution.504 Attribute attr;505 if (datasetName.contains("REACTOME")) {506 attr = stub.filterToAttributeName(datasetName, "REACTOME", filterName);507 } else if (datasetName.contains("UNIPROT")) {508 attr = stub.filterToAttributeName(datasetName, "UNIPROT", filterName);509 } else if (datasetName.contains("VARIATION")) {510 attr = stub.getAttribute(datasetName, filterName + "_stable_id");511 } else {512 attr = stub.getAttribute(datasetName, filterName);513 }514 515 attrs[n] = attr;516 517 return attrs;518 312 } 519 313 … … 535 329 * free text search is not supported for BioMart-based IDMapper. 536 330 */ 537 public Set<Xref> freeSearch (String text, int limit) throws IDMapperException { 331 public Set<Xref> freeSearch (String text, int limit) 332 throws IDMapperException { 538 333 throw new UnsupportedOperationException(); 539 334 } … … 544 339 * @throws IOException if failed to read the filters 545 340 */ 546 protected Set<DataSource> getSupportedSrcDataSources() throws IOException { 341 protected Set<DataSource> getSupportedSrcDataSources() 342 throws IDMapperException { 547 343 Set<DataSource> dss = new HashSet(); 548 Map<String, Filter> filters = stub.getFilters(datasetName); 549 for (Filter filter : filters.values()) { 550 //String fullName = filter.getDisplayName()+" ("+filter.getName()+")"; 551 String fullName = filter.getDisplayName(); 552 if (fullName.endsWith("(s)")) { 553 fullName = fullName.substring(0, fullName.length()-3); 554 } 555 //TODO: mapping to bridgedb system code 556 DataSource ds = DataSource.getByFullName(fullName); 557 dss.add(ds); 558 mapSrcDSFilter.put(ds, filter); 344 Set<String> filters = stub.availableFilters(mart, dataset); 345 for (String filter : filters) { 346 DataSource ds = DataSource.getByFullName(filter); 347 if (ds!=null) { 348 dss.add(ds); 349 } 559 350 } 560 351 return dss; … … 566 357 * @throws IOException if failed to read the filters 567 358 */ 568 protected Set<DataSource> getSupportedTgtDataSources() throws IOException {569 Map<String, Attribute> attributeVals = stub.getAttributes(datasetName);359 protected Set<DataSource> getSupportedTgtDataSources() 360 throws IDMapperException { 570 361 Set<DataSource> dss = new HashSet(); 571 for (Attribute attr : attributeVals.values()) { 572 String displayName = attr.getDisplayName(); 573 String name = attr.getName(); 574 if (idOnlyForTgtDataSource) { 575 if (!displayName.endsWith("ID") 576 && !displayName.endsWith("Accession") 577 && !name.endsWith("id") 578 && !name.endsWith("accession")) { 579 continue; 580 } 581 } 582 //String fullName = displayName + " ("+name+")"; 583 String fullName = displayName; 584 //TODO: mapping to bridgedb system code 585 DataSource ds = DataSource.getByFullName(fullName); 586 dss.add(ds); 587 mapSrcDSAttr.put(ds, attr); 362 Set<String> attributes = stub.availableAttributes(mart, dataset, 363 idOnlyForTgtDataSource); 364 365 for (String attr : attributes) { 366 DataSource ds = DataSource.getByFullName(attr); 367 if (ds!=null) { 368 dss.add(ds); 369 } 588 370 } 589 371 return dss; … … 603 385 /** {@inheritDoc} */ 604 386 public Set<DataSource> getSupportedSrcDataSources() throws IDMapperException { 605 try { 606 return IDMapperBiomart.this.getSupportedSrcDataSources(); 607 } catch (IOException ex) { 608 throw new IDMapperException(ex); 609 } 387 return IDMapperBiomart.this.supportedSrcDs; 610 388 } 611 389 612 390 /** {@inheritDoc} */ 613 391 public Set<DataSource> getSupportedTgtDataSources() throws IDMapperException { 614 try { 615 return IDMapperBiomart.this.getSupportedTgtDataSources(); 616 } catch (IOException ex) { 617 throw new IDMapperException(ex); 618 } 392 return IDMapperBiomart.this.supportedTgtDs; 619 393 } 620 394 -
trunk/corelib/src/org/bridgedb/webservice/biomart/package.html
r86 r161 6 6 you can use the following piece of code: 7 7 <pre> 8 class.forName("org.bridgedb. file.IDMapperBiomart");8 class.forName("org.bridgedb.webservice.biomart.IDMapperBiomart"); 9 9 mapper = BridgeDb.connect ("idmapper-biomart:path/to/biomart?dataset=datasetname"); 10 10 </pre> -
trunk/corelib/src/org/bridgedb/webservice/biomart/util/Attribute.java
r134 r161 16 16 // 17 17 18 package org.bridgedb.webservice.biomart ;18 package org.bridgedb.webservice.biomart.util; 19 19 20 20 /** -
trunk/corelib/src/org/bridgedb/webservice/biomart/util/BiomartClient.java
r156 r161 16 16 // 17 17 18 package org.bridgedb.webservice.biomart ;18 package org.bridgedb.webservice.biomart.util; 19 19 20 20 import java.io.BufferedReader; … … 30 30 import java.util.HashMap; 31 31 import java.util.Map; 32 import java.util.Vector;33 32 34 33 import javax.xml.parsers.DocumentBuilder; … … 45 44 * BioMart service class, adapted from BioMart client in Cytoscape. 46 45 */ 47 public final class Biomart Stub{46 public final class BiomartClient { 48 47 public static final String defaultBaseURL = "http://www.biomart.org/biomart/martservice"; 49 48 50 49 private final String baseURL; 51 private static final String RESOURCE = "/org/bridgedb/webservice/biomart/ filterconversion.txt";50 private static final String RESOURCE = "/org/bridgedb/webservice/biomart/util/filterconversion.txt"; 52 51 53 52 //private Map<String, Map<String, String>> databases = null; 54 private Map<String, Database> databases = null;53 private Map<String, Database> marts = null; 55 54 56 55 private Map<String, Dataset> datasets = new HashMap(); 57 private Map<String, Vector<Dataset>> mapDbDss = new HashMap();56 private Map<String, Map<String,Dataset>> mapDbDss = new HashMap(); 58 57 private Map<String, Map<String, Filter>> mapDsFilters = new HashMap(); 59 58 private Map<String, Map<String, Attribute>> mapDsAttrs = new HashMap(); … … 62 61 63 62 private static final int BUFFER_SIZE = 81920; 64 65 // one instance per base url66 private static Map<String, BiomartStub> instances = new HashMap();67 68 /**69 * Get a BioMartStub with the default base URL.70 * @return BiomartStub71 * @throws IOException if failed to read local resource72 */73 public static BiomartStub getInstance() throws IOException {74 return getInstance(defaultBaseURL);75 }76 77 /**78 * Get a BioMartStub with the base URL.79 * @param baseURL base URL of BioMart80 * @return BioMartStub81 * @throws IOException if failed to read local resource82 */83 public static BiomartStub getInstance(String baseURL) throws IOException {84 if (baseURL==null) {85 throw new IllegalArgumentException("base url cannot be null");86 }87 88 BiomartStub instance = instances.get(baseURL);89 if (instance==null) {90 instance = new BiomartStub(baseURL);91 instances.put(baseURL, instance);92 }93 94 return instance;95 }96 97 63 98 64 /** … … 102 68 * @throws IOException if failed to read local resource 103 69 */ 104 p rivate BiomartStub(String baseURL) throws IOException {70 public BiomartClient(String baseURL) throws IOException { 105 71 this.baseURL = baseURL + "?"; 106 72 loadConversionFile(); … … 149 115 * @return converted attribute 150 116 */ 151 p ublic Attribute filterToAttributeName(String dsName, String dbName,117 private Attribute filterToAttribute(String dsName, String dbName, 152 118 String filterID) { 153 119 if (filterConversionMap.get(dbName) == null) { … … 160 126 161 127 /** 128 * 129 * @param dataset 130 * @param filter 131 * @return 132 */ 133 public Attribute filterToAttribute(String dataset, String filter) { 134 Attribute attr; 135 if (dataset.contains("REACTOME")) { 136 attr = filterToAttribute(dataset, "REACTOME", filter); 137 } else if (dataset.contains("UNIPROT")) { 138 attr = filterToAttribute(dataset, "UNIPROT", filter); 139 } else if (dataset.contains("VARIATION")) { 140 attr = getAttribute(dataset, filter + "_stable_id"); 141 } else { 142 attr = getAttribute(dataset, filter); 143 } 144 145 return attr; 146 } 147 148 /** 162 149 * Get the registry information from the base URL. 163 150 * … … 170 157 throws IOException, ParserConfigurationException, SAXException { 171 158 // If already loaded, just return it. 172 if ( databases != null)173 return databases;159 if (marts != null) 160 return marts; 174 161 175 162 // Initialize database map. 176 databases = new HashMap<String, Database>();163 marts = new HashMap<String, Database>(); 177 164 178 165 // Prepare URL for the registry status … … 208 195 } 209 196 210 databases.put(dbID, new Database(dbID,entry));197 marts.put(dbID, new Database(dbID,entry)); 211 198 } 212 199 … … 214 201 is = null; 215 202 216 return databases;203 return marts; 217 204 } 218 205 … … 223 210 * @throws IOException if failed to read 224 211 */ 225 public Vector<Dataset> getAvailableDatasets(final String martName)212 public Map<String, Dataset> getAvailableDatasets(final String martName) 226 213 throws IOException { 227 Vector<Dataset> result = mapDbDss.get(martName);214 Map<String, Dataset> result = mapDbDss.get(martName); 228 215 if (result!=null) { 229 216 return result; … … 239 226 240 227 //final Map<String, String> datasources = new HashMap<String, String>(); 241 result = new Vector();242 243 Database database = databases.get(martName);228 result = new HashMap(); 229 230 Database database = marts.get(martName); 244 231 245 232 if (database==null) { … … 266 253 if ((parts.length > 4) && parts[3].equals("1")) { 267 254 Dataset dataset = new Dataset(parts[1], parts[2], database); 268 result. add(dataset);255 result.put(dataset.getName(),dataset); 269 256 //datasourceMap.put(parts[1], martName); 270 257 datasets.put(parts[1], dataset); … … 498 485 * @return database 499 486 */ 500 public Database get Database(final String dbname) {501 return databases.get(dbname);487 public Database getMart(final String dbname) { 488 return marts.get(dbname); 502 489 } 503 490 -
trunk/corelib/src/org/bridgedb/webservice/biomart/util/Database.java
r134 r161 16 16 // 17 17 18 package org.bridgedb.webservice.biomart ;18 package org.bridgedb.webservice.biomart.util; 19 19 20 20 import java.util.Map; -
trunk/corelib/src/org/bridgedb/webservice/biomart/util/Dataset.java
r134 r161 16 16 // 17 17 18 package org.bridgedb.webservice.biomart ;18 package org.bridgedb.webservice.biomart.util; 19 19 20 20 /** … … 51 51 * @return dataset display name 52 52 */ 53 public String getDisplyName() {53 public String displayName() { 54 54 return displayName; 55 55 } … … 67 67 */ 68 68 public String toString() { 69 return getDisplyName();69 return displayName(); 70 70 } 71 71 -
trunk/corelib/src/org/bridgedb/webservice/biomart/util/Filter.java
r134 r161 16 16 // 17 17 18 package org.bridgedb.webservice.biomart ;18 package org.bridgedb.webservice.biomart.util; 19 19 20 20 /** -
trunk/corelib/src/org/bridgedb/webservice/biomart/util/XMLQueryBuilder.java
r134 r161 16 16 // 17 17 18 package org.bridgedb.webservice.biomart ;18 package org.bridgedb.webservice.biomart.util; 19 19 20 20 import java.io.StringWriter; -
trunk/corelib/test/org/bridgedb/TestBiomart.java
r156 r161 17 17 package org.bridgedb; 18 18 19 import org.bridgedb.webservice.biomart.util.BiomartClient; 19 20 import buildsystem.Measure; 20 21 … … 30 31 import junit.framework.TestCase; 31 32 32 import org.bridgedb.webservice. IDMapperBiomart;33 import org.bridgedb.webservice.biomart.IDMapperBiomart; 33 34 import org.bridgedb.webservice.biomart.*; 34 35 … … 41 42 BiomartStub biomartStub = BiomartStub.getInstance(); 42 43 43 Map<String, Database> reg = null; 44 try { 45 reg = biomartStub.getRegistry(); 46 } catch (Exception e) { 47 e.printStackTrace(); 48 } 44 Set<String> marts = biomartStub.availableMarts(); 49 45 50 Set<Database> dbs = new HashSet(reg.size()); 51 for (Database db : reg.values()) { 52 //if (db.visible()) { 53 dbs.add(db); 54 //} 55 } 56 57 for (Database db : dbs) { 58 System.out.println (db.getName()); 59 Set<Dataset> datasets = new HashSet(biomartStub.getAvailableDatasets(db.getName())); 46 for (String mart : marts) { 47 System.out.println (mart); 48 Set<String> datasets = biomartStub.availableDatasets(mart); 60 49 int nds = datasets.size(); 61 for ( Datasetds : datasets) {62 System.out.println ("\t" + ds .getName());63 IDMapperBiomart idMapper = new IDMapperBiomart( db.getName(),ds.getName());50 for (String ds : datasets) { 51 System.out.println ("\t" + ds); 52 IDMapperBiomart idMapper = new IDMapperBiomart(mart, ds); 64 53 //IDMapper idMapper = BridgeDb.connect("idmapper-biomart:dataset="+ds.getName()); 65 54 IDMapperCapabilities cap = idMapper.getCapabilities(); … … 83 72 } 84 73 } 85 74 86 75 public void testBioMartConnector() throws IOException, IDMapperException 87 76 { … … 101 90 // } 102 91 //BiomartStub biomartStub = BiomartStub.getInstance(); 103 92 104 93 //Set<Dataset> datasets = new HashSet(biomartStub.getAvailableDatasets("ensembl")); 105 94 106 95 IDMapperBiomart mapper = new IDMapperBiomart("ensembl", "hsapiens_gene_ensembl"); 107 96 System.out.println("\n===Supported source data sources==="); … … 120 109 public void testBioMartConnector2() throws IOException, IDMapperException, ClassNotFoundException 121 110 { 122 BiomartStub biomartStub = BiomartStub.getInstance();123 111 124 Class.forName("org.bridgedb.webservice.IDMapperBiomart"); 125 126 biomartStub.getAvailableDatasets("ensembl"); 112 Class.forName("org.bridgedb.webservice.biomart.IDMapperBiomart"); 127 113 128 114 //IDMapperBiomart mapper = new IDMapperBiomart("hsapiens_gene_ensembl"); 129 IDMapper mapper = BridgeDb.connect ("idmapper-biomart: mart=ensembl&dataset=hsapiens_gene_ensembl");115 IDMapper mapper = BridgeDb.connect ("idmapper-biomart:id-type-filter=false@http://www.biomart.org/biomart/martservice?mart=ensembl&dataset=hsapiens_gene_ensembl"); 130 116 System.out.println("\n===Supported source data sources==="); 131 117 for (DataSource ds : mapper.getCapabilities().getSupportedSrcDataSources())
