root/trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/SimpleGdbImpl2.java @ 317

Revision 317, 6.2 KB (checked in by martijn, 6 months ago)

Refactoring dead code:
Removed all methods pertaining to constructing Derby databases
to a separate package (org.bridgedb.rdb.construct). Also
factored out common code between SimpleGdbImpl?2/3 into
SimpleGdbImplCommon?

  • Property svn:eol-style set to native
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.rdb;
18
19import java.sql.Connection;
20import java.sql.ResultSet;
21import java.sql.SQLException;
22import java.util.HashMap;
23import java.util.HashSet;
24import java.util.Map;
25import java.util.Set;
26import java.util.regex.Matcher;
27import java.util.regex.Pattern;
28
29import org.bridgedb.IDMapperException;
30import org.bridgedb.Xref;
31
32/** {@inheritDoc} */
33class SimpleGdbImpl2 extends SimpleGdbImplCommon
34{               
35        private static final int GDB_COMPAT_VERSION = 2; //Preferred schema version
36       
37        private final SimpleGdb.QueryLifeCycle qBackpage = new SimpleGdb.QueryLifeCycle(
38                        "SELECT backpageText FROM datanode " +
39                        " WHERE id = ? AND code = ?"
40                );
41
42        /**
43         * get Backpage info. In Schema v2, this was not stored in
44         * the attribute table but as a separate column, so this is treated
45         * as a special case. This method is called by <pre>getAttribute (ref, "Backpage")</pre>
46         * @param ref the entity to get backpage info for.
47         * @return Backpage info as string
48         * @throws IDMapperException when database is unavailable
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); } //Gene not found
65                finally {pst.cleanup(); }
66        }
67
68        /**
69         * Opens a connection to the Gene Database located in the given file.
70         * A new instance of this class is created automatically.
71         * @param dbName The file containing the Gene Database.
72         * @param con An existing SQL Connector.
73         * @param props PROP_RECREATE if you want to create a new database (possibly overwriting an existing one)
74         *      or PROP_NONE if you want to connect read-only
75         * @throws IDMapperException when the database could not be created or connected to
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         * look at the info table of the current database to determine the schema version.
97         * @throws IDMapperException when looking up the schema version failed
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                        //Ignore, older db's don't even have schema version
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        /** {@inheritDoc} */
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); } // Database unavailable
162                finally {pst.cleanup(); }
163        }
164
165        /** {@inheritDoc} */
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); } // Database unavailable
210                finally {pst.cleanup(); }
211        }
212}
Note: See TracBrowser for help on using the browser.