1 package compbio.cassandra;
5 import org.apache.log4j.Logger;
7 import com.datastax.driver.core.Row;
8 import com.datastax.driver.core.Session;
9 import com.datastax.driver.core.ResultSet;
10 import com.datastax.driver.core.exceptions.QueryExecutionException;
11 import com.datastax.driver.core.exceptions.QueryValidationException;
13 import compbio.data.sequence.FastaSequence;
14 import compbio.engine.JpredJob;
15 import compbio.engine.ProteoCachePropertyHelperManager;
16 import compbio.util.PropertyHelper;
18 public class CassandraWriter {
19 private Session session;
20 private static final PropertyHelper ph = ProteoCachePropertyHelperManager.getPropertyHelper();
21 private static Logger log = Logger.getLogger(CassandraNativeConnector.class);
24 Session inis = CassandraNativeConnector.getSession();
28 public void setSession(Session s) {
33 private ResultSet execute(String command) {
35 ResultSet results = session.execute(command);
37 } catch (QueryExecutionException e) {
38 String mess = "CassandraWriter: query execution exception...";
39 System.out.println(mess);
41 log.error(e.getLocalizedMessage(), e.getCause());
43 } catch (QueryValidationException e) {
44 String mess = "CassandraWriter: query validation exception... Command: " + command;
45 System.out.println(mess);
47 log.error(e.getLocalizedMessage(), e.getCause());
52 public boolean JobisNotInsterted(String jobid) {
53 ResultSet results = execute("SELECT * FROM ProteinLog WHERE JobID = '" + jobid + "';");
54 if (null != results && results.isExhausted()) {
60 public boolean JobisNotArchived(String jobid) {
61 ResultSet results = execute("SELECT * FROM JpredArchive WHERE JobID = '" + jobid + "';");
62 if (null != results && results.isExhausted()) {
69 * inserting data into the tables for queries
71 public int FormQueryTables(JpredJob job) {
72 if (JobisNotInsterted(job.getJobID())) {
73 String id = job.getJobID();
74 String protein = job.getProtein();
75 String finalstatus = job.getFinalStatus();
76 String execstatus = job.getExecutionStatus();
77 String ProgramName = job.getProgramName();
78 String ProgramVersion = job.getProgramVersion();
80 String com1 = "INSERT INTO ProteinLog (JobID, IP, DataBegin, DataEnd, FinalStatus, ExecutionStatus, Protein, ProgramName, ProgramVersion) VALUES ('"
85 + job.getStartingTimeStr()
90 + "','" + execstatus + "','" + protein + "','" + ProgramName + "','" + ProgramVersion + "');";
91 ResultSet insert = execute(com1);
93 System.out.println("CassandraWriter.FormQueryTables: couldn't insert into ProteinLog");
96 if (finalstatus.equals("OK")) {
97 String com2 = "INSERT INTO ProteinData " + "(jobtime, JobID, ExecTime, Protein)" + " VALUES (" + job.getStartingDate()
98 + ",'" + id + "'," + job.getExecutionTime() + ",'" + protein + "');";
99 if (null == execute(com2)) {
100 System.out.println("CassandraWriter.FormQueryTables: couldn't insert into ProteinData");
104 String allpredictions = "";
105 List<FastaSequence> pr = job.getPredictions();
106 for (FastaSequence pred : pr) {
107 String predictionname = pred.getId();
108 String prediction = pred.getSequence().replaceAll("\n", "");
109 allpredictions += "'" + predictionname + "':'" + prediction + "',";
111 String final_prediction = "";
112 if (!allpredictions.equals("")) {
113 final_prediction = allpredictions.substring(0, allpredictions.length() - 1);
116 ResultSet results2 = execute("SELECT * FROM ProteinRow WHERE JobID = '" + job.getJobID() + "';");
117 if (null != results2 && results2.isExhausted()) {
118 String com3 = "INSERT INTO ProteinRow (Protein, JobID, Predictions) VALUES ('" + protein + "','" + id + "',{"
119 + final_prediction + "});";
120 if (null == execute(com3)) {
121 System.out.println("CassandraWriter.FormQueryTables: couldn't insert into ProteinRow");
126 String com5 = "INSERT INTO FailLog (jobtime, JobID, ExecTime, ip, FinalStatus) VALUES (" + job.getStartingDate() + ",'"
127 + id + "'," + job.getExecutionTime() + ",'" + job.getIP() + "', '" + finalstatus + "');";
128 if (null == execute(com5)) {
129 System.out.println("CassandraWriter.FormQueryTables: couldn't insert into FailLog");
134 // update Main parameters if the job is the earliest job so far
135 ResultSet results3 = execute("SELECT * FROM MainParameters WHERE Name = 'EarliestJobDate';");
136 if (null == results3) {
137 System.out.println("CassandraWriter.FormQueryTables: couldn't get results from MainParameters");
140 boolean updateparameter = true;
141 if (!results3.isExhausted()) {
142 Row r = results3.one();
143 if (job.getStartingDate() >= Long.parseLong(r.getString("Value")))
144 updateparameter = false;
146 if (updateparameter) {
147 String com6 = "INSERT INTO MainParameters (Name, Value) VALUES ('EarliestJobDate','" + job.getStartingDate() + "');";
148 if (null == execute(com6)) {
149 System.out.println("CassandraWriter.FormQueryTables: couldn't insert into MainParameters");
154 // update internal job counts (used by the Daily Statistics
156 // TODO I don't like the bit of code. There should not be so many
162 int njobsTimeOut = 0;
163 if (finalstatus.equals("OK"))
165 else if (finalstatus.equals("TIMEDOUT"))
167 else if (finalstatus.equals("JPREDERROR"))
169 else if (finalstatus.equals("STOPPED"))
171 ResultSet results4 = execute("SELECT * FROM JobDateInfo WHERE jobday = " + job.getStartingDate() + ";");
172 if (null == results4) {
173 System.out.println("CassandraWriter.FormQueryTables: couldn't get data from JobDateInfo");
176 if (!results4.isExhausted()) {
177 Row r = results4.one();
178 njobsTotal += r.getLong("Total");
179 njobsOk += r.getLong("TotalOK");
180 njobsError += r.getLong("TotalError");
181 njobsStop += r.getLong("TotalStopped");
182 njobsTimeOut += r.getLong("TotalTimeOut");
184 String com = "INSERT INTO JobDateInfo " + "(jobday, Total, TotalOK, TotalStopped, TotalError, TotalTimeOut)" + " VALUES ("
185 + job.getStartingDate() + "," + njobsTotal + "," + njobsOk + "," + njobsStop + "," + njobsError + "," + njobsTimeOut
187 if (null == execute(com)) {
188 System.out.println("CassandraWriter.FormQueryTables: couldn't insert into JobDateInfo");
197 * insert data from a real Jpred job: timing+IP, Execution Status, Final
198 * status, protein sequence, predictions, alignment, LOG and tar.gz files
200 public int ArchiveData(JpredJob job, String archivepath) {
201 if (JobisNotArchived(job.getJobID())) {
202 String id = job.getJobID();
203 String log = job.getLog().replaceAll("'", "");
204 String com = "INSERT INTO JpredArchive (JobID, Protein, IP, StartTime, ExecTime, FinalStatus, ExecutionStatus, LOG, ArchiveLink, ProgramName, ProgramVersion) VALUES ('"
211 + job.getStartingTime()
213 + job.getExecutionTime()
215 + job.getFinalStatus()
217 + job.getExecutionStatus()
223 + job.getProgramName() + "','" + job.getProgramVersion() + "');";
224 if (null == execute(com)) {
225 System.out.println("CassandraWriter.ArchiveData: couldn't insert into JpredArchive");
227 List<FastaSequence> predictions = job.getPredictions();
228 for (FastaSequence p : predictions) {
229 if (null == execute("UPDATE JpredArchive SET predictions = predictions + {'" + p.getId() + "':'"
230 + p.getSequence().replaceAll("\n", "") + "'} WHERE JobID = '" + id + "';")) {
231 System.out.println("CassandraWriter.ArchiveData: couldn't update data in JpredArchive");
235 List<FastaSequence> seqs = job.getAlignment();
237 for (FastaSequence s : seqs) {
238 String com2 = "UPDATE JpredArchive SET alignment = alignment + {'" + s.getId() + "':'"
239 + s.getSequence().replaceAll("\n", "") + "'} WHERE JobID = '" + id + "';";
240 if (null == execute(com2)) {
241 System.out.println("CassandraWriter.ArchiveData: couldn't update data in JpredArchive");