DisEMBL and GlobProt web services further work
[jabaws.git] / runner / compbio / runner / disorder / GlobPlot.java
1 /*\r
2  * Copyright (c) 2011 Peter Troshin JAva Bioinformatics Analysis Web Services\r
3  * (JABAWS) @version: 2.0 This library is free software; you can redistribute it\r
4  * and/or modify it under the terms of the Apache License version 2 as published\r
5  * by the Apache Software Foundation This library is distributed in the hope\r
6  * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied\r
7  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
8  * Apache License for more details. A copy of the license is in\r
9  * apache_license.txt. It is also available here:\r
10  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt Any republication or\r
11  * derived work distributed in source code form must include this copyright and\r
12  * license notice.\r
13  */\r
14 \r
15 package compbio.runner.disorder;\r
16 \r
17 import java.io.File;\r
18 import java.io.FileInputStream;\r
19 import java.io.FileNotFoundException;\r
20 import java.io.IOException;\r
21 import java.io.InputStream;\r
22 import java.util.HashMap;\r
23 import java.util.HashSet;\r
24 \r
25 import org.apache.log4j.Logger;\r
26 \r
27 import compbio.data.sequence.Score;\r
28 import compbio.data.sequence.SequenceUtil;\r
29 import compbio.data.sequence.UnknownFileFormatException;\r
30 import compbio.engine.client.Executable;\r
31 import compbio.engine.client.PipedExecutable;\r
32 import compbio.engine.client.SkeletalExecutable;\r
33 import compbio.metadata.Limit;\r
34 import compbio.metadata.LimitsManager;\r
35 import compbio.metadata.ResultNotAvailableException;\r
36 import compbio.runner.Util;\r
37 \r
38 /**\r
39  * ./GlobPipe.py SmoothFrame DOMjoinFrame DOMpeakFrame DISjoinFrame DISpeakFrame\r
40  * \r
41  * FASTAfile' Optimised for ELM: ./GlobPlot.py 10 8 75 8 8 sequence_file'\r
42  * Webserver settings: ./GlobPlot.py 10 15 74 4 5 sequence_file'\r
43  * \r
44  * Hard-coded values are 10 15 74 4 5.\r
45  * \r
46  * Changing these values are not recommended by developers, apart from smoothing\r
47  * window. However, the binary, GlobPlot depends on - Tisean which is not happy\r
48  * with arbitrary changes to these values, so changing them can lead to\r
49  * problems. May be we can offer preset?\r
50  * \r
51  * This is not a standard GlobPlot! The script has been modified!\r
52  * \r
53  */\r
54 public class GlobPlot extends SkeletalExecutable<GlobPlot>\r
55                 implements\r
56                         PipedExecutable<GlobPlot> {\r
57 \r
58         private static Logger log = Logger.getLogger(GlobPlot.class);\r
59 \r
60         // Cache for Limits information\r
61         private static LimitsManager<GlobPlot> limits;\r
62 \r
63         public static final String KEY_VALUE_SEPARATOR = Util.SPACE;\r
64 \r
65         /* The parameter list there must not contain same values! */\r
66         public GlobPlot() {\r
67                 // remove default input to prevent it to appear in the parameters list\r
68                 // that could happen if the parameters are set first\r
69                 // super.setInput("");\r
70         }\r
71 \r
72         @SuppressWarnings("unchecked")\r
73         public HashMap<String, HashSet<Score>> getResults(String workDirectory)\r
74                         throws ResultNotAvailableException {\r
75 \r
76                 InputStream inStream = null;\r
77                 HashMap<String, HashSet<Score>> results = null;\r
78                 // How about getting ranges?\r
79                 try {\r
80                         inStream = new FileInputStream(new File(workDirectory, getOutput()));\r
81                         results = SequenceUtil.removeSequences(SequenceUtil\r
82                                         .readGlobPlot(inStream));\r
83                         inStream.close();\r
84                 } catch (FileNotFoundException e) {\r
85                         log.error(e.getMessage(), e.getCause());\r
86                         throw new ResultNotAvailableException(e);\r
87                 } catch (IOException e) {\r
88                         log.error(e.getMessage(), e.getCause());\r
89                         throw new ResultNotAvailableException(e);\r
90                 } catch (UnknownFileFormatException e) {\r
91                         log.error(e.getMessage(), e.getCause());\r
92                         throw new ResultNotAvailableException(e);\r
93                 } catch (NullPointerException e) {\r
94                         log.error(e.getMessage(), e.getCause());\r
95                         throw new ResultNotAvailableException(e);\r
96                 }\r
97 \r
98                 return results;\r
99         }\r
100 \r
101         @Override\r
102         public GlobPlot setInput(String inFile) {\r
103                 super.setInput(inFile);\r
104                 cbuilder.setLast(inFile);\r
105                 return this;\r
106         }\r
107 \r
108         @Override\r
109         public Limit<GlobPlot> getLimit(String presetName) {\r
110                 if (limits == null) {\r
111                         limits = getLimits();\r
112                 }\r
113 \r
114                 Limit<GlobPlot> limit = null;\r
115                 if (limits != null) {\r
116                         // this returns default limit if preset is undefined!\r
117                         limit = limits.getLimitByName(presetName);\r
118                 }\r
119                 // If limit is not defined for a particular preset, then return default\r
120                 // limit\r
121                 if (limit == null) {\r
122                         log.debug("Limit for the preset " + presetName\r
123                                         + " is not found. Using default");\r
124                         limit = limits.getDefaultLimit();\r
125                 }\r
126                 return limit;\r
127         }\r
128 \r
129         @Override\r
130         public LimitsManager<GlobPlot> getLimits() {\r
131                 // synchronise on static field\r
132                 synchronized (log) {\r
133                         if (limits == null) {\r
134                                 limits = Util.getLimits(this.getClass());\r
135                         }\r
136                 }\r
137                 return limits;\r
138         }\r
139 \r
140         @Override\r
141         public Class<? extends Executable<?>> getType() {\r
142                 return this.getClass();\r
143         }\r
144 \r
145 }\r