Debian Codegraph @

current/gff/CompareGff.java

raw symbols SCIP
1 package gff;
2
3 import java.io.PrintStream;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.Map.Entry;
7
8 import fileIO.ByteFile;
9 import fileIO.FileFormat;
10 import fileIO.ReadWrite;
11 import prok.ProkObject;
12 import shared.Parse;
13 import shared.Parser;
14 import shared.PreParser;
15 import shared.Shared;
16 import shared.Timer;
17 import shared.Tools;
18 import structures.StringNum;
19
20 /**
21 * Compares gff files for the purpose of grading gene-calling.
22 * @author Brian Bushnell
23 * @date October 3, 2018
24 *
25 */
26 public class CompareGff {
27
28 /*--------------------------------------------------------------*/
29 /*---------------- Initialization ----------------*/
30 /*--------------------------------------------------------------*/
31
32 /**
33 * Code entrance from the command line.
34 * @param args Command line arguments
35 */
36 public static void main(String[] args){
37 //Start a timer immediately upon code entrance.
38 Timer t=new Timer();
39
40 //Create an instance of this class
41 CompareGff x=new CompareGff(args);
42
43 //Run the object
44 x.process(t);
45
46 //Close the print stream if it was redirected
47 Shared.closeStream(x.outstream);
48 }
49
50 /**
51 * Constructor.
52 * @param args Command line arguments
53 */
54 public CompareGff(String[] args){
55
56 {//Preparse block for help, config files, and outstream
57 PreParser pp=new PreParser(args, getClass(), false);
58 args=pp.args;
59 outstream=pp.outstream;
60 }
61
62 //Set shared static variables prior to parsing
63 ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true;
64 ReadWrite.setZipThreads(Shared.threads());
65
66 {//Parse the arguments
67 final Parser parser=parse(args);
68 overwrite=parser.overwrite;
69 append=parser.append;
70
71 in=parser.in1;
72 }
73
74 fixExtensions(); //Add or remove .gz or .bz2 as needed
75 checkFileExistence(); //Ensure files can be read and written
76 checkStatics(); //Adjust file-related static fields as needed for this program
77
78 ffin=FileFormat.testInput(in, FileFormat.GFF, null, true, true);
79 ffref=FileFormat.testInput(ref, FileFormat.GFF, null, true, true);
80 }
81
82 /*--------------------------------------------------------------*/
83 /*---------------- Initialization Helpers ----------------*/
84 /*--------------------------------------------------------------*/
85
86 /** Parse arguments from the command line */
87 private Parser parse(String[] args){
88
89 Parser parser=new Parser();
90 for(int i=0; i<args.length; i++){
91 String arg=args[i];
92 String[] split=arg.split("=");
93 String a=split[0].toLowerCase();
94 String b=split.length>1 ? split[1] : null;
95 if(b!=null && b.equalsIgnoreCase("null")){b=null;}
96
97 if(a.equals("ref")){
98 ref=b;
99 }else if(a.equals("lines")){
100 maxLines=Long.parseLong(b);
101 if(maxLines<0){maxLines=Long.MAX_VALUE;}
102 }else if(a.equals("verbose")){
103 verbose=Parse.parseBoolean(b);
104 // ByteFile1.verbose=verbose;
105 // ByteFile2.verbose=verbose;
106 // ReadWrite.verbose=verbose;
107 }else if(parser.parse(arg, a, b)){
108 //do nothing
109 }else if(i==0 && arg.indexOf('=')<0){
110 parser.in1=arg;
111 }else if(i==1 && arg.indexOf('=')<0 && ref==null){
112 ref=arg;
113 }else{
114 outstream.println("Unknown parameter "+args[i]);
115 assert(false) : "Unknown parameter "+args[i];
116 // throw new RuntimeException("Unknown parameter "+args[i]);
117 }
118 }
119
120 return parser;
121 }
122
123 /** Add or remove .gz or .bz2 as needed */
124 private void fixExtensions(){
125 in=Tools.fixExtension(in);
126 ref=Tools.fixExtension(ref);
127 if(in==null || ref==null){throw new RuntimeException("Error - at least two input files are required.");}
128 }
129
130 /** Ensure files can be read and written */
131 private void checkFileExistence(){
132
133 //Ensure input files can be read
134 if(!Tools.testInputFiles(true, true, in, ref)){
135 throw new RuntimeException("\nCan't read some input files.\n");
136 }
137 }
138
139 /** Adjust file-related static fields as needed for this program */
140 private static void checkStatics(){
141 //Adjust the number of threads for input file reading
142 if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){
143 ByteFile.FORCE_MODE_BF2=true;
144 }
145
146 // if(!ByteFile.FORCE_MODE_BF2){
147 // ByteFile.FORCE_MODE_BF2=false;
148 // ByteFile.FORCE_MODE_BF1=true;
149 // }
150 }
151
152 /*--------------------------------------------------------------*/
153 /*---------------- Outer Methods ----------------*/
154 /*--------------------------------------------------------------*/
155
156 void process(Timer t){
157
158 ByteFile bf=ByteFile.makeByteFile(ffin);
159
160 processInner(bf);
161
162 errorState|=bf.close();
163
164 t.stop();
165
166 outstream.println(Tools.timeLinesBytesProcessed(t, linesProcessed, bytesProcessed, 8));
167
168 outstream.println();
169 outstream.println("Ref count: \t"+refCount);
170 outstream.println("Query count: \t"+queryCount);
171
172 outstream.println();
173 outstream.println("Ref-relative counts:");
174 outstream.println("True Positive Start: \t"+truePositiveStart+"\t"+(Tools.format("%.3f%%", truePositiveStart*100.0/refCount)));
175 outstream.println("True Positive Stop: \t"+truePositiveStop+"\t"+(Tools.format("%.3f%%", truePositiveStop*100.0/refCount)));
176 // outstream.println("False Positive Start:\t"+falsePositiveStart+"\t"+(Tools.format("%.3f%%", falsePositiveStart*100.0/refCount)));
177 // outstream.println("False Positive Stop: \t"+falsePositiveStop+"\t"+(Tools.format("%.3f%%", falsePositiveStop*100.0/refCount)));
178 outstream.println("False Negative Start:\t"+falseNegativeStart+"\t"+(Tools.format("%.3f%%", falseNegativeStart*100.0/refCount)));
179 outstream.println("False Negative Stop: \t"+falseNegativeStop+"\t"+(Tools.format("%.3f%%", falseNegativeStop*100.0/refCount)));
180
181 outstream.println();
182 outstream.println("Query-relative counts:");
183 outstream.println("True Positive Start: \t"+truePositiveStart2+"\t"+(Tools.format("%.3f%%", truePositiveStart2*100.0/queryCount)));
184 outstream.println("True Positive Stop: \t"+truePositiveStop2+"\t"+(Tools.format("%.3f%%", truePositiveStop2*100.0/queryCount)));
185 outstream.println("False Positive Start:\t"+falsePositiveStart2+"\t"+(Tools.format("%.3f%%", falsePositiveStart2*100.0/queryCount)));
186 outstream.println("False Positive Stop: \t"+falsePositiveStop2+"\t"+(Tools.format("%.3f%%", falsePositiveStop2*100.0/queryCount)));
187
188 outstream.println();
189 outstream.println("SNR: \t"+Tools.format("%.4f", 10*Math.log10((truePositiveStart2+truePositiveStop2+0.1)/(falsePositiveStart2+falsePositiveStop2+0.1))));
190
191 if(errorState){
192 throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt.");
193 }
194 }
195
196 /*--------------------------------------------------------------*/
197 /*---------------- Inner Methods ----------------*/
198 /*--------------------------------------------------------------*/
199
200 @SuppressWarnings("unchecked")
201 private void processInner(ByteFile bf){
202 byte[] line=bf.nextLine();
203
204 {
205 ArrayList<GffLine> refLines=GffLine.loadGffFile(ffref, "CDS,rRNA,tRNA", true);
206
207 refCount=refLines.size();
208 lineMap=new HashMap<StringNum, GffLine>();
209 startCountMap=new HashMap<StringNum, Integer>();
210 stopCountMap=new HashMap<StringNum, Integer>();
211
212 for(GffLine gline : refLines){
213 final int stop=gline.trueStop();
214 StringNum sn=new StringNum(gline.seqid, stop);
215 lineMap.put(sn, gline);
216 startCountMap.put(sn, 0);
217 stopCountMap.put(sn, 0);
218 assert(lineMap.get(sn)==gline);
219 // assert(false) : "\n\nsn='"+sn+"'\n"+lineMap.containsKey(sn)+"\n"+lineMap.keySet();
220 }
221 if(verbose){
222 System.err.println(lineMap);
223 System.err.println(startCountMap);
224 System.err.println(stopCountMap);
225 }
226 }
227
228 while(line!=null){
229 if(line.length>0){
230 if(maxLines>0 && linesProcessed>=maxLines){break;}
231 linesProcessed++;
232 bytesProcessed+=(line.length+1);
233
234 final boolean valid=(line[0]!='#');
235 if(valid){
236 queryCount++;
237 GffLine gline=new GffLine(line);
238 processLine(gline);
239 }
240 }
241 line=bf.nextLine();
242 }
243
244 for(Entry<StringNum, Integer> e : startCountMap.entrySet()){
245 if(e.getValue()<1){
246 falseNegativeStart++;
247 }
248 }
249 for(Entry<StringNum, Integer> e : stopCountMap.entrySet()){
250 if(e.getValue()<1){
251 falseNegativeStop++;
252 }
253 }
254 }
255
256 private void processLine(GffLine gline){
257 // boolean cds=gline.type.equals("CDS");
258 // boolean trna=gline.type.equals("tRNA");
259 // boolean rrna=gline.type.equals("rRNA");
260 // if(!cds && !trna && !rrna){return;}
261 // if(cds && !ProkObject.callCDS){return;}
262 // if(trna && !ProkObject.calltRNA){return;}
263 // if(rrna){
264 // int type=gline.prokType();
265 // if(ProkObject.processType(type)){return;}
266 // }
267 int type=gline.prokType();
268 if(!ProkObject.processType(type)){return;}
269
270 final int stop=gline.trueStop();
271 final int start=gline.trueStart();
272
273 // System.err.println("Considering "+start+", "+stop);
274
275 StringNum sn=new StringNum(gline.seqid, stop);
276 GffLine refline=lineMap.get(sn);
277
278 boolean fail=(refline==null || refline.strand!=gline.strand || !refline.type.equals(gline.type));
279 if(fail){
280 if(verbose){
281 System.err.println("Can't find "+sn+"\n"+gline+"\n"+refline);
282 assert(false) : "\n\nsn='"+sn+"'\n"+lineMap.containsKey(sn)+"\n"+lineMap.keySet();
283 }
284 falsePositiveStart++;
285 falsePositiveStop++;
286 falsePositiveStart2++;
287 falsePositiveStop2++;
288 }else{
289 assert(stop==refline.trueStop());
290 truePositiveStop++;
291 truePositiveStop2++;
292 stopCountMap.put(sn, stopCountMap.get(sn)+1);
293 if(start==refline.trueStart()){
294 truePositiveStart++;
295 truePositiveStart2++;
296 startCountMap.put(sn, startCountMap.get(sn)+1);
297 }else{
298 falsePositiveStart++;
299 falsePositiveStart2++;
300 }
301 }
302 }
303
304 /*--------------------------------------------------------------*/
305 /*---------------- Fields ----------------*/
306 /*--------------------------------------------------------------*/
307
308 private String in=null;
309 private String ref=null;
310
311
312 /*--------------------------------------------------------------*/
313
314 private HashMap<StringNum, GffLine> lineMap;
315 private HashMap<StringNum, Integer> startCountMap;
316 private HashMap<StringNum, Integer> stopCountMap;
317
318 // private HashMap<Integer, ArrayList<GffLine>> map;
319 // private HashSet<Integer> stopSet;
320 // private HashSet<Integer> startSet;
321 // private HashSet<Integer> stopSetM;
322 // private HashSet<Integer> startSetM;
323
324 private long linesProcessed=0;
325 private long linesOut=0;
326 private long bytesProcessed=0;
327 private long bytesOut=0;
328
329 private long maxLines=Long.MAX_VALUE;
330
331 private long falsePositiveStart=0;
332 private long falsePositiveStop=0;
333 private long truePositiveStart=0;
334 private long truePositiveStop=0;
335 private long falseNegativeStart=0;
336 private long falseNegativeStop=0;
337
338 private long falsePositiveStart2=0;
339 private long falsePositiveStop2=0;
340 private long truePositiveStart2=0;
341 private long truePositiveStop2=0;
342
343 private long refCount=0;
344 private long queryCount=0;
345
346 /*--------------------------------------------------------------*/
347 /*---------------- Final Fields ----------------*/
348 /*--------------------------------------------------------------*/
349
350 private final FileFormat ffin;
351 private final FileFormat ffref;
352
353 /*--------------------------------------------------------------*/
354 /*---------------- Common Fields ----------------*/
355 /*--------------------------------------------------------------*/
356
357 private PrintStream outstream=System.err;
358 public static boolean verbose=false;
359 public boolean errorState=false;
360 private boolean overwrite=true;
361 private boolean append=false;
362
363 }