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.engine.JpredJob;
12 import compbio.engine.ProteoCachePropertyHelperManager;
13 import compbio.util.PropertyHelper;
15 public class CassandraWriter {
16 private Session session;
17 private static final PropertyHelper ph = ProteoCachePropertyHelperManager.getPropertyHelper();
18 private static Logger log = Logger.getLogger(CassandraNativeConnector.class);
21 Session inis = CassandraNativeConnector.getSession();
25 public void setSession(Session s) {
30 public boolean JobisNotInsterted(String jobid) {
31 ResultSet results1 = session.execute("SELECT * FROM ProteinLog WHERE JobID = '" + jobid + "';");
32 if (results1.isExhausted()) {
38 public boolean JobisNotArchived(String jobid) {
39 ResultSet results1 = session.execute("SELECT * FROM JpredArchive WHERE JobID = '" + jobid + "';");
40 if (results1.isExhausted()) {
47 * inserting data into the tables for queries
49 public int FormQueryTables(JpredJob job) {
50 if (JobisNotInsterted(job.getJobID())) {
51 String id = job.getJobID();
52 String ip = job.getIP();
53 String protein = job.getProtein();
54 String finalstatus = job.getFinalStatus();
55 String execstatus = job.getExecutionStatus();
56 String com1 = "INSERT INTO ProteinLog " + "(JobID, IP, DataBegin, DataEnd, FinalStatus, ExecutionStatus, Protein)"
57 + " VALUES ('" + id + "','" + ip + "','" + job.getStartingTimeStr() + "','" + job.getEndTimeStr() + "','" + finalstatus
58 + "','" + execstatus + "','" + protein + "');";
59 session.execute(com1);
61 String com2 = "INSERT INTO ProteinData " + "(jobtime, JobID, Protein)" + " VALUES (" + job.getStartingDate() + ",'" + id
62 + "','" + protein + "');";
63 session.execute(com2);
65 String allpredictions = "";
66 List<FastaSequence> pr = job.getPredictions();
67 for (FastaSequence pred : pr) {
68 String predictionname = pred.getId();
69 String prediction = pred.getSequence().replaceAll("\n", "");
70 allpredictions += "'" + predictionname + "':'" + prediction + "',";
72 String final_prediction = "";
73 if (!allpredictions.equals("")) {
74 final_prediction = allpredictions.substring(0, allpredictions.length() - 1);
77 String check2 = "SELECT * FROM ProteinRow WHERE JobID = '" + job.getJobID() + "';";
78 ResultSet results2 = session.execute(check2);
79 if (results2.isExhausted()) {
80 String com3 = "INSERT INTO ProteinRow " + "(Protein, JobID, Predictions)" + " VALUES ('" + protein + "','" + id + "',{"
81 + final_prediction + "});";
82 session.execute(com3);
85 // update some internal query tables
86 String check3 = "SELECT * FROM MainParameters WHERE Name = 'EarliestJobDate';";
87 ResultSet results3 = session.execute(check3);
88 boolean updateparameter = true;
89 if (!results3.isExhausted()) {
90 Row r = results3.one();
91 if (job.getStartingDate() >= Long.parseLong(r.getString("Value")))
92 updateparameter = false;
94 if (updateparameter) {
95 String com = "INSERT INTO MainParameters " + "(Name, Value)" + " VALUES ('EarliestJobDate','" + job.getStartingDateStr()
99 String check4 = "SELECT * FROM JobDateInfo WHERE jobday = " + job.getStartingDate() + ";";
100 ResultSet results4 = session.execute(check4);
101 updateparameter = true;
103 if (!results4.isExhausted()) {
104 Row r = results4.one();
105 njobs += r.getLong("Total");
107 String com = "INSERT INTO JobDateInfo " + "(jobday, Total)" + " VALUES (" + job.getStartingDate() + "," + njobs + ");";
108 session.execute(com);
116 * insert data from a real Jpred job: timing+IP, Execution Status, Final
117 * status, protein sequence, predictions, alignment, LOG and tar.gz files
119 public int ArchiveData(JpredJob job, String archivepath) {
120 if (JobisNotArchived(job.getJobID())) {
121 String id = job.getJobID();
122 String log = job.getLog().replaceAll("'", "");
123 String com = "INSERT INTO JpredArchive (JobID, Protein, IP, StartTime, ExecTime,LOG, ArchiveLink) VALUES ('" + id + "','"
124 + job.getProtein() + "','" + job.getIP() + "'," + job.getStartingTime() + "," + job.getExecutionTime() + ",'" + log
125 + "','" + archivepath + "');";
126 session.execute(com);
128 List<FastaSequence> predictions = job.getPredictions();
129 for (FastaSequence p : predictions) {
130 session.execute("UPDATE JpredArchive SET predictions = predictions + {'" + p.getId() + "':'"
131 + p.getSequence().replaceAll("\n", "") + "'} WHERE JobID = '" + id + "';");
134 List<FastaSequence> seqs = job.getAlignment();
135 for (FastaSequence s : seqs) {
136 session.execute("UPDATE JpredArchive SET alignment = alignment + {'" + s.getId() + "':'"
137 + s.getSequence().replaceAll("\n", "") + "'} WHERE JobID = '" + id + "';");