Fix copyright header
[jabaws.git] / runner / compbio / runner / disorder / Jronn.java
1 /* Copyright (c) 2009 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0     \r
4  * \r
5  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
6  *  Apache License version 2 as published by the Apache Software Foundation\r
7  * \r
8  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
9  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
10  *  License for more details.\r
11  * \r
12  *  A copy of the license is in apache_license.txt. It is also available here:\r
13  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 \r
19 package compbio.runner.disorder;\r
20 \r
21 import java.io.File;\r
22 import java.io.FileInputStream;\r
23 import java.io.FileNotFoundException;\r
24 import java.io.IOException;\r
25 import java.io.InputStream;\r
26 import java.util.Arrays;\r
27 import java.util.List;\r
28 \r
29 import org.apache.log4j.Logger;\r
30 \r
31 import compbio.data.sequence.AnnotatedSequence;\r
32 import compbio.data.sequence.SequenceUtil;\r
33 import compbio.data.sequence.UnknownFileFormatException;\r
34 import compbio.engine.client.Executable;\r
35 import compbio.engine.client.SkeletalExecutable;\r
36 import compbio.metadata.Limit;\r
37 import compbio.metadata.LimitsManager;\r
38 import compbio.metadata.ResultNotAvailableException;\r
39 import compbio.runner.Util;\r
40 \r
41 /**\r
42  * Command line\r
43  * \r
44  * java -Xmx512 -jar jronn_v3.jar -i=test_seq.txt -n=1 -o=out.txt -s=stat.out\r
45  * \r
46  * @author pvtroshin\r
47  * \r
48  */\r
49 public class Jronn extends SkeletalExecutable<Jronn> {\r
50 \r
51     private static Logger log = Logger.getLogger(Jronn.class);\r
52 \r
53     // Cache for Limits information\r
54     private static LimitsManager<Jronn> limits;\r
55 \r
56     public static final String KEY_VALUE_SEPARATOR = Util.SPACE;\r
57     public static final String STAT_FILE = "stat.txt";\r
58 \r
59     public Jronn() {\r
60         addParameters(Arrays.asList("-jar", getLibPath(), "-n=1", "-s="\r
61                 + STAT_FILE, "-f=H"));\r
62     }\r
63 \r
64     @SuppressWarnings("unchecked")\r
65     @Override\r
66     public List<AnnotatedSequence> getResults(String workDirectory)\r
67             throws ResultNotAvailableException {\r
68         List<AnnotatedSequence> sequences = null;\r
69         try {\r
70             InputStream inStream = new FileInputStream(new File(workDirectory,\r
71                     getOutput()));\r
72             sequences = SequenceUtil.readJRonn(inStream);\r
73             inStream.close();\r
74         } catch (FileNotFoundException e) {\r
75             log.error(e.getMessage(), e.getCause());\r
76             throw new ResultNotAvailableException(e);\r
77         } catch (IOException e) {\r
78             log.error(e.getMessage(), e.getCause());\r
79             throw new ResultNotAvailableException(e);\r
80         } catch (UnknownFileFormatException e) {\r
81             log.error(e.getMessage(), e.getCause());\r
82             throw new ResultNotAvailableException(e);\r
83         } catch (NullPointerException e) {\r
84             log.error(e.getMessage(), e.getCause());\r
85             throw new ResultNotAvailableException(e);\r
86         }\r
87         return sequences;\r
88     }\r
89 \r
90     private static String getLibPath() {\r
91 \r
92         String settings = ph.getProperty("jronn.jar.file");\r
93         if (compbio.util.Util.isEmpty(settings)) {\r
94             throw new NullPointerException(\r
95                     "Please define jronn.jar.file property in Executable.properties file"\r
96                             + "and initialize it with the location of jronn jar file");\r
97         }\r
98         if (new File(settings).isAbsolute()) {\r
99             // Jronn jar can be found so no actions necessary\r
100             // no further actions is necessary\r
101             return settings;\r
102         }\r
103         return compbio.engine.client.Util.convertToAbsolute(settings);\r
104     }\r
105 \r
106     @Override\r
107     public List<String> getCreatedFiles() {\r
108         return Arrays.asList(getOutput(), getError());\r
109     }\r
110 \r
111     @Override\r
112     public Jronn setInput(String inFile) {\r
113         super.setInput(inFile);\r
114         cbuilder.setParam("-i=" + inFile);\r
115         return this;\r
116     }\r
117 \r
118     @Override\r
119     public Jronn setOutput(String outFile) {\r
120         super.setOutput(outFile);\r
121         cbuilder.setParam("-o=" + outFile);\r
122         return this;\r
123     }\r
124 \r
125     @Override\r
126     public Limit<Jronn> getLimit(String presetName) {\r
127         if (limits == null) {\r
128             limits = getLimits();\r
129         }\r
130         Limit<Jronn> limit = null;\r
131         if (limits != null) {\r
132             // this returns default limit if preset is undefined!\r
133             limit = limits.getLimitByName(presetName);\r
134         }\r
135         // If limit is not defined for a particular preset, then return default\r
136         // limit\r
137         if (limit == null) {\r
138             log.debug("Limit for the preset " + presetName\r
139                     + " is not found. Using default");\r
140             limit = limits.getDefaultLimit();\r
141         }\r
142         return limit;\r
143     }\r
144 \r
145     @Override\r
146     public LimitsManager<Jronn> getLimits() {\r
147         // synchronise on static field\r
148         synchronized (log) {\r
149             if (limits == null) {\r
150                 limits = Util.getLimits(this.getClass());\r
151             }\r
152         }\r
153         return limits;\r
154     }\r
155 \r
156     @Override\r
157     public Class<? extends Executable<?>> getType() {\r
158         return this.getClass();\r
159     }\r
160 \r
161     public static String getStatFile() {\r
162         return STAT_FILE;\r
163     }\r
164 }\r