Debian Codegraph @

metastudentPkg/lib/groupB/pp2/datahandler/GenerateCAFAOutput.java

raw
1 package pp2.datahandler;
2
3 import java.io.BufferedReader;
4 import java.io.BufferedWriter;
5 import java.io.IOException;
6 import java.util.ArrayList;
7 import java.util.Collections;
8 import java.util.HashMap;
9 import java.util.HashSet;
10 import java.util.LinkedList;
11 import java.util.List;
12 import java.util.Map;
13 import java.util.Set;
14
15 import pp2.go.Relations;
16 import pp2.tools.Tools;
17
18 public class GenerateCAFAOutput {
19
20 public static final String teamName = "rostlab";
21 public static final String[] keywords = new String[] {
22 "sequence alignments"
23 };
24 public static final int confCol = 4;
25 public static final int ontCol = 2;
26
27 /**
28 * Converts output file with predictions and corresponding
29 * scores to CAFA output file format
30 * @param inputFile Prediction file
31 * @param outputFile Name of the output file
32 * @param goClosure Name of the file containing go ontology
33 * @param goTermFile Name of the file containing namespace/ontology
34 * @param teamName Name of the team
35 * @param keywords List of keywords (have to be CAFA keywords!)
36 * @param model Number of model (1-3)
37 * @param pr Precision of method
38 * @param rc Recall of method
39 * @param confCol Column containing the confidence score
40 * @param n Parameter n = Number of Predictions per target
41 */
42 public static void parseToCAFAOutput(String inputFile, String outputFile,
43 String goClosure, String goTermFile,
44 String teamName, String[] keywords, int model, double pr, double rc,
45 int confCol, int n) {
46 try {
47 BufferedReader input = Tools.openFile(inputFile);
48 BufferedWriter output = Tools.writeToFile(outputFile);
49 output.write("AUTHOR\t"+teamName+"\n"
50 +"MODEL\t"+model+"\n"
51 +"KEYWORDS\t"+listKeywords(keywords)+"\n");
52 if(pr > -1 && rc > -1) {
53 output.write("ACCURACY\tPR="+String.format("%3.2f",pr).replace(",", ".")+
54 "; RC="+String.format("%3.2f",rc).replace(",", ".")+"\n");
55 }
56 String line;
57 String target = "";
58 Map<Double,LinkedList<String>> scoreToHitM = new HashMap<Double, LinkedList<String>>();
59 //Map<Double,String> scoreToHitB = new HashMap<Double, String>();
60 //Map<Double,String> scoreToHitC = new HashMap<Double, String>();
61 Relations rel = null;
62 if(!goClosure.isEmpty() && !goTermFile.isEmpty()) {
63 rel = new Relations(goClosure, goTermFile, true);
64 }
65
66 while((line = input.readLine()) != null) {
67 if(line.startsWith("//")) {
68 output.write(filterScores(scoreToHitM, n, target, rel));
69 //output.write(filterScores(scoreToHitB, n, target));
70 //output.write(filterScores(scoreToHitC, n, target));
71 } else if(line.startsWith(">")) {
72 target = line.substring(1);
73 scoreToHitM = new HashMap<Double, LinkedList<String>>();
74 } else {
75 String[] content = line.split("\t");
76 double score = Double.parseDouble(content[confCol].replace(",","."));
77 String go = content[0];
78
79 if(scoreToHitM.containsKey(score))
80 scoreToHitM.get(score).add(go+"\t"+formatScore(score));
81 else
82 {
83 LinkedList<String> l = new LinkedList<String>();
84 l.add(go+"\t"+formatScore(score));
85 scoreToHitM.put(score, l);
86 }
87 //scoreToHitM.put(score, go+"\t"+String.format("%1.2f", score).replace(",", "."));
88 }
89 }
90 output.write("END");
91 input.close();
92 output.flush();
93 output.close();
94 } catch (IOException e) {
95 e.printStackTrace();
96 }
97 }
98
99 /**
100 * Truncated version of converter from prediction file with
101 * scores to CAFA output file format
102 * @param inputFile Prediction file
103 * @param outputFile Output file name
104 * @param model Number of model (1-3)
105 * @param pr Precision of method
106 * @param rc Recall of method
107 * @param k Parameter n = Number of Predictions per target
108 */
109 public static void parseToCAFAOutput(String inputFile, String outputFile,
110 int model, double pr, double rc, int k) {
111 parseToCAFAOutput(inputFile, outputFile, "", "", teamName, keywords,
112 model, pr, rc, confCol, k);
113 }
114
115 public static String formatScore(double score) {
116 return String.format("%1.2f", score).replace(",", ".");
117 }
118
119 public static String listKeywords(String[] keywords) {
120 StringBuffer result = new StringBuffer("");
121 for(int i=0; i<keywords.length-1; i++) {
122 result.append(keywords[i]+", ");
123 }
124 return result.append(keywords[keywords.length-1]+".").toString();
125 }
126
127 public static void printUsage() {
128 System.err.println(" Usage: GenerateCAFAOutput -i scorefile -o outputfile" +
129 " -m model -pr precision -rc recall [-n number_of_predictions/target] " +
130 "[-t teamname] [-c column_index_of_score] [-k keywords (komma separated)] " +
131 "[-gc deductive_closure_go -gn namespace_go_file]");
132 System.exit(1);
133 }
134
135 private static String filterScores(Map<Double,LinkedList<String>> scoreToHit,
136 int n, String target, Relations rel) {
137 /*if(n == 0) {
138 n = scoreToHit.size();
139 }*/
140 //TreeSet<Double> scores = new TreeSet<Double>(scoreToHit.keySet());
141 StringBuffer output = new StringBuffer("");
142 int i = 0;
143
144 // sort scores in descending order
145 List<Double> sortedScores = new ArrayList<Double>();
146 sortedScores.addAll(scoreToHit.keySet());
147 Collections.sort(sortedScores);
148 Collections.reverse(sortedScores);
149 Set<String> parentNodes = new HashSet<String>();
150
151 for(Double score: sortedScores)
152 {
153 for(String pred: scoreToHit.get(score))
154 {
155 if(n > 0 && i >= n)
156 break;
157
158 output.append(target+"\t"+pred+"\n");
159 i++;
160 if(rel != null) {
161 String go = pred.split("\t")[0];
162 String[] parents = rel.getAncestors(go);
163 for(String p : parents) {
164 if(!parentNodes.contains(p)) {
165 output.append(target+"\t"+p+"\t"+formatScore(score)+"\n");
166 parentNodes.add(p);
167 }
168 }
169 }
170
171
172 }
173
174 if(n > 0 && i >= n)
175 break;
176 }
177
178 /*for(int i=0; i<n; i++) {
179 if(scores.size() > 0) {
180 double s = scores.last();
181 output.append(target+"\t"+scoreToHit.get(s)+"\n");
182 scores.remove(s);
183 }
184 }*/
185 return output.toString();
186 }
187
188 public static void main(String[] args) {
189 String inputfile = "";
190 String outputfile = "";
191 String goClosure = "";
192 String goTermFile = "";
193 int model = 0;
194 double pr = 0;
195 double rc = 0;
196 int n = 0;
197 String teamName = "";
198 String[] keywords = null;
199 int confCol = -1;
200
201 for(int i=0; i<args.length; i+=2) {
202 if(args[i].equals("-i")) {
203 inputfile = args[i+1];
204 } else if(args[i].equals("-o")) {
205 outputfile = args[i+1];
206 } else if(args[i].equals("-m")) {
207 model = Integer.parseInt(args[i+1]);
208 } else if(args[i].equals("-pr")) {
209 try {
210 pr = Double.parseDouble(args[i+1]);
211 } catch (NumberFormatException e)
212 {
213 e.printStackTrace();
214 }
215 } else if(args[i].equals("-rc")) {
216 try {
217 rc = Double.parseDouble(args[i+1]);
218 } catch (NumberFormatException e)
219 {
220 e.printStackTrace();
221 }
222 } else if(args[i].equals("-n")) {
223 n = Integer.parseInt(args[i+1]);
224 } else if(args[i].equals("-t")) {
225 teamName = args[i+1];
226 } else if(args[i].equals("-k")) {
227 keywords = args[i+1].split(",");
228 } else if(args[i].equals("-c")) {
229 confCol = Integer.parseInt(args[i+1]);
230 } else if(args[i].equals("-gc")) {
231 goClosure = args[i+1];
232 } else if(args[i].equals("-gn")) {
233 goTermFile = args[i+1];
234 } else {
235 printUsage();
236 }
237 }
238 if(teamName.isEmpty()) {
239 teamName = GenerateCAFAOutput.teamName;
240 }
241 if(keywords == null) {
242 keywords = GenerateCAFAOutput.keywords;
243 }
244 if(confCol == -1) {
245 confCol = GenerateCAFAOutput.confCol;
246 }
247 if(!inputfile.isEmpty() && !outputfile.isEmpty()
248 && model > 0) {
249 parseToCAFAOutput(inputfile, outputfile, goClosure, goTermFile,
250 teamName, keywords, model, pr, rc, confCol, n);
251 } else {
252 printUsage();
253 }
254
255
256 }
257 }