| 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 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public class ParseCrispr { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public static void main(String[] args){ |
| 35 |
|
| 36 |
Timer t=new Timer(); |
| 37 |
|
| 38 |
|
| 39 |
ParseCrispr x=new ParseCrispr(args); |
| 40 |
|
| 41 |
|
| 42 |
x.process(t); |
| 43 |
|
| 44 |
|
| 45 |
Shared.closeStream(x.outstream); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public ParseCrispr(String[] args){ |
| 53 |
|
| 54 |
{ |
| 55 |
PreParser pp=new PreParser(args, null, false); |
| 56 |
args=pp.args; |
| 57 |
outstream=pp.outstream; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 62 |
ReadWrite.setZipThreads(Shared.threads()); |
| 63 |
|
| 64 |
{ |
| 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(); |
| 76 |
checkFileExistence(); |
| 77 |
checkStatics(); |
| 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 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
private Parser parse(String[] args){ |
| 90 |
|
| 91 |
|
| 92 |
Parser parser=new Parser(); |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
for(int i=0; i<args.length; i++){ |
| 100 |
String arg=args[i]; |
| 101 |
|
| 102 |
|
| 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 |
|
| 121 |
}else if(parser.parse(arg, a, b)){ |
| 122 |
|
| 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 |
|
| 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 |
|
| 140 |
private void checkFileExistence(){ |
| 141 |
|
| 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 |
|
| 148 |
if(!Tools.testInputFiles(false, true, in1)){ |
| 149 |
throw new RuntimeException("\nCan't read some input files.\n"); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
if(!Tools.testForDuplicateFiles(true, in1, out1)){ |
| 154 |
throw new RuntimeException("\nSome file names were specified multiple times.\n"); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
private static void checkStatics(){ |
| 160 |
|
| 161 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 162 |
ByteFile.FORCE_MODE_BF2=true; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
private boolean validateParams(){ |
| 173 |
|
| 174 |
assert(false) : "TODO"; |
| 175 |
return true; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
void process(Timer t){ |
| 184 |
|
| 185 |
ByteFile bf=ByteFile.makeByteFile(ffin1); |
| 186 |
ByteStreamWriter bsw=makeBSW(ffout1); |
| 187 |
ByteStreamWriter bswInvalid=makeBSW(ffoutInvalid); |
| 188 |
|
| 189 |
|
| 190 |
if(bsw!=null){ |
| 191 |
|
| 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 |
|
| 209 |
if(errorState){ |
| 210 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 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 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
private String in1=null; |
| 279 |
|
| 280 |
|
| 281 |
private String out1=null; |
| 282 |
|
| 283 |
|
| 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 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
private final FileFormat ffin1; |
| 301 |
|
| 302 |
private final FileFormat ffout1; |
| 303 |
|
| 304 |
private final FileFormat ffoutInvalid; |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
private PrintStream outstream=System.err; |
| 312 |
|
| 313 |
public static boolean verbose=false; |
| 314 |
|
| 315 |
public boolean errorState=false; |
| 316 |
|
| 317 |
private boolean overwrite=true; |
| 318 |
|
| 319 |
private boolean append=false; |
| 320 |
|
| 321 |
} |