root/trunk/picr/src/org/bridgedb/webservice/picr/IDMapperPicr.java @ 252

Revision 252, 7.4 KB (checked in by martijn, 10 months ago)

Made Picr and PicrRest? comply with basic unit tests

Line 
1// BridgeDb,
2// An abstraction layer for identifer mapping services, both local and online.
3// Copyright 2006-2009 BridgeDb developers
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
17package org.bridgedb.webservice.picr;
18
19import java.util.ArrayList;
20import java.util.Arrays;
21import java.util.Collection;
22import java.util.HashMap;
23import java.util.HashSet;
24import java.util.List;
25import java.util.Map;
26import java.util.Set;
27
28import org.bridgedb.AbstractIDMapperCapabilities;
29import org.bridgedb.AttributeMapper;
30import org.bridgedb.BridgeDb;
31import org.bridgedb.DataSource;
32import org.bridgedb.IDMapper;
33import org.bridgedb.IDMapperCapabilities;
34import org.bridgedb.IDMapperException;
35import org.bridgedb.impl.InternalUtils;
36import org.bridgedb.webservice.IDMapperWebservice;
37import org.bridgedb.Xref;
38
39import uk.ac.ebi.demo.picr.business.PICRClient;
40import uk.ac.ebi.demo.picr.soap.CrossReference;
41import uk.ac.ebi.demo.picr.soap.UPEntry;
42
43public final class IDMapperPicr extends IDMapperWebservice implements AttributeMapper
44{
45    static 
46    {
47                BridgeDb.register ("idmapper-picr", new Driver());
48        }
49
50    private boolean onlyActive;
51   
52    private final Set<DataSource> supportedDatabases = new HashSet<DataSource>();
53    private final Object[] supportedDbObjects;
54    private final PICRClient client;
55
56    /**
57     *
58     * @param onlyActive using only active mappings if true
59     */
60    public IDMapperPicr(boolean onlyActive)
61    {
62        client = new PICRClient();
63        List<String> databases = client.loadDatabases();
64        for (String s : databases)
65        {
66                supportedDatabases.add(DataSource.getByFullName(s));
67        }
68        supportedDbObjects = databases.toArray();
69        this.onlyActive = onlyActive;
70    }
71
72    /**
73     *
74     * @return true if using only active mappings; false otherwise
75     */
76    public boolean getOnlyActive() {
77        return onlyActive;
78    }
79
80    /**
81     *
82     * @param onlyActive using only active mappings if true
83     */
84    public void setOnlyActive(boolean onlyActive) {
85        this.onlyActive = onlyActive;
86    }
87
88    private static class Driver implements org.bridgedb.Driver
89        {
90        private Driver() { } // prevent outside instantiation
91
92                public IDMapper connect(String location) throws IDMapperException 
93                {
94            Map<String, String> args =
95                InternalUtils.parseLocation(location, "only-active");
96
97                        boolean isOnlyActive = true;
98                        if (args.containsKey("only-active"))
99                        {
100                                isOnlyActive = Boolean.parseBoolean(args.get("only-active"));
101                        }
102                        return new IDMapperPicr(isOnlyActive);
103                }
104        }
105       
106        private boolean closed = false;
107        public void close() throws IDMapperException
108        {
109                closed = true;
110        }
111
112        public Set<Xref> freeSearch(String text, int limit)
113                        throws IDMapperException {
114                throw new UnsupportedOperationException();
115        }
116
117        private class PICRCapabilities extends AbstractIDMapperCapabilities
118        {
119                public PICRCapabilities()
120                {
121                        super (supportedDatabases, false, null);
122                }
123        }
124       
125        private PICRCapabilities picrCapabilities = new PICRCapabilities();
126       
127        public IDMapperCapabilities getCapabilities()
128        {
129                return picrCapabilities;
130        }
131
132        public boolean isConnected()
133        {
134                return !closed;
135        }
136
137        /**
138         * @{inheritDocs}
139         */
140        public Map<Xref, Set<Xref>> mapID(Collection<Xref> srcXrefs,
141                        DataSource... tgtDataSources) throws IDMapperException
142        {
143                return InternalUtils.mapMultiFromSingle(this, srcXrefs, tgtDataSources);
144        }
145                       
146        /**
147         * @{inheritDocs}
148         */
149        public Set<Xref> mapID(Xref srcXref,
150                        DataSource... tgtDataSources) throws IDMapperException
151        {               
152                Object[] databases;
153                if (tgtDataSources.length == 0)
154                        databases = supportedDbObjects;
155                else
156                        databases = objectsFromDataSources(tgtDataSources);
157               
158                Set<Xref> result = new HashSet<Xref>();
159                if (databases.length == 0) return result;
160                if (!supportedDatabases.contains(srcXref.getDataSource())) return result;
161
162                List<CrossReference> refs = new ArrayList<CrossReference>();
163               
164                List<UPEntry> entries = client.performAccessionMapping(srcXref.getId(), databases);
165        for (UPEntry entry : entries) {
166                refs.addAll (entry.getIdenticalCrossReferences());
167            refs.addAll (entry.getLogicalCrossReferences());
168        }
169       
170        for (CrossReference ref : refs)
171        {
172                // in onlyActive mode, check if it is deleted first
173                if (!(onlyActive && ref.isDeleted()))
174                        {
175                        Xref xref = new Xref (ref.getAccession(), DataSource.getByFullName(ref.getDatabaseName()));
176                        result.add (xref);
177                        }
178        }
179                return result;
180        }
181       
182        /**
183         * Only returns supported databases. Resulting array may be shorter than input.
184         */
185        private Object[] objectsFromDataSources (DataSource... ds)
186        {
187                List<Object> databases = new ArrayList<Object>(ds.length);             
188                for (DataSource tgt : ds)
189                {
190                        if (supportedDatabases.contains(tgt))
191                                databases.add(tgt.getFullName());
192                }
193                return databases.toArray();
194        }
195       
196        public boolean xrefExists(Xref xref) throws IDMapperException
197        {
198                if (!supportedDatabases.contains(xref.getDataSource())) return false;
199                List<UPEntry> result = client.performAccessionMapping(
200                                xref.getId(), objectsFromDataSources(xref.getDataSource()));
201                return result.size() > 0;
202        }
203
204        public Map<Xref, String> freeAttributeSearch(String query, String attrType,
205                        int limit) throws IDMapperException
206        {
207                throw new UnsupportedOperationException();
208        }
209
210        public Set<String> getAttributes(Xref ref, String attrType)
211                        throws IDMapperException
212        {
213                Set<String> result = new HashSet<String>();
214                if (!supportedDatabases.contains(ref.getDataSource())) return result;
215               
216                List<UPEntry> entries = client.performAccessionMapping(ref.getId(), supportedDbObjects);
217                for (UPEntry entry : entries)
218                {
219                        if ("CRC64".equals (attrType))
220                        {
221                                result.add (entry.getCRC64());
222                        }
223                        else if ("Sequence".equals (attrType))
224                        {
225                                result.add (entry.getSequence());
226                        }
227                        else if ("UPI".equals (attrType))
228                        {
229                                result.add (entry.getUPI());
230                        }
231                        else if ("Timestamp".equals (attrType))
232                        {
233                                result.add ("" + entry.getTimestamp());
234                        }
235                }
236               
237                return result;
238        }
239
240        private static final Set<String> SUPPORTED_ATTRIBUTES = 
241                new HashSet<String>(Arrays.asList(
242                                new String[] {"CRC64", "Sequence", "UPI", "Timestamp"} ));
243       
244        public Set<String> getAttributeSet() throws IDMapperException
245        {
246                return SUPPORTED_ATTRIBUTES;
247        }
248
249        public Map<String, Set<String>> getAttributes(Xref ref)
250                        throws IDMapperException
251        {
252                Map<String, Set<String>> result = new HashMap<String, Set<String>>();
253                if (!supportedDatabases.contains(ref.getDataSource())) return result;
254
255                List<UPEntry> entries = client.performAccessionMapping(ref.getId(), supportedDbObjects);
256                for (UPEntry entry : entries)
257                {                       
258                        InternalUtils.multiMapPut (result, "CRC64", entry.getCRC64());
259                        InternalUtils.multiMapPut (result, "Sequence", entry.getSequence());
260                        InternalUtils.multiMapPut (result, "UPI", entry.getUPI());
261                        InternalUtils.multiMapPut (result, "Timestamp", "" + entry.getTimestamp());
262                }
263                return result;
264        }
265}
Note: See TracBrowser for help on using the browser.