| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | package org.bridgedb.webservice.picr; |
|---|
| 18 | |
|---|
| 19 | import java.util.ArrayList; |
|---|
| 20 | import java.util.Arrays; |
|---|
| 21 | import java.util.Collection; |
|---|
| 22 | import java.util.HashMap; |
|---|
| 23 | import java.util.HashSet; |
|---|
| 24 | import java.util.List; |
|---|
| 25 | import java.util.Map; |
|---|
| 26 | import java.util.Set; |
|---|
| 27 | |
|---|
| 28 | import org.bridgedb.AbstractIDMapperCapabilities; |
|---|
| 29 | import org.bridgedb.AttributeMapper; |
|---|
| 30 | import org.bridgedb.BridgeDb; |
|---|
| 31 | import org.bridgedb.DataSource; |
|---|
| 32 | import org.bridgedb.IDMapper; |
|---|
| 33 | import org.bridgedb.IDMapperCapabilities; |
|---|
| 34 | import org.bridgedb.IDMapperException; |
|---|
| 35 | import org.bridgedb.impl.InternalUtils; |
|---|
| 36 | import org.bridgedb.webservice.IDMapperWebservice; |
|---|
| 37 | import org.bridgedb.Xref; |
|---|
| 38 | |
|---|
| 39 | import uk.ac.ebi.demo.picr.business.PICRClient; |
|---|
| 40 | import uk.ac.ebi.demo.picr.soap.CrossReference; |
|---|
| 41 | import uk.ac.ebi.demo.picr.soap.UPEntry; |
|---|
| 42 | |
|---|
| 43 | public 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 | |
|---|
| 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 | |
|---|
| 75 | |
|---|
| 76 | public boolean getOnlyActive() { |
|---|
| 77 | return onlyActive; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 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() { } |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | } |
|---|