3688fb674cb4901777d88d224d9dac99f530295e
[proteocache.git] / JpredWS.java
1 package compbio.ws.jpred;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import javax.jws.WebService;
8
9 import org.apache.log4j.Logger;
10
11 import compbio.beans.ProteinBean;
12 import compbio.cassandra.CassandraNativeConnector;
13 import compbio.cassandra.readers.JobReader;
14 import compbio.cassandra.readers.SequenceReader;
15 import compbio.engine.archive.ArchivedJob;
16
17 /**
18  * Implementation of the Jpred web service in ProteoCache
19  * 
20  * @author Alexander Sherstnev
21  * @author Natasha Sherstneva
22  * 
23  * @version 1.0
24  * @since Jan 2014
25  */
26 @WebService(endpointInterface = "compbio.ws.jpred.Jpred", targetNamespace = "http://server.proteocache.ws", serviceName = "ProteoCacheWS", portName = "ProteoCacheWSPort")
27 public class JpredWS implements Jpred {
28         private static Logger log = Logger.getLogger(CassandraNativeConnector.class);
29         String jobid;
30
31         private List<String> findJobs(String sequence, String program, String version) {
32                 CassandraNativeConnector dbconnector = new CassandraNativeConnector();
33                 SequenceReader reader = new SequenceReader();
34                 reader.setSession(dbconnector.getSession());
35                 log.debug("ProteoCacheWS is connected:\n search for sequence: " + sequence + "\ncalculated with  " + program + " (version: "
36                                 + version + ")");
37                 List<String> jobs = new ArrayList<String>();
38                 List<ProteinBean> result = reader.readProteins(sequence, "whole");
39                 if (null != result) {
40                         for (ProteinBean protein : result) {
41                                 List<String> thejobs = protein.getJobid();
42                                 jobid = thejobs.get(0);
43                                 for (String job : thejobs) {
44                                         jobs.add(job);
45                                 }
46                         }
47                 }
48                 return jobs;
49         }
50
51         @Override
52         public int findSequence(String sequence, String program, String version) {
53                 List<String> jobs = findJobs(sequence, program, version);
54                 if (null != jobs) {
55                         return jobs.size();
56                 }
57                 return 0;
58         }
59
60         @Override
61         public String findJobForSequence(String sequence, String program, String version) {
62                 List<String> jobs = findJobs(sequence, program, version);
63                 if (null != jobs) {
64                         for (String job : jobs) {
65                                 String link = null;
66                                 ArchivedJob aj = new ArchivedJob(job);
67                                 try {
68                                         link = aj.prepareJobArchiveToWeb();
69                                 } catch (IOException e) {
70                                         log.error("JpredWS.findSequence: IO exception with job archive file");
71                                         log.error(e.getLocalizedMessage(), e.getCause());
72                                 }
73                                 if (null != link) {
74                                         return job;
75                                 }
76                         }
77                         return "";
78                 }
79                 return "";
80         }
81
82         @Override
83         public String getArchive(String jobid) {
84                 String link = null;
85                 ArchivedJob aj = new ArchivedJob(jobid);
86                 try {
87                         link = aj.prepareJobArchiveToWeb();
88                 } catch (IOException e) {
89                         log.error("JpredWS.findSequence: IO exception with job archive file");
90                         log.error(e.getLocalizedMessage(), e.getCause());
91                 }
92                 // if the archive file is not available null is returned
93                 return link;
94         }
95
96         @Override
97         public int jobExists(String jobid) {
98                 CassandraNativeConnector dbconnector = new CassandraNativeConnector();
99                 JobReader reader = new JobReader();
100                 reader.setSession(dbconnector.getSession());
101                 if (null != reader.readJobLog(jobid)) {
102                         return 1;
103                 }
104                 return 0;
105         }
106
107 }