| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | package org.bridgedb.rdb; |
|---|
| 18 | |
|---|
| 19 | import java.sql.Connection; |
|---|
| 20 | import java.sql.PreparedStatement; |
|---|
| 21 | import java.sql.ResultSet; |
|---|
| 22 | import java.sql.ResultSetMetaData; |
|---|
| 23 | import java.sql.SQLException; |
|---|
| 24 | import java.sql.Statement; |
|---|
| 25 | import java.util.ArrayList; |
|---|
| 26 | import java.util.Arrays; |
|---|
| 27 | import java.util.HashMap; |
|---|
| 28 | import java.util.HashSet; |
|---|
| 29 | import java.util.List; |
|---|
| 30 | import java.util.Map; |
|---|
| 31 | import java.util.Set; |
|---|
| 32 | import java.util.regex.Matcher; |
|---|
| 33 | import java.util.regex.Pattern; |
|---|
| 34 | |
|---|
| 35 | import org.bridgedb.AbstractIDMapperCapabilities; |
|---|
| 36 | import org.bridgedb.DataSource; |
|---|
| 37 | import org.bridgedb.IDMapperCapabilities; |
|---|
| 38 | import org.bridgedb.IDMapperException; |
|---|
| 39 | import org.bridgedb.Xref; |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | class SimpleGdbImpl2 extends SimpleGdb |
|---|
| 43 | { |
|---|
| 44 | private static final int GDB_COMPAT_VERSION = 2; |
|---|
| 45 | |
|---|
| 46 | private final SimpleGdb.LazyPst pstDatasources = new SimpleGdb.LazyPst( |
|---|
| 47 | "SELECT codeRight FROM link GROUP BY codeRight" |
|---|
| 48 | ); |
|---|
| 49 | private final SimpleGdb.LazyPst pstInfo = new SimpleGdb.LazyPst( |
|---|
| 50 | "SELECT * FROM info" |
|---|
| 51 | ); |
|---|
| 52 | private final SimpleGdb.LazyPst pstXrefExists = new SimpleGdb.LazyPst( |
|---|
| 53 | "SELECT id FROM " + "datanode" + " WHERE " + |
|---|
| 54 | "id = ? AND code = ?" |
|---|
| 55 | ); |
|---|
| 56 | private final SimpleGdb.LazyPst pstBackpage = new SimpleGdb.LazyPst( |
|---|
| 57 | "SELECT backpageText FROM datanode " + |
|---|
| 58 | " WHERE id = ? AND code = ?" |
|---|
| 59 | ); |
|---|
| 60 | private final SimpleGdb.LazyPst pstAttribute = new SimpleGdb.LazyPst( |
|---|
| 61 | "SELECT attrvalue FROM attribute " + |
|---|
| 62 | " WHERE id = ? AND code = ? AND attrname = ?" |
|---|
| 63 | ); |
|---|
| 64 | private final SimpleGdb.LazyPst pstAllAttributes = new SimpleGdb.LazyPst( |
|---|
| 65 | "SELECT attrname, attrvalue FROM attribute " + |
|---|
| 66 | " WHERE id = ? AND code = ?" |
|---|
| 67 | ); |
|---|
| 68 | private final SimpleGdb.LazyPst pstAttributesSet = new SimpleGdb.LazyPst( |
|---|
| 69 | "SELECT attrname FROM attribute GROUP BY attrname" |
|---|
| 70 | ); |
|---|
| 71 | private final SimpleGdb.LazyPst pstCrossRefs = new SimpleGdb.LazyPst ( |
|---|
| 72 | "SELECT dest.idRight, dest.codeRight FROM link AS src JOIN link AS dest " + |
|---|
| 73 | "ON src.idLeft = dest.idLeft and src.codeLeft = dest.codeLeft " + |
|---|
| 74 | "WHERE src.idRight = ? AND src.codeRight = ?" |
|---|
| 75 | ); |
|---|
| 76 | private final SimpleGdb.LazyPst pstCrossRefsWithCode = new SimpleGdb.LazyPst ( |
|---|
| 77 | "SELECT dest.idRight, dest.codeRight FROM link AS src JOIN link AS dest " + |
|---|
| 78 | "ON src.idLeft = dest.idLeft and src.codeLeft = dest.codeLeft " + |
|---|
| 79 | "WHERE src.idRight = ? AND src.codeRight = ? AND dest.codeRight = ?" |
|---|
| 80 | ); |
|---|
| 81 | private final SimpleGdb.LazyPst pstRefsByAttribute = new SimpleGdb.LazyPst ( |
|---|
| 82 | "SELECT datanode.id, datanode.code FROM datanode " + |
|---|
| 83 | " LEFT JOIN attribute ON attribute.code = datanode.code AND attribute.id = datanode.id " + |
|---|
| 84 | "WHERE attrName = ? AND attrValue = ?" |
|---|
| 85 | ); |
|---|
| 86 | private final SimpleGdb.LazyPst pstFreeSearch = new SimpleGdb.LazyPst ( |
|---|
| 87 | "SELECT id, code FROM datanode WHERE " + |
|---|
| 88 | "LOWER(ID) LIKE ?" |
|---|
| 89 | ); |
|---|
| 90 | private final SimpleGdb.LazyPst pstAttributeSearch = new SimpleGdb.LazyPst ( |
|---|
| 91 | "SELECT id, code, attrvalue FROM attribute WHERE " + |
|---|
| 92 | "attrname = 'Symbol' AND LOWER(attrvalue) LIKE ?" |
|---|
| 93 | ); |
|---|
| 94 | private final SimpleGdb.LazyPst pstIdSearchWithAttributes = new SimpleGdb.LazyPst ( |
|---|
| 95 | "SELECT id, code, attrvalue FROM attribute WHERE " + |
|---|
| 96 | "attrname = 'Symbol' AND LOWER(ID) LIKE ?" |
|---|
| 97 | ); |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | public boolean xrefExists(Xref xref) throws IDMapperException |
|---|
| 101 | { |
|---|
| 102 | try |
|---|
| 103 | { |
|---|
| 104 | PreparedStatement pst = pstXrefExists.getPreparedStatement(); |
|---|
| 105 | pst.setString(1, xref.getId()); |
|---|
| 106 | pst.setString(2, xref.getDataSource().getSystemCode()); |
|---|
| 107 | ResultSet r = pst.executeQuery(); |
|---|
| 108 | |
|---|
| 109 | while(r.next()) |
|---|
| 110 | { |
|---|
| 111 | return true; |
|---|
| 112 | } |
|---|
| 113 | } |
|---|
| 114 | catch (SQLException e) |
|---|
| 115 | { |
|---|
| 116 | throw new IDMapperException (e); |
|---|
| 117 | } |
|---|
| 118 | return false; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | private Map<String, String> getInfo() throws IDMapperException |
|---|
| 127 | { |
|---|
| 128 | Map<String, String> result = new HashMap<String, String>(); |
|---|
| 129 | try |
|---|
| 130 | { |
|---|
| 131 | PreparedStatement pst = pstInfo.getPreparedStatement(); |
|---|
| 132 | ResultSet rs = pst.executeQuery(); |
|---|
| 133 | |
|---|
| 134 | if (rs.next()) |
|---|
| 135 | { |
|---|
| 136 | ResultSetMetaData rsmd = rs.getMetaData(); |
|---|
| 137 | for (int i = 1; i <= rsmd.getColumnCount(); ++i) |
|---|
| 138 | { |
|---|
| 139 | String key = rsmd.getColumnName(i); |
|---|
| 140 | String val = rs.getString(i); |
|---|
| 141 | result.put (key, val); |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | catch (SQLException ex) |
|---|
| 146 | { |
|---|
| 147 | throw new IDMapperException (ex); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | return result; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | private String getBpInfo(Xref ref) throws IDMapperException |
|---|
| 163 | { |
|---|
| 164 | try { |
|---|
| 165 | PreparedStatement pst = pstBackpage.getPreparedStatement(); |
|---|
| 166 | pst.setString (1, ref.getId()); |
|---|
| 167 | pst.setString (2, ref.getDataSource().getSystemCode()); |
|---|
| 168 | ResultSet r = pst.executeQuery(); |
|---|
| 169 | String result = null; |
|---|
| 170 | if (r.next()) |
|---|
| 171 | { |
|---|
| 172 | result = r.getString(1); |
|---|
| 173 | } |
|---|
| 174 | return result; |
|---|
| 175 | } catch (SQLException e) { throw new IDMapperException (e); } |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | public Set<Xref> mapID (Xref idc, DataSource... resultDs) throws IDMapperException |
|---|
| 180 | { |
|---|
| 181 | Set<Xref> refs = new HashSet<Xref>(); |
|---|
| 182 | |
|---|
| 183 | if (idc.getDataSource() == null) return refs; |
|---|
| 184 | try |
|---|
| 185 | { |
|---|
| 186 | PreparedStatement pst; |
|---|
| 187 | if (resultDs.length != 1) |
|---|
| 188 | { |
|---|
| 189 | pst = pstCrossRefs.getPreparedStatement(); |
|---|
| 190 | } |
|---|
| 191 | else |
|---|
| 192 | { |
|---|
| 193 | pst = pstCrossRefsWithCode.getPreparedStatement(); |
|---|
| 194 | pst.setString(3, resultDs[0].getSystemCode()); |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | pst.setString(1, idc.getId()); |
|---|
| 198 | pst.setString(2, idc.getDataSource().getSystemCode()); |
|---|
| 199 | |
|---|
| 200 | Set<DataSource> dsFilter = new HashSet<DataSource>(Arrays.asList(resultDs)); |
|---|
| 201 | |
|---|
| 202 | ResultSet rs = pst.executeQuery(); |
|---|
| 203 | while (rs.next()) |
|---|
| 204 | { |
|---|
| 205 | DataSource ds = DataSource.getBySystemCode(rs.getString(2)); |
|---|
| 206 | if (resultDs.length == 0 || dsFilter.contains(ds)) |
|---|
| 207 | { |
|---|
| 208 | refs.add (new Xref (rs.getString(1), ds)); |
|---|
| 209 | } |
|---|
| 210 | } |
|---|
| 211 | } |
|---|
| 212 | catch (SQLException e) |
|---|
| 213 | { |
|---|
| 214 | throw new IDMapperException (e); |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | return refs; |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | public List<Xref> getCrossRefsByAttribute(String attrName, String attrValue) throws IDMapperException { |
|---|
| 222 | |
|---|
| 223 | List<Xref> refs = new ArrayList<Xref>(); |
|---|
| 224 | |
|---|
| 225 | try { |
|---|
| 226 | PreparedStatement pst = pstRefsByAttribute.getPreparedStatement(); |
|---|
| 227 | pst.setString(1, attrName); |
|---|
| 228 | pst.setString(2, attrValue); |
|---|
| 229 | ResultSet r = pst.executeQuery(); |
|---|
| 230 | while(r.next()) { |
|---|
| 231 | Xref ref = new Xref(r.getString(1), DataSource.getBySystemCode(r.getString(2))); |
|---|
| 232 | refs.add(ref); |
|---|
| 233 | } |
|---|
| 234 | } catch(SQLException e) { |
|---|
| 235 | throw new IDMapperException (e); |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | return refs; |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | public SimpleGdbImpl2(String dbName, Connection con, int props) throws IDMapperException |
|---|
| 251 | { |
|---|
| 252 | super (con); |
|---|
| 253 | |
|---|
| 254 | if(dbName == null) throw new NullPointerException(); |
|---|
| 255 | this.dbName = dbName; |
|---|
| 256 | |
|---|
| 257 | if ((props & DBConnector.PROP_RECREATE) == 0) |
|---|
| 258 | { |
|---|
| 259 | try |
|---|
| 260 | { |
|---|
| 261 | con.setReadOnly(true); |
|---|
| 262 | } |
|---|
| 263 | catch (SQLException e) |
|---|
| 264 | { |
|---|
| 265 | throw new IDMapperException (e); |
|---|
| 266 | } |
|---|
| 267 | checkSchemaVersion(); |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | caps = new SimpleGdbCapabilities(); |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | private void checkSchemaVersion() throws IDMapperException |
|---|
| 278 | { |
|---|
| 279 | int version = 0; |
|---|
| 280 | try |
|---|
| 281 | { |
|---|
| 282 | ResultSet r = con.createStatement().executeQuery("SELECT schemaversion FROM info"); |
|---|
| 283 | if(r.next()) version = r.getInt(1); |
|---|
| 284 | } |
|---|
| 285 | catch (SQLException e) |
|---|
| 286 | { |
|---|
| 287 | |
|---|
| 288 | } |
|---|
| 289 | if(version != GDB_COMPAT_VERSION) |
|---|
| 290 | { |
|---|
| 291 | throw new IDMapperException ("Implementation and schema version mismatch"); |
|---|
| 292 | } |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | |
|---|
| 301 | public void createGdbTables() |
|---|
| 302 | { |
|---|
| 303 | |
|---|
| 304 | try |
|---|
| 305 | { |
|---|
| 306 | Statement sh = con.createStatement(); |
|---|
| 307 | sh.execute("DROP TABLE info"); |
|---|
| 308 | sh.execute("DROP TABLE link"); |
|---|
| 309 | sh.execute("DROP TABLE datanode"); |
|---|
| 310 | sh.execute("DROP TABLE attribute"); |
|---|
| 311 | } |
|---|
| 312 | catch(SQLException e) |
|---|
| 313 | { |
|---|
| 314 | |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | try |
|---|
| 318 | { |
|---|
| 319 | Statement sh = con.createStatement(); |
|---|
| 320 | sh.execute( |
|---|
| 321 | "CREATE TABLE " + |
|---|
| 322 | " info " + |
|---|
| 323 | "( schemaversion INTEGER PRIMARY KEY " + |
|---|
| 324 | ")"); |
|---|
| 325 | |
|---|
| 326 | sh.execute( |
|---|
| 327 | "INSERT INTO info VALUES ( " + GDB_COMPAT_VERSION + ")"); |
|---|
| 328 | |
|---|
| 329 | sh.execute( |
|---|
| 330 | "CREATE TABLE " + |
|---|
| 331 | " link " + |
|---|
| 332 | " ( idLeft VARCHAR(50) NOT NULL, " + |
|---|
| 333 | " codeLeft VARCHAR(50) NOT NULL, " + |
|---|
| 334 | " idRight VARCHAR(50) NOT NULL, " + |
|---|
| 335 | " codeRight VARCHAR(50) NOT NULL, " + |
|---|
| 336 | " bridge VARCHAR(50), " + |
|---|
| 337 | " PRIMARY KEY (idLeft, codeLeft, " + |
|---|
| 338 | " idRight, codeRight) " + |
|---|
| 339 | " ) "); |
|---|
| 340 | |
|---|
| 341 | sh.execute( |
|---|
| 342 | "CREATE TABLE " + |
|---|
| 343 | " datanode " + |
|---|
| 344 | " ( id VARCHAR(50), " + |
|---|
| 345 | " code VARCHAR(50), " + |
|---|
| 346 | " backpageText VARCHAR(800), " + |
|---|
| 347 | " PRIMARY KEY (id, code) " + |
|---|
| 348 | " ) "); |
|---|
| 349 | |
|---|
| 350 | sh.execute( |
|---|
| 351 | "CREATE TABLE " + |
|---|
| 352 | " attribute " + |
|---|
| 353 | " ( id VARCHAR(50), " + |
|---|
| 354 | " code VARCHAR(50), " + |
|---|
| 355 | " attrname VARCHAR(50), " + |
|---|
| 356 | " attrvalue VARCHAR(255) " + |
|---|
| 357 | " ) "); |
|---|
| 358 | |
|---|
| 359 | } |
|---|
| 360 | catch (SQLException e) |
|---|
| 361 | { |
|---|
| 362 | |
|---|
| 363 | } |
|---|
| 364 | } |
|---|
| 365 | |
|---|
| 366 | |
|---|
| 367 | public static final int NO_LIMIT = 0; |
|---|
| 368 | public static final int NO_TIMEOUT = 0; |
|---|
| 369 | public static final int QUERY_TIMEOUT = 20; |
|---|
| 370 | |
|---|
| 371 | |
|---|
| 372 | public Set<Xref> freeSearch (String text, int limit) throws IDMapperException |
|---|
| 373 | { |
|---|
| 374 | Set<Xref> result = new HashSet<Xref>(); |
|---|
| 375 | try { |
|---|
| 376 | PreparedStatement ps1 = pstFreeSearch.getPreparedStatement(); |
|---|
| 377 | ps1.setQueryTimeout(QUERY_TIMEOUT); |
|---|
| 378 | if(limit > NO_LIMIT) |
|---|
| 379 | { |
|---|
| 380 | ps1.setMaxRows(limit); |
|---|
| 381 | } |
|---|
| 382 | |
|---|
| 383 | ps1.setString(1, "%" + text.toLowerCase() + "%"); |
|---|
| 384 | ResultSet r = ps1.executeQuery(); |
|---|
| 385 | while(r.next()) { |
|---|
| 386 | String id = r.getString(1); |
|---|
| 387 | DataSource ds = DataSource.getBySystemCode(r.getString(2)); |
|---|
| 388 | Xref ref = new Xref (id, ds); |
|---|
| 389 | result.add (ref); |
|---|
| 390 | } |
|---|
| 391 | } |
|---|
| 392 | catch (SQLException e) |
|---|
| 393 | { |
|---|
| 394 | throw new IDMapperException(e); |
|---|
| 395 | } |
|---|
| 396 | return result; |
|---|
| 397 | } |
|---|
| 398 | |
|---|
| 399 | private PreparedStatement pstGene = null; |
|---|
| 400 | private PreparedStatement pstLink = null; |
|---|
| 401 | private PreparedStatement pstAttr = null; |
|---|
| 402 | |
|---|
| 403 | |
|---|
| 404 | public int addGene(Xref ref, String bpText) |
|---|
| 405 | { |
|---|
| 406 | if (pstGene == null) throw new NullPointerException(); |
|---|
| 407 | try |
|---|
| 408 | { |
|---|
| 409 | pstGene.setString(1, ref.getId()); |
|---|
| 410 | pstGene.setString(2, ref.getDataSource().getSystemCode()); |
|---|
| 411 | pstGene.setString(3, bpText); |
|---|
| 412 | pstGene.executeUpdate(); |
|---|
| 413 | } |
|---|
| 414 | catch (SQLException e) |
|---|
| 415 | { |
|---|
| 416 | |
|---|
| 417 | return 1; |
|---|
| 418 | } |
|---|
| 419 | return 0; |
|---|
| 420 | } |
|---|
| 421 | |
|---|
| 422 | |
|---|
| 423 | public int addAttribute(Xref ref, String attr, String val) |
|---|
| 424 | { |
|---|
| 425 | try { |
|---|
| 426 | pstAttr.setString(1, attr); |
|---|
| 427 | pstAttr.setString(2, val); |
|---|
| 428 | pstAttr.setString(3, ref.getId()); |
|---|
| 429 | pstAttr.setString(4, ref.getDataSource().getSystemCode()); |
|---|
| 430 | pstAttr.executeUpdate(); |
|---|
| 431 | } catch (SQLException e) { |
|---|
| 432 | |
|---|
| 433 | return 1; |
|---|
| 434 | } |
|---|
| 435 | return 0; |
|---|
| 436 | } |
|---|
| 437 | |
|---|
| 438 | |
|---|
| 439 | public int addLink(Xref left, Xref right) |
|---|
| 440 | { |
|---|
| 441 | if (pstLink == null) throw new NullPointerException(); |
|---|
| 442 | try |
|---|
| 443 | { |
|---|
| 444 | pstLink.setString(1, left.getId()); |
|---|
| 445 | pstLink.setString(2, left.getDataSource().getSystemCode()); |
|---|
| 446 | pstLink.setString(3, right.getId()); |
|---|
| 447 | pstLink.setString(4, right.getDataSource().getSystemCode()); |
|---|
| 448 | pstLink.executeUpdate(); |
|---|
| 449 | } |
|---|
| 450 | catch (SQLException e) |
|---|
| 451 | { |
|---|
| 452 | |
|---|
| 453 | return 1; |
|---|
| 454 | } |
|---|
| 455 | return 0; |
|---|
| 456 | } |
|---|
| 457 | |
|---|
| 458 | |
|---|
| 459 | |
|---|
| 460 | |
|---|
| 461 | |
|---|
| 462 | |
|---|
| 463 | |
|---|
| 464 | public void createGdbIndices() throws IDMapperException |
|---|
| 465 | { |
|---|
| 466 | try |
|---|
| 467 | { |
|---|
| 468 | Statement sh = con.createStatement(); |
|---|
| 469 | sh.execute( |
|---|
| 470 | "CREATE INDEX i_codeLeft" + |
|---|
| 471 | " ON link(codeLeft)" |
|---|
| 472 | ); |
|---|
| 473 | sh.execute( |
|---|
| 474 | "CREATE INDEX i_idRight" + |
|---|
| 475 | " ON link(idRight)" |
|---|
| 476 | ); |
|---|
| 477 | sh.execute( |
|---|
| 478 | "CREATE INDEX i_codeRight" + |
|---|
| 479 | " ON link(codeRight)" |
|---|
| 480 | ); |
|---|
| 481 | sh.execute( |
|---|
| 482 | "CREATE INDEX i_code" + |
|---|
| 483 | " ON " + "datanode" + "(code)" |
|---|
| 484 | ); |
|---|
| 485 | } |
|---|
| 486 | catch (SQLException e) |
|---|
| 487 | { |
|---|
| 488 | throw new IDMapperException (e); |
|---|
| 489 | } |
|---|
| 490 | } |
|---|
| 491 | |
|---|
| 492 | |
|---|
| 493 | |
|---|
| 494 | |
|---|
| 495 | |
|---|
| 496 | public void preInsert() throws IDMapperException |
|---|
| 497 | { |
|---|
| 498 | try |
|---|
| 499 | { |
|---|
| 500 | con.setAutoCommit(false); |
|---|
| 501 | pstGene = con.prepareStatement( |
|---|
| 502 | "INSERT INTO datanode " + |
|---|
| 503 | " (id, code," + |
|---|
| 504 | " backpageText)" + |
|---|
| 505 | "VALUES (?, ?, ?)" |
|---|
| 506 | ); |
|---|
| 507 | pstLink = con.prepareStatement( |
|---|
| 508 | "INSERT INTO link " + |
|---|
| 509 | " (idLeft, codeLeft," + |
|---|
| 510 | " idRight, codeRight)" + |
|---|
| 511 | "VALUES (?, ?, ?, ?)" |
|---|
| 512 | ); |
|---|
| 513 | pstAttr = con.prepareStatement( |
|---|
| 514 | "INSERT INTO attribute " + |
|---|
| 515 | " (attrname, attrvalue, id, code)" + |
|---|
| 516 | "VALUES (?, ?, ?, ?)" |
|---|
| 517 | ); |
|---|
| 518 | } |
|---|
| 519 | catch (SQLException e) |
|---|
| 520 | { |
|---|
| 521 | throw new IDMapperException (e); |
|---|
| 522 | } |
|---|
| 523 | } |
|---|
| 524 | |
|---|
| 525 | |
|---|
| 526 | |
|---|
| 527 | |
|---|
| 528 | |
|---|
| 529 | private Set<DataSource> getDataSources() throws IDMapperException |
|---|
| 530 | { |
|---|
| 531 | Set<DataSource> result = new HashSet<DataSource>(); |
|---|
| 532 | try |
|---|
| 533 | { |
|---|
| 534 | PreparedStatement pst = pstDatasources.getPreparedStatement(); |
|---|
| 535 | ResultSet rs = pst.executeQuery(); |
|---|
| 536 | while (rs.next()) |
|---|
| 537 | { |
|---|
| 538 | DataSource ds = DataSource.getBySystemCode(rs.getString(1)); |
|---|
| 539 | result.add (ds); |
|---|
| 540 | } |
|---|
| 541 | } |
|---|
| 542 | catch (SQLException ignore) |
|---|
| 543 | { |
|---|
| 544 | throw new IDMapperException(ignore); |
|---|
| 545 | } |
|---|
| 546 | return result; |
|---|
| 547 | } |
|---|
| 548 | |
|---|
| 549 | private final IDMapperCapabilities caps; |
|---|
| 550 | |
|---|
| 551 | private class SimpleGdbCapabilities extends AbstractIDMapperCapabilities |
|---|
| 552 | { |
|---|
| 553 | |
|---|
| 554 | |
|---|
| 555 | public SimpleGdbCapabilities() throws IDMapperException |
|---|
| 556 | { |
|---|
| 557 | super (SimpleGdbImpl2.this.getDataSources(), true, |
|---|
| 558 | SimpleGdbImpl2.this.getInfo()); |
|---|
| 559 | } |
|---|
| 560 | } |
|---|
| 561 | |
|---|
| 562 | |
|---|
| 563 | |
|---|
| 564 | |
|---|
| 565 | public IDMapperCapabilities getCapabilities() |
|---|
| 566 | { |
|---|
| 567 | return caps; |
|---|
| 568 | } |
|---|
| 569 | |
|---|
| 570 | private static final Map<String, String> ATTRIBUTES_FROM_BACKPAGE; |
|---|
| 571 | |
|---|
| 572 | static |
|---|
| 573 | { |
|---|
| 574 | ATTRIBUTES_FROM_BACKPAGE = new HashMap<String, String>(); |
|---|
| 575 | ATTRIBUTES_FROM_BACKPAGE.put ("Chromosome", "<TH>Chr:<TH>([^<]*)<"); |
|---|
| 576 | ATTRIBUTES_FROM_BACKPAGE.put ("Description", "<TH>Description:<TH>([^<]*)<"); |
|---|
| 577 | ATTRIBUTES_FROM_BACKPAGE.put ("Synonyms", "<TH>Synonyms:<TH>([^<]*)<"); |
|---|
| 578 | ATTRIBUTES_FROM_BACKPAGE.put ("Symbol", "<TH>(?:Gene Symbol|Metabolite):<TH>([^<]*)<"); |
|---|
| 579 | ATTRIBUTES_FROM_BACKPAGE.put ("BrutoFormula", "<TH>Bruto Formula:<TH>([^<]*)<"); |
|---|
| 580 | } |
|---|
| 581 | |
|---|
| 582 | |
|---|
| 583 | public Set<String> getAttributes(Xref ref, String attrname) |
|---|
| 584 | throws IDMapperException |
|---|
| 585 | { |
|---|
| 586 | Set<String> result = new HashSet<String>(); |
|---|
| 587 | |
|---|
| 588 | if (ATTRIBUTES_FROM_BACKPAGE.containsKey(attrname)) |
|---|
| 589 | { |
|---|
| 590 | String bpInfo = getBpInfo(ref); |
|---|
| 591 | if (bpInfo != null) |
|---|
| 592 | { |
|---|
| 593 | Pattern pat = Pattern.compile(ATTRIBUTES_FROM_BACKPAGE.get (attrname)); |
|---|
| 594 | Matcher matcher = pat.matcher(bpInfo); |
|---|
| 595 | if (matcher.find()) |
|---|
| 596 | { |
|---|
| 597 | result.add (matcher.group(1)); |
|---|
| 598 | } |
|---|
| 599 | } |
|---|
| 600 | } |
|---|
| 601 | |
|---|
| 602 | try { |
|---|
| 603 | PreparedStatement pst = pstAttribute.getPreparedStatement(); |
|---|
| 604 | pst.setString (1, ref.getId()); |
|---|
| 605 | pst.setString (2, ref.getDataSource().getSystemCode()); |
|---|
| 606 | pst.setString (3, attrname); |
|---|
| 607 | ResultSet r = pst.executeQuery(); |
|---|
| 608 | if (r.next()) |
|---|
| 609 | { |
|---|
| 610 | result.add (r.getString(1)); |
|---|
| 611 | } |
|---|
| 612 | return result; |
|---|
| 613 | } catch (SQLException e) { throw new IDMapperException ("Xref:" + ref + ", Attribute: " + attrname, e); } |
|---|
| 614 | } |
|---|
| 615 | |
|---|
| 616 | |
|---|
| 617 | public Map<String, Set<String>> getAttributes(Xref ref) |
|---|
| 618 | throws IDMapperException |
|---|
| 619 | { |
|---|
| 620 | Map<String, Set<String>> result = new HashMap<String, Set<String>>(); |
|---|
| 621 | |
|---|
| 622 | String bpInfo = getBpInfo(ref); |
|---|
| 623 | if (bpInfo != null) |
|---|
| 624 | { |
|---|
| 625 | for (String attrname : ATTRIBUTES_FROM_BACKPAGE.keySet()) |
|---|
| 626 | { |
|---|
| 627 | Pattern pat = Pattern.compile(ATTRIBUTES_FROM_BACKPAGE.get (attrname)); |
|---|
| 628 | Matcher matcher = pat.matcher(bpInfo); |
|---|
| 629 | if (matcher.find()) |
|---|
| 630 | { |
|---|
| 631 | Set<String> attrSet = new HashSet<String>(); |
|---|
| 632 | attrSet.add (matcher.group(1)); |
|---|
| 633 | result.put (attrname, attrSet); |
|---|
| 634 | } |
|---|
| 635 | } |
|---|
| 636 | } |
|---|
| 637 | |
|---|
| 638 | try { |
|---|
| 639 | PreparedStatement pst = pstAllAttributes.getPreparedStatement(); |
|---|
| 640 | pst.setString (1, ref.getId()); |
|---|
| 641 | pst.setString (2, ref.getDataSource().getSystemCode()); |
|---|
| 642 | ResultSet r = pst.executeQuery(); |
|---|
| 643 | if (r.next()) |
|---|
| 644 | { |
|---|
| 645 | String key = r.getString(1); |
|---|
| 646 | String value = r.getString(2); |
|---|
| 647 | if (result.containsKey (key)) |
|---|
| 648 | { |
|---|
| 649 | result.get(key).add (value); |
|---|
| 650 | } |
|---|
| 651 | else |
|---|
| 652 | { |
|---|
| 653 | Set<String> valueSet = new HashSet<String>(); |
|---|
| 654 | valueSet.add (value); |
|---|
| 655 | result.put (key, valueSet); |
|---|
| 656 | } |
|---|
| 657 | } |
|---|
| 658 | return result; |
|---|
| 659 | } catch (SQLException e) { throw new IDMapperException ("Xref:" + ref, e); } |
|---|
| 660 | } |
|---|
| 661 | |
|---|
| 662 | |
|---|
| 663 | |
|---|
| 664 | |
|---|
| 665 | |
|---|
| 666 | public boolean isFreeAttributeSearchSupported() |
|---|
| 667 | { |
|---|
| 668 | return true; |
|---|
| 669 | } |
|---|
| 670 | |
|---|
| 671 | |
|---|
| 672 | |
|---|
| 673 | |
|---|
| 674 | |
|---|
| 675 | |
|---|
| 676 | |
|---|
| 677 | |
|---|
| 678 | |
|---|
| 679 | public Map<Xref, String> freeAttributeSearch (String query, String attrType, int limit) throws IDMapperException |
|---|
| 680 | { |
|---|
| 681 | Map<Xref, String> result = new HashMap<Xref, String>(); |
|---|
| 682 | try { |
|---|
| 683 | PreparedStatement pst = (MATCH_ID.equals (attrType)) ? |
|---|
| 684 | pstIdSearchWithAttributes.getPreparedStatement() : pstAttributeSearch.getPreparedStatement(); |
|---|
| 685 | pst.setQueryTimeout(QUERY_TIMEOUT); |
|---|
| 686 | if(limit > NO_LIMIT) pst.setMaxRows(limit); |
|---|
| 687 | pst.setString(1, "%" + query.toLowerCase() + "%"); |
|---|
| 688 | ResultSet r = pst.executeQuery(); |
|---|
| 689 | |
|---|
| 690 | while(r.next()) |
|---|
| 691 | { |
|---|
| 692 | String id = r.getString("id"); |
|---|
| 693 | String code = r.getString("code"); |
|---|
| 694 | String symbol = r.getString("attrValue"); |
|---|
| 695 | result.put(new Xref (id, DataSource.getBySystemCode(code)), symbol); |
|---|
| 696 | } |
|---|
| 697 | } catch (SQLException e) { |
|---|
| 698 | throw new IDMapperException (e); |
|---|
| 699 | } |
|---|
| 700 | return result; |
|---|
| 701 | } |
|---|
| 702 | |
|---|
| 703 | |
|---|
| 704 | public Set<String> getAttributeSet() throws IDMapperException |
|---|
| 705 | { |
|---|
| 706 | Set<String> result = new HashSet<String>(); |
|---|
| 707 | try |
|---|
| 708 | { |
|---|
| 709 | PreparedStatement pst = pstAttributesSet.getPreparedStatement(); |
|---|
| 710 | ResultSet rs = pst.executeQuery(); |
|---|
| 711 | while (rs.next()) |
|---|
| 712 | { |
|---|
| 713 | result.add (rs.getString(1)); |
|---|
| 714 | } |
|---|
| 715 | } |
|---|
| 716 | catch (SQLException ignore) |
|---|
| 717 | { |
|---|
| 718 | throw new IDMapperException(ignore); |
|---|
| 719 | } |
|---|
| 720 | return result; |
|---|
| 721 | } |
|---|
| 722 | } |
|---|