Debian Codegraph @

current/gff/ParseCrispr.java

raw symbols SCIP
1 package gff;
2
3 import java.io.PrintStream;
4
5 import fileIO.ByteFile;
6 import fileIO.ByteFile1;
7 import fileIO.ByteFile2;
8 import fileIO.ByteStreamWriter;
9 import fileIO.FileFormat;
10 import fileIO.ReadWrite;
11 import shared.Parse;
12 import shared.Parser;
13 import shared.PreParser;
14 import shared.Shared;
15 import shared.Timer;
16 import shared.Tools;
17 import structures.ByteBuilder;
18
19 /**
20 * @author Brian Bushnell
21 * @date Nov 3, 2023
22 *
23 */
24 public class ParseCrispr {
25
26 /*--------------------------------------------------------------*/
27 /*---------------- Initialization ----------------*/
28 /*--------------------------------------------------------------*/
29
30 /**
31 * Code entrance from the command line.
32 * @param args Command line arguments
33 */
34 public static void main(String[] args){
35 //Start a timer immediately upon code entrance.
36 Timer t=new Timer();
37
38 //Create an instance of this class
39 ParseCrispr x=new ParseCrispr(args);
40
41 //Run the object
42 x.process(t);
43
44 //Close the print stream if it was redirected
45 Shared.closeStream(x.outstream);
46 }
47
48 /**
49 * Constructor.
50 * @param args Command line arguments
51 */
52 public ParseCrispr(String[] args){
53
54 {//Preparse block for help, config files, and outstream
55 PreParser pp=new PreParser(args, /*getClass()*/null, false);
56 args=pp.args;
57 outstream=pp.outstream;
58 }
59
60 //Set shared static variables prior to parsing
61 ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true;
62 ReadWrite.setZipThreads(Shared.threads());
63
64 {//Parse the arguments
65 final Parser parser=parse(args);
66 overwrite=parser.overwrite;
67 append=parser.append;
68
69 in1=parser.in1;
70
71 out1=parser.out1;
72 }
73
74 validateParams();
75 fixExtensions(); //Add or remove .gz or .bz2 as needed
76 checkFileExistence(); //Ensure files can be read and written
77 checkStatics(); //Adjust file-related static fields as needed for this program
78
79 ffout1=FileFormat.testOutput(out1, FileFormat.TXT, null, true, overwrite, append, false);
80 ffoutInvalid=FileFormat.testOutput(outInvalid, FileFormat.TXT, null, true, overwrite, append, false);
81 ffin1=FileFormat.testInput(in1, FileFormat.TXT, null, true, true);
82 }
83
84 /*--------------------------------------------------------------*/
85 /*---------------- Initialization Helpers ----------------*/
86 /*--------------------------------------------------------------*/
87
88 /** Parse arguments from the command line */
89 private Parser parse(String[] args){
90
91 //Create a parser object
92 Parser parser=new Parser();
93
94 //Set any necessary Parser defaults here
95 //parser.foo=bar;
96 // parser.out1="stdout";
97
98 //Parse each argument
99 for(int i=0; i<args.length; i++){
100 String arg=args[i];
101
102 //Break arguments into their constituent parts, in the form of "a=b"
103 String[] split=arg.split("=");
104 String a=split[0].toLowerCase();
105 String b=split.length>1 ? split[1] : null;
106 if(b!=null && b.equalsIgnoreCase("null")){b=null;}
107
108 if(a.equals("invalid")){
109 outInvalid=b;
110 }else if(a.equals("lines")){
111 maxLines=Long.parseLong(b);
112 if(maxLines<0){maxLines=Long.MAX_VALUE;}
113 }else if(a.equals("verbose")){
114 verbose=Parse.parseBoolean(b);
115 ByteFile1.verbose=verbose;
116 ByteFile2.verbose=verbose;
117 ReadWrite.verbose=verbose;
118 }else if(a.equals("parse_flag_goes_here")){
119 long fake_variable=Parse.parseKMG(b);
120 //Set a variable here
121 }else if(parser.parse(arg, a, b)){
122 //do nothing
123 }else{
124 outstream.println("Unknown parameter "+args[i]);
125 assert(false) : "Unknown parameter "+args[i];
126 throw new RuntimeException("Unknown parameter "+args[i]);
127 }
128 }
129
130 return parser;
131 }
132
133 /** Add or remove .gz or .bz2 as needed */
134 private void fixExtensions(){
135 in1=Tools.fixExtension(in1);
136 if(in1==null){throw new RuntimeException("Error - at least one input file is required.");}
137 }
138
139 /** Ensure files can be read and written */
140 private void checkFileExistence(){
141 //Ensure output files can be written
142 if(!Tools.testOutputFiles(overwrite, append, false, out1)){
143 outstream.println((out1==null)+", "+out1);
144 throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output file "+out1+"\n");
145 }
146
147 //Ensure input files can be read
148 if(!Tools.testInputFiles(false, true, in1)){
149 throw new RuntimeException("\nCan't read some input files.\n");
150 }
151
152 //Ensure that no file was specified multiple times
153 if(!Tools.testForDuplicateFiles(true, in1, out1)){
154 throw new RuntimeException("\nSome file names were specified multiple times.\n");
155 }
156 }
157
158 /** Adjust file-related static fields as needed for this program */
159 private static void checkStatics(){
160 //Adjust the number of threads for input file reading
161 if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){
162 ByteFile.FORCE_MODE_BF2=true;
163 }
164
165 // if(!ByteFile.FORCE_MODE_BF2){
166 // ByteFile.FORCE_MODE_BF2=false;
167 // ByteFile.FORCE_MODE_BF1=true;
168 // }
169 }
170
171 /** Ensure parameter ranges are within bounds and required parameters are set */
172 private boolean validateParams(){
173 // assert(minfoo>0 && minfoo<=maxfoo) : minfoo+", "+maxfoo;
174 assert(false) : "TODO";
175 return true;
176 }
177
178 /*--------------------------------------------------------------*/
179 /*---------------- Outer Methods ----------------*/
180 /*--------------------------------------------------------------*/
181
182 /** Create streams and process all data */
183 void process(Timer t){
184
185 ByteFile bf=ByteFile.makeByteFile(ffin1);
186 ByteStreamWriter bsw=makeBSW(ffout1);
187 ByteStreamWriter bswInvalid=makeBSW(ffoutInvalid);
188
189 // assert(false) : "Header goes here.";
190 if(bsw!=null){
191 // assert(false) : "Header goes here.";
192 }
193
194 processInner(bf, bsw, bswInvalid);
195
196 errorState|=bf.close();
197 if(bsw!=null){errorState|=bsw.poisonAndWait();}
198 if(bswInvalid!=null){errorState|=bswInvalid.poisonAndWait();}
199
200 t.stop();
201
202 outstream.println(Tools.timeLinesBytesProcessed(t, linesProcessed, bytesProcessed, 8));
203
204 outstream.println();
205 outstream.println("Valid Lines: \t"+linesOut);
206 outstream.println("Invalid Lines: \t"+(linesProcessed-linesOut));
207
208 //Throw an exception of there was an error in a thread
209 if(errorState){
210 throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt.");
211 }
212 }
213
214 /*--------------------------------------------------------------*/
215 /*---------------- Inner Methods ----------------*/
216 /*--------------------------------------------------------------*/
217
218 //tid|1000566|NZ_JACGWZ010000001.1 RefSeq direct_repeat 1135444 1135673 . + .
219 //ID=id-NZ_JACGWZ010000001.1:1135444..1135673;gbkey=repeat_region;
220 //inference=COORDINATES: alignment:pilercr:v1.02;rpt_family=CRISPR;
221 //rpt_type=direct;rpt_unit_range=1135453..1135473;rpt_unit_seq=gcccctacgagggatcgcaac
222
223 private void processInner(ByteFile bf, ByteStreamWriter bsw, ByteStreamWriter bswInvalid){
224 byte[] line=bf.nextLine();
225 ByteBuilder bb=new ByteBuilder();
226 final int taglen="rpt_unit_seq=".length();
227
228 long number=1;
229 while(line!=null){
230 if(line.length>0){
231 if(maxLines>0 && linesProcessed>=maxLines){break;}
232 linesProcessed++;
233 bytesProcessed+=(line.length+1);
234
235
236
237 boolean valid=(line[0]!='#' && Tools.indexOf(line, "rpt_family=CRISPR;rpt_type=direct;", 0)>=0);
238
239 if(valid){
240 int idx=Tools.indexOf(line, "rpt_unit_seq=", 0);
241 assert(idx>=0) : new String(line);
242
243 bb.clear().append('>').append(number).nl();
244 number++;
245 for(int i=idx+taglen; i<line.length; i++) {
246 byte b=line[i];
247 if(!Tools.isLetter(b)) {break;}
248 bb.append(Tools.toUpperCase(b));
249 }
250 bb.nl();
251
252 linesOut+=2;
253 bytesOut+=(bb.length);
254 bsw.print(bb);
255 bb.clear();
256 }else{
257 if(bswInvalid!=null){
258 bswInvalid.println(line);
259 }
260 }
261 }
262 line=bf.nextLine();
263 }
264 }
265
266 private static ByteStreamWriter makeBSW(FileFormat ff){
267 if(ff==null){return null;}
268 ByteStreamWriter bsw=new ByteStreamWriter(ff);
269 bsw.start();
270 return bsw;
271 }
272
273 /*--------------------------------------------------------------*/
274 /*---------------- Fields ----------------*/
275 /*--------------------------------------------------------------*/
276
277 /** Primary input file path */
278 private String in1=null;
279
280 /** Primary output file path */
281 private String out1=null;
282
283 /** Junk output file path */
284 private String outInvalid=null;
285
286 /*--------------------------------------------------------------*/
287
288 private long linesProcessed=0;
289 private long linesOut=0;
290 private long bytesProcessed=0;
291 private long bytesOut=0;
292
293 private long maxLines=Long.MAX_VALUE;
294
295 /*--------------------------------------------------------------*/
296 /*---------------- Final Fields ----------------*/
297 /*--------------------------------------------------------------*/
298
299 /** Input File */
300 private final FileFormat ffin1;
301 /** Output File */
302 private final FileFormat ffout1;
303 /** Optional Output File for Junk */
304 private final FileFormat ffoutInvalid;
305
306 /*--------------------------------------------------------------*/
307 /*---------------- Common Fields ----------------*/
308 /*--------------------------------------------------------------*/
309
310 /** Print status messages to this output stream */
311 private PrintStream outstream=System.err;
312 /** Print verbose messages */
313 public static boolean verbose=false;
314 /** True if an error was encountered */
315 public boolean errorState=false;
316 /** Overwrite existing output files */
317 private boolean overwrite=true;
318 /** Append to existing output files */
319 private boolean append=false;
320
321 }