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 Map<String, String> output = new HashMap<String, String>();
220 String protein = r.getString("Protein");
221 String prediction = findJnetpred(r.getMap("Predictions", String.class, String.class));
222 if (protein != null && prediction != null) {
223 output.put(protein, prediction);
226 //final long endTime = System.currentTimeMillis();
230 private String findJnetpred(Map<String, String> pred) {
232 if (pred.containsKey("jnetpred"))
233 return pred.get("jnetpred");
239 * getting protein sequences by counter
241 public Map<String, Integer> ReadProteinSequenceByCounter() {
242 final long startTime = System.currentTimeMillis();
243 String com = "SELECT Protein, JobID FROM ProteinRow;";
244 System.out.println("Command: " + com);
245 ResultSet results = session.execute(com);
246 if (results.isExhausted())
248 final long queryTime = System.currentTimeMillis();
249 List<Row> rows = results.all();
250 System.out.println("Query time is " + (queryTime - startTime) + " msec");
251 System.out.println(" rows analysed, " + rows.size());
252 Map<String, Integer> res = new HashMap<String, Integer>();
255 String protein = r.getString("Protein");
256 if (res.containsKey(protein))
257 res.put(protein, res.get(protein) + 1);
261 final long endTime = System.currentTimeMillis();
262 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
267 * getting ip by counter
269 public Map<String, Integer> ReadIpByCounter() {
270 final long startTime = System.currentTimeMillis();
271 String com = "SELECT JobID, ip FROM ProteinLog;";
272 System.out.println("Command: " + com);
273 ResultSet results = session.execute(com);
274 if (results.isExhausted())
276 final long queryTime = System.currentTimeMillis();
277 List<Row> rows = results.all();
278 System.out.println("Query time is " + (queryTime - startTime) + " msec");
279 System.out.println(" rows analysed, " + rows.size());
280 Map<String, Integer> res = new HashMap<String, Integer>();
283 String ip = r.getString("ip");
284 if (res.containsKey(ip))
285 res.put(ip, res.get(ip) + 1);
289 final long endTime = System.currentTimeMillis();
290 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
295 * getting log info for jobid
297 public JobBean ReadJobLog(String jobid) {
298 final long startTime = System.currentTimeMillis();
299 String com = "SELECT * FROM ProteinLog WHERE JobID = '" + jobid + "';";
300 System.out.println("Command: " + com);
301 ResultSet results = session.execute(com);
302 if (results.isExhausted())
304 final long queryTime = System.currentTimeMillis();
305 Row row = results.one();
306 String com1 = "SELECT * FROM ProteinRow WHERE JobID = '" + jobid + "' ALLOW FILTERING;";
307 System.out.println("Command: " + com1);
308 ResultSet results1 = session.execute(com1);
309 if (results1.isExhausted())
311 Row row1 = results1.one();
312 JobBean res = new JobBean(row.getString("Protein"), row.getString("JobID"), row.getString("DataBegin"), row.getString("DataEnd"),
313 row.getString("ip"), row1.getMap("Predictions", String.class, String.class));
314 String program = row.getString("ProgramName");
315 String version = row.getString("ProgramVersion");
316 if (null != program && null != version) {
317 res.setProgramName(program);
318 res.setProgramVersion(version);
320 System.out.println("Query time is " + (queryTime - startTime) + " msec");
321 final long endTime = System.currentTimeMillis();
322 System.out.println(" rows analysed, execution time is " + (endTime - startTime) + " msec");