Show
Ignore:
Timestamp:
08/14/09 21:50:23 (3 years ago)
Author:
jgao
Message:

IDMapperText & IDMapperBiomart driver update,
IDMapperBiomart constructor with mart

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/corelib/src/org/bridgedb/webservice/IDMapperBiomart.java

    r155 r156  
    2828import java.util.Vector; 
    2929 
     30import javax.xml.parsers.ParserConfigurationException; 
     31 
    3032import org.bridgedb.AbstractIDMapperCapabilities; 
    3133import org.bridgedb.BridgeDb; 
     
    4143import org.bridgedb.webservice.biomart.XMLQueryBuilder; 
    4244 
     45import org.xml.sax.SAXException; 
     46 
    4347/** 
    4448 * 
     
    5559                private Driver() { }  
    5660 
    57         private static final String DATASET_TAG = "dataset="; 
    58  
    5961        /** {@inheritDoc} */ 
    60                 public IDMapper connect(String location) throws IDMapperException  { 
    61             // e.g.: dataset=oanatinus_gene_ensembl 
    62                         // e.g.: http://www.biomart.org/biomart/martservice?dataset=oanatinus_gene_ensembl 
     62        public IDMapper connect(String location) throws IDMapperException  { 
     63            // e.g.: transitivity=false,id-type-filter=true@dataset=oanatinus_gene_ensembl 
     64            // e.g.: http://www.biomart.org/biomart/martservice?dataset=oanatinus_gene_ensembl 
    6365            String baseURL = BiomartStub.defaultBaseURL; 
    64             String param = location; 
    65             int idx = location.indexOf('?'); 
     66            boolean transitivity = false; 
     67            boolean idTypeFilter = true; 
     68             
     69            String config = null; 
     70            String path = location; 
     71            int idx = location.indexOf("@"); 
     72            if (idx==0) { 
     73                path =location.substring(idx+1); 
     74            } else if (idx>0) { 
     75                config = location.substring(0, idx); 
     76                path = location.substring(idx+1); 
     77            } 
     78 
     79            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                String idTypeFilterTag = "id-type-filter="; 
     96                idx = config.indexOf(idTypeFilterTag); 
     97                if (idx!=-1) { 
     98                    String filter = config.substring(idx+idTypeFilterTag.length()); 
     99                    if (filter.toLowerCase().startsWith("true")) { 
     100                        idTypeFilter = true; 
     101                    } else if (filter.toLowerCase().startsWith("false")) { 
     102                        idTypeFilter = false; 
     103                    } else { 
     104                        throw new IDMapperException( 
     105                                "id-type-filter can only be true or false"); 
     106                    } 
     107                } 
     108            } 
     109 
     110            String param = path; 
     111            idx = path.indexOf('?'); 
    66112            if (idx>-1) { 
    67                 baseURL = location.substring(0,idx); 
    68                 param = location.substring(idx+1); 
    69             } 
    70  
    71             idx = param.indexOf(DATASET_TAG); 
    72             String datasetName = param.substring(idx+DATASET_TAG.length()); 
     113                baseURL = path.substring(0,idx); 
     114                param = path.substring(idx+1); 
     115            } 
     116 
     117            String martTag = "mart="; 
     118            idx = param.indexOf(martTag); 
     119            String martName = param.substring(idx+martTag.length()); 
     120 
     121            idx = martName.indexOf("&"); 
     122            if (idx>-1) { 
     123                martName = martName.substring(0,idx); 
     124            } 
     125 
     126            String datasetTag = "dataset="; 
     127            idx = param.indexOf(datasetTag); 
     128            String datasetName = param.substring(idx+datasetTag.length()); 
    73129 
    74130            idx = datasetName.indexOf("&"); 
     
    77133            } 
    78134 
    79             return new IDMapperBiomart(datasetName, baseURL); 
    80                 } 
    81         } 
    82  
     135            return new IDMapperBiomart(martName, datasetName, baseURL,  
     136                    idTypeFilter, transitivity); 
     137        } 
     138    } 
     139 
     140    private String martName; 
    83141    private String datasetName; 
    84142    private BiomartStub stub; 
     
    94152     * Transitivity is unsupported.ID only. ID only for target data sources. 
    95153     * Use default url of BiMart. 
     154     * @param martName name of mart 
    96155     * @param datasetName name of dataset 
    97156     * @throws IDMapperException if failed to link to the dataset 
    98157     */ 
    99     public IDMapperBiomart(String datasetName) throws IDMapperException { 
    100         this(datasetName, null); 
     158    public IDMapperBiomart(String martName, String datasetName) throws IDMapperException { 
     159        this(martName, datasetName, null); 
    101160    } 
    102161 
    103162    /** 
    104163     * Use default url of BiMart. 
     164     * @param martName name of mart 
    105165     * @param datasetName name of dataset 
    106166     * @param idOnlyForTgtDataSource id-only option, filter data source ends 
     
    109169     * @throws IDMapperException if failed to link to the dataset 
    110170     */ 
    111     public IDMapperBiomart(String datasetName, boolean idOnlyForTgtDataSource, 
     171    public IDMapperBiomart(String martName, String datasetName, boolean idOnlyForTgtDataSource, 
    112172                boolean transitivity) throws IDMapperException { 
    113         this(datasetName, null, idOnlyForTgtDataSource, transitivity); 
     173        this(martName, datasetName, null, idOnlyForTgtDataSource, transitivity); 
    114174    } 
    115175 
    116176    /** 
    117177     * Transitivity is unsupported.ID only. ID only for target data sources. 
     178     * @param martName name of mart 
    118179     * @param datasetName name of dataset 
    119180     * @param baseURL base url of BioMart 
    120181     * @throws IDMapperException if failed to link to the dataset 
    121182     */ 
    122     public IDMapperBiomart(String datasetName, String baseURL) 
     183    public IDMapperBiomart(String martName, String datasetName, String baseURL) 
    123184                throws IDMapperException { 
    124         this(datasetName, baseURL, true); 
     185        this(martName, datasetName, baseURL, true); 
    125186    } 
    126187 
    127188    /** 
    128189     * Transitivity is unsupported. 
     190     * @param martName name of mart 
    129191     * @param datasetName name of dataset 
    130192     * @param baseURL base url of BioMart 
     
    133195     * @throws IDMapperException if failed to link to the dataset 
    134196     */ 
    135     public IDMapperBiomart(String datasetName, String baseURL, 
     197    public IDMapperBiomart(String martName, String datasetName, String baseURL, 
    136198            boolean idOnlyForTgtDataSource) throws IDMapperException { 
    137         this(datasetName, baseURL, idOnlyForTgtDataSource, false); 
     199        this(martName, datasetName, baseURL, idOnlyForTgtDataSource, false); 
    138200    } 
    139201 
     
    141203     * Construct from a dataset, a database, id-only option and transitivity 
    142204     * option. 
     205     * @param martName name of mart 
    143206     * @param datasetName name of dataset 
    144207     * @param baseURL base url of BioMart 
     
    148211     * @throws IDMapperException if failed to link to the dataset 
    149212     */ 
    150     public IDMapperBiomart(String datasetName, String baseURL,  
     213    public IDMapperBiomart(String martName, String datasetName, String baseURL, 
    151214            boolean idOnlyForTgtDataSource, boolean transitivity) throws IDMapperException { 
     215        this.martName = martName; 
    152216        this.datasetName = datasetName; 
    153217        if (baseURL!=null) { 
     
    162226            throw new IDMapperException(e); 
    163227        } 
     228 
     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        } 
    164244         
    165245        setIDOnlyForTgtDataSource(idOnlyForTgtDataSource); 
     
    224304    public String getBaseURL() { 
    225305        return baseURL; 
     306    } 
     307 
     308    /** 
     309     * 
     310     * @param mart mart name 
     311     */ 
     312    public void setMart(final String mart) { 
     313        this.martName = mart; 
     314    } 
     315 
     316    /** 
     317     * 
     318     * @return mart name 
     319     */ 
     320    public String getMart() { 
     321        return martName; 
    226322    } 
    227323