1 package compbio.cassandra;
4 import java.util.HashMap;
6 import java.util.ArrayList;
9 import org.apache.log4j.Logger;
11 import com.datastax.driver.core.Row;
12 import com.datastax.driver.core.Session;
13 import com.datastax.driver.core.ResultSet;
15 import compbio.beans.DateBean;
16 import compbio.beans.JobBean;
17 import compbio.beans.ProteinBean;
18 import compbio.beans.Total;
19 import compbio.engine.JobStatus;
20 import compbio.engine.Pair;
22 public class CassandraReaderOld {
23 private Session session;
24 private static Logger log = Logger.getLogger(CassandraNativeConnector.class);
26 public CassandraReaderOld() {
27 Session inis = CassandraNativeConnector.getSession();
31 public void setSession(Session s) {
37 * getting data from the db
39 public List<Pair<String, String>> ReadProteinDataTable() {
40 final long startTime = System.currentTimeMillis();
41 String com = "SELECT DataBegin,DataEnd FROM ProteinLog;";
42 System.out.println("Command: " + com);
43 ResultSet results = session.execute(com);
44 final long queryTime = System.currentTimeMillis();
45 List<Row> rows = results.all();
46 System.out.println("Query time is " + (queryTime - startTime) + " msec");
48 List<Pair<String, String>> res = new ArrayList<Pair<String, String>>();
51 Pair<String, String> pair = new Pair<String, String>(r.getString("DataBegin"), r.getString("DataEnd"));
55 final long endTime = System.currentTimeMillis();
56 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
61 * getting data from the db
63 public DateBean ReadProteinData(long day, String date) {
64 String com = "SELECT JobID, Protein FROM ProteinData WHERE jobtime = " + day + ";";
65 System.out.println("Command: " + com);
66 ResultSet results = session.execute(com);
67 if (results.isExhausted())
69 List<Row> rows = results.all();
70 DateBean res = new DateBean(date);
72 res.setJobidAndSeq(r.getString("JobID"), r.getString("Protein"));
78 * getting data from the db
80 public DateBean ReadFailedJobs(long day, String date, JobStatus status) {
81 // FailLog (jobtime, JobID, ExecTime, ip, FinalStatus)
82 String com = "SELECT JobID FROM FailLog WHERE jobtime = " + day + " and FinalStatus = '" + status.name() + "';";
83 ResultSet results = session.execute(com);
84 if (results.isExhausted())
86 List<Row> rows = results.all();
87 DateBean res = new DateBean(date);
89 String jobid = r.getString("JobID");
90 String com1 = "SELECT Protein FROM ProteinLog WHERE JobID = '" + jobid + "';";
91 System.out.println("Command: " + com1);
92 ResultSet results2 = session.execute(com1);
93 List<Row> jrows = results2.all();
94 if (1 == jrows.size()) {
95 String protein = jrows.get(0).getString("Protein");
96 res.setJobidAndSeq(jobid, protein);
103 * getting data from the db JobDateInfo
105 public Total ReadDateTable(long queryDate) {
106 ResultSet results = session.execute("SELECT * FROM JobDateInfo WHERE jobday = " + queryDate + ";");
107 if (results.isExhausted())
109 Row therow = results.one();
110 Total res = new Total(therow.getLong("Total"), therow.getLong("TotalOK"), therow.getLong("TotalStopped"),
111 therow.getLong("TotalError"), therow.getLong("TotalTimeOut"));
112 if (!results.isExhausted()) {
113 Date date = new Date(queryDate);
114 log.warn("CassandraReader.ReadDateTable: date row for " + date.toString() + " (" + queryDate + ") duplicated ");
120 * getting whole protein sequence from the db ProteinRow
122 public List<ProteinBean> ReadWholeSequence(String queryProtein) {
123 final long startTime = System.currentTimeMillis();
124 String com = "SELECT JobID, Predictions FROM ProteinRow WHERE Protein = '" + queryProtein + "';";
125 System.out.println("Command: " + com);
126 ResultSet results = session.execute(com);
127 if (results.isExhausted())
129 final long queryTime = System.currentTimeMillis();
130 List<Row> rows = results.all();
131 System.out.println("first size : " + rows.size());
132 System.out.println("Query time is " + (queryTime - startTime) + " msec");
133 System.out.println(" rows analysed, " + rows.size());
134 List<ProteinBean> res = new ArrayList<ProteinBean>();
135 ProteinBean structure = new ProteinBean(queryProtein, rows.get(0).getMap("Predictions", String.class, String.class));
136 System.out.println("second size : " + rows.size());
139 structure.setJobid(r.getString("JobID"));
143 final long endTime = System.currentTimeMillis();
144 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
151 public Map<String, String[]> ReadIpWithJobs(String ip) {
152 final long startTime = System.currentTimeMillis();
153 String com = "SELECT JobID, Protein, FinalStatus, DataBegin FROM ProteinLog WHERE ip = '" + ip + "';";
154 System.out.println("Command: " + com);
155 ResultSet results = session.execute(com);
156 if (results.isExhausted())
158 final long queryTime = System.currentTimeMillis();
159 List<Row> rows = results.all();
160 Map<String, String[]> res = new HashMap<String, String[]>();
161 System.out.println("Query time is " + (queryTime - startTime) + " msec");
162 System.out.println(" rows analysed, " + rows.size());
165 if (r.getString("FinalStatus").equals("OK")) {
166 String date = r.getString("DataBegin");
167 res.put(r.getString("JobID"), new String[] { date.substring(0, date.indexOf(":")), r.getString("Protein") });
171 final long endTime = System.currentTimeMillis();
172 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
177 * getting part of protein sequence from the db ProteinRow
179 public List<ProteinBean> ReadPartOfSequence(String queryProtein) {
180 final long startTime = System.currentTimeMillis();
181 String com = "SELECT * FROM ProteinRow;";
182 System.out.println("Command: " + com);
183 ResultSet results = session.execute(com);
184 if (results.isExhausted())
186 final long queryTime = System.currentTimeMillis();
187 List<Row> rows = results.all();
188 System.out.println("Query time is " + (queryTime - startTime) + " msec");
189 System.out.println(" rows analysed, " + rows.size());
190 List<ProteinBean> res = new ArrayList<ProteinBean>();
193 String prot = r.getString("Protein");
194 if (prot.matches("(.*)" + queryProtein + "(.*)")) {
195 ProteinBean structure = new ProteinBean(prot, r.getMap("Predictions", String.class, String.class));
196 structure.setJobid(r.getString("JobID"));
201 final long endTime = System.currentTimeMillis();
202 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
207 * getting protein sequence from the db ProteinRow
209 public Map<String, String> ReadProtein() {
210 final long startTime = System.currentTimeMillis();
211 String com = "SELECT * FROM ProteinRow;";
212 System.out.println("Command: " + com);
213 ResultSet results = session.execute(com);
214 if (results.isExhausted())
216 final long queryTime = System.currentTimeMillis();
217 List<Row> rows = results.all();
218 System.out.println("Query time is " + (queryTime - startTime) + " msec");
219 System.out.println(" rows analysed, " + rows.size());
220 Map<String, String> res = new HashMap<String, String>();
223 String prot = r.getString("Protein");
224 String prediction = findJnetpred(r.getMap("Predictions", String.class, String.class));
225 if (prot != null || prediction != null)
226 res.put(prot, prediction);
228 final long endTime = System.currentTimeMillis();
229 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
233 private String findJnetpred(Map<String, String> pred) {
235 if (pred.containsKey("jnetpred"))
236 return pred.get("jnetpred");
242 * getting protein sequences by counter
244 public Map<String, Integer> ReadProteinSequenceByCounter() {
245 final long startTime = System.currentTimeMillis();
246 String com = "SELECT Protein, JobID FROM ProteinRow;";
247 System.out.println("Command: " + com);
248 ResultSet results = session.execute(com);
249 if (results.isExhausted())
251 final long queryTime = System.currentTimeMillis();
252 List<Row> rows = results.all();
253 System.out.println("Query time is " + (queryTime - startTime) + " msec");
254 System.out.println(" rows analysed, " + rows.size());
255 Map<String, Integer> res = new HashMap<String, Integer>();
258 String protein = r.getString("Protein");
259 if (res.containsKey(protein))
260 res.put(protein, res.get(protein) + 1);
264 final long endTime = System.currentTimeMillis();
265 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
270 * getting ip by counter
272 public Map<String, Integer> ReadIpByCounter() {
273 final long startTime = System.currentTimeMillis();
274 String com = "SELECT JobID, ip FROM ProteinLog;";
275 System.out.println("Command: " + com);
276 ResultSet results = session.execute(com);
277 if (results.isExhausted())
279 final long queryTime = System.currentTimeMillis();
280 List<Row> rows = results.all();
281 System.out.println("Query time is " + (queryTime - startTime) + " msec");
282 System.out.println(" rows analysed, " + rows.size());
283 Map<String, Integer> res = new HashMap<String, Integer>();
286 String ip = r.getString("ip");
287 if (res.containsKey(ip))
288 res.put(ip, res.get(ip) + 1);
292 final long endTime = System.currentTimeMillis();
293 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
298 * getting log info for jobid
300 public JobBean ReadJobLog(String jobid) {
301 final long startTime = System.currentTimeMillis();
302 String com = "SELECT * FROM ProteinLog WHERE JobID = '" + jobid + "';";
303 System.out.println("Command: " + com);
304 ResultSet results = session.execute(com);
305 if (results.isExhausted())
307 final long queryTime = System.currentTimeMillis();
308 Row row = results.one();
309 String com1 = "SELECT * FROM ProteinRow WHERE JobID = '" + jobid + "' ALLOW FILTERING;";
310 System.out.println("Command: " + com1);
311 ResultSet results1 = session.execute(com1);
312 if (results1.isExhausted())
314 Row row1 = results1.one();
315 JobBean res = new JobBean(row.getString("Protein"), row.getString("JobID"), row.getString("DataBegin"), row.getString("DataEnd"),
316 row.getString("ip"), row1.getMap("Predictions", String.class, String.class));
317 String program = row.getString("ProgramName");
318 String version = row.getString("ProgramVersion");
319 if (null != program && null != version) {
320 res.setProgramName(program);
321 res.setProgramVersion(version);
323 System.out.println("Query time is " + (queryTime - startTime) + " msec");
324 final long endTime = System.currentTimeMillis();
325 System.out.println(" rows analysed, execution time is " + (endTime - startTime) + " msec");