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 public class CassandraReader {
16 private Session session;
17 private static Logger log = Logger.getLogger(CassandraNativeConnector.class);
19 public CassandraReader() {
20 Session inis = CassandraNativeConnector.getSession();
24 public void setSession(Session s) {
30 * getting data from the db
32 public List<Pair<String, String>> ReadProteinDataTable() {
33 final long startTime = System.currentTimeMillis();
34 String com = "SELECT DataBegin,DataEnd FROM ProteinLog;";
35 System.out.println("Command: " + com);
36 ResultSet results = session.execute(com);
37 final long queryTime = System.currentTimeMillis();
38 List<Row> rows = results.all();
39 System.out.println("Query time is " + (queryTime - startTime) + " msec");
41 List<Pair<String, String>> res = new ArrayList<Pair<String, String>>();
44 Pair<String, String> pair = new Pair<String, String>(r.getString("DataBegin"), r.getString("DataEnd"));
48 final long endTime = System.currentTimeMillis();
49 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
54 * getting data from the db
56 public List<Pair<String, String>> ReadProteinData(long day) {
57 final long startTime = System.currentTimeMillis();
58 String com = "SELECT JobID, Protein FROM ProteinData WHERE jobtime = " + day + ";";
59 System.out.println("Command: " + com);
60 ResultSet results = session.execute(com);
61 if (results.isExhausted())
63 final long queryTime = System.currentTimeMillis();
64 List<Row> rows = results.all();
65 System.out.println("Query time is " + (queryTime - startTime) + " msec");
66 List<Pair<String, String>> res = new ArrayList<Pair<String, String>>();
69 Pair<String, String> pair = new Pair<String, String>(r.getString("JobID"), r.getString("Protein"));
73 final long endTime = System.currentTimeMillis();
74 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
78 * getting data from the db JobDateInfo
80 public List<Long> ReadDateTable(long queryDate) {
81 ResultSet results = session.execute("SELECT * FROM JobDateInfo WHERE jobday = " + queryDate + ";");
82 if (results.isExhausted())
84 Row therow = results.one();
85 List<Long> res = new ArrayList<Long>();
86 res.add(therow.getLong("Total"));
87 res.add(therow.getLong("TotalOK"));
88 res.add(therow.getLong("TotalStopped"));
89 res.add(therow.getLong("TotalError"));
90 res.add(therow.getLong("TotalTimeOut"));
91 if (!results.isExhausted()) {
92 Date date = new Date (queryDate);
93 log.warn("CassandraReader.ReadDateTable: date row for " + date.toString () + " ("+ queryDate + ") duplicated ");
99 * getting whole protein sequence from the db ProteinRow
101 public List<AnnotatedProteinSequenceBean> ReadWholeSequence(String queryProtein) {
102 final long startTime = System.currentTimeMillis();
103 String com = "SELECT JobID, Predictions FROM ProteinRow WHERE Protein = '" + queryProtein + "';";
104 System.out.println("Command: " + com);
105 ResultSet results = session.execute(com);
106 if (results.isExhausted())
108 final long queryTime = System.currentTimeMillis();
109 List<Row> rows = results.all();
110 System.out.println("Query time is " + (queryTime - startTime) + " msec");
111 System.out.println(" rows analysed, " + rows.size());
112 List<AnnotatedProteinSequenceBean> res = new ArrayList<AnnotatedProteinSequenceBean>();
115 AnnotatedProteinSequenceBean structure = new AnnotatedProteinSequenceBean(queryProtein, r.getString("JobID"), r.getMap(
116 "Predictions", String.class, String.class));
120 final long endTime = System.currentTimeMillis();
121 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
126 * getting part of protein sequence from the db ProteinRow
128 public List<AnnotatedProteinSequenceBean> ReadPartOfSequence(String queryProtein) {
129 final long startTime = System.currentTimeMillis();
130 String com = "SELECT * FROM ProteinRow;";
131 System.out.println("Command: " + com);
132 ResultSet results = session.execute(com);
133 if (results.isExhausted())
135 final long queryTime = System.currentTimeMillis();
136 List<Row> rows = results.all();
137 System.out.println("Query time is " + (queryTime - startTime) + " msec");
138 System.out.println(" rows analysed, " + rows.size());
139 List<AnnotatedProteinSequenceBean> res = new ArrayList<AnnotatedProteinSequenceBean>();
142 String prot = r.getString("Protein");
143 if (prot.matches("(.*)" + queryProtein + "(.*)")) {
144 AnnotatedProteinSequenceBean structure = new AnnotatedProteinSequenceBean(prot, r.getString("JobID"), r.getMap("Predictions",
145 String.class, String.class));
150 final long endTime = System.currentTimeMillis();
151 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
156 * getting protein sequences by counter
158 public Map<String, Integer> ReadProteinSequenceByCounter() {
159 final long startTime = System.currentTimeMillis();
160 String com = "SELECT Protein, JobID FROM ProteinRow;";
161 System.out.println("Command: " + com);
162 ResultSet results = session.execute(com);
163 if (results.isExhausted())
165 final long queryTime = System.currentTimeMillis();
166 List<Row> rows = results.all();
167 System.out.println("Query time is " + (queryTime - startTime) + " msec");
168 System.out.println(" rows analysed, " + rows.size());
169 Map<String, Integer> res = new HashMap<String, Integer>();
172 String protein = r.getString("Protein");
173 String id = r.getString("JobID");
174 if (res.containsKey(protein))
175 res.put(protein, res.get(protein) + 1);
179 final long endTime = System.currentTimeMillis();
180 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
185 * getting log info for jobid
187 public StructureJobLog ReadJobLog(String jobid) {
188 final long startTime = System.currentTimeMillis();
189 String com = "SELECT * FROM ProteinLog WHERE JobID = '" + jobid + "';";
190 System.out.println("Command: " + com);
191 ResultSet results = session.execute(com);
192 if (results.isExhausted())
194 final long queryTime = System.currentTimeMillis();
195 Row row = results.one();
196 String com1 = "SELECT * FROM ProteinRow WHERE JobID = '" + jobid + "' ALLOW FILTERING;";
197 System.out.println("Command: " + com1);
198 ResultSet results1 = session.execute(com1);
199 if (results1.isExhausted())
201 Row row1 = results1.one();
202 StructureJobLog res = new StructureJobLog(row.getString("Protein"), row.getString("JobID"), row.getString("DataBegin"),
203 row.getString("DataEnd"), row.getString("ip"), row1.getMap("Predictions", String.class, String.class));
204 System.out.println("Query time is " + (queryTime - startTime) + " msec");
205 final long endTime = System.currentTimeMillis();
206 System.out.println(" rows analysed, execution time is " + (endTime - startTime) + " msec");