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.engine.JobStatus;
17 public class CassandraReader {
18 private Session session;
19 private static Logger log = Logger.getLogger(CassandraNativeConnector.class);
21 public CassandraReader() {
22 Session inis = CassandraNativeConnector.getSession();
26 public void setSession(Session s) {
32 * getting data from the db
34 public List<Pair<String, String>> ReadProteinDataTable() {
35 final long startTime = System.currentTimeMillis();
36 String com = "SELECT DataBegin,DataEnd FROM ProteinLog;";
37 System.out.println("Command: " + com);
38 ResultSet results = session.execute(com);
39 final long queryTime = System.currentTimeMillis();
40 List<Row> rows = results.all();
41 System.out.println("Query time is " + (queryTime - startTime) + " msec");
43 List<Pair<String, String>> res = new ArrayList<Pair<String, String>>();
46 Pair<String, String> pair = new Pair<String, String>(r.getString("DataBegin"), r.getString("DataEnd"));
50 final long endTime = System.currentTimeMillis();
51 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
56 * getting data from the db
58 public DateBean ReadProteinData(long day, String date) {
59 String com = "SELECT JobID, Protein FROM ProteinData WHERE jobtime = " + day + ";";
60 System.out.println("Command: " + com);
61 ResultSet results = session.execute(com);
62 if (results.isExhausted())
64 List<Row> rows = results.all();
65 DateBean res = new DateBean(date);
67 res.setJobidAndSeq(r.getString("JobID"), r.getString("Protein"));
73 * getting data from the db
75 public DateBean ReadFailedJobs(long day, String date, JobStatus status) {
76 // FailLog (jobtime, JobID, ExecTime, ip, FinalStatus)
77 String com = "SELECT JobID FROM FailLog WHERE jobtime = " + day + " and FinalStatus = '" + status.name() + "';";
78 ResultSet results = session.execute(com);
79 if (results.isExhausted())
81 List<Row> rows = results.all();
82 DateBean res = new DateBean(date);
84 String jobid = r.getString("JobID");
85 String com1 = "SELECT Protein FROM ProteinLog WHERE JobID = '" + jobid + "';";
86 System.out.println("Command: " + com1);
87 ResultSet results2 = session.execute(com1);
88 List<Row> jrows = results2.all();
89 if (1 == jrows.size()) {
90 String protein = jrows.get(0).getString("Protein");
91 res.setJobidAndSeq(jobid, protein);
98 * getting data from the db JobDateInfo
100 public Total ReadDateTable(long queryDate) {
101 ResultSet results = session.execute("SELECT * FROM JobDateInfo WHERE jobday = " + queryDate + ";");
102 if (results.isExhausted())
104 Row therow = results.one();
105 Total res = new Total(therow.getLong("Total"), therow.getLong("TotalOK"), therow.getLong("TotalStopped"),
106 therow.getLong("TotalError"), therow.getLong("TotalTimeOut"));
107 if (!results.isExhausted()) {
108 Date date = new Date(queryDate);
109 log.warn("CassandraReader.ReadDateTable: date row for " + date.toString() + " (" + queryDate + ") duplicated ");
115 * getting whole protein sequence from the db ProteinRow
117 public List<ProteinBean> ReadWholeSequence(String queryProtein) {
118 final long startTime = System.currentTimeMillis();
119 String com = "SELECT JobID, Predictions FROM ProteinRow WHERE Protein = '" + queryProtein + "';";
120 System.out.println("Command: " + com);
121 ResultSet results = session.execute(com);
122 if (results.isExhausted())
124 final long queryTime = System.currentTimeMillis();
125 List<Row> rows = results.all();
126 System.out.println("first size : " + rows.size());
127 System.out.println("Query time is " + (queryTime - startTime) + " msec");
128 System.out.println(" rows analysed, " + rows.size());
129 List<ProteinBean> res = new ArrayList<ProteinBean>();
130 ProteinBean structure = new ProteinBean(queryProtein, rows.get(0).getMap("Predictions", String.class, String.class));
131 System.out.println("second size : " + rows.size());
134 structure.setJobid(r.getString("JobID"));
138 final long endTime = System.currentTimeMillis();
139 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
146 public Map<String, String[]> ReadIpWithJobs(String ip) {
147 final long startTime = System.currentTimeMillis();
148 String com = "SELECT JobID, Protein, FinalStatus, DataBegin FROM ProteinLog WHERE ip = '" + ip + "';";
149 System.out.println("Command: " + com);
150 ResultSet results = session.execute(com);
151 if (results.isExhausted())
153 final long queryTime = System.currentTimeMillis();
154 List<Row> rows = results.all();
155 Map<String, String[]> res = new HashMap<String, String[]>();
156 System.out.println("Query time is " + (queryTime - startTime) + " msec");
157 System.out.println(" rows analysed, " + rows.size());
160 if (r.getString("FinalStatus").equals("OK")) {
161 String date = r.getString("DataBegin");
162 res.put(r.getString("JobID"), new String[] { date.substring(0, date.indexOf(":")), r.getString("Protein") });
166 final long endTime = System.currentTimeMillis();
167 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
172 * getting part of protein sequence from the db ProteinRow
174 public List<ProteinBean> ReadPartOfSequence(String queryProtein) {
175 final long startTime = System.currentTimeMillis();
176 String com = "SELECT * FROM ProteinRow;";
177 System.out.println("Command: " + com);
178 ResultSet results = session.execute(com);
179 if (results.isExhausted())
181 final long queryTime = System.currentTimeMillis();
182 List<Row> rows = results.all();
183 System.out.println("Query time is " + (queryTime - startTime) + " msec");
184 System.out.println(" rows analysed, " + rows.size());
185 List<ProteinBean> res = new ArrayList<ProteinBean>();
188 String prot = r.getString("Protein");
189 if (prot.matches("(.*)" + queryProtein + "(.*)")) {
190 ProteinBean structure = new ProteinBean(prot, r.getMap("Predictions", String.class, String.class));
191 structure.setJobid(r.getString("JobID"));
196 final long endTime = System.currentTimeMillis();
197 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
202 * getting protein sequence from the db ProteinRow
204 public Map<String, String> ReadProtein() {
205 final long startTime = System.currentTimeMillis();
206 String com = "SELECT * FROM ProteinRow;";
207 System.out.println("Command: " + com);
208 ResultSet results = session.execute(com);
209 if (results.isExhausted())
211 final long queryTime = System.currentTimeMillis();
212 List<Row> rows = results.all();
213 System.out.println("Query time is " + (queryTime - startTime) + " msec");
214 System.out.println(" rows analysed, " + rows.size());
215 Map<String, String> res = new HashMap<String, String>();
218 String prot = r.getString("Protein");
219 String prediction = findJnetpred(r.getMap("Predictions", String.class, String.class));
220 if (prot != null || prediction != null)
221 res.put(prot, prediction);
223 final long endTime = System.currentTimeMillis();
224 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
228 private String findJnetpred (Map<String,String> pred) {
230 if (pred.containsKey("jnetpred"))
231 return pred.get("jnetpred");
236 * getting protein sequences by counter
238 public Map<String, Integer> ReadProteinSequenceByCounter() {
239 final long startTime = System.currentTimeMillis();
240 String com = "SELECT Protein, JobID FROM ProteinRow;";
241 System.out.println("Command: " + com);
242 ResultSet results = session.execute(com);
243 if (results.isExhausted())
245 final long queryTime = System.currentTimeMillis();
246 List<Row> rows = results.all();
247 System.out.println("Query time is " + (queryTime - startTime) + " msec");
248 System.out.println(" rows analysed, " + rows.size());
249 Map<String, Integer> res = new HashMap<String, Integer>();
252 String protein = r.getString("Protein");
253 String id = r.getString("JobID");
254 if (res.containsKey(protein))
255 res.put(protein, res.get(protein) + 1);
259 final long endTime = System.currentTimeMillis();
260 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
265 * getting ip by counter
267 public Map<String, Integer> ReadIpByCounter() {
268 final long startTime = System.currentTimeMillis();
269 String com = "SELECT JobID, ip FROM ProteinLog;";
270 System.out.println("Command: " + com);
271 ResultSet results = session.execute(com);
272 if (results.isExhausted())
274 final long queryTime = System.currentTimeMillis();
275 List<Row> rows = results.all();
276 System.out.println("Query time is " + (queryTime - startTime) + " msec");
277 System.out.println(" rows analysed, " + rows.size());
278 Map<String, Integer> res = new HashMap<String, Integer>();
281 String ip = r.getString("ip");
282 String id = r.getString("JobID");
283 if (res.containsKey(ip))
284 res.put(ip, res.get(ip) + 1);
288 final long endTime = System.currentTimeMillis();
289 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
294 * getting log info for jobid
296 public JobBean ReadJobLog(String jobid) {
297 final long startTime = System.currentTimeMillis();
298 String com = "SELECT * FROM ProteinLog WHERE JobID = '" + jobid + "';";
299 System.out.println("Command: " + com);
300 ResultSet results = session.execute(com);
301 if (results.isExhausted())
303 final long queryTime = System.currentTimeMillis();
304 Row row = results.one();
305 String com1 = "SELECT * FROM ProteinRow WHERE JobID = '" + jobid + "' ALLOW FILTERING;";
306 System.out.println("Command: " + com1);
307 ResultSet results1 = session.execute(com1);
308 if (results1.isExhausted())
310 Row row1 = results1.one();
311 JobBean res = new JobBean(row.getString("Protein"), row.getString("JobID"), row.getString("DataBegin"), row.getString("DataEnd"),
312 row.getString("ip"), row1.getMap("Predictions", String.class, String.class));
313 System.out.println("Query time is " + (queryTime - startTime) + " msec");
314 final long endTime = System.currentTimeMillis();
315 System.out.println(" rows analysed, execution time is " + (endTime - startTime) + " msec");