6e3587bca084484f3c1b3a45ef96c7a14eddda69
[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          * getting whole protein sequence from the db ProteinRow
99          */
100         public List<AnnotatedProteinSequenceBean> ReadWholeSequence(String queryProtein) {
101                 final long startTime = System.currentTimeMillis();
102                 String com = "SELECT JobID, Predictions FROM ProteinRow WHERE Protein = '" + queryProtein + "';";
103                 System.out.println("Command: " + com);
104                 ResultSet results = session.execute(com);
105                 if (results.isExhausted())
106                         return null;
107                 final long queryTime = System.currentTimeMillis();
108                 List<Row> rows = results.all();
109                 System.out.println("Query time is " + (queryTime - startTime) + " msec");
110                 System.out.println(" rows analysed,  " + rows.size());
111                 List<AnnotatedProteinSequenceBean> res = new ArrayList<AnnotatedProteinSequenceBean>();
112                 int c = 0;
113                 for (Row r : rows) {
114                         AnnotatedProteinSequenceBean structure = new AnnotatedProteinSequenceBean(queryProtein, r.getString("JobID"), r.getMap(
115                                         "Predictions", String.class, String.class));
116                         if (structure.getPrediction().containsKey("jnetpred"))
117                                 structure.setJnetpred(structure.getPrediction().get("jnetpred"));
118                         else
119                                 structure.setJnetpred("");
120                         res.add(structure);
121                         ++c;
122                 }
123                 final long endTime = System.currentTimeMillis();
124                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
125                 return res;
126         }
127
128         /*
129          * getting part of protein sequence from the db ProteinRow
130          */
131         public List<AnnotatedProteinSequenceBean> ReadPartOfSequence(String queryProtein) {
132                 final long startTime = System.currentTimeMillis();
133                 String com = "SELECT * FROM ProteinRow;";
134                 System.out.println("Command: " + com);
135                 ResultSet results = session.execute(com);
136                 if (results.isExhausted())
137                         return null;
138                 final long queryTime = System.currentTimeMillis();
139                 List<Row> rows = results.all();
140                 System.out.println("Query time is " + (queryTime - startTime) + " msec");
141                 System.out.println(" rows analysed,  " + rows.size());
142                 List<AnnotatedProteinSequenceBean> res = new ArrayList<AnnotatedProteinSequenceBean>();
143                 int c = 0;
144                 for (Row r : rows) {
145                         String prot = r.getString("Protein");
146                         if (prot.matches("(.*)" + queryProtein + "(.*)")) {
147                                 AnnotatedProteinSequenceBean structure = new AnnotatedProteinSequenceBean(prot, r.getString("JobID"), r.getMap("Predictions",
148                                                 String.class, String.class));
149                                 res.add(structure);
150                                 ++c;
151                         }
152                 }
153                 final long endTime = System.currentTimeMillis();
154                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
155                 return res;
156         }
157
158         /*
159          * getting protein sequences by counter
160          */
161         public Map<String, Integer> ReadProteinSequenceByCounter() {
162                 final long startTime = System.currentTimeMillis();
163                 String com = "SELECT Protein, JobID FROM ProteinRow;";
164                 System.out.println("Command: " + com);
165                 ResultSet results = session.execute(com);
166                 if (results.isExhausted())
167                         return null;
168                 final long queryTime = System.currentTimeMillis();
169                 List<Row> rows = results.all();
170                 System.out.println("Query time is " + (queryTime - startTime) + " msec");
171                 System.out.println(" rows analysed,  " + rows.size());
172                 Map<String, Integer> res = new HashMap<String, Integer>();
173                 int c = 0;
174                 for (Row r : rows) {
175                         String protein = r.getString("Protein");
176                         String id = r.getString("JobID");
177                         if (res.containsKey(protein))
178                                 res.put(protein, res.get(protein) + 1);
179                         else
180                                 res.put(protein, 1);
181                 }
182                 final long endTime = System.currentTimeMillis();
183                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
184                 return res;
185         }
186
187         /*
188          * getting log info for jobid
189          */
190         public StructureJobLog ReadJobLog(String jobid) {
191                 final long startTime = System.currentTimeMillis();
192                 String com = "SELECT * FROM ProteinLog WHERE JobID = '" + jobid + "';";
193                 System.out.println("Command: " + com);
194                 ResultSet results = session.execute(com);
195                 if (results.isExhausted())
196                         return null;
197                 final long queryTime = System.currentTimeMillis();
198                 Row row = results.one();
199                 String com1 = "SELECT * FROM ProteinRow WHERE JobID = '" + jobid + "' ALLOW FILTERING;";
200                 System.out.println("Command: " + com1);
201                 ResultSet results1 = session.execute(com1);
202                 if (results1.isExhausted())
203                         return null;
204                 Row row1 = results1.one();
205                 StructureJobLog res = new StructureJobLog(row.getString("Protein"), row.getString("JobID"), row.getString("DataBegin"),
206                                 row.getString("DataEnd"), row.getString("ip"), row1.getMap("Predictions", String.class, String.class));
207                 System.out.println("Query time is " + (queryTime - startTime) + " msec");
208                 final long endTime = System.currentTimeMillis();
209                 System.out.println(" rows analysed, execution time is " + (endTime - startTime) + " msec");
210                 return res;
211         }
212 }