| 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.ResultSet; |
|---|
| 21 | import java.sql.SQLException; |
|---|
| 22 | import java.util.HashMap; |
|---|
| 23 | import java.util.HashSet; |
|---|
| 24 | import java.util.Map; |
|---|
| 25 | import java.util.Set; |
|---|
| 26 | import java.util.regex.Matcher; |
|---|
| 27 | import java.util.regex.Pattern; |
|---|
| 28 | |
|---|
| 29 | import org.bridgedb.IDMapperException; |
|---|
| 30 | import org.bridgedb.Xref; |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | class SimpleGdbImpl2 extends SimpleGdbImplCommon |
|---|
| 34 | { |
|---|
| 35 | private static final int GDB_COMPAT_VERSION = 2; |
|---|
| 36 | |
|---|
| 37 | private final SimpleGdb.QueryLifeCycle qBackpage = new SimpleGdb.QueryLifeCycle( |
|---|
| 38 | "SELECT backpageText FROM datanode " + |
|---|
| 39 | " WHERE id = ? AND code = ?" |
|---|
| 40 | ); |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | private String getBpInfo(Xref ref) throws IDMapperException |
|---|
| 51 | { |
|---|
| 52 | final QueryLifeCycle pst = qBackpage; |
|---|
| 53 | try { |
|---|
| 54 | pst.init(); |
|---|
| 55 | pst.setString (1, ref.getId()); |
|---|
| 56 | pst.setString (2, ref.getDataSource().getSystemCode()); |
|---|
| 57 | ResultSet r = pst.executeQuery(); |
|---|
| 58 | String result = null; |
|---|
| 59 | if (r.next()) |
|---|
| 60 | { |
|---|
| 61 | result = r.getString(1); |
|---|
| 62 | } |
|---|
| 63 | return result; |
|---|
| 64 | } catch (SQLException e) { throw new IDMapperException (e); } |
|---|
| 65 | finally {pst.cleanup(); } |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | public SimpleGdbImpl2(String dbName, Connection con) throws IDMapperException |
|---|
| 78 | { |
|---|
| 79 | super (con); |
|---|
| 80 | |
|---|
| 81 | if(dbName == null) throw new NullPointerException(); |
|---|
| 82 | this.dbName = dbName; |
|---|
| 83 | |
|---|
| 84 | try |
|---|
| 85 | { |
|---|
| 86 | con.setReadOnly(true); |
|---|
| 87 | } |
|---|
| 88 | catch (SQLException e) |
|---|
| 89 | { |
|---|
| 90 | throw new IDMapperException (e); |
|---|
| 91 | } |
|---|
| 92 | checkSchemaVersion(); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | private void checkSchemaVersion() throws IDMapperException |
|---|
| 100 | { |
|---|
| 101 | int version = 0; |
|---|
| 102 | try |
|---|
| 103 | { |
|---|
| 104 | ResultSet r = con.createStatement().executeQuery("SELECT schemaversion FROM info"); |
|---|
| 105 | if(r.next()) version = r.getInt(1); |
|---|
| 106 | } |
|---|
| 107 | catch (SQLException e) |
|---|
| 108 | { |
|---|
| 109 | |
|---|
| 110 | } |
|---|
| 111 | if(version != GDB_COMPAT_VERSION) |
|---|
| 112 | { |
|---|
| 113 | throw new IDMapperException ("Implementation and schema version mismatch"); |
|---|
| 114 | } |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | private static final Map<String, String> ATTRIBUTES_FROM_BACKPAGE; |
|---|
| 118 | |
|---|
| 119 | static |
|---|
| 120 | { |
|---|
| 121 | ATTRIBUTES_FROM_BACKPAGE = new HashMap<String, String>(); |
|---|
| 122 | ATTRIBUTES_FROM_BACKPAGE.put ("Chromosome", "<TH>Chr:<TH>([^<]*)<"); |
|---|
| 123 | ATTRIBUTES_FROM_BACKPAGE.put ("Description", "<TH>Description:<TH>([^<]*)<"); |
|---|
| 124 | ATTRIBUTES_FROM_BACKPAGE.put ("Synonyms", "<TH>Synonyms:<TH>([^<]*)<"); |
|---|
| 125 | ATTRIBUTES_FROM_BACKPAGE.put ("Symbol", "<TH>(?:Gene Symbol|Metabolite):<TH>([^<]*)<"); |
|---|
| 126 | ATTRIBUTES_FROM_BACKPAGE.put ("BrutoFormula", "<TH>Bruto Formula:<TH>([^<]*)<"); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | public Set<String> getAttributes(Xref ref, String attrname) |
|---|
| 131 | throws IDMapperException |
|---|
| 132 | { |
|---|
| 133 | Set<String> result = new HashSet<String>(); |
|---|
| 134 | final QueryLifeCycle pst = qAttribute; |
|---|
| 135 | |
|---|
| 136 | if (ATTRIBUTES_FROM_BACKPAGE.containsKey(attrname)) |
|---|
| 137 | { |
|---|
| 138 | String bpInfo = getBpInfo(ref); |
|---|
| 139 | if (bpInfo != null) |
|---|
| 140 | { |
|---|
| 141 | Pattern pat = Pattern.compile(ATTRIBUTES_FROM_BACKPAGE.get (attrname)); |
|---|
| 142 | Matcher matcher = pat.matcher(bpInfo); |
|---|
| 143 | if (matcher.find()) |
|---|
| 144 | { |
|---|
| 145 | result.add (matcher.group(1)); |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | try { |
|---|
| 151 | pst.init(); |
|---|
| 152 | pst.setString (1, ref.getId()); |
|---|
| 153 | pst.setString (2, ref.getDataSource().getSystemCode()); |
|---|
| 154 | pst.setString (3, attrname); |
|---|
| 155 | ResultSet r = pst.executeQuery(); |
|---|
| 156 | if (r.next()) |
|---|
| 157 | { |
|---|
| 158 | result.add (r.getString(1)); |
|---|
| 159 | } |
|---|
| 160 | return result; |
|---|
| 161 | } catch (SQLException e) { throw new IDMapperException ("Xref:" + ref + ", Attribute: " + attrname, e); } |
|---|
| 162 | finally {pst.cleanup(); } |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | public Map<String, Set<String>> getAttributes(Xref ref) |
|---|
| 167 | throws IDMapperException |
|---|
| 168 | { |
|---|
| 169 | Map<String, Set<String>> result = new HashMap<String, Set<String>>(); |
|---|
| 170 | final QueryLifeCycle pst = qAllAttributes; |
|---|
| 171 | |
|---|
| 172 | String bpInfo = getBpInfo(ref); |
|---|
| 173 | if (bpInfo != null) |
|---|
| 174 | { |
|---|
| 175 | for (String attrname : ATTRIBUTES_FROM_BACKPAGE.keySet()) |
|---|
| 176 | { |
|---|
| 177 | Pattern pat = Pattern.compile(ATTRIBUTES_FROM_BACKPAGE.get (attrname)); |
|---|
| 178 | Matcher matcher = pat.matcher(bpInfo); |
|---|
| 179 | if (matcher.find()) |
|---|
| 180 | { |
|---|
| 181 | Set<String> attrSet = new HashSet<String>(); |
|---|
| 182 | attrSet.add (matcher.group(1)); |
|---|
| 183 | result.put (attrname, attrSet); |
|---|
| 184 | } |
|---|
| 185 | } |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | try { |
|---|
| 189 | pst.init(); |
|---|
| 190 | pst.setString (1, ref.getId()); |
|---|
| 191 | pst.setString (2, ref.getDataSource().getSystemCode()); |
|---|
| 192 | ResultSet r = pst.executeQuery(); |
|---|
| 193 | if (r.next()) |
|---|
| 194 | { |
|---|
| 195 | String key = r.getString(1); |
|---|
| 196 | String value = r.getString(2); |
|---|
| 197 | if (result.containsKey (key)) |
|---|
| 198 | { |
|---|
| 199 | result.get(key).add (value); |
|---|
| 200 | } |
|---|
| 201 | else |
|---|
| 202 | { |
|---|
| 203 | Set<String> valueSet = new HashSet<String>(); |
|---|
| 204 | valueSet.add (value); |
|---|
| 205 | result.put (key, valueSet); |
|---|
| 206 | } |
|---|
| 207 | } |
|---|
| 208 | return result; |
|---|
| 209 | } catch (SQLException e) { throw new IDMapperException ("Xref:" + ref, e); } |
|---|
| 210 | finally {pst.cleanup(); } |
|---|
| 211 | } |
|---|
| 212 | } |
|---|