592016e2dbacfbfd76b4d129c5ed87be14c6213f
[proteocache.git] / datadb / compbio / cassandra / CassandraWriter.java
1 package compbio.cassandra;
2
3 import java.util.List;
4
5 import org.apache.log4j.Logger;
6
7 import com.datastax.driver.core.Row;
8 import com.datastax.driver.core.Session;
9 import com.datastax.driver.core.ResultSet;
10
11 import compbio.engine.JpredJob;
12 import compbio.engine.ProteoCachePropertyHelperManager;
13 import compbio.util.PropertyHelper;
14
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);
19
20         CassandraWriter() {
21                 Session inis = CassandraNativeConnector.getSession();
22                 setSession(inis);
23         }
24
25         public void setSession(Session s) {
26                 assert s != null;
27                 session = s;
28         }
29
30         public boolean JobisNotInsterted(String jobid) {
31                 ResultSet results1 = session.execute("SELECT * FROM ProteinLog WHERE JobID = '" + jobid + "';");
32                 if (results1.isExhausted()) {
33                         return true;
34                 }
35                 return false;
36         }
37
38         public boolean JobisNotArchived(String jobid) {
39                 ResultSet results1 = session.execute("SELECT * FROM JpredArchive WHERE JobID = '" + jobid + "';");
40                 if (results1.isExhausted()) {
41                         return true;
42                 }
43                 return false;
44         }
45
46         /*
47          * inserting data into the tables for queries
48          */
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);
60
61                         String com2 = "INSERT INTO ProteinData " + "(jobtime, JobID, ExecTime, Protein)" + " VALUES (" + job.getStartingDate() + ",'" + id
62                                         + "'," + job.getExecutionTime() + ",'" + protein + "');";
63                         session.execute(com2);
64
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 + "',";
71                         }
72                         String final_prediction = "";
73                         if (!allpredictions.equals("")) {
74                                 final_prediction = allpredictions.substring(0, allpredictions.length() - 1);
75                         }
76
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);
83                         }
84
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;
93                         }
94                         if (updateparameter) {
95                                 String com = "INSERT INTO MainParameters " + "(Name, Value)" + " VALUES ('EarliestJobDate','" + job.getStartingDateStr()
96                                                 + "');";
97                                 session.execute(com);
98                         }
99                         String check4 = "SELECT * FROM JobDateInfo WHERE jobday = " + job.getStartingDate() + ";";
100                         ResultSet results4 = session.execute(check4);
101                         updateparameter = true;
102                         int njobs = 1;
103                         if (!results4.isExhausted()) {
104                                 Row r = results4.one();
105                                 njobs += r.getLong("Total");
106                         }
107                         String com = "INSERT INTO JobDateInfo " + "(jobday, Total)" + " VALUES (" + job.getStartingDate() + "," + njobs + ");";
108                         session.execute(com);
109
110                         return 1;
111                 }
112                 return 0;
113         }
114
115         /*
116          * insert data from a real Jpred job: timing+IP, Execution Status, Final
117          * status, protein sequence, predictions, alignment, LOG and tar.gz files
118          */
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, FinalStatus, LOG, ArchiveLink) VALUES ('" + id + "','"
124                                         + job.getProtein() + "','" + job.getIP() + "'," + job.getStartingTime() + "," + job.getExecutionTime() + ",'" + job.getFinalStatus() + "','" + log
125                                         + "','" + archivepath + "');";
126                         session.execute(com);
127
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 + "';");
132                         }
133
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 + "';");
138                         }
139                         return 1;
140                 }
141                 return 0;
142         }
143
144 }