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.data.sequence.FastaSequence;
15 import compbio.engine.JpredJob;
16 import compbio.engine.ProteoCachePropertyHelperManager;
17 import compbio.util.PropertyHelper;
19 public class CassandraWriter {
20 private Session session;
21 private static final PropertyHelper ph = ProteoCachePropertyHelperManager.getPropertyHelper();
22 private static Logger log = Logger.getLogger(CassandraNativeConnector.class);
25 Session inis = CassandraNativeConnector.getSession();
29 public void setSession(Session s) {
34 public boolean JobisNotInsterted(String jobid) {
35 ResultSet results1 = session.execute("SELECT * FROM ProteinLog WHERE JobID = '" + jobid + "';");
36 if (results1.isExhausted()) {
42 public boolean JobisNotArchived(String jobid) {
43 ResultSet results1 = session.execute("SELECT * FROM JpredArchive WHERE JobID = '" + jobid + "';");
44 if (results1.isExhausted()) {
51 * inserting data into the tables for queries
53 public int FormQueryTables(JpredJob job) {
54 if (JobisNotInsterted(job.getJobID())) {
59 String id = job.getJobID();
60 String ip = job.getIP();
61 String protein = job.getProtein();
62 String finalstatus = job.getFinalStatus();
63 String execstatus = job.getExecutionStatus();
64 String com1 = "INSERT INTO ProteinLog " + "(JobID, IP, DataBegin, DataEnd, FinalStatus, ExecutionStatus, Protein)"
65 + " VALUES ('" + id + "','" + ip + "','" + job.getStartingTimeStr() + "','" + job.getEndTimeStr() + "','" + finalstatus
66 + "','" + execstatus + "','" + protein + "');";
67 session.execute(com1);
68 if (execstatus.equals("OK")) {
69 String com2 = "INSERT INTO ProteinData " + "(jobtime, JobID, ExecTime, Protein)" + " VALUES (" + job.getStartingDate() + ",'" + id
70 + "'," + job.getExecutionTime() + ",'" + protein + "');";
71 session.execute(com2);
73 String allpredictions = "";
74 List<FastaSequence> pr = job.getPredictions();
75 for (FastaSequence pred : pr) {
76 String predictionname = pred.getId();
77 String prediction = pred.getSequence().replaceAll("\n", "");
78 allpredictions += "'" + predictionname + "':'" + prediction + "',";
80 String final_prediction = "";
81 if (!allpredictions.equals("")) {
82 final_prediction = allpredictions.substring(0, allpredictions.length() - 1);
85 String check2 = "SELECT * FROM ProteinRow WHERE JobID = '" + job.getJobID() + "';";
86 ResultSet results2 = session.execute(check2);
87 if (results2.isExhausted()) {
88 String com3 = "INSERT INTO ProteinRow " + "(Protein, JobID, Predictions)" + " VALUES ('" + protein + "','" + id + "',{"
89 + final_prediction + "});";
90 session.execute(com3);
94 String com5 = "INSERT INTO FailLog " + "(jobtime, JobID, ExecTime, ip, FinalStatus)" + " VALUES ("
95 + job.getStartingDate() + ",'" + id
96 + "'," + job.getExecutionTime() + ",'" + ip + "', '" + finalstatus + "');";
97 session.execute(com5);
98 if (finalstatus.equals("TIMEDOUT"))
100 else if (finalstatus.equals("JPREDERROR"))
102 else if (finalstatus.equals("STOPPED"))
105 // update some internal query tables
106 String check3 = "SELECT * FROM MainParameters WHERE Name = 'EarliestJobDate';";
107 ResultSet results3 = session.execute(check3);
108 boolean updateparameter = true;
109 if (!results3.isExhausted()) {
110 Row r = results3.one();
111 if (job.getStartingDate() >= Long.parseLong(r.getString("Value")))
112 updateparameter = false;
114 if (updateparameter) {
115 String com = "INSERT INTO MainParameters " + "(Name, Value)" + " VALUES ('EarliestJobDate','" + job.getStartingDate()
117 session.execute(com);
119 String check4 = "SELECT * FROM JobDateInfo WHERE jobday = " + job.getStartingDate() + ";";
120 ResultSet results4 = session.execute(check4);
121 updateparameter = true;
123 if (!results4.isExhausted()) {
124 Row r = results4.one();
125 njobs += r.getLong("Total");
126 njobsOk += r.getLong("TotalOK");
127 njobsError += r.getLong("TotalError");
128 njobsStop += r.getLong("TotalStopped");
129 njobsTimeOut += r.getLong("TotalTimeOut");
131 String com = "INSERT INTO JobDateInfo " + "(jobday, Total, TotalOK, TotalStopped, TotalError, TotalTimeOut)" + " VALUES ("
132 + job.getStartingDate() + "," + njobs + "," + njobsOk + "," + njobsStop + "," + njobsError + "," + njobsTimeOut + ");";
133 session.execute(com);
140 * insert data from a real Jpred job: timing+IP, Execution Status, Final
141 * status, protein sequence, predictions, alignment, LOG and tar.gz files
143 public int ArchiveData(JpredJob job, String archivepath) {
144 if (JobisNotArchived(job.getJobID())) {
145 String id = job.getJobID();
146 String log = job.getLog().replaceAll("'", "");
147 String com = "INSERT INTO JpredArchive (JobID, Protein, IP, StartTime, ExecTime, FinalStatus, ExecutionStatus, LOG, ArchiveLink) VALUES ('" + id + "','"
148 + job.getProtein() + "','" + job.getIP() + "'," + job.getStartingTime() + "," + job.getExecutionTime() + ",'" + job.getFinalStatus() + "','"
149 + job.getExecutionStatus() + "','" + log + "','" + archivepath + "');";
150 session.execute(com);
151 List<FastaSequence> predictions = job.getPredictions();
152 for (FastaSequence p : predictions) {
153 session.execute("UPDATE JpredArchive SET predictions = predictions + {'" + p.getId() + "':'"
154 + p.getSequence().replaceAll("\n", "") + "'} WHERE JobID = '" + id + "';");
157 List<FastaSequence> seqs = job.getAlignment();
158 for (FastaSequence s : seqs) {
159 session.execute("UPDATE JpredArchive SET alignment = alignment + {'" + s.getId() + "':'"
160 + s.getSequence().replaceAll("\n", "") + "'} WHERE JobID = '" + id + "';");