f29bc9f3b9954f7b135b9615206084684ba5b206
[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.engine.JobStatus;
16
17 public class CassandraReaderOld {
18         private Session session;
19         private static Logger log = Logger.getLogger(CassandraNativeConnector.class);
20
21         public CassandraReaderOld() {
22                 Session inis = CassandraNativeConnector.getSession();
23                 setSession(inis);
24         }
25
26         public void setSession(Session s) {
27                 assert s != null;
28                 session = s;
29         }
30
31         /*
32          * getting data from the db
33          */
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");
42
43                 List<Pair<String, String>> res = new ArrayList<Pair<String, String>>();
44                 int c = 0;
45                 for (Row r : rows) {
46                         Pair<String, String> pair = new Pair<String, String>(r.getString("DataBegin"), r.getString("DataEnd"));
47                         res.add(pair);
48                         ++c;
49                 }
50                 final long endTime = System.currentTimeMillis();
51                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
52                 return res;
53         }
54
55         /*
56          * getting data from the db
57          */
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())
63                         return null;
64                 List<Row> rows = results.all();
65                 DateBean res = new DateBean(date);
66                 for (Row r : rows) {
67                         res.setJobidAndSeq(r.getString("JobID"), r.getString("Protein"));
68                 }
69                 return res;
70         }
71
72         /**
73          * getting data from the db
74          */
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())
80                         return null;
81                 List<Row> rows = results.all();
82                 DateBean res = new DateBean(date);
83                 for (Row r : rows) {
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);
92                         }
93                 }
94                 return res;
95         }
96
97         /*
98          * getting data from the db JobDateInfo
99          */
100         public Total ReadDateTable(long queryDate) {
101                 ResultSet results = session.execute("SELECT * FROM JobDateInfo WHERE jobday = " + queryDate + ";");
102                 if (results.isExhausted())
103                         return null;
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 ");
110                 }
111                 return res;
112         }
113
114         /*
115          * getting whole protein sequence from the db ProteinRow
116          */
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())
123                         return null;
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());
132                 int c = 0;
133                 for (Row r : rows) {
134                         structure.setJobid(r.getString("JobID"));
135                         ++c;
136                 }
137                 res.add(structure);
138                 final long endTime = System.currentTimeMillis();
139                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
140                 return res;
141         }
142
143         /*
144          * getting jobs by ip
145          */
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())
152                         return null;
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());
158                 int c = 0;
159                 for (Row r : rows) {
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") });
163                                 ++c;
164                         }
165                 }
166                 final long endTime = System.currentTimeMillis();
167                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
168                 return res;
169         }
170
171         /*
172          * getting part of protein sequence from the db ProteinRow
173          */
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())
180                         return null;
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>();
186                 int c = 0;
187                 for (Row r : rows) {
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"));
192                                 res.add(structure);
193                                 ++c;
194                         }
195                 }
196                 final long endTime = System.currentTimeMillis();
197                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
198                 return res;
199         }
200
201         /*
202          * getting protein sequence from the db ProteinRow
203          */
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())
210                         return null;
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>();
216                 int c = 0;
217                 for (Row r : rows) {
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);
222                 }
223                 final long endTime = System.currentTimeMillis();
224                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
225                 return res;
226         }
227
228         private String findJnetpred (Map<String,String> pred) {
229                 if (pred != null) {
230                         if (pred.containsKey("jnetpred")) 
231                                 return pred.get("jnetpred");
232                 }
233                 return null;
234         }
235         /*
236          * getting protein sequences by counter
237          */
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())
244                         return null;
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>();
250                 int c = 0;
251                 for (Row r : rows) {
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);
256                         else
257                                 res.put(protein, 1);
258                 }
259                 final long endTime = System.currentTimeMillis();
260                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
261                 return res;
262         }
263
264         /*
265          * getting ip by counter
266          */
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())
273                         return null;
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>();
279                 int c = 0;
280                 for (Row r : rows) {
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);
285                         else
286                                 res.put(ip, 1);
287                 }
288                 final long endTime = System.currentTimeMillis();
289                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
290                 return res;
291         }
292
293         /*
294          * getting log info for jobid
295          */
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())
302                         return null;
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())
309                         return null;
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");
316                 return res;
317         }
318 }