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.util.ArrayList;
13 import java.util.Calendar;
14 import java.util.Date;
15 import java.util.List;
17 import compbio.cassandra.JpredParser;
18 import compbio.data.sequence.FastaReader;
19 import compbio.data.sequence.FastaSequence;
20 import compbio.engine.JpredJob;
22 public class JpredParserHTTP implements JpredParser {
23 private CassandraWriter cw = new CassandraWriter();
24 private String dirprefix;
25 private List<FastaSequence> alignment;
26 private List<FastaSequence> predictions;
27 private int countNoData;
29 public JpredParserHTTP() {
30 dirprefix = "http://www.compbio.dundee.ac.uk/www-jpred/results";
33 public JpredParserHTTP(String sourceurl) {
34 dirprefix = sourceurl;
37 public void setSource(String newsourceprefix) {
38 dirprefix = newsourceprefix;
41 public void Parsing(String source, int nDays) throws IOException {
42 Calendar cal = Calendar.getInstance();
43 cal.add(Calendar.DATE, -nDays);
44 for (int i = 0; i < nDays; ++i) {
45 cal.add(Calendar.DATE, 1);
46 String date = cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DATE);
47 ParsingForDate(source, date);
52 * The method parses the Jpred output concise file in the FASTA format If
53 * there is a record with ID = QUERY or jobid, this a "one protein" job
54 * otherwise this is an alignment job
56 private String parsePredictions(final InputStream stream, String jobid) throws FileNotFoundException {
57 final FastaReader fr = new FastaReader(stream);
59 alignment = new ArrayList<FastaSequence>();
60 predictions = new ArrayList<FastaSequence>();
61 while (fr.hasNext()) {
62 final FastaSequence fs = fr.next();
63 String seqid = fs.getId();
64 String seq = fs.getSequence().replaceAll("\n", "");
65 if (seqid.equals("jnetpred") || seqid.equals("Lupas_21") || seqid.equals("Lupas_14") || seqid.equals("Lupas_28")
66 || seqid.equals("JNETSOL25") || seqid.equals("JNETSOL5") || seqid.equals("JNETSOL0") || seqid.equals("JNETCONF")
67 || seqid.equals("JNETHMM") || seqid.equals("JNETPSSM") || seqid.equals("JNETCONF")) {
71 if (seqid.equals("QUERY") || seqid.equals(jobid))
78 private String parseLogFile(final InputStream stream) throws IOException {
80 BufferedReader buffer = new BufferedReader(new InputStreamReader(stream));
82 while (null != (line = buffer.readLine())) {
88 private int analyseJob(String[] jobinfo) throws IOException {
89 boolean running = true;
90 boolean ConcisefileExists = false;
91 boolean LogfileExists = false;
92 JpredJob job = new JpredJob (jobinfo[jobinfo.length - 1], jobinfo[0], jobinfo[1]);
93 job.setIP(jobinfo[2]);
94 Date currDate = new Date();
95 String maindir = dirprefix + "/" + job.getJobID() + "/";
97 //System.out.println("analyzing job " + job.getJobID());
99 URL dirurl = new URL(maindir);
100 HttpURLConnection httpConnection_dirurl = (HttpURLConnection) dirurl.openConnection();
101 if (httpConnection_dirurl.getResponseCode() < 199 || 300 <= httpConnection_dirurl.getResponseCode()) {
104 URL conciseurl = new URL(maindir + job.getJobID() + ".concise.fasta");
105 URL archiveurl = new URL(maindir + job.getJobID() + ".tar.gz");
106 URL logurl = new URL(maindir + "LOG");
107 HttpURLConnection httpConnection_conciseurl = (HttpURLConnection) conciseurl.openConnection();
108 HttpURLConnection httpConnection_logurl = (HttpURLConnection) logurl.openConnection();
109 HttpURLConnection httpConnection_archiveurl = (HttpURLConnection) archiveurl.openConnection();
110 if (199 < httpConnection_conciseurl.getResponseCode() && httpConnection_conciseurl.getResponseCode() < 300) {
111 ConcisefileExists = true;
114 job.setProtein(parsePredictions(conciseurl.openStream(), job.getJobID()));
115 } catch (IOException e) {
119 // The job still can be running of failed...
122 if (199 < httpConnection_logurl.getResponseCode() && httpConnection_logurl.getResponseCode() < 300) {
123 LogfileExists = true;
124 job.setLog(parseLogFile(logurl.openStream()));
126 // The job has not been started at all...
127 job.setExecutionStatus("FAIL");
128 job.setFinalStatus("STOPPED");
131 if (job.getLog().matches("(.*)TIMEOUT\\syour\\sjob\\stimed\\sout(.*)")) {
132 // blast job was too long (more than 3600 secs by default)...
133 job.setExecutionStatus("FAIL");
134 job.setFinalStatus("TIMEDOUT");
136 } else if (job.getLog().matches("(.*)Jpred\\serror:\\sDied\\sat(.*)")) {
137 // an internal Jpred error...
138 job.setExecutionStatus("FAIL");
139 job.setFinalStatus("JPREDERROR");
141 } else if ((currDate.getTime() - job.getEndTime()) / 1000 > 3601 && LogfileExists && !ConcisefileExists) {
142 // the job was stopped with unknown reason...
143 job.setExecutionStatus("FAIL");
144 job.setFinalStatus("STOPPED");
148 httpConnection_conciseurl.disconnect();
149 httpConnection_logurl.disconnect();
150 httpConnection_archiveurl.disconnect();
151 } catch (MalformedURLException e) {
156 job.setAlignment(alignment);
157 job.setPredictions(predictions);
158 cw.FormQueryTables(job);
159 cw.ArchiveData(job, "undefined");
166 private void ParsingForDate(String input, String date) {
168 int countinsertions = 0;
169 int countinserted = 0;
170 int countNotanalyzed = 0;
173 System.out.println("Inserting jobs for " + date);
175 URL url = new URL(input);
176 URLConnection conn = url.openConnection();
177 BufferedReader alljobs = new BufferedReader(new InputStreamReader(conn.getInputStream()));
180 while ((line = alljobs.readLine()) != null) {
181 if (line.matches(date + ":(.*)jp_[^\\s]+")) {
183 String[] job = line.split("\\s+");
184 String jobid = job[job.length - 1];
185 if (cw.JobisNotInsterted(jobid)) {
186 countinsertions += analyseJob(job);
195 System.out.println("Total number of jobs = " + totalcount);
196 System.out.println(" " + countinserted + " jobs inserted already");
197 System.out.println(" " + countNotanalyzed + " not analysed jobs");
198 System.out.println(" " + countNoData + " jobs without *.concise.fasta file (RUNNING or FAILED)");
199 System.out.println(" " + countinsertions + " new job insertions\n");
200 } catch (MalformedURLException e) {
202 } catch (IOException e) {