1 package compbio.cassandra;
3 import java.io.BufferedReader;
4 import java.io.FileNotFoundException;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.InputStreamReader;
8 import java.net.HttpURLConnection;
9 import java.net.MalformedURLException;
11 import java.net.URLConnection;
12 import java.text.ParseException;
13 import java.text.SimpleDateFormat;
14 import java.util.ArrayList;
15 import java.util.Calendar;
16 import java.util.Date;
17 import java.util.List;
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
21 import compbio.cassandra.JpredParser;
22 import compbio.data.sequence.FastaReader;
23 import compbio.data.sequence.FastaSequence;
24 import compbio.engine.JpredJob;
25 import compbio.engine.ProteoCachePropertyHelperManager;
26 import compbio.engine.archive.Archive;
27 import compbio.engine.archive.ArchivedJob;
28 import compbio.util.PropertyHelper;
29 import compbio.util.Util;
31 public class JpredParserHTTP implements JpredParser {
32 private CassandraWriter cw = new CassandraWriter();
33 private static Archive archive;
34 private String dirprefix;
35 private List<FastaSequence> alignment;
36 private List<FastaSequence> predictions;
37 private int countNoData;
38 private static boolean archiving = false;
39 private static final PropertyHelper ph = ProteoCachePropertyHelperManager.getPropertyHelper();
40 static SimpleDateFormat timeformatter = new SimpleDateFormat("yyyy/MM/dd:H:m:s");
42 public JpredParserHTTP() {
43 dirprefix = "http://www.compbio.dundee.ac.uk/www-jpred/results";
46 public JpredParserHTTP(String sourceurl) {
47 dirprefix = sourceurl;
50 public void setSource(String newsourceprefix) {
51 dirprefix = newsourceprefix;
54 private boolean initBooleanValue(String key) {
56 String status = ph.getProperty(key);
57 if (Util.isEmpty(status)) {
60 return new Boolean(status.trim()).booleanValue();
63 public void Parsing(String source, int nDays) throws IOException {
64 Calendar cal = Calendar.getInstance();
65 cal.add(Calendar.DATE, -nDays);
66 archiving = initBooleanValue("archive.enable");
68 archive = new Archive();
70 for (int i = 0; i < nDays; ++i) {
71 cal.add(Calendar.DATE, 1);
72 String date = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
73 ParsingOneDay(source, date);
78 * The method parses the Jpred output concise file in the FASTA format If
79 * there is a record with ID = QUERY or jobid, this a "one protein" job
80 * otherwise this is an alignment job
82 private String parsePredictions(final InputStream stream, String jobid) throws FileNotFoundException {
83 final FastaReader fr = new FastaReader(stream);
85 while (fr.hasNext()) {
86 final FastaSequence fs = fr.next();
87 String seqid = fs.getId();
88 String seq = fs.getSequence().replaceAll("\n", "");
89 if (seqid.equals("jnetpred") || seqid.equals("Lupas_21") || seqid.equals("Lupas_14") || seqid.equals("Lupas_28")
90 || seqid.equals("JNETSOL25") || seqid.equals("JNETSOL5") || seqid.equals("JNETSOL0") || seqid.equals("JNETCONF")
91 || seqid.equals("JNETHMM") || seqid.equals("JNETPSSM") || seqid.equals("JNETCONF")) {
95 if (seqid.equals("QUERY") || seqid.equals(jobid))
102 private String parseSeqFile(final InputStream stream, String jobid) throws FileNotFoundException {
103 final FastaReader fr = new FastaReader(stream);
105 final FastaSequence fs = fr.next();
106 protein = fs.getSequence().replaceAll("\n", "");
108 // this is an aligment job...
114 private String parseLogFile(final InputStream stream, JpredJob job) throws IOException {
116 BufferedReader buffer = new BufferedReader(new InputStreamReader(stream));
118 if (null != (out = buffer.readLine()) && (out.contains("version"))) {
119 Matcher matcher = Pattern.compile("((\\d|\\.)+)").matcher(out);
121 job.setProgramVersion(matcher.group(0));
123 while (null != (line = buffer.readLine())) {
129 private int analyseJob(String[] jobinfo) throws IOException {
130 alignment = new ArrayList<FastaSequence>();
131 predictions = new ArrayList<FastaSequence>();
132 boolean running = true;
133 boolean ConcisefileExists = false;
134 boolean LogfileExists = false;
135 JpredJob job = new JpredJob(jobinfo[jobinfo.length - 1], jobinfo[0], jobinfo[1]);
136 job.setIP(jobinfo[2]);
137 job.setProgramName("Jpred");
138 job.setProgramVersion("3.0.1");
139 Date currDate = new Date();
140 String maindir = dirprefix + "/" + job.getJobID() + "/";
143 Date finishTime = timeformatter.parse(jobinfo[1]);
144 long delay = currDate.getTime() / 1000 - finishTime.getTime() / 1000;
145 if (delay < 120) return 0;
146 } catch (ParseException e) {
151 URL dirurl = new URL(maindir);
152 HttpURLConnection httpConnection_dirurl = (HttpURLConnection) dirurl.openConnection();
153 if (httpConnection_dirurl.getResponseCode() < 199 || 300 <= httpConnection_dirurl.getResponseCode()) {
156 URL conciseurl = new URL(maindir + job.getJobID() + ".concise.fasta");
157 URL logurl = new URL(maindir + "LOG");
158 HttpURLConnection httpConnection_conciseurl = (HttpURLConnection) conciseurl.openConnection();
159 HttpURLConnection httpConnection_logurl = (HttpURLConnection) logurl.openConnection();
160 if (199 < httpConnection_conciseurl.getResponseCode() && httpConnection_conciseurl.getResponseCode() < 300) {
161 ConcisefileExists = true;
164 job.setProtein(parsePredictions(conciseurl.openStream(), job.getJobID()));
165 } catch (IOException e) {
169 // The job still can be running of failed...
172 if (199 < httpConnection_logurl.getResponseCode() && httpConnection_logurl.getResponseCode() < 300) {
173 LogfileExists = true;
174 job.setLog(parseLogFile(logurl.openStream(), job));
176 // The job has not been started at all...
177 System.out.println ("WARNING! Job " + job.getJobID() + " has status FAIL/STOPPED");
178 job.setExecutionStatus("FAIL");
179 job.setFinalStatus("STOPPED");
182 if (job.getLog().matches("(.*)TIMEOUT\\syour\\sjob\\stimed\\sout(.*)")) {
183 // blast job was too long (more than 3600 secs by default)...
184 job.setExecutionStatus("FAIL");
185 job.setFinalStatus("TIMEDOUT");
187 } else if (job.getLog().matches("(.*)Jpred\\serror:\\sDied\\sat(.*)")) {
188 // an internal Jpred error...
189 job.setExecutionStatus("FAIL");
190 job.setFinalStatus("JPREDERROR");
192 } else if ((currDate.getTime() - job.getEndTime()) / 1000 > 3601 && LogfileExists && !ConcisefileExists) {
193 // the job was stopped with unknown reason...
194 job.setExecutionStatus("FAIL");
195 job.setFinalStatus("STOPPED");
199 httpConnection_conciseurl.disconnect();
200 httpConnection_logurl.disconnect();
201 } catch (MalformedURLException e) {
207 job.setAlignment(alignment);
208 job.setPredictions(predictions);
209 if (job.getExecutionStatus().equals("FAIL")) {
210 URL sequrl = new URL(maindir + job.getJobID() + ".seq");
211 HttpURLConnection httpConnection_sequrl = (HttpURLConnection) sequrl.openConnection();
212 if (199 < httpConnection_sequrl.getResponseCode() && httpConnection_sequrl.getResponseCode() < 300) {
214 job.setProtein(parseSeqFile(sequrl.openStream(), job.getJobID()));
215 } catch (IOException e) {
220 cw.FormQueryTables(job);
224 ArchivedJob ajob = new ArchivedJob(job.getJobID());
225 String arlink = archive.createJob(job.getJobID());
226 if (job.getFinalStatus().equals("OK")) {
227 ajob.setArchivePath(arlink);
228 ajob.copyArchiveFromWeb(maindir + job.getJobID() + ".tar.gz");
229 cw.ArchiveData(job, arlink);
231 cw.ArchiveData(job, "undefined");
240 private void ParsingOneDay(String input, String date) {
242 int countinsertions = 0;
243 int countinserted = 0;
244 int countNotanalyzed = 0;
247 System.out.println("Inserting jobs for " + date);
249 URL url = new URL(input);
250 URLConnection conn = url.openConnection();
251 BufferedReader alljobs = new BufferedReader(new InputStreamReader(conn.getInputStream()));
254 while ((line = alljobs.readLine()) != null) {
255 if (line.matches(date + ":(.*)jp_[^\\s]+")) {
257 String[] job = line.split("\\s+");
258 String jobid = job[job.length - 1];
259 if (cw.JobisNotInsterted(jobid)) {
260 countinsertions += analyseJob(job);
269 System.out.println("Total number of jobs = " + totalcount);
270 System.out.println(" " + countinserted + " jobs inserted already");
271 System.out.println(" " + countNotanalyzed + " not analysed jobs");
272 System.out.println(" " + countNoData + " jobs without *.concise.fasta file (RUNNING or FAILED)");
273 System.out.println(" " + countinsertions + " new job insertions\n");
274 } catch (MalformedURLException e) {
276 } catch (IOException e) {