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;
11 import compbio.data.sequence.FastaSequence;
12 import compbio.engine.JpredJob;
13 import compbio.engine.ProteoCachePropertyHelperManager;
14 import compbio.util.PropertyHelper;
16 public class CassandraWriter {
17 private Session session;
18 private static final PropertyHelper ph = ProteoCachePropertyHelperManager.getPropertyHelper();
19 private static Logger log = Logger.getLogger(CassandraNativeConnector.class);
22 Session inis = CassandraNativeConnector.getSession();
26 public void setSession(Session s) {
31 public boolean JobisNotInsterted(String jobid) {
32 ResultSet results1 = session.execute("SELECT * FROM ProteinLog WHERE JobID = '" + jobid + "';");
33 if (results1.isExhausted()) {
39 public boolean JobisNotArchived(String jobid) {
40 ResultSet results1 = session.execute("SELECT * FROM JpredArchive WHERE JobID = '" + jobid + "';");
41 if (results1.isExhausted()) {
48 * inserting data into the tables for queries
50 public int FormQueryTables(JpredJob job) {
51 if (JobisNotInsterted(job.getJobID())) {
52 String id = job.getJobID();
53 String protein = job.getProtein();
54 String finalstatus = job.getFinalStatus();
55 String execstatus = job.getExecutionStatus();
57 String com1 = "INSERT INTO ProteinLog (JobID, IP, DataBegin, DataEnd, FinalStatus, ExecutionStatus, Protein) VALUES ('" + id
58 + "','" + job.getIP() + "','" + job.getStartingTimeStr() + "','" + job.getEndTimeStr() + "','" + finalstatus + "','"
59 + execstatus + "','" + protein + "');";
60 session.execute(com1);
61 if (finalstatus.equals("OK")) {
62 String com2 = "INSERT INTO ProteinData " + "(jobtime, JobID, ExecTime, Protein)" + " VALUES (" + job.getStartingDate()
63 + ",'" + id + "'," + job.getExecutionTime() + ",'" + protein + "');";
64 session.execute(com2);
66 String allpredictions = "";
67 List<FastaSequence> pr = job.getPredictions();
68 for (FastaSequence pred : pr) {
69 String predictionname = pred.getId();
70 String prediction = pred.getSequence().replaceAll("\n", "");
71 allpredictions += "'" + predictionname + "':'" + prediction + "',";
73 String final_prediction = "";
74 if (!allpredictions.equals("")) {
75 final_prediction = allpredictions.substring(0, allpredictions.length() - 1);
78 ResultSet results2 = session.execute("SELECT * FROM ProteinRow WHERE JobID = '" + job.getJobID() + "';");
79 if (results2.isExhausted()) {
80 String com3 = "INSERT INTO ProteinRow (Protein, JobID, Predictions) VALUES ('" + protein + "','" + id + "',{"
81 + final_prediction + "});";
82 session.execute(com3);
85 String com5 = "INSERT INTO FailLog (jobtime, JobID, ExecTime, ip, FinalStatus) VALUES (" + job.getStartingDate() + ",'"
86 + id + "'," + job.getExecutionTime() + ",'" + job.getIP() + "', '" + finalstatus + "');";
87 session.execute(com5);
90 // update Main parameters if the job is the earliest job so far
91 ResultSet results3 = session.execute("SELECT * FROM MainParameters WHERE Name = 'EarliestJobDate';");
92 boolean updateparameter = true;
93 if (!results3.isExhausted()) {
94 Row r = results3.one();
95 if (job.getStartingDate() >= Long.parseLong(r.getString("Value")))
96 updateparameter = false;
98 if (updateparameter) {
99 session.execute("INSERT INTO MainParameters (Name, Value) VALUES ('EarliestJobDate','" + job.getStartingDate() + "');");
102 // update internal job counts (used by the Daily Statistics
104 //TODO I don't like the bit of code. There should not be so many counters...
109 int njobsTimeOut = 0;
110 if (finalstatus.equals("OK"))
112 else if (finalstatus.equals("TIMEDOUT"))
114 else if (finalstatus.equals("JPREDERROR"))
116 else if (finalstatus.equals("STOPPED"))
118 ResultSet results4 = session.execute("SELECT * FROM JobDateInfo WHERE jobday = " + job.getStartingDate() + ";");
119 if (!results4.isExhausted()) {
120 Row r = results4.one();
121 njobsTotal += r.getLong("Total");
122 njobsOk += r.getLong("TotalOK");
123 njobsError += r.getLong("TotalError");
124 njobsStop += r.getLong("TotalStopped");
125 njobsTimeOut += r.getLong("TotalTimeOut");
127 String com = "INSERT INTO JobDateInfo " + "(jobday, Total, TotalOK, TotalStopped, TotalError, TotalTimeOut)" + " VALUES ("
128 + job.getStartingDate() + "," + njobsTotal + "," + njobsOk + "," + njobsStop + "," + njobsError + "," + njobsTimeOut
130 session.execute(com);
137 * insert data from a real Jpred job: timing+IP, Execution Status, Final
138 * status, protein sequence, predictions, alignment, LOG and tar.gz files
140 public int ArchiveData(JpredJob job, String archivepath) {
141 if (JobisNotArchived(job.getJobID())) {
142 String id = job.getJobID();
143 String log = job.getLog().replaceAll("'", "");
144 String com = "INSERT INTO JpredArchive (JobID, Protein, IP, StartTime, ExecTime, FinalStatus, ExecutionStatus, LOG, ArchiveLink) VALUES ('"
151 + job.getStartingTime()
153 + job.getExecutionTime()
154 + ",'" + job.getFinalStatus() + "','" + job.getExecutionStatus() + "','" + log + "','" + archivepath + "');";
155 session.execute(com);
156 List<FastaSequence> predictions = job.getPredictions();
157 for (FastaSequence p : predictions) {
158 session.execute("UPDATE JpredArchive SET predictions = predictions + {'" + p.getId() + "':'"
159 + p.getSequence().replaceAll("\n", "") + "'} WHERE JobID = '" + id + "';");
162 List<FastaSequence> seqs = job.getAlignment();
163 for (FastaSequence s : seqs) {
164 session.execute("UPDATE JpredArchive SET alignment = alignment + {'" + s.getId() + "':'"
165 + s.getSequence().replaceAll("\n", "") + "'} WHERE JobID = '" + id + "';");