| 1 |
package covid; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.PrintStream; |
| 5 |
import java.util.LinkedHashSet; |
| 6 |
|
| 7 |
import fileIO.ByteFile; |
| 8 |
import fileIO.ByteFile1; |
| 9 |
import fileIO.ByteFile2; |
| 10 |
import fileIO.ByteStreamWriter; |
| 11 |
import fileIO.FileFormat; |
| 12 |
import fileIO.ReadWrite; |
| 13 |
import shared.Parse; |
| 14 |
import shared.Parser; |
| 15 |
import shared.PreParser; |
| 16 |
import shared.Shared; |
| 17 |
import shared.Timer; |
| 18 |
import shared.Tools; |
| 19 |
import structures.ByteBuilder; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class SummarizeCoverage { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public static void main(String[] args){ |
| 37 |
|
| 38 |
Timer t=new Timer(); |
| 39 |
|
| 40 |
|
| 41 |
SummarizeCoverage x=new SummarizeCoverage(args); |
| 42 |
|
| 43 |
|
| 44 |
x.process(t); |
| 45 |
|
| 46 |
|
| 47 |
Shared.closeStream(x.outstream); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public SummarizeCoverage(String[] args){ |
| 55 |
|
| 56 |
{ |
| 57 |
PreParser pp=new PreParser(args, null, false); |
| 58 |
args=pp.args; |
| 59 |
outstream=pp.outstream; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 64 |
ReadWrite.setZipThreads(Shared.threads()); |
| 65 |
|
| 66 |
{ |
| 67 |
final Parser parser=parse(args); |
| 68 |
overwrite=parser.overwrite; |
| 69 |
append=parser.append; |
| 70 |
|
| 71 |
if(in.isEmpty() && parser.in1!=null){ |
| 72 |
in.add(parser.in1); |
| 73 |
} |
| 74 |
|
| 75 |
out1=parser.out1; |
| 76 |
} |
| 77 |
|
| 78 |
fixExtensions(); |
| 79 |
checkFileExistence(); |
| 80 |
checkStatics(); |
| 81 |
|
| 82 |
ffout1=FileFormat.testOutput(out1, FileFormat.TXT, null, true, overwrite, append, false); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
private Parser parse(String[] args){ |
| 91 |
|
| 92 |
Parser parser=new Parser(); |
| 93 |
parser.out1=out1; |
| 94 |
for(int i=0; i<args.length; i++){ |
| 95 |
String arg=args[i]; |
| 96 |
String[] split=arg.split("="); |
| 97 |
String a=split[0].toLowerCase(); |
| 98 |
String b=split.length>1 ? split[1] : null; |
| 99 |
if(b!=null && b.equalsIgnoreCase("null")){b=null;} |
| 100 |
|
| 101 |
if(a.equals("lines")){ |
| 102 |
maxLines=Long.parseLong(b); |
| 103 |
if(maxLines<0){maxLines=Long.MAX_VALUE;} |
| 104 |
}else if(a.equals("refbases") || a.equals("refsize") || a.equals("reflen")){ |
| 105 |
refLen=Long.parseLong(b); |
| 106 |
}else if(a.equals("verbose")){ |
| 107 |
verbose=Parse.parseBoolean(b); |
| 108 |
ByteFile1.verbose=verbose; |
| 109 |
ByteFile2.verbose=verbose; |
| 110 |
ReadWrite.verbose=verbose; |
| 111 |
}else if(parser.parse(arg, a, b)){ |
| 112 |
|
| 113 |
}else if(b==null && new File(arg).exists()){ |
| 114 |
in.add(arg); |
| 115 |
}else{ |
| 116 |
outstream.println("Unknown parameter "+args[i]); |
| 117 |
assert(false) : "Unknown parameter "+args[i]; |
| 118 |
|
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
return parser; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
private void fixExtensions(){ |
| 127 |
|
| 128 |
if(in.isEmpty()){throw new RuntimeException("Error - at least one input file is required.");} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
private void checkFileExistence(){ |
| 133 |
|
| 134 |
if(!Tools.testOutputFiles(overwrite, append, false, out1)){ |
| 135 |
outstream.println((out1==null)+", "+out1); |
| 136 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output file "+out1+"\n"); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
if(!Tools.testInputFiles(false, true, in.toArray(new String[0]))){ |
| 141 |
throw new RuntimeException("\nCan't read some input files.\n"); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
private static void checkStatics(){ |
| 152 |
|
| 153 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 154 |
ByteFile.FORCE_MODE_BF2=true; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
void process(Timer t){ |
| 168 |
|
| 169 |
ByteStreamWriter bsw=makeBSW(ffout1); |
| 170 |
|
| 171 |
if(bsw!=null){ |
| 172 |
|
| 173 |
String header="#Sample\tAvgCov\t%>=1x\t%>=2x\t%>=3x\t%>=4x\t%>=5x\t%>=10x\t%>=20x"; |
| 174 |
bsw.println(header); |
| 175 |
linesOut++; |
| 176 |
bytesOut+=header.length(); |
| 177 |
} |
| 178 |
|
| 179 |
for(String fname : in){ |
| 180 |
FileFormat ffin=FileFormat.testInput(fname, FileFormat.TXT, null, true, true); |
| 181 |
ByteFile bf=ByteFile.makeByteFile(ffin); |
| 182 |
String name=ReadWrite.stripToCore(fname); |
| 183 |
if(name.toLowerCase().endsWith("_basecov")){ |
| 184 |
name=name.substring(0, name.lastIndexOf('_')); |
| 185 |
} |
| 186 |
processInner(bf, bsw, name); |
| 187 |
errorState|=bf.close(); |
| 188 |
} |
| 189 |
|
| 190 |
if(bsw!=null){errorState|=bsw.poisonAndWait();} |
| 191 |
|
| 192 |
t.stop(); |
| 193 |
|
| 194 |
outstream.println(Tools.timeLinesBytesProcessed(t, linesProcessed, bytesProcessed, 8)); |
| 195 |
|
| 196 |
outstream.println(); |
| 197 |
outstream.println("Valid Lines: \t"+linesOut); |
| 198 |
outstream.println("Invalid Lines: \t"+(linesProcessed-linesOut)); |
| 199 |
|
| 200 |
if(errorState){ |
| 201 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
private void processInner(ByteFile bf, ByteStreamWriter bsw, String name){ |
| 210 |
byte[] line=bf.nextLine(); |
| 211 |
final int max=20; |
| 212 |
long[] hist=new long[max+1]; |
| 213 |
long covSum=0; |
| 214 |
while(line!=null){ |
| 215 |
if(line.length>0){ |
| 216 |
if(maxLines>0 && linesProcessed>=maxLines){break;} |
| 217 |
linesProcessed++; |
| 218 |
bytesProcessed+=(line.length+1); |
| 219 |
|
| 220 |
final boolean valid=(line[0]!='#'); |
| 221 |
|
| 222 |
if(valid){ |
| 223 |
int tab=Tools.lastIndexOf(line, (byte)'\t'); |
| 224 |
int cov=Parse.parseInt(line, tab+1); |
| 225 |
covSum+=cov; |
| 226 |
hist[Tools.min(max, cov)]++; |
| 227 |
}else{ |
| 228 |
|
| 229 |
} |
| 230 |
} |
| 231 |
line=bf.nextLine(); |
| 232 |
} |
| 233 |
long len=shared.Vector.sum(hist); |
| 234 |
if(refLen>0){ |
| 235 |
assert(refLen>=len); |
| 236 |
len=refLen; |
| 237 |
} |
| 238 |
final double mult=1.0/Tools.max(1, len); |
| 239 |
|
| 240 |
final ByteBuilder bb=new ByteBuilder(); |
| 241 |
|
| 242 |
for(int i=hist.length-1; i>0; i--){ |
| 243 |
hist[i-1]+=hist[i]; |
| 244 |
} |
| 245 |
bb.append(name).tab(); |
| 246 |
bb.append(covSum*mult, 2).tab(); |
| 247 |
bb.append(hist[1]*mult*100, 2).tab(); |
| 248 |
bb.append(hist[2]*mult*100, 2).tab(); |
| 249 |
bb.append(hist[3]*mult*100, 2).tab(); |
| 250 |
bb.append(hist[4]*mult*100, 2).tab(); |
| 251 |
bb.append(hist[5]*mult*100, 2).tab(); |
| 252 |
bb.append(hist[10]*mult*100, 2).tab(); |
| 253 |
bb.append(hist[20]*mult*100, 2).tab(); |
| 254 |
bb.nl(); |
| 255 |
linesOut++; |
| 256 |
bytesOut+=bb.length; |
| 257 |
if(bsw!=null){bsw.print(bb);} |
| 258 |
} |
| 259 |
|
| 260 |
private static ByteStreamWriter makeBSW(FileFormat ff){ |
| 261 |
if(ff==null){return null;} |
| 262 |
ByteStreamWriter bsw=new ByteStreamWriter(ff); |
| 263 |
bsw.start(); |
| 264 |
return bsw; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
private LinkedHashSet<String> in=new LinkedHashSet<String>(); |
| 272 |
private String out1="stdout.txt"; |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
private long linesProcessed=0; |
| 277 |
private long linesOut=0; |
| 278 |
private long bytesProcessed=0; |
| 279 |
private long bytesOut=0; |
| 280 |
|
| 281 |
private long maxLines=Long.MAX_VALUE; |
| 282 |
|
| 283 |
private long refLen=-1; |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
private final FileFormat ffout1; |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
private PrintStream outstream=System.err; |
| 297 |
public static boolean verbose=false; |
| 298 |
public boolean errorState=false; |
| 299 |
private boolean overwrite=true; |
| 300 |
private boolean append=false; |
| 301 |
|
| 302 |
} |