Improve presentation of job on the web (servlet ServletSequenceProtein)
[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 JobDateInfo
55          */
56         public List<Long> ReadDateTable(long queryDate) {
57                 ResultSet results = session.execute("SELECT * FROM JobDateInfo WHERE jobday = " + queryDate + ";");
58                 if (results.isExhausted())
59                         return null;
60                 Row therow = results.one();
61                 List<Long> res = new ArrayList<Long>();
62                 res.add(therow.getLong("Total"));
63                 res.add(therow.getLong("TotalOK"));
64                 res.add(therow.getLong("TotalStopped"));
65                 res.add(therow.getLong("TotalError"));
66                 res.add(therow.getLong("TotalTimeOut"));
67                 if (!results.isExhausted()) {
68                         Date date = new Date (queryDate);
69                         log.warn("CassandraReader.ReadDateTable: date row for " + date.toString () + " ("+ queryDate + ") duplicated ");
70                 }
71                 return res;
72         }
73         /*
74          * getting whole protein sequence from the db ProteinRow
75          */
76         public List<AnnotatedProteinSequenceBean> ReadWholeSequence(String queryProtein) {
77                 final long startTime = System.currentTimeMillis();
78                 String com = "SELECT JobID, Predictions FROM ProteinRow WHERE Protein = '" + queryProtein + "';";
79                 System.out.println("Command: " + com);
80                 ResultSet results = session.execute(com);
81                 if (results.isExhausted())
82                         return null;
83                 final long queryTime = System.currentTimeMillis();
84                 List<Row> rows = results.all();
85                 System.out.println("Query time is " + (queryTime - startTime) + " msec");
86                 System.out.println(" rows analysed,  " + rows.size());
87                 List<AnnotatedProteinSequenceBean> res = new ArrayList<AnnotatedProteinSequenceBean>();
88                 int c = 0;
89                 for (Row r : rows) {
90                         AnnotatedProteinSequenceBean structure = new AnnotatedProteinSequenceBean(queryProtein, r.getString("JobID"), r.getMap(
91                                         "Predictions", String.class, String.class));
92                         res.add(structure);
93                         ++c;
94                 }
95                 final long endTime = System.currentTimeMillis();
96                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
97                 return res;
98         }
99
100         /*
101          * getting part of protein sequence from the db ProteinRow
102          */
103         public List<AnnotatedProteinSequenceBean> ReadPartOfSequence(String queryProtein) {
104                 final long startTime = System.currentTimeMillis();
105                 String com = "SELECT * FROM ProteinRow;";
106                 System.out.println("Command: " + com);
107                 ResultSet results = session.execute(com);
108                 if (results.isExhausted())
109                         return null;
110                 final long queryTime = System.currentTimeMillis();
111                 List<Row> rows = results.all();
112                 System.out.println("Query time is " + (queryTime - startTime) + " msec");
113                 System.out.println(" rows analysed,  " + rows.size());
114                 List<AnnotatedProteinSequenceBean> res = new ArrayList<AnnotatedProteinSequenceBean>();
115                 int c = 0;
116                 for (Row r : rows) {
117                         String prot = r.getString("Protein");
118                         if (prot.matches("(.*)" + queryProtein + "(.*)")) {
119                                 AnnotatedProteinSequenceBean structure = new AnnotatedProteinSequenceBean(prot, r.getString("JobID"), r.getMap("Predictions",
120                                                 String.class, String.class));
121                                 res.add(structure);
122                                 ++c;
123                         }
124                 }
125                 final long endTime = System.currentTimeMillis();
126                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
127                 return res;
128         }
129
130         /*
131          * getting protein sequences by counter
132          */
133         public Map<String, Integer> ReadProteinSequenceByCounter() {
134                 final long startTime = System.currentTimeMillis();
135                 String com = "SELECT Protein, JobID FROM ProteinRow;";
136                 System.out.println("Command: " + com);
137                 ResultSet results = session.execute(com);
138                 if (results.isExhausted())
139                         return null;
140                 final long queryTime = System.currentTimeMillis();
141                 List<Row> rows = results.all();
142                 System.out.println("Query time is " + (queryTime - startTime) + " msec");
143                 System.out.println(" rows analysed,  " + rows.size());
144                 Map<String, Integer> res = new HashMap<String, Integer>();
145                 int c = 0;
146                 for (Row r : rows) {
147                         String protein = r.getString("Protein");
148                         String id = r.getString("JobID");
149                         if (res.containsKey(protein))
150                                 res.put(protein, res.get(protein) + 1);
151                         else
152                                 res.put(protein, 1);
153                 }
154                 final long endTime = System.currentTimeMillis();
155                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
156                 return res;
157         }
158
159         /*
160          * getting protein sequences by counter
161          */
162         public StructureJobLog ReadJobLog(String jobid) {
163                 final long startTime = System.currentTimeMillis();
164                 String com = "SELECT * FROM ProteinLog WHERE JobID = '" + jobid + "';";
165                 System.out.println("Command: " + com);
166                 ResultSet results = session.execute(com);
167                 if (results.isExhausted())
168                         return null;
169                 final long queryTime = System.currentTimeMillis();
170                 Row row = results.one();
171                 String com1 = "SELECT * FROM ProteinRow WHERE JobID = '" + jobid + "' ALLOW FILTERING;";
172                 System.out.println("Command: " + com1);
173                 ResultSet results1 = session.execute(com1);
174                 if (results1.isExhausted())
175                         return null;
176                 Row row1 = results1.one();
177                 StructureJobLog res = new StructureJobLog(row.getString("Protein"), row.getString("JobID"), row.getString("DataBegin"),
178                                 row.getString("DataEnd"), row.getString("ip"), row1.getMap("Predictions", String.class, String.class));
179                 System.out.println("Query time is " + (queryTime - startTime) + " msec");
180                 final long endTime = System.currentTimeMillis();
181                 System.out.println(" rows analysed, execution time is " + (endTime - startTime) + " msec");
182                 return res;
183         }
184 }