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 ip = job.getIP();
54 String protein = job.getProtein();
55 String finalstatus = job.getFinalStatus();
56 String execstatus = job.getExecutionStatus();
57 String com1 = "INSERT INTO ProteinLog " + "(JobID, IP, DataBegin, DataEnd, FinalStatus, ExecutionStatus, Protein)"
58 + " VALUES ('" + id + "','" + ip + "','" + job.getStartingTimeStr() + "','" + job.getEndTimeStr() + "','" + finalstatus
59 + "','" + execstatus + "','" + protein + "');";
60 session.execute(com1);
62 String com2 = "INSERT INTO ProteinData " + "(jobtime, JobID, Protein)" + " VALUES (" + job.getStartingDate() + ",'" + id
63 + "','" + 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 String check2 = "SELECT * FROM ProteinRow WHERE JobID = '" + job.getJobID() + "';";
79 ResultSet results2 = session.execute(check2);
80 if (results2.isExhausted()) {
81 String com3 = "INSERT INTO ProteinRow " + "(Protein, JobID, Predictions)" + " VALUES ('" + protein + "','" + id + "',{"
82 + final_prediction + "});";
83 session.execute(com3);
86 // update some internal query tables
87 String check3 = "SELECT * FROM MainParameters WHERE Name = 'EarliestJobDate';";
88 ResultSet results3 = session.execute(check3);
89 boolean updateparameter = true;
90 if (!results3.isExhausted()) {
91 Row r = results3.one();
92 if (job.getStartingDate() >= Long.parseLong(r.getString("Value")))
93 updateparameter = false;
95 if (updateparameter) {
96 String com = "INSERT INTO MainParameters " + "(Name, Value)" + " VALUES ('EarliestJobDate','" + job.getStartingDateStr()
100 String check4 = "SELECT * FROM JobDateInfo WHERE jobday = " + job.getStartingDate() + ";";
101 ResultSet results4 = session.execute(check4);
102 updateparameter = true;
104 if (!results4.isExhausted()) {
105 Row r = results4.one();
106 njobs += r.getLong("Total");
108 String com = "INSERT INTO JobDateInfo " + "(jobday, Total)" + " VALUES (" + job.getStartingDate() + "," + njobs + ");";
109 session.execute(com);
117 * insert data from a real Jpred job: timing+IP, Execution Status, Final
118 * status, protein sequence, predictions, alignment, LOG and tar.gz files
120 public int ArchiveData(JpredJob job, String archivepath) {
121 if (JobisNotArchived(job.getJobID())) {
122 String id = job.getJobID();
123 String log = job.getLog().replaceAll("'", "");
124 String com = "INSERT INTO JpredArchive (JobID, Protein, IP, StartTime, ExecTime,LOG, ArchiveLink) VALUES ('" + id + "','"
125 + job.getProtein() + "','" + job.getIP() + "'," + job.getStartingTime() + "," + job.getExecutionTime() + ",'" + log
126 + "','" + archivepath + "');";
127 session.execute(com);
129 List<FastaSequence> predictions = job.getPredictions();
130 for (FastaSequence p : predictions) {
131 session.execute("UPDATE JpredArchive SET predictions = predictions + {'" + p.getId() + "':'"
132 + p.getSequence().replaceAll("\n", "") + "'} WHERE JobID = '" + id + "';");
135 List<FastaSequence> seqs = job.getAlignment();
136 for (FastaSequence s : seqs) {
137 session.execute("UPDATE JpredArchive SET alignment = alignment + {'" + s.getId() + "':'"
138 + s.getSequence().replaceAll("\n", "") + "'} WHERE JobID = '" + id + "';");