1 package uk.ac.ebi.intenz.stats.db;
2
3 import java.io.IOException;
4 import java.sql.Connection;
5 import java.sql.PreparedStatement;
6 import java.sql.ResultSet;
7 import java.sql.SQLException;
8 import java.sql.Statement;
9 import java.util.Date;
10 import java.util.HashMap;
11 import java.util.LinkedHashMap;
12 import java.util.Map;
13
14 import uk.ac.ebi.biobabel.util.db.SQLLoader;
15 import uk.ac.ebi.intenz.domain.constants.XrefDatabaseConstant;
16 import uk.ac.ebi.intenz.domain.exceptions.DomainException;
17 import uk.ac.ebi.intenz.stats.IIntEnzStatistics;
18
19
20
21
22
23
24 public class IntEnzDbStatistics implements IIntEnzStatistics {
25
26 private int relNo;
27 private Date relDate;
28
29 private int classes;
30 private int subclasses;
31 private int subSubclasses;
32 private Map<String, Map<Boolean, Integer>> enzymesByStatus;
33 private int synonyms;
34 private Map<XrefDatabaseConstant, XrefsStats> xrefs;
35 private Map<XrefDatabaseConstant, XrefsStats> links;
36
37 private SQLLoader sqlLoader;
38 private Statement synonymsStm;
39
40 public int getReleaseNumber() {
41 return relNo;
42 }
43
44 public Date getReleaseDate() {
45 return relDate;
46 }
47
48 public int getClasses() {
49 return classes;
50 }
51
52 public int getSubclasses() {
53 return subclasses;
54 }
55
56 public int getSubSubclasses() {
57 return subSubclasses;
58 }
59
60 public Map<String, Map<Boolean, Integer>> getEnzymesByStatus() {
61 return enzymesByStatus;
62 }
63
64 public int getSynonyms() {
65 return synonyms;
66 }
67
68 public Map<XrefDatabaseConstant, XrefsStats> getXrefs() {
69 return xrefs;
70 }
71
72 public Map<XrefDatabaseConstant, XrefsStats> getLinks() {
73 return links;
74 }
75
76 public IntEnzDbStatistics(Connection con)
77 throws IOException, SQLException, DomainException{
78 this.setConnection(con);
79 updateStatistics();
80 }
81
82 public void setConnection(Connection con) throws IOException, SQLException {
83 if (sqlLoader != null) sqlLoader.close();
84 sqlLoader = new SQLLoader(this.getClass(), con);
85 synonymsStm = con.createStatement();
86 }
87
88 public void updateStatistics() throws SQLException, DomainException {
89 PreparedStatement stm = null;
90 ResultSet rs = null;
91
92 stm = sqlLoader.getPreparedStatement("--release");
93 rs = stm.executeQuery();
94 if (rs.next()){
95 relNo = rs.getInt("rel_no");
96 relDate = rs.getDate("rel_date");
97 }
98
99 stm = sqlLoader.getPreparedStatement("--classes");
100 rs = stm.executeQuery();
101 if (rs.next()) classes = rs.getInt("c");
102
103 stm = sqlLoader.getPreparedStatement("--subclasses");
104 rs = stm.executeQuery();
105 if (rs.next()) subclasses = rs.getInt("c");
106
107 stm = sqlLoader.getPreparedStatement("--subsubclasses");
108 rs = stm.executeQuery();
109 if (rs.next()) subSubclasses = rs.getInt("c");
110
111 stm = sqlLoader.getPreparedStatement("--enzymes.by.status", "--constraint.non.transient");
112 rs = stm.executeQuery();
113 enzymesByStatus = new LinkedHashMap<String, Map<Boolean, Integer>>();
114 while (rs.next()){
115 String status = rs.getString("status");
116 boolean active = "Y".equals(rs.getString("active"));
117 int enzymes = rs.getInt("c");
118 if (!enzymesByStatus.containsKey(status)){
119 HashMap<Boolean, Integer> activeMap = new LinkedHashMap<Boolean, Integer>();
120 enzymesByStatus.put(status, activeMap);
121 }
122 enzymesByStatus.get(status).put(active, enzymes);
123 }
124
125
126
127 rs = synonymsStm.executeQuery("select count(distinct n.name) c from names n, enzymes e where n.status = 'OK' and n.name_class = 'OTH' and n.enzyme_id = e.enzyme_id and e.enzyme_id not in (select before_id from history_events where event_class = 'MOD')");
128 if (rs.next()) synonyms = rs.getInt("c");
129 synonymsStm.close();
130
131 stm = sqlLoader.getPreparedStatement("--xrefs");
132 rs = stm.executeQuery();
133 xrefs = new LinkedHashMap<XrefDatabaseConstant, XrefsStats>();
134 while (rs.next()){
135 XrefDatabaseConstant db = getDb(rs.getString("dbname"));
136 XrefsStats xrefsStats = new XrefsStats(rs.getInt("c"), rs.getInt("cd"));
137 xrefs.put(db, xrefsStats);
138 }
139
140 stm = sqlLoader.getPreparedStatement("--links");
141 rs = stm.executeQuery();
142 links = new LinkedHashMap<XrefDatabaseConstant, XrefsStats>();
143 while (rs.next()){
144 XrefDatabaseConstant db = getDb(rs.getString("dbname"));
145 XrefsStats xrefsStats = new XrefsStats(rs.getInt("c"), rs.getInt("cd"));
146 links.put(db, xrefsStats);
147 }
148
149 sqlLoader.close();
150 }
151
152 private XrefDatabaseConstant getDb(String dbName) throws DomainException{
153 XrefDatabaseConstant db = null;
154 try {
155 db = XrefDatabaseConstant.valueOf(dbName);
156 } catch (DomainException e) {
157 db = XrefDatabaseConstant.valueOf(XrefDatabaseConstant.getDatabaseCodeOf(dbName));
158 }
159 return db;
160 }
161 }