new readers for the queries
[proteocache.git] / datadb / compbio / cassandra / CassandraReaderOld.java
1 package compbio.cassandra;
2
3 import java.util.Date;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.ArrayList;
7 import java.util.Map;
8
9 import org.apache.log4j.Logger;
10
11 import com.datastax.driver.core.Row;
12 import com.datastax.driver.core.Session;
13 import com.datastax.driver.core.ResultSet;
14
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
21 public class CassandraReaderOld {
22         private Session session;
23         private static Logger log = Logger.getLogger(CassandraNativeConnector.class);
24
25         public CassandraReaderOld() {
26                 Session inis = CassandraNativeConnector.getSession();
27                 setSession(inis);
28         }
29
30         public void setSession(Session s) {
31                 assert s != null;
32                 session = s;
33         }
34
35         /*
36          * getting data from the db
37          */
38         public List<Pair<String, String>> ReadProteinDataTable() {
39                 final long startTime = System.currentTimeMillis();
40                 String com = "SELECT DataBegin,DataEnd FROM ProteinLog;";
41                 System.out.println("Command: " + com);
42                 ResultSet results = session.execute(com);
43                 final long queryTime = System.currentTimeMillis();
44                 List<Row> rows = results.all();
45                 System.out.println("Query time is " + (queryTime - startTime) + " msec");
46
47                 List<Pair<String, String>> res = new ArrayList<Pair<String, String>>();
48                 int c = 0;
49                 for (Row r : rows) {
50                         Pair<String, String> pair = new Pair<String, String>(r.getString("DataBegin"), r.getString("DataEnd"));
51                         res.add(pair);
52                         ++c;
53                 }
54                 final long endTime = System.currentTimeMillis();
55                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
56                 return res;
57         }
58
59         /*
60          * getting data from the db
61          */
62         public DateBean ReadProteinData(long day, String date) {
63                 String com = "SELECT JobID, Protein FROM ProteinData WHERE jobtime = " + day + ";";
64                 System.out.println("Command: " + com);
65                 ResultSet results = session.execute(com);
66                 if (results.isExhausted())
67                         return null;
68                 List<Row> rows = results.all();
69                 DateBean res = new DateBean(date);
70                 for (Row r : rows) {
71                         res.setJobidAndSeq(r.getString("JobID"), r.getString("Protein"));
72                 }
73                 return res;
74         }
75
76         /**
77          * getting data from the db
78          */
79         public DateBean ReadFailedJobs(long day, String date, JobStatus status) {
80                 // FailLog (jobtime, JobID, ExecTime, ip, FinalStatus)
81                 String com = "SELECT JobID FROM FailLog WHERE jobtime = " + day + " and FinalStatus = '" + status.name() + "';";
82                 ResultSet results = session.execute(com);
83                 if (results.isExhausted())
84                         return null;
85                 List<Row> rows = results.all();
86                 DateBean res = new DateBean(date);
87                 for (Row r : rows) {
88                         String jobid = r.getString("JobID");
89                         String com1 = "SELECT Protein FROM ProteinLog WHERE JobID = '" + jobid + "';";
90                         System.out.println("Command: " + com1);
91                         ResultSet results2 = session.execute(com1);
92                         List<Row> jrows = results2.all();
93                         if (1 == jrows.size()) {
94                                 String protein = jrows.get(0).getString("Protein");
95                                 res.setJobidAndSeq(jobid, protein);
96                         }
97                 }
98                 return res;
99         }
100
101         /*
102          * getting data from the db JobDateInfo
103          */
104         public Total ReadDateTable(long queryDate) {
105                 ResultSet results = session.execute("SELECT * FROM JobDateInfo WHERE jobday = " + queryDate + ";");
106                 if (results.isExhausted())
107                         return null;
108                 Row therow = results.one();
109                 Total res = new Total(therow.getLong("Total"), therow.getLong("TotalOK"), therow.getLong("TotalStopped"),
110                                 therow.getLong("TotalError"), therow.getLong("TotalTimeOut"));
111                 if (!results.isExhausted()) {
112                         Date date = new Date(queryDate);
113                         log.warn("CassandraReader.ReadDateTable: date row for " + date.toString() + " (" + queryDate + ") duplicated ");
114                 }
115                 return res;
116         }
117
118         /*
119          * getting whole protein sequence from the db ProteinRow
120          */
121         public List<ProteinBean> ReadWholeSequence(String queryProtein) {
122                 final long startTime = System.currentTimeMillis();
123                 String com = "SELECT JobID, Predictions FROM ProteinRow WHERE Protein = '" + queryProtein + "';";
124                 System.out.println("Command: " + com);
125                 ResultSet results = session.execute(com);
126                 if (results.isExhausted())
127                         return null;
128                 final long queryTime = System.currentTimeMillis();
129                 List<Row> rows = results.all();
130                 System.out.println("first size : " + rows.size());
131                 System.out.println("Query time is " + (queryTime - startTime) + " msec");
132                 System.out.println(" rows analysed,  " + rows.size());
133                 List<ProteinBean> res = new ArrayList<ProteinBean>();
134                 ProteinBean structure = new ProteinBean(queryProtein, rows.get(0).getMap("Predictions", String.class, String.class));
135                 System.out.println("second size : " + rows.size());
136                 int c = 0;
137                 for (Row r : rows) {
138                         structure.setJobid(r.getString("JobID"));
139                         ++c;
140                 }
141                 res.add(structure);
142                 final long endTime = System.currentTimeMillis();
143                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
144                 return res;
145         }
146
147         /*
148          * getting jobs by ip
149          */
150         public Map<String, String[]> ReadIpWithJobs(String ip) {
151                 final long startTime = System.currentTimeMillis();
152                 String com = "SELECT JobID, Protein, FinalStatus, DataBegin FROM ProteinLog WHERE ip = '" + ip + "';";
153                 System.out.println("Command: " + com);
154                 ResultSet results = session.execute(com);
155                 if (results.isExhausted())
156                         return null;
157                 final long queryTime = System.currentTimeMillis();
158                 List<Row> rows = results.all();
159                 Map<String, String[]> res = new HashMap<String, String[]>();
160                 System.out.println("Query time is " + (queryTime - startTime) + " msec");
161                 System.out.println(" rows analysed,  " + rows.size());
162                 int c = 0;
163                 for (Row r : rows) {
164                         if (r.getString("FinalStatus").equals("OK")) {
165                                 String date = r.getString("DataBegin");
166                                 res.put(r.getString("JobID"), new String[] { date.substring(0, date.indexOf(":")), r.getString("Protein") });
167                                 ++c;
168                         }
169                 }
170                 final long endTime = System.currentTimeMillis();
171                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
172                 return res;
173         }
174
175         /*
176          * getting part of protein sequence from the db ProteinRow
177          */
178         public List<ProteinBean> ReadPartOfSequence(String queryProtein) {
179                 final long startTime = System.currentTimeMillis();
180                 String com = "SELECT * FROM ProteinRow;";
181                 System.out.println("Command: " + com);
182                 ResultSet results = session.execute(com);
183                 if (results.isExhausted())
184                         return null;
185                 final long queryTime = System.currentTimeMillis();
186                 List<Row> rows = results.all();
187                 System.out.println("Query time is " + (queryTime - startTime) + " msec");
188                 System.out.println(" rows analysed,  " + rows.size());
189                 List<ProteinBean> res = new ArrayList<ProteinBean>();
190                 int c = 0;
191                 for (Row r : rows) {
192                         String prot = r.getString("Protein");
193                         if (prot.matches("(.*)" + queryProtein + "(.*)")) {
194                                 ProteinBean structure = new ProteinBean(prot, r.getMap("Predictions", String.class, String.class));
195                                 structure.setJobid(r.getString("JobID"));
196                                 res.add(structure);
197                                 ++c;
198                         }
199                 }
200                 final long endTime = System.currentTimeMillis();
201                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
202                 return res;
203         }
204
205         /*
206          * getting protein sequence from the db ProteinRow
207          */
208         public Map<String, String> ReadProtein() {
209                 final long startTime = System.currentTimeMillis();
210                 String com = "SELECT * FROM ProteinRow;";
211                 System.out.println("Command: " + com);
212                 ResultSet results = session.execute(com);
213                 if (results.isExhausted())
214                         return null;
215                 final long queryTime = System.currentTimeMillis();
216                 List<Row> rows = results.all();
217                 System.out.println("Query time is " + (queryTime - startTime) + " msec");
218                 System.out.println(" rows analysed,  " + rows.size());
219                 Map<String, String> res = new HashMap<String, String>();
220                 int c = 0;
221                 for (Row r : rows) {
222                         String prot = r.getString("Protein");
223                         String prediction = findJnetpred(r.getMap("Predictions", String.class, String.class));
224                         if (prot != null || prediction != null) 
225                                 res.put(prot, prediction);
226                 }
227                 final long endTime = System.currentTimeMillis();
228                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
229                 return res;
230         }
231
232         private String findJnetpred (Map<String,String> pred) {
233                 if (pred != null) {
234                         if (pred.containsKey("jnetpred")) 
235                                 return pred.get("jnetpred");
236                 }
237                 return null;
238         }
239         /*
240          * getting protein sequences by counter
241          */
242         public Map<String, Integer> ReadProteinSequenceByCounter() {
243                 final long startTime = System.currentTimeMillis();
244                 String com = "SELECT Protein, JobID FROM ProteinRow;";
245                 System.out.println("Command: " + com);
246                 ResultSet results = session.execute(com);
247                 if (results.isExhausted())
248                         return null;
249                 final long queryTime = System.currentTimeMillis();
250                 List<Row> rows = results.all();
251                 System.out.println("Query time is " + (queryTime - startTime) + " msec");
252                 System.out.println(" rows analysed,  " + rows.size());
253                 Map<String, Integer> res = new HashMap<String, Integer>();
254                 int c = 0;
255                 for (Row r : rows) {
256                         String protein = r.getString("Protein");
257                         String id = r.getString("JobID");
258                         if (res.containsKey(protein))
259                                 res.put(protein, res.get(protein) + 1);
260                         else
261                                 res.put(protein, 1);
262                 }
263                 final long endTime = System.currentTimeMillis();
264                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
265                 return res;
266         }
267
268         /*
269          * getting ip by counter
270          */
271         public Map<String, Integer> ReadIpByCounter() {
272                 final long startTime = System.currentTimeMillis();
273                 String com = "SELECT JobID, ip FROM ProteinLog;";
274                 System.out.println("Command: " + com);
275                 ResultSet results = session.execute(com);
276                 if (results.isExhausted())
277                         return null;
278                 final long queryTime = System.currentTimeMillis();
279                 List<Row> rows = results.all();
280                 System.out.println("Query time is " + (queryTime - startTime) + " msec");
281                 System.out.println(" rows analysed,  " + rows.size());
282                 Map<String, Integer> res = new HashMap<String, Integer>();
283                 int c = 0;
284                 for (Row r : rows) {
285                         String ip = r.getString("ip");
286                         String id = r.getString("JobID");
287                         if (res.containsKey(ip))
288                                 res.put(ip, res.get(ip) + 1);
289                         else
290                                 res.put(ip, 1);
291                 }
292                 final long endTime = System.currentTimeMillis();
293                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
294                 return res;
295         }
296
297         /*
298          * getting log info for jobid
299          */
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())
306                         return null;
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())
313                         return null;
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                 System.out.println("Query time is " + (queryTime - startTime) + " msec");
318                 final long endTime = System.currentTimeMillis();
319                 System.out.println(" rows analysed, execution time is " + (endTime - startTime) + " msec");
320                 return res;
321         }
322 }