| 1 |
package sort; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.HashMap; |
| 6 |
|
| 7 |
import dna.Data; |
| 8 |
import fileIO.FileFormat; |
| 9 |
import fileIO.ReadWrite; |
| 10 |
import fileIO.TextStreamWriter; |
| 11 |
import shared.Parse; |
| 12 |
import shared.Parser; |
| 13 |
import shared.PreParser; |
| 14 |
import shared.Shared; |
| 15 |
import shared.Timer; |
| 16 |
import stream.ConcurrentReadInputStream; |
| 17 |
import stream.Read; |
| 18 |
import structures.ByteBuilder; |
| 19 |
import structures.ListNum; |
| 20 |
import tracker.ReadStats; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public class SortReadsByID { |
| 28 |
|
| 29 |
public static void main(String[] args){ |
| 30 |
|
| 31 |
{ |
| 32 |
PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false); |
| 33 |
args=pp.args; |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
Parser parser=new Parser(); |
| 38 |
String in1=null; |
| 39 |
String in2=null; |
| 40 |
String out="raw_idsorted#.txt.gz"; |
| 41 |
|
| 42 |
for(int i=0; i<args.length; i++){ |
| 43 |
final String arg=args[i]; |
| 44 |
final String[] split=arg.split("="); |
| 45 |
String a=split[0].toLowerCase(); |
| 46 |
String b=split.length>1 ? split[1] : null; |
| 47 |
|
| 48 |
if(Parser.parseCommonStatic(arg, a, b)){ |
| 49 |
|
| 50 |
}else if(Parser.parseZip(arg, a, b)){ |
| 51 |
|
| 52 |
}else if(Parser.parseQuality(arg, a, b)){ |
| 53 |
|
| 54 |
}else if(parser.parseInterleaved(arg, a, b)){ |
| 55 |
|
| 56 |
}else if(a.equals("i") || a.equals("in") || a.equals("input") || a.equals("in1") || a.equals("input1")){ |
| 57 |
assert(b!=null) : "Bad parameter: "+arg; |
| 58 |
in1=b; |
| 59 |
if(b.indexOf('#')>=0){ |
| 60 |
in1=b.replaceFirst("#", "1"); |
| 61 |
in2=b.replaceFirst("#", "2"); |
| 62 |
} |
| 63 |
}else if(a.equals("in2") || a.equals("input2")){ |
| 64 |
in2=b; |
| 65 |
}else if(a.equals("o") || a.equals("out") || a.equals("output")){ |
| 66 |
out=b; |
| 67 |
}else if(a.endsWith("renumber")){ |
| 68 |
RENUMBER=Parse.parseBoolean(b); |
| 69 |
}else if(a.equals("append") || a.equals("app")){ |
| 70 |
append=ReadStats.append=Parse.parseBoolean(b); |
| 71 |
}else if(a.equals("overwrite") || a.equals("ow")){ |
| 72 |
overwrite=Parse.parseBoolean(b); |
| 73 |
Data.sysout.println("Set overwrite to "+overwrite); |
| 74 |
}else if(a.endsWith("blocksize")){ |
| 75 |
BLOCKSIZE=Integer.parseInt(b); |
| 76 |
}else{ |
| 77 |
throw new RuntimeException("Unknown parameter: "+args[i]); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
if(in1==null){throw new RuntimeException("Please specify input file.");} |
| 82 |
if(out==null){throw new RuntimeException("Please specify output file.");} |
| 83 |
if(in1.equalsIgnoreCase(in2) || in1.equalsIgnoreCase(out) || (in2!=null && in2.equalsIgnoreCase(out))){ |
| 84 |
throw new RuntimeException("Duplicate filenames."); |
| 85 |
} |
| 86 |
|
| 87 |
if(out!=null && !out.contains("#")){ |
| 88 |
throw new RuntimeException("Output filename must contain '#' symbol."); |
| 89 |
} |
| 90 |
|
| 91 |
SortReadsByID srid=new SortReadsByID(in1, in2, out); |
| 92 |
srid.process(); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
public void process(){ |
| 97 |
|
| 98 |
Timer tRead=new Timer(); |
| 99 |
Timer tSort=new Timer(); |
| 100 |
Timer tAll=new Timer(); |
| 101 |
|
| 102 |
tRead.start(); |
| 103 |
tAll.start(); |
| 104 |
|
| 105 |
final long maxReads=-1; |
| 106 |
ConcurrentReadInputStream cris; |
| 107 |
{ |
| 108 |
FileFormat ff1=FileFormat.testInput(in1, FileFormat.FASTQ, null, true, true); |
| 109 |
FileFormat ff2=FileFormat.testInput(in2, FileFormat.FASTQ, null, true, true); |
| 110 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ff1, ff2); |
| 111 |
cris.start(); |
| 112 |
} |
| 113 |
|
| 114 |
HashMap<Integer, Block> map=new HashMap<Integer, Block>(); |
| 115 |
|
| 116 |
{ |
| 117 |
ListNum<Read> ln=cris.nextList(); |
| 118 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 119 |
|
| 120 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 121 |
|
| 122 |
for(Read r : reads){ |
| 123 |
|
| 124 |
int bin=(int)(r.numericID/BLOCKSIZE); |
| 125 |
Block b=map.get(bin); |
| 126 |
if(b==null){ |
| 127 |
String o1=out.replaceFirst("#", "_bin"+bin+"_1"); |
| 128 |
String o2=(cris.paired() && !OUT_INTERLEAVED) ? out.replaceFirst("#", "_bin"+bin+"_2") : null; |
| 129 |
b=new Block(o1, o2); |
| 130 |
map.put(bin, b); |
| 131 |
} |
| 132 |
b.add(r); |
| 133 |
} |
| 134 |
|
| 135 |
cris.returnList(ln); |
| 136 |
|
| 137 |
ln=cris.nextList(); |
| 138 |
reads=(ln!=null ? ln.list : null); |
| 139 |
} |
| 140 |
|
| 141 |
cris.returnList(ln); |
| 142 |
ReadWrite.closeStream(cris); |
| 143 |
} |
| 144 |
|
| 145 |
for(Block b : map.values()){b.close();} |
| 146 |
|
| 147 |
tRead.stop(); |
| 148 |
Data.sysout.println("Read time: \t"+tRead); |
| 149 |
tSort.start(); |
| 150 |
|
| 151 |
String o1=out.replaceFirst("#", "1"); |
| 152 |
String o2=(cris.paired() && !OUT_INTERLEAVED) ? out.replaceFirst("#", "2") : null; |
| 153 |
Block sorted=new Block(o1, o2); |
| 154 |
|
| 155 |
long count=0; |
| 156 |
|
| 157 |
ArrayList<Integer> keys=new ArrayList<Integer>(); |
| 158 |
keys.addAll(map.keySet()); |
| 159 |
Shared.sort(keys); |
| 160 |
for(Integer key : keys){ |
| 161 |
Block b=map.get(key); |
| 162 |
b.join(); |
| 163 |
map.remove(key); |
| 164 |
{ |
| 165 |
FileFormat ff1=FileFormat.testInput(b.out1, FileFormat.FASTQ, null, true, true); |
| 166 |
FileFormat ff2=FileFormat.testInput(b.out2, FileFormat.FASTQ, null, true, true); |
| 167 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ff1, ff2); |
| 168 |
cris.start(); |
| 169 |
} |
| 170 |
ArrayList<Read> reads2=new ArrayList<Read>((int)b.count); |
| 171 |
count+=b.count; |
| 172 |
|
| 173 |
{ |
| 174 |
ListNum<Read> ln=cris.nextList(); |
| 175 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 176 |
|
| 177 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 178 |
reads2.addAll(reads); |
| 179 |
|
| 180 |
cris.returnList(ln); |
| 181 |
|
| 182 |
ln=cris.nextList(); |
| 183 |
reads=(ln!=null ? ln.list : null); |
| 184 |
} |
| 185 |
|
| 186 |
cris.returnList(ln); |
| 187 |
ReadWrite.closeStream(cris); |
| 188 |
} |
| 189 |
|
| 190 |
Shared.sort(reads2, ReadComparatorID.comparator); |
| 191 |
for(Read r : reads2){sorted.add(r);} |
| 192 |
new File(b.out1).delete(); |
| 193 |
if(b.out2!=null){new File(b.out2).delete();} |
| 194 |
} |
| 195 |
|
| 196 |
sorted.close(); |
| 197 |
sorted.join(); |
| 198 |
|
| 199 |
tSort.stop(); |
| 200 |
tAll.stop(); |
| 201 |
|
| 202 |
Data.sysout.println("Total reads: \t"+count); |
| 203 |
Data.sysout.println("Sort time: \t"+tSort); |
| 204 |
Data.sysout.println("Total time: \t"+tAll); |
| 205 |
|
| 206 |
} |
| 207 |
|
| 208 |
public SortReadsByID(String in1_, String in2_, String out_) { |
| 209 |
in1=in1_; |
| 210 |
in2=in2_; |
| 211 |
out=out_; |
| 212 |
|
| 213 |
FileFormat ff=FileFormat.testOutput(out, FileFormat.BREAD, null, true, false, append, false); |
| 214 |
outFastq=ff.fastq(); |
| 215 |
outFasta=ff.fasta(); |
| 216 |
outText=ff.bread(); |
| 217 |
} |
| 218 |
|
| 219 |
public String in1; |
| 220 |
public String in2; |
| 221 |
public String out; |
| 222 |
|
| 223 |
private final boolean outText; |
| 224 |
private final boolean outFasta; |
| 225 |
private final boolean outFastq; |
| 226 |
|
| 227 |
public static int BLOCKSIZE=8000000; |
| 228 |
public static boolean overwrite=true; |
| 229 |
public static boolean append=false; |
| 230 |
public static boolean RENUMBER=false; |
| 231 |
public static boolean OUT_INTERLEAVED=false; |
| 232 |
|
| 233 |
private class Block{ |
| 234 |
|
| 235 |
public Block(String out1_, String out2_){ |
| 236 |
out1=out1_; |
| 237 |
out2=out2_; |
| 238 |
|
| 239 |
tsw1=new TextStreamWriter(out1, overwrite, false, false); |
| 240 |
tsw2=(out2==null ? null : new TextStreamWriter(out2, overwrite, false, false)); |
| 241 |
|
| 242 |
tsw1.start(); |
| 243 |
if(tsw2!=null){tsw2.start();} |
| 244 |
} |
| 245 |
|
| 246 |
public void add(Read r){ |
| 247 |
count++; |
| 248 |
Read r2=r.mate; |
| 249 |
|
| 250 |
ByteBuilder sb1=outText ? r.toText(true) : outFastq ? r.toFastq() : outFasta ? r.toFasta() : null; |
| 251 |
ByteBuilder sb2=r2==null ? null : outText ? r2.toText(true) : outFastq ? r2.toFastq() : outFasta ? r2.toFasta() : null; |
| 252 |
|
| 253 |
tsw1.print(sb1.append('\n')); |
| 254 |
if(sb2!=null){ |
| 255 |
if(tsw2!=null){ |
| 256 |
tsw2.print(sb2.append('\n')); |
| 257 |
}else{ |
| 258 |
tsw1.print(sb2.append('\n')); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
} |
| 263 |
|
| 264 |
public void close(){ |
| 265 |
tsw1.poison(); |
| 266 |
if(tsw2!=null){tsw2.poison();} |
| 267 |
} |
| 268 |
|
| 269 |
public void join(){ |
| 270 |
tsw1.waitForFinish(); |
| 271 |
if(tsw2!=null){tsw2.waitForFinish();} |
| 272 |
} |
| 273 |
|
| 274 |
String out1; |
| 275 |
String out2; |
| 276 |
|
| 277 |
TextStreamWriter tsw1; |
| 278 |
TextStreamWriter tsw2; |
| 279 |
|
| 280 |
long count=0; |
| 281 |
|
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
} |