Make all necessary changes for combining old and new servlets
[proteocache.git] / datadb / compbio / cassandra / CassandraReader.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 public class CassandraReader {
16         private Session session;
17         private static Logger log = Logger.getLogger(CassandraNativeConnector.class);
18
19         public CassandraReader() {
20                 Session inis = CassandraNativeConnector.getSession();
21                 setSession (inis);
22         }
23
24         public void setSession(Session s) {
25                 assert s != null;
26                 session = s;
27         }
28
29         /*
30          * getting data from the db
31          */
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");
40
41                 List<Pair<String, String>> res = new ArrayList<Pair<String, String>>();
42                 int c = 0;
43                 for (Row r : rows) {
44                         Pair<String, String> pair = new Pair<String, String>(r.getString("DataBegin"), r.getString("DataEnd"));
45                         res.add(pair);
46                         ++c;
47                 }
48                 final long endTime = System.currentTimeMillis();
49                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
50                 return res;
51         }
52
53         /*
54          * getting data from the db
55          */
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())
62                         return null;
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>>();
67                 int c = 0;
68                 for (Row r : rows) {
69                         Pair<String, String> pair = new Pair<String, String>(r.getString("JobID"), r.getString("Protein"));
70                         res.add(pair);
71                         ++c;
72                 }
73                 final long endTime = System.currentTimeMillis();
74                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
75                 return res;
76         }
77         /*
78          * getting data from the db JobDateInfo
79          */
80         public List<Long> ReadDateTable(long queryDate) {
81                 ResultSet results = session.execute("SELECT * FROM JobDateInfo WHERE jobday = " + queryDate + ";");
82                 if (results.isExhausted())
83                         return null;
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 ");
94                 }
95                 return res;
96         }
97
98         /*
99          * getting whole protein sequence from the db ProteinRow
100          */
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())
107                         return null;
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>();
113                 int c = 0;
114                 for (Row r : rows) {
115                         AnnotatedProteinSequenceBean structure = new AnnotatedProteinSequenceBean(queryProtein, r.getString("JobID"), r.getMap(
116                                         "Predictions", String.class, String.class));
117                         res.add(structure);
118                         ++c;
119                 }
120                 final long endTime = System.currentTimeMillis();
121                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
122                 return res;
123         }
124
125         /*
126          * getting part of protein sequence from the db ProteinRow
127          */
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())
134                         return null;
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>();
140                 int c = 0;
141                 for (Row r : rows) {
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));
146                                 res.add(structure);
147                                 ++c;
148                         }
149                 }
150                 final long endTime = System.currentTimeMillis();
151                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
152                 return res;
153         }
154
155         /*
156          * getting protein sequences by counter
157          */
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())
164                         return null;
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>();
170                 int c = 0;
171                 for (Row r : rows) {
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);
176                         else
177                                 res.put(protein, 1);
178                 }
179                 final long endTime = System.currentTimeMillis();
180                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
181                 return res;
182         }
183
184         /*
185          * getting log info for jobid
186          */
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())
193                         return null;
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())
200                         return null;
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");
207                 return res;
208         }
209 }