Remove internal FastaReader/FastaSequence and replace this with classes from JABAWS
[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.data.sequence.FastaSequence;
12 import compbio.engine.JpredJob;
13 import compbio.engine.ProteoCachePropertyHelperManager;
14 import compbio.util.PropertyHelper;
15
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);
20
21         CassandraWriter() {
22                 Session inis = CassandraNativeConnector.getSession();
23                 setSession(inis);
24         }
25
26         public void setSession(Session s) {
27                 assert s != null;
28                 session = s;
29         }
30
31         public boolean JobisNotInsterted(String jobid) {
32                 ResultSet results1 = session.execute("SELECT * FROM ProteinLog WHERE JobID = '" + jobid + "';");
33                 if (results1.isExhausted()) {
34                         return true;
35                 }
36                 return false;
37         }
38
39         public boolean JobisNotArchived(String jobid) {
40                 ResultSet results1 = session.execute("SELECT * FROM JpredArchive WHERE JobID = '" + jobid + "';");
41                 if (results1.isExhausted()) {
42                         return true;
43                 }
44                 return false;
45         }
46
47         /*
48          * inserting data into the tables for queries
49          */
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);
61
62                         String com2 = "INSERT INTO ProteinData " + "(jobtime, JobID, Protein)" + " VALUES (" + job.getStartingDate() + ",'" + id
63                                         + "','" + protein + "');";
64                         session.execute(com2);
65
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 + "',";
72                         }
73                         String final_prediction = "";
74                         if (!allpredictions.equals("")) {
75                                 final_prediction = allpredictions.substring(0, allpredictions.length() - 1);
76                         }
77
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);
84                         }
85
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;
94                         }
95                         if (updateparameter) {
96                                 String com = "INSERT INTO MainParameters " + "(Name, Value)" + " VALUES ('EarliestJobDate','" + job.getStartingDateStr()
97                                                 + "');";
98                                 session.execute(com);
99                         }
100                         String check4 = "SELECT * FROM JobDateInfo WHERE jobday = " + job.getStartingDate() + ";";
101                         ResultSet results4 = session.execute(check4);
102                         updateparameter = true;
103                         int njobs = 1;
104                         if (!results4.isExhausted()) {
105                                 Row r = results4.one();
106                                 njobs += r.getLong("Total");
107                         }
108                         String com = "INSERT INTO JobDateInfo " + "(jobday, Total)" + " VALUES (" + job.getStartingDate() + "," + njobs + ");";
109                         session.execute(com);
110
111                         return 1;
112                 }
113                 return 0;
114         }
115
116         /*
117          * insert data from a real Jpred job: timing+IP, Execution Status, Final
118          * status, protein sequence, predictions, alignment, LOG and tar.gz files
119          */
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);
128
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 + "';");
133                         }
134
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 + "';");
139                         }
140                         return 1;
141                 }
142                 return 0;
143         }
144
145 }