Changeset 156

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

IDMapperText & IDMapperBiomart driver update,
IDMapperBiomart constructor with mart

Location:
trunk/corelib
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • trunk/corelib/src/org/bridgedb/file/IDMapperText.java

    r134 r156  
    4949            { 
    5050                // parse arguments to determine idsep and dssep 
    51                 // sample: dssep:\t,idsep:;,idsep:,@file:/localfile.txt 
     51                // sample: dssep=\t,idsep=;,idsep=,,transitivity=false@file:/localfile.txt 
    5252                // \t represents tab, \@ represents @ 
    5353                String  path = null; 
    5454                char[] dssep = null; 
    5555                char[] idsep = null; 
     56                boolean transitivity = false; 
    5657 
    5758                int idx = location.indexOf("@"); 
     
    8182 
    8283                        String config = location.substring(0, idx)+","; 
     84                        String prefixTran = "transitivity="; 
     85                        idx = config.indexOf(prefixTran); 
     86                        String tran = config.substring(idx+prefixTran.length()); 
     87                        if (tran.toLowerCase().startsWith("true")) { 
     88                            transitivity = true; 
     89                        } else if (tran.toLowerCase().startsWith("false")) { 
     90                            transitivity = false; 
     91                        } else { 
     92                            throw new IDMapperException( 
     93                                    "transivity can only be true or false"); 
     94                        } 
     95 
    8396                        dssep = parseConfig(config, "dssep"); 
    8497                        idsep = parseConfig(config, "idsep"); 
     
    88101                try 
    89102                { 
    90                         return new IDMapperText(new URL(path), dssep, idsep); 
     103                        return new IDMapperText(new URL(path), dssep, idsep, 
     104                                transitivity); 
    91105                } 
    92106                catch (MalformedURLException ex) 
     
    98112            private char[] parseConfig(String config, String head) { 
    99113                Set<Character> delimiters = new HashSet(); 
    100                 Pattern p = Pattern.compile(head+":(.|\\t|\\@),", Pattern.CASE_INSENSITIVE); 
     114                Pattern p = Pattern.compile(head+"=(.|\\t|\\@),", 
     115                        Pattern.CASE_INSENSITIVE); 
    101116                Matcher m = p.matcher(config); 
    102117                while (m.find()) { 
  • 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 
  • trunk/corelib/src/org/bridgedb/webservice/biomart/BiomartStub.java

    r134 r156  
    242242 
    243243        Database database = databases.get(martName); 
     244 
     245        if (database==null) { 
     246            return null; 
     247        } 
    244248 
    245249        Map<String, String> detail = database.getParam(); 
  • trunk/corelib/test/org/bridgedb/TestBiomart.java

    r149 r156  
    6161            for (Dataset ds : datasets) { 
    6262                System.out.println ("\t" + ds.getName()); 
    63                 IDMapperBiomart idMapper = new IDMapperBiomart(ds.getName()); 
     63                IDMapperBiomart idMapper = new IDMapperBiomart(db.getName(),ds.getName()); 
    6464                //IDMapper idMapper = BridgeDb.connect("idmapper-biomart:dataset="+ds.getName()); 
    6565                IDMapperCapabilities cap = idMapper.getCapabilities(); 
     
    100100//                System.out.println (db.getName()); 
    101101//         } 
    102         BiomartStub biomartStub = BiomartStub.getInstance(); 
     102        //BiomartStub biomartStub = BiomartStub.getInstance(); 
    103103         
    104         Set<Dataset> datasets = new HashSet(biomartStub.getAvailableDatasets("ensembl")); 
     104        //Set<Dataset> datasets = new HashSet(biomartStub.getAvailableDatasets("ensembl")); 
    105105         
    106          IDMapperBiomart mapper = new IDMapperBiomart("hsapiens_gene_ensembl"); 
     106         IDMapperBiomart mapper = new IDMapperBiomart("ensembl", "hsapiens_gene_ensembl"); 
    107107         System.out.println("\n===Supported source data sources==="); 
    108108         for (DataSource ds : mapper.getCapabilities().getSupportedSrcDataSources()) 
     
    127127         
    128128         //IDMapperBiomart mapper = new IDMapperBiomart("hsapiens_gene_ensembl"); 
    129         IDMapper mapper = BridgeDb.connect ("idmapper-biomart:dataset=hsapiens_gene_ensembl"); 
     129        IDMapper mapper = BridgeDb.connect ("idmapper-biomart:mart=ensembl&dataset=hsapiens_gene_ensembl"); 
    130130        System.out.println("\n===Supported source data sources==="); 
    131131         for (DataSource ds : mapper.getCapabilities().getSupportedSrcDataSources()) 
  • trunk/corelib/test/org/bridgedb/TestFile.java

    r105 r156  
    5757         
    5858        public void testRead() throws IDMapperException, IOException 
    59         {        
     59        { 
    6060                IDMapperFile idMapper = new IDMapperText (YEAST_IDS.toURL()); 
    6161 
     
    6767        tgtDataSources.add(ENTREZ); 
    6868        tgtDataSources.add(EMBL); 
    69                  
     69 
    7070                long start = System.currentTimeMillis(); 
    7171        // mapID for the first time will trigger reading 
     
    7575                System.out.println (delta); 
    7676                measure.add ("timing::text file non-transitive", "" + delta, "msec"); 
    77          
     77 
    7878                Set<Xref> expected = new HashSet<Xref>(); 
    7979        expected.addAll (Arrays.asList( 
    80                         new Xref("YHR055C", ENS_YEAST),  
    81                         new Xref("U00061", EMBL),  
     80                        new Xref("YHR055C", ENS_YEAST), 
     81                        new Xref("U00061", EMBL), 
    8282                        new Xref("K02204", EMBL), 
    8383                        new Xref("AY558517", EMBL), 
     
    8888        Set<Xref> xrefs = mapXrefs.get(XREF1); 
    8989        assertEquals (expected, xrefs); 
    90          
     90 
    9191        for (Xref xr : xrefs) { 
    9292            System.out.println(xr.getDataSource().getFullName() + ": " + xr.getId()); 
    9393        } 
    94          
     94 
    9595        } 
    9696 
    9797        public void testTransitive() throws MalformedURLException, IDMapperException 
    9898        { 
    99                 IDMapperFile idMapper = new IDMapperText (YEAST_IDS.toURL(),  
     99                IDMapperFile idMapper = new IDMapperText (YEAST_IDS.toURL(), 
    100100                                new char[] { '\t' }, 
    101101                                new char[] { ',' }, 
    102102                                true); 
    103                  
     103 
    104104        Set<Xref> srcXrefs = new HashSet<Xref>(); 
    105105        srcXrefs.add(XREF1); 
     
    109109        tgtDataSources.add(ENTREZ); 
    110110        tgtDataSources.add(EMBL); 
    111          
     111 
    112112                long start = System.currentTimeMillis(); 
    113113        // mapID for the first time will trigger reading 
     
    117117                System.out.println (delta); 
    118118                measure.add ("timing::text file transitive", "" + delta, "msec"); 
    119                  
     119 
    120120                System.out.println (mapXrefs); 
    121121        Set<Xref> xrefs = mapXrefs.get(XREF1); 
    122          
     122 
    123123        for (Xref xr : xrefs) { 
    124124            System.out.println(xr.getDataSource().getFullName() + ": " + xr.getId());