1 package compbio.cassandra;
3 import java.io.IOException;
6 import org.apache.log4j.Logger;
8 import com.datastax.driver.core.Row;
9 import com.datastax.driver.core.Session;
10 import com.datastax.driver.core.ResultSet;
11 import com.datastax.driver.core.PreparedStatement;
12 import com.datastax.driver.core.BoundStatement;
14 import compbio.engine.ProteoCachePropertyHelperManager;
15 import compbio.util.PropertyHelper;
17 public class CassandraWriter {
18 private Session session;
19 private static final PropertyHelper ph = ProteoCachePropertyHelperManager.getPropertyHelper();
20 private static Logger log = Logger.getLogger(CassandraNativeConnector.class);
23 Session inis = CassandraNativeConnector.getSession();
27 public void setSession(Session s) {
32 public boolean JobisNotInsterted(String jobid) {
33 ResultSet results1 = session.execute("SELECT * FROM ProteinLog WHERE JobID = '" + jobid + "';");
34 if (results1.isExhausted()) {
40 public boolean JobisNotArchived(String jobid) {
41 ResultSet results1 = session.execute("SELECT * FROM JpredArchive WHERE JobID = '" + jobid + "';");
42 if (results1.isExhausted()) {
49 * inserting data into the tables for queries
51 public int FormQueryTables(long jobtime, String startdate, String enddate, String ip, String jobid, String statusEx,
52 String statusFinal, String protein, List<FastaSequence> predictions) {
53 if (JobisNotInsterted(jobid)) {
54 String com1 = "INSERT INTO ProteinLog " + "(JobID, IP, DataBegin, DataEnd, FinalStatus, ExecutionStatus, Protein)"
55 + " VALUES ('" + jobid + "','" + ip + "','" + startdate + "','" + enddate + "','" + statusFinal + "','" + statusEx
56 + "','" + protein + "');";
57 session.execute(com1);
59 String com2 = "INSERT INTO ProteinData " + "(jobtime, JobID, Protein)" + " VALUES (" + jobtime + ",'" + jobid + "','" + protein
61 session.execute(com2);
63 String allpredictions = "";
64 for (FastaSequence pred : predictions) {
65 String predictionname = pred.getId();
66 String prediction = pred.getSequence().replaceAll("\n", "");
67 allpredictions += "'" + predictionname + "':'" + prediction + "',";
69 String final_prediction = "";
70 if (null != allpredictions) {
71 final_prediction = allpredictions.substring(0, allpredictions.length() - 1);
74 String check2 = "SELECT * FROM ProteinRow WHERE JobID = '" + jobid + "';";
75 ResultSet results2 = session.execute(check2);
76 if (results2.isExhausted()) {
77 String com3 = "INSERT INTO ProteinRow " + "(Protein, JobID, Predictions)" + " VALUES ('" + protein + "','" + jobid + "',{"
78 + final_prediction + "});";
79 session.execute(com3);
82 // update some internal tables
83 String check3 = "SELECT * FROM MainParameters WHERE Name = 'EarliestJobDate';";
84 ResultSet results3 = session.execute(check3);
85 boolean updateparameter = true;
86 if (!results3.isExhausted()) {
87 Row r = results3.one();
88 if (jobtime >= Long.parseLong(r.getString("Value")))
89 updateparameter = false;
91 if (updateparameter) {
92 String com = "INSERT INTO MainParameters " + "(Name, Value)" + " VALUES ('EarliestJobDate','" + String.valueOf(jobtime)
96 String check4 = "SELECT * FROM MainParameters WHERE Name = 'TotalNumberOfJobs';";
97 ResultSet results4 = session.execute(check4);
98 updateparameter = true;
100 if (!results4.isExhausted()) {
101 Row r = results4.one();
102 njobs += Integer.parseInt(r.getString("Value"));
104 String com = "INSERT INTO MainParameters " + "(Name, Value)" + " VALUES ('TotalNumberOfJobs','" + String.valueOf(njobs) + "');";
105 session.execute(com);
113 * insert data from a real Jpred job: timing+IP, Execution Status, Final
114 * status, protein sequence, predictions, alignment, LOG and tar.gz files
116 public int ArchiveData(long starttime, long exectime, String ip, String jobid, String statusEx, String statusFinal, String protein,
117 List<FastaSequence> predictions, List<FastaSequence> seqs, String LogFile, String archivepath) {
118 if (JobisNotArchived(jobid)) {
119 String log = LogFile.replaceAll("'", "");
120 session.execute("INSERT INTO JpredArchive (JobID, Protein, IP, StartTime, ExecTime,LOG) VALUES ('" + jobid + "','" + protein
121 + "','" + ip + "'," + starttime + "," + exectime + ",'" + log + "');");
123 PreparedStatement statement = session.prepare("INSERT INTO JpredArchive (JobID, archive) VALUES (?,?);");
124 BoundStatement boundStatement = new BoundStatement(statement);
125 session.execute(boundStatement.bind(jobid, archivepath));
128 for (FastaSequence p : predictions) {
129 session.execute("UPDATE JpredArchive SET predictions = predictions + {'" + p.getId() + "':'"
130 + p.getSequence().replaceAll("\n", "") + "'} WHERE JobID = '" + jobid + "';");
133 for (FastaSequence s : seqs) {
134 session.execute("UPDATE JpredArchive SET alignment = alignment + {'" + s.getId() + "':'"
135 + s.getSequence().replaceAll("\n", "") + "'} WHERE JobID = '" + jobid + "';");