| 1 |
package jgi; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.PrintStream; |
| 5 |
import java.util.ArrayList; |
| 6 |
|
| 7 |
import dna.AminoAcid; |
| 8 |
import fileIO.ByteFile; |
| 9 |
import fileIO.ByteFile1; |
| 10 |
import fileIO.ByteFile2; |
| 11 |
import fileIO.FileFormat; |
| 12 |
import fileIO.ReadWrite; |
| 13 |
import shared.KillSwitch; |
| 14 |
import shared.Parse; |
| 15 |
import shared.Parser; |
| 16 |
import shared.PreParser; |
| 17 |
import shared.Shared; |
| 18 |
import shared.Timer; |
| 19 |
import shared.Tools; |
| 20 |
import stream.ConcurrentGenericReadInputStream; |
| 21 |
import stream.ConcurrentReadInputStream; |
| 22 |
import stream.ConcurrentReadOutputStream; |
| 23 |
import stream.FASTQ; |
| 24 |
import stream.FastaReadInputStream; |
| 25 |
import stream.Read; |
| 26 |
import structures.ListNum; |
| 27 |
import tracker.ReadStats; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class FakeReads { |
| 34 |
|
| 35 |
public static void main(String[] args){ |
| 36 |
Timer t=new Timer(); |
| 37 |
FakeReads x=new FakeReads(args); |
| 38 |
x.process(t); |
| 39 |
|
| 40 |
|
| 41 |
Shared.closeStream(x.outstream); |
| 42 |
} |
| 43 |
|
| 44 |
public FakeReads(String[] args){ |
| 45 |
|
| 46 |
{ |
| 47 |
PreParser pp=new PreParser(args, getClass(), false); |
| 48 |
args=pp.args; |
| 49 |
outstream=pp.outstream; |
| 50 |
} |
| 51 |
|
| 52 |
FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false; |
| 53 |
|
| 54 |
Shared.capBuffers(4); |
| 55 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 56 |
ReadWrite.setZipThreads(Shared.threads()); |
| 57 |
|
| 58 |
|
| 59 |
for(int i=0; i<args.length; i++){ |
| 60 |
String arg=args[i]; |
| 61 |
String[] split=arg.split("="); |
| 62 |
String a=split[0].toLowerCase(); |
| 63 |
String b=split.length>1 ? split[1] : null; |
| 64 |
|
| 65 |
if(Parser.parseCommonStatic(arg, a, b)){ |
| 66 |
|
| 67 |
}else if(Parser.parseZip(arg, a, b)){ |
| 68 |
|
| 69 |
}else if(Parser.parseQuality(arg, a, b)){ |
| 70 |
|
| 71 |
}else if(Parser.parseFasta(arg, a, b)){ |
| 72 |
|
| 73 |
}else if(a.equals("passes")){ |
| 74 |
assert(false) : "'passes' is disabled."; |
| 75 |
|
| 76 |
}else if(a.equals("verbose")){ |
| 77 |
verbose=Parse.parseBoolean(b); |
| 78 |
ByteFile1.verbose=verbose; |
| 79 |
ByteFile2.verbose=verbose; |
| 80 |
stream.FastaReadInputStream.verbose=verbose; |
| 81 |
ConcurrentGenericReadInputStream.verbose=verbose; |
| 82 |
stream.FastqReadInputStream.verbose=verbose; |
| 83 |
ReadWrite.verbose=verbose; |
| 84 |
}else if(a.equals("addspacer") || a.equals("addspace") || a.equals("usespacer")){ |
| 85 |
addSpacer=Parse.parseBoolean(b); |
| 86 |
}else if(a.equals("reads") || a.equals("maxreads")){ |
| 87 |
maxReads=Parse.parseKMG(b); |
| 88 |
}else if(a.equals("t") || a.equals("threads")){ |
| 89 |
Shared.setThreads(b); |
| 90 |
}else if(a.equals("in") || a.equals("input") || a.equals("in1") || a.equals("input1")){ |
| 91 |
in1=b; |
| 92 |
}else if(a.equals("out") || a.equals("output") || a.equals("out1") || a.equals("output1")){ |
| 93 |
out1=b; |
| 94 |
}else if(a.equals("out2") || a.equals("output2")){ |
| 95 |
out2=b; |
| 96 |
}else if(a.equals("identifier") || a.equals("id")){ |
| 97 |
identifier=b; |
| 98 |
}else if(a.equals("qfin") || a.equals("qfin1")){ |
| 99 |
qfin1=b; |
| 100 |
}else if(a.equals("qfout") || a.equals("qfout1")){ |
| 101 |
qfout1=b; |
| 102 |
}else if(a.equals("qfout2")){ |
| 103 |
qfout2=b; |
| 104 |
}else if(a.equals("extin")){ |
| 105 |
extin=b; |
| 106 |
}else if(a.equals("extout")){ |
| 107 |
extout=b; |
| 108 |
}else if(a.equals("append") || a.equals("app")){ |
| 109 |
append=ReadStats.append=Parse.parseBoolean(b); |
| 110 |
}else if(a.equals("overwrite") || a.equals("ow")){ |
| 111 |
overwrite=Parse.parseBoolean(b); |
| 112 |
}else if(a.startsWith("minscaf") || a.startsWith("mincontig")){ |
| 113 |
stream.FastaReadInputStream.MIN_READ_LEN=Integer.parseInt(b); |
| 114 |
}else if(a.equals("ml") || a.equals("minlen") || a.equals("minlength")){ |
| 115 |
minReadLength=Integer.parseInt(b); |
| 116 |
}else if(a.equals("length") || a.equals("maxlen") || a.equals("length")){ |
| 117 |
desiredLength=Integer.parseInt(b); |
| 118 |
}else if(a.equals("split")){ |
| 119 |
SPLITMODE=Parse.parseBoolean(b); |
| 120 |
}else if(a.equals("overlap")){ |
| 121 |
SPLITMODE=true; |
| 122 |
overlap=Integer.parseInt(b); |
| 123 |
}else if(in1==null && i==0 && Tools.looksLikeInputStream(arg)){ |
| 124 |
in1=arg; |
| 125 |
if(arg.indexOf('#')>-1 && !new File(arg).exists() && b!=null){ |
| 126 |
in1=b.replace("#", "1"); |
| 127 |
} |
| 128 |
}else{ |
| 129 |
System.err.println("Unknown parameter "+args[i]); |
| 130 |
assert(false) : "Unknown parameter "+args[i]; |
| 131 |
|
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
{ |
| 136 |
Parser.processQuality(); |
| 137 |
} |
| 138 |
|
| 139 |
if(identifier==null){identifier="";} |
| 140 |
else{identifier=identifier+"_";} |
| 141 |
|
| 142 |
if(!addSpacer){spacer="";} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
if(out1!=null && out2==null && out1.indexOf('#')>-1){ |
| 149 |
out2=out1.replace("#", "2"); |
| 150 |
out1=out1.replace("#", "1"); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
assert(FastaReadInputStream.settingsOK()); |
| 158 |
|
| 159 |
if(in1==null){throw new RuntimeException("Error - at least one input file is required.");} |
| 160 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 161 |
|
| 162 |
ByteFile.FORCE_MODE_BF2=true; |
| 163 |
} |
| 164 |
|
| 165 |
if(out1==null && out2!=null){throw new RuntimeException("Error - cannot define out2 without defining out1.");} |
| 166 |
|
| 167 |
if(out1!=null && out1.equalsIgnoreCase("null")){out1=null;} |
| 168 |
if(out2!=null && out2.equalsIgnoreCase("null")){out2=null;} |
| 169 |
|
| 170 |
if(!Tools.testOutputFiles(overwrite, append, false, out1, out2)){ |
| 171 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+", "+out2+"\n"); |
| 172 |
} |
| 173 |
|
| 174 |
ffout1=FileFormat.testOutput(out1, FileFormat.FASTQ, extout, true, overwrite, append, false); |
| 175 |
ffout2=FileFormat.testOutput(out2, FileFormat.FASTQ, extout, true, overwrite, append, false); |
| 176 |
|
| 177 |
ffin1=FileFormat.testInput(in1, FileFormat.FASTQ, extin, true, true); |
| 178 |
} |
| 179 |
|
| 180 |
void process(Timer t){ |
| 181 |
|
| 182 |
|
| 183 |
final ConcurrentReadInputStream cris; |
| 184 |
{ |
| 185 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, null, qfin1, null); |
| 186 |
if(verbose){System.err.println("Started cris");} |
| 187 |
cris.start(); |
| 188 |
} |
| 189 |
boolean paired=cris.paired(); |
| 190 |
if(verbose){System.err.println("Input is "+(paired ? "paired" : "unpaired"));} |
| 191 |
|
| 192 |
ConcurrentReadOutputStream ros=null; |
| 193 |
if(out1!=null){ |
| 194 |
final int buff=4; |
| 195 |
|
| 196 |
if(cris.paired() && out2==null && (in1==null || !in1.contains(".sam"))){ |
| 197 |
outstream.println("Writing interleaved."); |
| 198 |
} |
| 199 |
|
| 200 |
assert(!out1.equalsIgnoreCase(in1) && !out1.equalsIgnoreCase(in1)) : "Input file and output file have same name."; |
| 201 |
assert(out2==null || (!out2.equalsIgnoreCase(in1) && !out2.equalsIgnoreCase(out1))) : "out1 and out2 have same name."; |
| 202 |
|
| 203 |
ros=ConcurrentReadOutputStream.getStream(ffout1, ffout2, qfout1, qfout2, buff, null, false); |
| 204 |
ros.start(); |
| 205 |
} |
| 206 |
|
| 207 |
long readsProcessed=0; |
| 208 |
long basesProcessed=0; |
| 209 |
|
| 210 |
{ |
| 211 |
|
| 212 |
ListNum<Read> ln=cris.nextList(); |
| 213 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 214 |
|
| 215 |
if(reads!=null && !reads.isEmpty()){ |
| 216 |
Read r=reads.get(0); |
| 217 |
assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired()); |
| 218 |
} |
| 219 |
|
| 220 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 221 |
ArrayList<Read> fake=new ArrayList<Read>(reads.size()); |
| 222 |
|
| 223 |
for(int idx=0; idx<reads.size(); idx++){ |
| 224 |
Read r=reads.get(idx); |
| 225 |
{ |
| 226 |
readsProcessed++; |
| 227 |
basesProcessed+=r.length(); |
| 228 |
} |
| 229 |
assert(r.mate==null); |
| 230 |
|
| 231 |
boolean remove=r.length()<minReadLength || (minReadLength+overlap)<2; |
| 232 |
|
| 233 |
if(remove){ |
| 234 |
|
| 235 |
}else{ |
| 236 |
int len=Tools.min(r.length(), desiredLength); |
| 237 |
if(SPLITMODE){len=Tools.min(r.length(), (r.length()+overlap+1)/2);} |
| 238 |
|
| 239 |
byte[] bases1=KillSwitch.copyOfRange(r.bases, 0, len); |
| 240 |
byte[] bases2=KillSwitch.copyOfRange(r.bases, r.length()-len, r.length()); |
| 241 |
AminoAcid.reverseComplementBasesInPlace(bases2); |
| 242 |
|
| 243 |
byte[] qual1=null; |
| 244 |
byte[] qual2=null; |
| 245 |
if(r.quality!=null){ |
| 246 |
qual1=KillSwitch.copyOfRange(r.quality, 0, len); |
| 247 |
qual2=KillSwitch.copyOfRange(r.quality, r.quality.length-len, r.quality.length); |
| 248 |
Tools.reverseInPlace(qual2); |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
Read a=new Read(bases1, qual1, identifier+r.numericID+spacer+"/1", r.numericID, 0); |
| 253 |
Read b=new Read(bases2, qual2, identifier+r.numericID+spacer+"/2", r.numericID, Read.PAIRNUMMASK); |
| 254 |
a.mate=b; |
| 255 |
b.mate=a; |
| 256 |
fake.add(a); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
if(ros!=null){ros.add(fake, ln.id);} |
| 261 |
|
| 262 |
cris.returnList(ln); |
| 263 |
ln=cris.nextList(); |
| 264 |
reads=(ln!=null ? ln.list : null); |
| 265 |
} |
| 266 |
if(ln!=null){ |
| 267 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
errorState|=ReadWrite.closeStreams(cris, ros); |
| 272 |
|
| 273 |
t.stop(); |
| 274 |
outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8)); |
| 275 |
|
| 276 |
if(errorState){ |
| 277 |
throw new RuntimeException("FakeReads terminated in an error state; the output may be corrupt."); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
public boolean errorState=false; |
| 286 |
|
| 287 |
public String identifier=null; |
| 288 |
|
| 289 |
private String in1=null; |
| 290 |
|
| 291 |
private boolean addSpacer=true; |
| 292 |
private String spacer=" "; |
| 293 |
|
| 294 |
private String qfin1=null; |
| 295 |
|
| 296 |
private String out1=null; |
| 297 |
private String out2=null; |
| 298 |
|
| 299 |
private String qfout1=null; |
| 300 |
private String qfout2=null; |
| 301 |
|
| 302 |
private String extin=null; |
| 303 |
private String extout=null; |
| 304 |
|
| 305 |
private boolean overwrite=true; |
| 306 |
private boolean append=false; |
| 307 |
|
| 308 |
private long maxReads=-1; |
| 309 |
private int minReadLength=1; |
| 310 |
private int desiredLength=250; |
| 311 |
private int overlap=50; |
| 312 |
private boolean SPLITMODE=false; |
| 313 |
|
| 314 |
private final FileFormat ffin1; |
| 315 |
|
| 316 |
private final FileFormat ffout1; |
| 317 |
private final FileFormat ffout2; |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
private PrintStream outstream=System.err; |
| 323 |
public static boolean verbose=false; |
| 324 |
|
| 325 |
} |
| 326 |
|