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