Clean up logging system
[jabaws.git] / runner / compbio / runner / disorder / IUPred.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.IOException;\r
19 import java.util.Map;\r
20 import java.util.Set;\r
21 import java.util.TreeMap;\r
22 import java.util.TreeSet;\r
23 \r
24 import org.apache.log4j.Logger;\r
25 \r
26 import compbio.data.sequence.Score;\r
27 import compbio.data.sequence.ScoreManager;\r
28 import compbio.data.sequence.SequenceUtil;\r
29 import compbio.data.sequence.UnknownFileFormatException;\r
30 import compbio.engine.client.SkeletalExecutable;\r
31 import compbio.metadata.ResultNotAvailableException;\r
32 \r
33 /**\r
34  * iupred sequenceFile <short long glob >\r
35  * \r
36  * Maximum sequence length is 40000 chars. Single string length max is a 1000\r
37  * chars!\r
38  * \r
39  */\r
40 public class IUPred extends SkeletalExecutable<IUPred> {\r
41 \r
42         private static Logger log = Logger.getLogger(IUPred.class);\r
43         private static final String GLOB_OUTPUT = "out.glob";\r
44         private static final String SHORT_OUTPUT = "out.short";\r
45         private static final String LONG_OUTPUT = "out.long";\r
46 \r
47         @Override\r
48         @SuppressWarnings("unchecked")\r
49         public ScoreManager getResults(String workDirectory)\r
50                         throws ResultNotAvailableException {\r
51 \r
52                 ScoreManager results = null;\r
53                 try {\r
54                         Map<String, Set<Score>> combined = new TreeMap<String, Set<Score>>();\r
55                         \r
56                         File shortf = new File(workDirectory, SHORT_OUTPUT);\r
57                         if(shortf.exists()) {\r
58                                 combineScores(combined, SequenceUtil.readIUPred(shortf));\r
59                         }\r
60                         File longf = new File(workDirectory, LONG_OUTPUT);\r
61                         if(longf.exists()) {\r
62                                 combineScores(combined, SequenceUtil.readIUPred(longf));\r
63                         } \r
64                         \r
65                         File glob = new File(workDirectory, GLOB_OUTPUT);\r
66                         if(glob.exists()) {\r
67                                 combineScores(combined, SequenceUtil.readIUPred(glob));\r
68                         }\r
69                         if(combined.isEmpty()) {\r
70                                 throw new ResultNotAvailableException("Could not find any result " +\r
71                                                 "files for the job: " + workDirectory); \r
72                         }\r
73                         results = ScoreManager.newInstance(combined);\r
74 \r
75                 } catch (IOException e) {\r
76                         log.error(e.getMessage(), e.getCause());\r
77                         throw new ResultNotAvailableException(e);\r
78                 } catch (UnknownFileFormatException e) {\r
79                         log.error(e.getMessage(), e.getCause());\r
80                         throw new ResultNotAvailableException(e);\r
81                 } catch (NullPointerException e) {\r
82                         log.error(e.getMessage(), e.getCause());\r
83                         throw new ResultNotAvailableException(e);\r
84                 }\r
85                 return results;\r
86         }\r
87 \r
88         void combineScores(Map<String, Set<Score>> combined, Map<String, Score> scores)  { \r
89                 if(scores==null) { \r
90                         return; \r
91                 }\r
92                 if (combined.isEmpty()) {\r
93                         for (String key : scores.keySet()) {\r
94                                 Set<Score> allScores = new TreeSet<Score>();\r
95                                 combined.put(key, allScores);\r
96                         }\r
97                 }\r
98                 for (String key : scores.keySet()) {\r
99                         Set<Score>      allScores= combined.get(key);\r
100                         assert allScores!=null; \r
101                         Score score = scores.get(key);\r
102                         allScores.add(score);\r
103                         combined.put(key, allScores);\r
104                 }\r
105         }\r
106 \r
107         @Override\r
108         public IUPred setInput(String inFile) {\r
109                 super.setInput(inFile);\r
110                 cbuilder.setFirst(inFile);\r
111                 return this;\r
112         } \r
113 \r
114         @Override\r
115         public IUPred setOutput(String outFile) {\r
116                 log.warn("IUpred output is predefined and cannot be set!"); \r
117                 return this; \r
118         }\r
119 \r
120         UnsupportedOperationException newUnsupportedOutputException() { \r
121                 return new UnsupportedOperationException("The outputs from this executable " +\r
122                                 "are always either of those 3 files (depending on the method called): " + GLOB_OUTPUT + ", " \r
123                                 + SHORT_OUTPUT + ", "+ LONG_OUTPUT);\r
124         }\r
125 \r
126         @Override\r
127         public String getOutput() {\r
128                 log.warn("IUpred output is predefined and is one of the three files " +\r
129                                 "(depending on the method called): " + GLOB_OUTPUT + ", " \r
130                                 + SHORT_OUTPUT + ", "+ LONG_OUTPUT);\r
131                 return LONG_OUTPUT;\r
132         }\r
133 \r
134         @SuppressWarnings("unchecked")\r
135         @Override\r
136         public Class<IUPred> getType() {\r
137                 return (Class<IUPred>) this.getClass();\r
138         }\r
139 \r
140 }\r