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 Date currDate = new Date();
139 String maindir = dirprefix + "/" + job.getJobID() + "/";
142 Date finishTime = timeformatter.parse(jobinfo[1]);
143 long delay = currDate.getTime() / 1000 - finishTime.getTime() / 1000;
144 if (delay < 120) return 0;
145 } catch (ParseException e) {
150 URL dirurl = new URL(maindir);
151 HttpURLConnection httpConnection_dirurl = (HttpURLConnection) dirurl.openConnection();
152 if (httpConnection_dirurl.getResponseCode() < 199 || 300 <= httpConnection_dirurl.getResponseCode()) {
155 URL conciseurl = new URL(maindir + job.getJobID() + ".concise.fasta");
156 URL logurl = new URL(maindir + "LOG");
157 HttpURLConnection httpConnection_conciseurl = (HttpURLConnection) conciseurl.openConnection();
158 HttpURLConnection httpConnection_logurl = (HttpURLConnection) logurl.openConnection();
159 if (199 < httpConnection_conciseurl.getResponseCode() && httpConnection_conciseurl.getResponseCode() < 300) {
160 ConcisefileExists = true;
163 job.setProtein(parsePredictions(conciseurl.openStream(), job.getJobID()));
164 } catch (IOException e) {
168 // The job still can be running of failed...
171 if (199 < httpConnection_logurl.getResponseCode() && httpConnection_logurl.getResponseCode() < 300) {
172 LogfileExists = true;
173 job.setLog(parseLogFile(logurl.openStream(), job));
175 // The job has not been started at all...
176 System.out.println ("WARNING! Job " + job.getJobID() + " has status FAIL/STOPPED");
177 job.setExecutionStatus("FAIL");
178 job.setFinalStatus("STOPPED");
181 if (job.getLog().matches("(.*)TIMEOUT\\syour\\sjob\\stimed\\sout(.*)")) {
182 // blast job was too long (more than 3600 secs by default)...
183 job.setExecutionStatus("FAIL");
184 job.setFinalStatus("TIMEDOUT");
186 } else if (job.getLog().matches("(.*)Jpred\\serror:\\sDied\\sat(.*)")) {
187 // an internal Jpred error...
188 job.setExecutionStatus("FAIL");
189 job.setFinalStatus("JPREDERROR");
191 } else if ((currDate.getTime() - job.getEndTime()) / 1000 > 3601 && LogfileExists && !ConcisefileExists) {
192 // the job was stopped with unknown reason...
193 job.setExecutionStatus("FAIL");
194 job.setFinalStatus("STOPPED");
198 httpConnection_conciseurl.disconnect();
199 httpConnection_logurl.disconnect();
200 } catch (MalformedURLException e) {
206 job.setAlignment(alignment);
207 job.setPredictions(predictions);
208 if (job.getExecutionStatus().equals("FAIL")) {
209 URL sequrl = new URL(maindir + job.getJobID() + ".seq");
210 HttpURLConnection httpConnection_sequrl = (HttpURLConnection) sequrl.openConnection();
211 if (199 < httpConnection_sequrl.getResponseCode() && httpConnection_sequrl.getResponseCode() < 300) {
213 job.setProtein(parseSeqFile(sequrl.openStream(), job.getJobID()));
214 } catch (IOException e) {
219 cw.FormQueryTables(job);
223 ArchivedJob ajob = new ArchivedJob(job.getJobID());
224 String arlink = archive.createJob(job.getJobID());
225 if (job.getFinalStatus().equals("OK")) {
226 ajob.setArchivePath(arlink);
227 ajob.copyArchiveFromWeb(maindir + job.getJobID() + ".tar.gz");
228 cw.ArchiveData(job, arlink);
230 cw.ArchiveData(job, "undefined");
239 private void ParsingOneDay(String input, String date) {
241 int countinsertions = 0;
242 int countinserted = 0;
243 int countNotanalyzed = 0;
246 System.out.println("Inserting jobs for " + date);
248 URL url = new URL(input);
249 URLConnection conn = url.openConnection();
250 BufferedReader alljobs = new BufferedReader(new InputStreamReader(conn.getInputStream()));
253 while ((line = alljobs.readLine()) != null) {
254 if (line.matches(date + ":(.*)jp_[^\\s]+")) {
256 String[] job = line.split("\\s+");
257 String jobid = job[job.length - 1];
258 if (cw.JobisNotInsterted(jobid)) {
259 countinsertions += analyseJob(job);
268 System.out.println("Total number of jobs = " + totalcount);
269 System.out.println(" " + countinserted + " jobs inserted already");
270 System.out.println(" " + countNotanalyzed + " not analysed jobs");
271 System.out.println(" " + countNoData + " jobs without *.concise.fasta file (RUNNING or FAILED)");
272 System.out.println(" " + countinsertions + " new job insertions\n");
273 } catch (MalformedURLException e) {
275 } catch (IOException e) {