| 1 |
package jgi; |
| 2 |
|
| 3 |
import java.io.PrintStream; |
| 4 |
import java.util.ArrayList; |
| 5 |
|
| 6 |
import dna.Data; |
| 7 |
import fileIO.FileFormat; |
| 8 |
import fileIO.ReadWrite; |
| 9 |
import fileIO.TextStreamWriter; |
| 10 |
import shared.MetadataWriter; |
| 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 stream.ConcurrentReadInputStream; |
| 18 |
import stream.FastaReadInputStream; |
| 19 |
import stream.Read; |
| 20 |
import structures.ListNum; |
| 21 |
import tracker.ReadStats; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class MakeLengthHistogram { |
| 29 |
|
| 30 |
public static void main(String[] args){ |
| 31 |
|
| 32 |
{ |
| 33 |
PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false); |
| 34 |
args=pp.args; |
| 35 |
outstream=pp.outstream; |
| 36 |
} |
| 37 |
|
| 38 |
Timer t=new Timer(); |
| 39 |
|
| 40 |
String in1=null, in2=null; |
| 41 |
String out=null; |
| 42 |
|
| 43 |
Data.GENOME_BUILD=-1; |
| 44 |
ReadWrite.USE_UNPIGZ=true; |
| 45 |
Shared.capBufferLen(20); |
| 46 |
|
| 47 |
|
| 48 |
for(int i=0; i<args.length; i++){ |
| 49 |
|
| 50 |
final String arg=args[i]; |
| 51 |
String[] split=arg.split("="); |
| 52 |
String a=split[0].toLowerCase(); |
| 53 |
String b=split.length>1 ? split[1] : null; |
| 54 |
|
| 55 |
if(Parser.parseZip(arg, a, b)){ |
| 56 |
|
| 57 |
}else if(Parser.parseQuality(arg, a, b)){ |
| 58 |
|
| 59 |
}else if(a.equals("reads") || a.equals("maxreads")){ |
| 60 |
maxReads=Parse.parseKMG(b); |
| 61 |
}else if(a.equals("append") || a.equals("app")){ |
| 62 |
append=ReadStats.append=Parse.parseBoolean(b); |
| 63 |
}else if(a.equals("overwrite") || a.equals("ow")){ |
| 64 |
overwrite=Parse.parseBoolean(b); |
| 65 |
}else if(a.equals("in") || a.equals("in1")){ |
| 66 |
in1=b; |
| 67 |
}else if(a.equals("in2")){ |
| 68 |
in2=b; |
| 69 |
}else if(a.equals("out") || a.equals("hist") || a.equals("lhist")){ |
| 70 |
out=b; |
| 71 |
}else if(a.equals("max") || a.equals("maxlength")){ |
| 72 |
MAX_LENGTH=Integer.parseInt(b); |
| 73 |
}else if(a.equals("nzo") || a.equals("nonzeroonly")){ |
| 74 |
NON_ZERO_ONLY=Parse.parseBoolean(b); |
| 75 |
}else if(a.startsWith("mult") || a.startsWith("div") || a.startsWith("bin")){ |
| 76 |
MULT=Integer.parseInt(b); |
| 77 |
}else if(a.equals("round")){ |
| 78 |
ROUND_BINS=Parse.parseBoolean(b); |
| 79 |
}else if(i==0 && !arg.contains("=")){ |
| 80 |
in1=arg; |
| 81 |
}else if(i==1 && !arg.contains("=")){ |
| 82 |
in2=arg; |
| 83 |
}else if(i==3 && !arg.contains("=")){ |
| 84 |
out=arg; |
| 85 |
}else{ |
| 86 |
throw new RuntimeException("Unknown argument: "+arg); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
{ |
| 91 |
Parser.processQuality(); |
| 92 |
} |
| 93 |
|
| 94 |
MAX_LENGTH/=MULT; |
| 95 |
|
| 96 |
calc(in1, in2, out); |
| 97 |
t.stop(); |
| 98 |
outstream.println("Time: \t"+t); |
| 99 |
MetadataWriter.write(null, readsProcessed, basesProcessed, 0, 0, false); |
| 100 |
|
| 101 |
|
| 102 |
Shared.closeStream(outstream); |
| 103 |
} |
| 104 |
|
| 105 |
public static void calc(String in1, String in2, String out){ |
| 106 |
|
| 107 |
FastaReadInputStream.MIN_READ_LEN=1; |
| 108 |
|
| 109 |
final ConcurrentReadInputStream cris; |
| 110 |
{ |
| 111 |
FileFormat ff1=FileFormat.testInput(in1, FileFormat.FASTQ, null, true, true); |
| 112 |
FileFormat ff2=FileFormat.testInput(in2, FileFormat.FASTQ, null, true, true); |
| 113 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ff1, ff2); |
| 114 |
|
| 115 |
cris.start(); |
| 116 |
} |
| 117 |
boolean paired=cris.paired(); |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
final int max=MAX_LENGTH; |
| 122 |
long[] readHist=new long[max+1]; |
| 123 |
long[] baseHist=new long[max+1]; |
| 124 |
|
| 125 |
int maxFound=0; |
| 126 |
int minFound=Integer.MAX_VALUE; |
| 127 |
|
| 128 |
{ |
| 129 |
ListNum<Read> ln=cris.nextList(); |
| 130 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 131 |
|
| 132 |
if(reads!=null && !reads.isEmpty()){ |
| 133 |
Read r=reads.get(0); |
| 134 |
assert(paired==(r.mate!=null)); |
| 135 |
} |
| 136 |
|
| 137 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 138 |
|
| 139 |
for(Read r1 : reads){ |
| 140 |
final Read r2=r1.mate; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
if(r1!=null && r1.bases!=null){ |
| 145 |
readsProcessed++; |
| 146 |
final int x=r1.length(); |
| 147 |
basesProcessed+=x; |
| 148 |
final int y=Tools.min(max, ((ROUND_BINS ? x+MULT/2 : x))/MULT); |
| 149 |
readHist[y]++; |
| 150 |
baseHist[y]+=x; |
| 151 |
maxFound=Tools.max(maxFound, x); |
| 152 |
minFound=Tools.min(minFound, x); |
| 153 |
} |
| 154 |
|
| 155 |
if(r2!=null && r2.bases!=null){ |
| 156 |
readsProcessed++; |
| 157 |
final int x=r2.length(); |
| 158 |
basesProcessed+=x; |
| 159 |
final int y=Tools.min(max, ((ROUND_BINS ? x+MULT/2 : x))/MULT); |
| 160 |
readHist[y]++; |
| 161 |
baseHist[y]+=x; |
| 162 |
maxFound=Tools.max(maxFound, x); |
| 163 |
minFound=Tools.min(minFound, x); |
| 164 |
} |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
cris.returnList(ln); |
| 169 |
|
| 170 |
ln=cris.nextList(); |
| 171 |
reads=(ln!=null ? ln.list : null); |
| 172 |
} |
| 173 |
if(verbose){outstream.println("Finished reading");} |
| 174 |
cris.returnList(ln); |
| 175 |
if(verbose){outstream.println("Returned list");} |
| 176 |
ReadWrite.closeStream(cris); |
| 177 |
if(verbose){outstream.println("Closed stream");} |
| 178 |
outstream.println("Processed "+readsProcessed+" reads."); |
| 179 |
} |
| 180 |
|
| 181 |
if(readsProcessed<1){minFound=0;} |
| 182 |
double stdev=Tools.standardDeviationHistogram(readHist)*MULT; |
| 183 |
int median=Tools.percentileHistogram(readHist, 0.5)*MULT; |
| 184 |
int mode=Tools.calcModeHistogram(readHist)*MULT; |
| 185 |
|
| 186 |
double[] readHistF=new double[max+1]; |
| 187 |
long[] readHistC=new long[max+1]; |
| 188 |
double[] readHistCF=new double[max+1]; |
| 189 |
|
| 190 |
double[] baseHistF=new double[max+1]; |
| 191 |
long[] baseHistC=new long[max+1]; |
| 192 |
double[] baseHistCF=new double[max+1]; |
| 193 |
|
| 194 |
|
| 195 |
readHistC[max]=readHist[max]; |
| 196 |
baseHistC[max]=baseHist[max]; |
| 197 |
for(int i=max; i>0; i--){ |
| 198 |
readHistC[i-1]=readHistC[i]+readHist[i-1]; |
| 199 |
baseHistC[i-1]=baseHistC[i]+baseHist[i-1]; |
| 200 |
} |
| 201 |
for(int i=0; i<=max; i++){ |
| 202 |
readHistCF[i]=readHistC[i]*100d/readHistC[0]; |
| 203 |
readHistF[i]=readHist[i]*100d/readHistC[0]; |
| 204 |
baseHistCF[i]=baseHistC[i]*100d/baseHistC[0]; |
| 205 |
baseHistF[i]=baseHist[i]*100d/baseHistC[0]; |
| 206 |
} |
| 207 |
|
| 208 |
TextStreamWriter tsw=new TextStreamWriter(out==null ? "stdout" : out, overwrite, append, false); |
| 209 |
tsw.start(); |
| 210 |
tsw.println("#Reads:\t"+readsProcessed); |
| 211 |
tsw.println("#Bases:\t"+baseHistC[0]); |
| 212 |
tsw.println("#Max:\t"+maxFound); |
| 213 |
tsw.println("#Min:\t"+minFound); |
| 214 |
tsw.println("#Avg:\t"+Tools.format("%.1f",(baseHistC[0]*1d/readsProcessed))); |
| 215 |
tsw.println("#Median:\t"+median); |
| 216 |
tsw.println("#Mode:\t"+mode); |
| 217 |
tsw.println("#Std_Dev:\t"+Tools.format("%.1f",stdev)); |
| 218 |
tsw.println("#Read Length Histogram:"); |
| 219 |
tsw.println("#Length\treads\tpct_reads\tcum_reads\tcum_pct_reads\tbases\tpct_bases\tcum_bases\tcum_pct_bases"); |
| 220 |
for(int i=0; i<=max; i++){ |
| 221 |
if(readHist[i]>0 || !NON_ZERO_ONLY){ |
| 222 |
tsw.println((i*MULT)+"\t"+readHist[i]+Tools.format("\t%.3f%%", readHistF[i])+"\t"+readHistC[i]+Tools.format("\t%.3f%%", readHistCF[i])+ |
| 223 |
"\t"+baseHist[i]+Tools.format("\t%.3f%%", baseHistF[i])+"\t"+baseHistC[i]+Tools.format("\t%.3f%%", baseHistCF[i])); |
| 224 |
} |
| 225 |
if(i*MULT>=maxFound){break;} |
| 226 |
} |
| 227 |
tsw.poisonAndWait(); |
| 228 |
} |
| 229 |
|
| 230 |
public static long maxReads=-1; |
| 231 |
public static long readsProcessed=0; |
| 232 |
public static long basesProcessed=0; |
| 233 |
public static int MAX_LENGTH=80000; |
| 234 |
public static int MULT=10; |
| 235 |
public static boolean ROUND_BINS=false; |
| 236 |
public static boolean NON_ZERO_ONLY=true; |
| 237 |
|
| 238 |
static PrintStream outstream=System.err; |
| 239 |
|
| 240 |
public static boolean append=false; |
| 241 |
public static boolean overwrite=true; |
| 242 |
public static boolean verbose=false; |
| 243 |
|
| 244 |
} |