62979adc4cbd634feab14fce09bb8dc514553f4f
[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         /*
241          * getting protein sequences by counter
242          */
243         public Map<String, Integer> ReadProteinSequenceByCounter() {
244                 final long startTime = System.currentTimeMillis();
245                 String com = "SELECT Protein, JobID FROM ProteinRow;";
246                 System.out.println("Command: " + com);
247                 ResultSet results = session.execute(com);
248                 if (results.isExhausted())
249                         return null;
250                 final long queryTime = System.currentTimeMillis();
251                 List<Row> rows = results.all();
252                 System.out.println("Query time is " + (queryTime - startTime) + " msec");
253                 System.out.println(" rows analysed,  " + rows.size());
254                 Map<String, Integer> res = new HashMap<String, Integer>();
255                 int c = 0;
256                 for (Row r : rows) {
257                         String protein = r.getString("Protein");
258                         String id = r.getString("JobID");
259                         if (res.containsKey(protein))
260                                 res.put(protein, res.get(protein) + 1);
261                         else
262                                 res.put(protein, 1);
263                 }
264                 final long endTime = System.currentTimeMillis();
265                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
266                 return res;
267         }
268
269         /*
270          * getting ip by counter
271          */
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())
278                         return null;
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>();
284                 int c = 0;
285                 for (Row r : rows) {
286                         String ip = r.getString("ip");
287                         String id = r.getString("JobID");
288                         if (res.containsKey(ip))
289                                 res.put(ip, res.get(ip) + 1);
290                         else
291                                 res.put(ip, 1);
292                 }
293                 final long endTime = System.currentTimeMillis();
294                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
295                 return res;
296         }
297
298         /*
299          * getting log info for jobid
300          */
301         public JobBean ReadJobLog(String jobid) {
302                 final long startTime = System.currentTimeMillis();
303                 String com = "SELECT * FROM ProteinLog WHERE JobID = '" + jobid + "';";
304                 System.out.println("Command: " + com);
305                 ResultSet results = session.execute(com);
306                 if (results.isExhausted())
307                         return null;
308                 final long queryTime = System.currentTimeMillis();
309                 Row row = results.one();
310                 String com1 = "SELECT * FROM ProteinRow WHERE JobID = '" + jobid + "' ALLOW FILTERING;";
311                 System.out.println("Command: " + com1);
312                 ResultSet results1 = session.execute(com1);
313                 if (results1.isExhausted())
314                         return null;
315                 Row row1 = results1.one();
316                 JobBean res = new JobBean(row.getString("Protein"), row.getString("JobID"), row.getString("DataBegin"), row.getString("DataEnd"),
317                                 row.getString("ip"), row1.getMap("Predictions", String.class, String.class), row.getString("ProgrammeName"), row.getString("Version"));
318                 System.out.println("Query time is " + (queryTime - startTime) + " msec");
319                 final long endTime = System.currentTimeMillis();
320                 System.out.println(" rows analysed, execution time is " + (endTime - startTime) + " msec");
321                 return res;
322         }
323 }