Merge branch 'master' into PROT-9-webservice
[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 import compbio.engine.Pair;
21
22 public class CassandraReaderOld {
23         private Session session;
24         private static Logger log = Logger.getLogger(CassandraNativeConnector.class);
25
26         public CassandraReaderOld() {
27                 Session inis = CassandraNativeConnector.getSession();
28                 setSession(inis);
29         }
30
31         public void setSession(Session s) {
32                 assert s != null;
33                 session = s;
34         }
35
36         /*
37          * getting data from the db
38          */
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");
47
48                 List<Pair<String, String>> res = new ArrayList<Pair<String, String>>();
49                 int c = 0;
50                 for (Row r : rows) {
51                         Pair<String, String> pair = new Pair<String, String>(r.getString("DataBegin"), r.getString("DataEnd"));
52                         res.add(pair);
53                         ++c;
54                 }
55                 final long endTime = System.currentTimeMillis();
56                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
57                 return res;
58         }
59
60         /*
61          * getting data from the db
62          */
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())
68                         return null;
69                 List<Row> rows = results.all();
70                 DateBean res = new DateBean(date);
71                 for (Row r : rows) {
72                         res.setJobidAndSeq(r.getString("JobID"), r.getString("Protein"));
73                 }
74                 return res;
75         }
76
77         /**
78          * getting data from the db
79          */
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())
85                         return null;
86                 List<Row> rows = results.all();
87                 DateBean res = new DateBean(date);
88                 for (Row r : rows) {
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);
97                         }
98                 }
99                 return res;
100         }
101
102         /*
103          * getting data from the db JobDateInfo
104          */
105         public Total ReadDateTable(long queryDate) {
106                 ResultSet results = session.execute("SELECT * FROM JobDateInfo WHERE jobday = " + queryDate + ";");
107                 if (results.isExhausted())
108                         return null;
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 ");
115                 }
116                 return res;
117         }
118
119         /*
120          * getting whole protein sequence from the db ProteinRow
121          */
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()) 
128                         return null;
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());
137                 int c = 0;
138                 for (Row r : rows) {
139                         structure.setJobid(r.getString("JobID"));
140                         ++c;
141                 }
142                 res.add(structure);
143                 final long endTime = System.currentTimeMillis();
144                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
145                 return res;
146         }
147
148         /*
149          * getting jobs by ip
150          */
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())
157                         return null;
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());
163                 int c = 0;
164                 for (Row r : rows) {
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") });
168                                 ++c;
169                         }
170                 }
171                 final long endTime = System.currentTimeMillis();
172                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
173                 return res;
174         }
175
176         /*
177          * getting part of protein sequence from the db ProteinRow
178          */
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())
185                         return null;
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>();
191                 int c = 0;
192                 for (Row r : rows) {
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"));
197                                 res.add(structure);
198                                 ++c;
199                         }
200                 }
201                 final long endTime = System.currentTimeMillis();
202                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
203                 return res;
204         }
205
206         /*
207          * getting protein sequence from the db ProteinRow
208          */
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())
215                         return null;
216                 //final long queryTime = System.currentTimeMillis();
217                 List<Row> rows = results.all();
218                 Map<String, String> output = new HashMap<String, String>();
219                 for (Row r : rows) {
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);
224                         }
225                 }
226                 //final long endTime = System.currentTimeMillis();
227                 return output;
228         }
229
230         private String findJnetpred(Map<String, String> pred) {
231                 if (pred != null) {
232                         if (pred.containsKey("jnetpred"))
233                                 return pred.get("jnetpred");
234                 }
235                 return null;
236         }
237
238         /*
239          * getting protein sequences by counter
240          */
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())
247                         return null;
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>();
253                 int c = 0;
254                 for (Row r : rows) {
255                         String protein = r.getString("Protein");
256                         if (res.containsKey(protein))
257                                 res.put(protein, res.get(protein) + 1);
258                         else
259                                 res.put(protein, 1);
260                 }
261                 final long endTime = System.currentTimeMillis();
262                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
263                 return res;
264         }
265
266         /*
267          * getting ip by counter
268          */
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())
275                         return null;
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>();
281                 int c = 0;
282                 for (Row r : rows) {
283                         String ip = r.getString("ip");
284                         if (res.containsKey(ip))
285                                 res.put(ip, res.get(ip) + 1);
286                         else
287                                 res.put(ip, 1);
288                 }
289                 final long endTime = System.currentTimeMillis();
290                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
291                 return res;
292         }
293
294         /*
295          * getting log info for jobid
296          */
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())
303                         return null;
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())
310                         return null;
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);
319                 }
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");
323                 return res;
324         }
325 }