Changeset 161

Show
Ignore:
Timestamp:
08/19/09 05:53:32 (2 years ago)
Author:
jgao
Message:

IDMapperBiomart refactor to clean the interface

Location:
trunk/corelib
Files:
1 added
3 modified
1 copied
7 moved

Legend:

Unmodified
Added
Removed
  • trunk/corelib/build.xml

    r140 r161  
    3434        </javac> 
    3535    <copy file="../resources/filterconversion.txt" 
    36           todir="build/org/bridgedb/webservice/biomart" /> 
     36          todir="build/org/bridgedb/webservice/biomart/util" /> 
    3737  </target> 
    3838   
  • trunk/corelib/src/org/bridgedb/webservice/biomart/BiomartStub.java

    r156 r161  
    1919 
    2020import java.io.BufferedReader; 
    21 import java.io.InputStream; 
    22 import java.io.InputStreamReader; 
    2321import java.io.IOException; 
    24 import java.io.OutputStream; 
    25 import java.io.PrintStream; 
    26  
    27 import java.net.URL; 
    28 import java.net.URLConnection; 
    2922 
    3023import java.util.HashMap; 
     24import java.util.HashSet; 
     25import java.util.Iterator; 
    3126import java.util.Map; 
     27import java.util.Set; 
    3228import java.util.Vector; 
    3329 
    34 import javax.xml.parsers.DocumentBuilder; 
    35 import javax.xml.parsers.DocumentBuilderFactory; 
    3630import javax.xml.parsers.ParserConfigurationException; 
    3731 
    38 import org.w3c.dom.Document; 
    39 import org.w3c.dom.NamedNodeMap; 
    40 import org.w3c.dom.NodeList; 
     32import org.bridgedb.DataSource; 
     33import org.bridgedb.IDMapperException; 
     34import org.bridgedb.Xref; 
     35import org.bridgedb.file.IDMappingReaderFromDelimitedReader; 
     36import org.bridgedb.webservice.biomart.util.Attribute; 
     37import org.bridgedb.webservice.biomart.util.BiomartClient; 
     38import org.bridgedb.webservice.biomart.util.Database; 
     39import org.bridgedb.webservice.biomart.util.Dataset; 
     40import org.bridgedb.webservice.biomart.util.Filter; 
     41import org.bridgedb.webservice.biomart.util.XMLQueryBuilder; 
    4142 
    4243import org.xml.sax.SAXException; 
    4344 
    4445/** 
    45  * BioMart service class, adapted from BioMart client in Cytoscape. 
     46 * Cache for SynergizerClient 
     47 * @author gjj 
    4648 */ 
    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 
     49public 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 
    6658    private static Map<String, BiomartStub> instances = new HashMap(); 
    6759 
    6860    /** 
    69      * Get a BioMartStub with the default base URL. 
    70      * @return BiomartStub 
    71      * @throws IOException if failed to read local resource 
     61     * 
     62     * @return SynergizerStub with the default server url 
     63     * @throws IOException if failed to connect 
    7264     */ 
    7365    public static BiomartStub getInstance() throws IOException { 
     
    7668 
    7769    /** 
    78      * Get a BioMartStub with the base URL. 
    79      * @param baseURL base URL of BioMart 
    80      * @return BioMartStub 
    81      * @throws IOException if failed to read local resource 
    82      */ 
    83     public static BiomartStub getInstance(String baseURL) throws IOException { 
    84         if (baseURL==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) { 
    8577            throw new IllegalArgumentException("base url cannot be null"); 
    8678        } 
    8779 
    88         BiomartStub instance = instances.get(baseURL); 
     80        BiomartStub instance = instances.get(baseUrl); 
    8981        if (instance==null) { 
    90             instance = new BiomartStub(baseURL); 
    91             instances.put(baseURL, instance); 
     82            instance = new BiomartStub(baseUrl); 
     83            instances.put(baseUrl, instance); 
    9284        } 
    9385 
    9486        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()); 
    135121            } 
    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) { 
    154135            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) { 
    246176            return null; 
    247177        } 
    248178 
    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); 
    344247                } 
    345248            } 
    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 
    412340            } 
    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; 
    536346    } 
    537347} 
  • trunk/corelib/src/org/bridgedb/webservice/biomart/IDMapperBiomart.java

    r156 r161  
    1616// 
    1717 
    18 package org.bridgedb.webservice; 
    19  
    20 import java.io.BufferedReader; 
     18package org.bridgedb.webservice.biomart; 
     19 
    2120import java.io.IOException; 
    2221 
    2322import java.util.HashMap; 
    2423import java.util.HashSet; 
    25 import java.util.Iterator; 
    2624import java.util.Map; 
    2725import java.util.Set; 
    28 import java.util.Vector; 
    29  
    30 import javax.xml.parsers.ParserConfigurationException; 
    3126 
    3227import org.bridgedb.AbstractIDMapperCapabilities; 
     
    3732import org.bridgedb.IDMapperException; 
    3833import 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; 
     34import org.bridgedb.webservice.IDMapperWebservice; 
     35import org.bridgedb.webservice.biomart.util.BiomartClient; 
    4636 
    4737/** 
     
    6151        /** {@inheritDoc} */ 
    6252        public IDMapper connect(String location) throws IDMapperException  { 
    63             // e.g.: transitivity=false,id-type-filter=true@dataset=oanatinus_gene_ensembl 
     53            // e.g.: id-type-filter=true@dataset=oanatinus_gene_ensembl 
    6454            // 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; 
    6756            boolean idTypeFilter = true; 
    6857             
     
    7867 
    7968            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  
    9569                String idTypeFilterTag = "id-type-filter="; 
    9670                idx = config.indexOf(idTypeFilterTag); 
     
    11791            String martTag = "mart="; 
    11892            idx = param.indexOf(martTag); 
    119             String martName = param.substring(idx+martTag.length()); 
    120  
    121             idx = martName.indexOf("&"); 
     93            String mart = param.substring(idx+martTag.length()); 
     94 
     95            idx = mart.indexOf("&"); 
    12296            if (idx>-1) { 
    123                 martName = martName.substring(0,idx); 
     97                mart = mart.substring(0,idx); 
    12498            } 
    12599 
    126100            String datasetTag = "dataset="; 
    127101            idx = param.indexOf(datasetTag); 
    128             String datasetName = param.substring(idx+datasetTag.length()); 
    129  
    130             idx = datasetName.indexOf("&"); 
     102            String dataset = param.substring(idx+datasetTag.length()); 
     103 
     104            idx = dataset.indexOf("&"); 
    131105            if (idx>-1) { 
    132                 datasetName = datasetName.substring(0,idx); 
    133             } 
    134  
    135             return new IDMapperBiomart(martName, datasetName, baseURL,  
    136                     idTypeFilter, transitivity); 
    137         } 
    138     } 
    139  
    140     private String martName; 
    141     private String datasetName; 
     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; 
    142116    private BiomartStub stub; 
    143     private boolean transitivity; 
    144117    private boolean idOnlyForTgtDataSource; 
    145118 
    146119    private String baseURL; 
    147120 
    148     private Map<DataSource, Filter> mapSrcDSFilter; 
    149     private Map<DataSource, Attribute> mapSrcDSAttr; 
     121    private Set<DataSource> supportedSrcDs; 
     122    private Set<DataSource> supportedTgtDs; 
    150123 
    151124    /** 
    152125     * Transitivity is unsupported.ID only. ID only for target data sources. 
    153126     * Use default url of BiMart. 
    154      * @param martName name of mart 
    155      * @param datasetName name of dataset 
     127     * @param mart name of mart 
     128     * @param dataset name of dataset 
    156129     * @throws IDMapperException if failed to link to the dataset 
    157130     */ 
    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); 
    160134    } 
    161135 
    162136    /** 
    163137     * Use default url of BiMart. 
    164      * @param martName name of mart 
    165      * @param datasetName name of dataset 
     138     * @param mart name of mart 
     139     * @param dataset name of dataset 
    166140     * @param idOnlyForTgtDataSource id-only option, filter data source ends 
    167141     *        with 'ID' or 'Accession'. 
     
    169143     * @throws IDMapperException if failed to link to the dataset 
    170144     */ 
    171     public IDMapperBiomart(String martName, String datasetName, boolean idOnlyForTgtDataSource, 
    172                 boolean transitivity) throws IDMapperException { 
    173         this(martName, datasetName, null, idOnlyForTgtDataSource, transitivity); 
     145    public IDMapperBiomart(String mart, String dataset, 
     146            boolean idOnlyForTgtDataSource) throws IDMapperException { 
     147        this(mart, dataset, null, idOnlyForTgtDataSource); 
    174148    } 
    175149 
    176150    /** 
    177151     * Transitivity is unsupported.ID only. ID only for target data sources. 
    178      * @param martName name of mart 
    179      * @param datasetName name of dataset 
     152     * @param mart name of mart 
     153     * @param dataset name of dataset 
    180154     * @param baseURL base url of BioMart 
    181155     * @throws IDMapperException if failed to link to the dataset 
    182156     */ 
    183     public IDMapperBiomart(String martName, String datasetName, String baseURL) 
     157    public IDMapperBiomart(String mart, String dataset, String baseURL) 
    184158                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); 
    200160    } 
    201161 
     
    203163     * Construct from a dataset, a database, id-only option and transitivity 
    204164     * option. 
    205      * @param martName name of mart 
    206      * @param datasetName name of dataset 
     165     * @param mart name of mart 
     166     * @param dataset name of dataset 
    207167     * @param baseURL base url of BioMart 
    208168     * @param idOnlyForTgtDataSource id-only option, filter data source ends 
     
    211171     * @throws IDMapperException if failed to link to the dataset 
    212172     */ 
    213     public IDMapperBiomart(String martName, String datasetName, String baseURL, 
    214             boolean idOnlyForTgtDataSource, boolean transitivity) throws IDMapperException { 
    215         this.martName = martName; 
    216         this.datasetName = datasetName; 
     173    public IDMapperBiomart(String mart, String dataset, String baseURL, 
     174            boolean idOnlyForTgtDataSource) throws IDMapperException { 
     175        this.mart = mart; 
     176        this.dataset = dataset; 
    217177        if (baseURL!=null) { 
    218178            this.baseURL = baseURL; 
    219179        } else { 
    220             this.baseURL = BiomartStub.defaultBaseURL; 
     180            this.baseURL = BiomartClient.defaultBaseURL; 
    221181        } 
    222182 
     
    227187        } 
    228188 
    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(); 
    250201         
    251202        cap = new BiomartCapabilities(); 
     
    253204 
    254205    /** 
    255      * Filter target datasource ending with "ID" or "Accession". 
    256      * @param idOnlyForTgtDataSource ID-only if true 
    257      */ 
    258     public void setIDOnlyForTgtDataSource(boolean idOnlyForTgtDataSource) { 
    259         this.idOnlyForTgtDataSource = idOnlyForTgtDataSource; 
    260     } 
    261  
    262     /** 
    263206     * 
    264207     * @return true if ID-only for target data sources. 
     
    269212 
    270213    /** 
    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     /** 
    301214     * 
    302215     * @return base URL of BioMart. 
     
    308221    /** 
    309222     * 
    310      * @param mart mart name 
    311      */ 
    312     public void setMart(final String mart) { 
    313         this.martName = mart; 
    314     } 
    315  
    316     /** 
    317      * 
    318223     * @return mart name 
    319224     */ 
    320225    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; 
    330227    } 
    331228 
     
    335232     */ 
    336233    public String getDataset() { 
    337         return datasetName; 
     234        return dataset; 
    338235    } 
    339236 
     
    353250        if (srcXrefs==null) { 
    354251            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; 
    435294                } 
    436295 
    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); 
    440304                    } 
    441305                } 
    442             } 
    443         } 
     306                 
     307                result.put(srcXref, tgtXrefs); 
     308            } 
     309        }         
    444310 
    445311        return result; 
    446     } 
    447  
    448     /** 
    449      * Create filters from the source xrefs. 
    450      * @param srcXrefs source xrefs 
    451      * @return map from data source to IDs 
    452      */ 
    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 sources 
    488      * @param filterName filter name 
    489      * @return attributes 
    490      */ 
    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; 
    518312    } 
    519313 
     
    535329     * free text search is not supported for BioMart-based IDMapper. 
    536330     */ 
    537     public Set<Xref> freeSearch (String text, int limit) throws IDMapperException { 
     331    public Set<Xref> freeSearch (String text, int limit) 
     332            throws IDMapperException { 
    538333        throw new UnsupportedOperationException(); 
    539334    } 
     
    544339     * @throws IOException if failed to read the filters 
    545340     */ 
    546     protected Set<DataSource> getSupportedSrcDataSources() throws IOException { 
     341    protected Set<DataSource> getSupportedSrcDataSources() 
     342            throws IDMapperException { 
    547343        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            } 
    559350        } 
    560351        return dss; 
     
    566357     * @throws IOException if failed to read the filters 
    567358     */ 
    568     protected Set<DataSource> getSupportedTgtDataSources() throws IOException { 
    569         Map<String, Attribute> attributeVals = stub.getAttributes(datasetName); 
     359    protected Set<DataSource> getSupportedTgtDataSources()  
     360            throws IDMapperException { 
    570361        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            } 
    588370        } 
    589371        return dss; 
     
    603385        /** {@inheritDoc} */ 
    604386        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; 
    610388        } 
    611389 
    612390        /** {@inheritDoc} */ 
    613391        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; 
    619393        } 
    620394 
  • trunk/corelib/src/org/bridgedb/webservice/biomart/package.html

    r86 r161  
    66you can use the following piece of code:   
    77<pre> 
    8         class.forName("org.bridgedb.file.IDMapperBiomart"); 
     8        class.forName("org.bridgedb.webservice.biomart.IDMapperBiomart"); 
    99        mapper = BridgeDb.connect ("idmapper-biomart:path/to/biomart?dataset=datasetname"); 
    1010</pre> 
  • trunk/corelib/src/org/bridgedb/webservice/biomart/util/Attribute.java

    r134 r161  
    1616// 
    1717 
    18 package org.bridgedb.webservice.biomart; 
     18package org.bridgedb.webservice.biomart.util; 
    1919 
    2020/** 
  • trunk/corelib/src/org/bridgedb/webservice/biomart/util/BiomartClient.java

    r156 r161  
    1616// 
    1717 
    18 package org.bridgedb.webservice.biomart; 
     18package org.bridgedb.webservice.biomart.util; 
    1919 
    2020import java.io.BufferedReader; 
     
    3030import java.util.HashMap; 
    3131import java.util.Map; 
    32 import java.util.Vector; 
    3332 
    3433import javax.xml.parsers.DocumentBuilder; 
     
    4544 * BioMart service class, adapted from BioMart client in Cytoscape. 
    4645 */ 
    47 public final class BiomartStub { 
     46public final class BiomartClient { 
    4847    public static final String defaultBaseURL = "http://www.biomart.org/biomart/martservice"; 
    4948     
    5049    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"; 
    5251 
    5352    //private Map<String, Map<String, String>> databases = null; 
    54     private Map<String, Database> databases = null; 
     53    private Map<String, Database> marts = null; 
    5554 
    5655    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(); 
    5857    private Map<String, Map<String, Filter>> mapDsFilters = new HashMap(); 
    5958    private Map<String, Map<String, Attribute>> mapDsAttrs = new HashMap(); 
     
    6261 
    6362    private static final int BUFFER_SIZE = 81920; 
    64  
    65     // one instance per base url 
    66     private static Map<String, BiomartStub> instances = new HashMap(); 
    67  
    68     /** 
    69      * Get a BioMartStub with the default base URL. 
    70      * @return BiomartStub 
    71      * @throws IOException if failed to read local resource 
    72      */ 
    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 BioMart 
    80      * @return BioMartStub 
    81      * @throws IOException if failed to read local resource 
    82      */ 
    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  
    9763 
    9864    /** 
     
    10268     * @throws IOException if failed to read local resource 
    10369     */ 
    104     private BiomartStub(String baseURL) throws IOException { 
     70    public BiomartClient(String baseURL) throws IOException { 
    10571        this.baseURL = baseURL + "?"; 
    10672        loadConversionFile(); 
     
    149115     * @return converted attribute 
    150116     */ 
    151     public Attribute filterToAttributeName(String dsName, String dbName, 
     117    private Attribute filterToAttribute(String dsName, String dbName, 
    152118                String filterID) { 
    153119        if (filterConversionMap.get(dbName) == null) { 
     
    160126 
    161127    /** 
     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    /** 
    162149     *  Get the registry information from the base URL. 
    163150     * 
     
    170157            throws IOException, ParserConfigurationException, SAXException { 
    171158        // If already loaded, just return it. 
    172         if (databases != null) 
    173             return databases; 
     159        if (marts != null) 
     160            return marts; 
    174161 
    175162        // Initialize database map. 
    176         databases = new HashMap<String, Database>(); 
     163        marts = new HashMap<String, Database>(); 
    177164 
    178165        // Prepare URL for the registry status 
     
    208195            } 
    209196 
    210             databases.put(dbID, new Database(dbID,entry)); 
     197            marts.put(dbID, new Database(dbID,entry)); 
    211198        } 
    212199 
     
    214201        is = null; 
    215202 
    216         return databases; 
     203        return marts; 
    217204    } 
    218205 
     
    223210     * @throws IOException if failed to read 
    224211     */ 
    225     public Vector<Dataset> getAvailableDatasets(final String martName) 
     212    public Map<String, Dataset> getAvailableDatasets(final String martName) 
    226213            throws IOException { 
    227         Vector<Dataset> result = mapDbDss.get(martName); 
     214        Map<String, Dataset> result = mapDbDss.get(martName); 
    228215        if (result!=null) { 
    229216            return result; 
     
    239226 
    240227        //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); 
    244231 
    245232        if (database==null) { 
     
    266253            if ((parts.length > 4) && parts[3].equals("1")) { 
    267254                Dataset dataset = new Dataset(parts[1], parts[2], database); 
    268                 result.add(dataset); 
     255                result.put(dataset.getName(),dataset); 
    269256                //datasourceMap.put(parts[1], martName); 
    270257                datasets.put(parts[1], dataset); 
     
    498485     * @return database 
    499486     */ 
    500     public Database getDatabase(final String dbname) { 
    501         return databases.get(dbname); 
     487    public Database getMart(final String dbname) { 
     488        return marts.get(dbname); 
    502489    } 
    503490 
  • trunk/corelib/src/org/bridgedb/webservice/biomart/util/Database.java

    r134 r161  
    1616// 
    1717 
    18 package org.bridgedb.webservice.biomart; 
     18package org.bridgedb.webservice.biomart.util; 
    1919 
    2020import java.util.Map; 
  • trunk/corelib/src/org/bridgedb/webservice/biomart/util/Dataset.java

    r134 r161  
    1616// 
    1717 
    18 package org.bridgedb.webservice.biomart; 
     18package org.bridgedb.webservice.biomart.util; 
    1919 
    2020/** 
     
    5151     * @return dataset display name 
    5252     */ 
    53     public String getDisplyName() { 
     53    public String displayName() { 
    5454        return displayName; 
    5555    } 
     
    6767     */ 
    6868    public String toString() { 
    69         return getDisplyName(); 
     69        return displayName(); 
    7070    } 
    7171 
  • trunk/corelib/src/org/bridgedb/webservice/biomart/util/Filter.java

    r134 r161  
    1616// 
    1717 
    18 package org.bridgedb.webservice.biomart; 
     18package org.bridgedb.webservice.biomart.util; 
    1919 
    2020/** 
  • trunk/corelib/src/org/bridgedb/webservice/biomart/util/XMLQueryBuilder.java

    r134 r161  
    1616// 
    1717 
    18 package org.bridgedb.webservice.biomart; 
     18package org.bridgedb.webservice.biomart.util; 
    1919 
    2020import java.io.StringWriter; 
  • trunk/corelib/test/org/bridgedb/TestBiomart.java

    r156 r161  
    1717package org.bridgedb; 
    1818 
     19import org.bridgedb.webservice.biomart.util.BiomartClient; 
    1920import buildsystem.Measure; 
    2021 
     
    3031import junit.framework.TestCase; 
    3132 
    32 import org.bridgedb.webservice.IDMapperBiomart; 
     33import org.bridgedb.webservice.biomart.IDMapperBiomart; 
    3334import org.bridgedb.webservice.biomart.*; 
    3435 
     
    4142        BiomartStub biomartStub = BiomartStub.getInstance(); 
    4243 
    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(); 
    4945 
    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); 
    6049            int nds = datasets.size(); 
    61             for (Dataset ds : 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); 
    6453                //IDMapper idMapper = BridgeDb.connect("idmapper-biomart:dataset="+ds.getName()); 
    6554                IDMapperCapabilities cap = idMapper.getCapabilities(); 
     
    8372        } 
    8473    } 
    85      
     74 
    8675    public void testBioMartConnector() throws IOException, IDMapperException 
    8776    { 
     
    10190//         } 
    10291        //BiomartStub biomartStub = BiomartStub.getInstance(); 
    103          
     92 
    10493        //Set<Dataset> datasets = new HashSet(biomartStub.getAvailableDatasets("ensembl")); 
    105          
     94 
    10695         IDMapperBiomart mapper = new IDMapperBiomart("ensembl", "hsapiens_gene_ensembl"); 
    10796         System.out.println("\n===Supported source data sources==="); 
     
    120109    public void testBioMartConnector2() throws IOException, IDMapperException, ClassNotFoundException 
    121110    { 
    122         BiomartStub biomartStub = BiomartStub.getInstance(); 
    123111         
    124         Class.forName("org.bridgedb.webservice.IDMapperBiomart"); 
    125          
    126         biomartStub.getAvailableDatasets("ensembl"); 
     112        Class.forName("org.bridgedb.webservice.biomart.IDMapperBiomart"); 
    127113         
    128114         //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"); 
    130116        System.out.println("\n===Supported source data sources==="); 
    131117         for (DataSource ds : mapper.getCapabilities().getSupportedSrcDataSources())