1 package compbio.cassandra;
3 import java.text.ParseException;
4 import java.text.SimpleDateFormat;
8 import org.apache.log4j.Logger;
10 import com.datastax.driver.core.Row;
11 import com.datastax.driver.core.Session;
12 import com.datastax.driver.core.ResultSet;
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 public boolean JobisNotInsterted(String jobid) {
34 ResultSet results1 = session.execute("SELECT * FROM ProteinLog WHERE JobID = '" + jobid + "';");
35 if (results1.isExhausted()) {
41 public boolean JobisNotArchived(String jobid) {
42 ResultSet results1 = session.execute("SELECT * FROM JpredArchive WHERE JobID = '" + jobid + "';");
43 if (results1.isExhausted()) {
50 * inserting data into the tables for queries
52 public int FormQueryTables(JpredJob job) {
53 if (JobisNotInsterted(job.getJobID())) {
58 String id = job.getJobID();
59 String ip = job.getIP();
60 String protein = job.getProtein();
61 String finalstatus = job.getFinalStatus();
62 String execstatus = job.getExecutionStatus();
63 String com1 = "INSERT INTO ProteinLog " + "(JobID, IP, DataBegin, DataEnd, FinalStatus, ExecutionStatus, Protein)"
64 + " VALUES ('" + id + "','" + ip + "','" + job.getStartingTimeStr() + "','" + job.getEndTimeStr() + "','" + finalstatus
65 + "','" + execstatus + "','" + protein + "');";
66 session.execute(com1);
67 if (execstatus.equals("OK")) {
68 String com2 = "INSERT INTO ProteinData " + "(jobtime, JobID, ExecTime, Protein)" + " VALUES (" + job.getStartingDate() + ",'" + id
69 + "'," + job.getExecutionTime() + ",'" + protein + "');";
70 session.execute(com2);
72 String allpredictions = "";
73 List<FastaSequence> pr = job.getPredictions();
74 for (FastaSequence pred : pr) {
75 String predictionname = pred.getId();
76 String prediction = pred.getSequence().replaceAll("\n", "");
77 allpredictions += "'" + predictionname + "':'" + prediction + "',";
79 String final_prediction = "";
80 if (!allpredictions.equals("")) {
81 final_prediction = allpredictions.substring(0, allpredictions.length() - 1);
84 String check2 = "SELECT * FROM ProteinRow WHERE JobID = '" + job.getJobID() + "';";
85 ResultSet results2 = session.execute(check2);
86 if (results2.isExhausted()) {
87 String com3 = "INSERT INTO ProteinRow " + "(Protein, JobID, Predictions)" + " VALUES ('" + protein + "','" + id + "',{"
88 + final_prediction + "});";
89 session.execute(com3);
93 String com5 = "INSERT INTO FailLog " + "(jobtime, JobID, ExecTime, ip, FinalStatus)" + " VALUES ("
94 + job.getStartingDate() + ",'" + id
95 + "'," + job.getExecutionTime() + ",'" + ip + "', '" + finalstatus + "');";
96 session.execute(com5);
97 if (finalstatus.equals("TIMEDOUT"))
99 else if (finalstatus.equals("JPREDERROR"))
101 else if (finalstatus.equals("STOPPED"))
104 // update some internal query tables
105 String check3 = "SELECT * FROM MainParameters WHERE Name = 'EarliestJobDate';";
106 ResultSet results3 = session.execute(check3);
107 boolean updateparameter = true;
108 if (!results3.isExhausted()) {
109 Row r = results3.one();
110 if (job.getStartingDate() >= Long.parseLong(r.getString("Value")))
111 updateparameter = false;
113 if (updateparameter) {
114 String com = "INSERT INTO MainParameters " + "(Name, Value)" + " VALUES ('EarliestJobDate','" + job.getStartingDate()
116 session.execute(com);
118 String check4 = "SELECT * FROM JobDateInfo WHERE jobday = " + job.getStartingDate() + ";";
119 ResultSet results4 = session.execute(check4);
120 updateparameter = true;
122 if (!results4.isExhausted()) {
123 Row r = results4.one();
124 njobs += r.getLong("Total");
125 njobsOk += r.getLong("TotalOK");
126 njobsError += r.getLong("TotalError");
127 njobsStop += r.getLong("TotalStopped");
128 njobsTimeOut += r.getLong("TotalTimeOut");
130 String com = "INSERT INTO JobDateInfo " + "(jobday, Total, TotalOK, TotalStopped, TotalError, TotalTimeOut)" + " VALUES ("
131 + job.getStartingDate() + "," + njobs + "," + njobsOk + "," + njobsStop + "," + njobsError + "," + njobsTimeOut + ");";
132 session.execute(com);
139 * insert data from a real Jpred job: timing+IP, Execution Status, Final
140 * status, protein sequence, predictions, alignment, LOG and tar.gz files
142 public int ArchiveData(JpredJob job, String archivepath) {
143 if (JobisNotArchived(job.getJobID())) {
144 String id = job.getJobID();
145 String log = job.getLog().replaceAll("'", "");
146 String com = "INSERT INTO JpredArchive (JobID, Protein, IP, StartTime, ExecTime, FinalStatus, ExecutionStatus, LOG, ArchiveLink) VALUES ('" + id + "','"
147 + job.getProtein() + "','" + job.getIP() + "'," + job.getStartingTime() + "," + job.getExecutionTime() + ",'" + job.getFinalStatus() + "','"
148 + job.getExecutionStatus() + "','" + log + "','" + archivepath + "');";
149 session.execute(com);
150 List<FastaSequence> predictions = job.getPredictions();
151 for (FastaSequence p : predictions) {
152 session.execute("UPDATE JpredArchive SET predictions = predictions + {'" + p.getId() + "':'"
153 + p.getSequence().replaceAll("\n", "") + "'} WHERE JobID = '" + id + "';");
156 List<FastaSequence> seqs = job.getAlignment();
157 for (FastaSequence s : seqs) {
158 session.execute("UPDATE JpredArchive SET alignment = alignment + {'" + s.getId() + "':'"
159 + s.getSequence().replaceAll("\n", "") + "'} WHERE JobID = '" + id + "';");