| 1 |
package pacbio; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
|
| 5 |
import dna.Data; |
| 6 |
import fileIO.FileFormat; |
| 7 |
import fileIO.ReadWrite; |
| 8 |
import fileIO.TextStreamWriter; |
| 9 |
import shared.Parse; |
| 10 |
import shared.Parser; |
| 11 |
import shared.PreParser; |
| 12 |
import shared.Timer; |
| 13 |
import stream.ConcurrentReadInputStream; |
| 14 |
import stream.FastaReadInputStream; |
| 15 |
import stream.Read; |
| 16 |
import structures.ByteBuilder; |
| 17 |
import structures.ListNum; |
| 18 |
import tracker.ReadStats; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public class PartitionReads { |
| 26 |
|
| 27 |
public static void main(String[] args){ |
| 28 |
{ |
| 29 |
PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false); |
| 30 |
args=pp.args; |
| 31 |
|
| 32 |
} |
| 33 |
|
| 34 |
Timer t=new Timer(); |
| 35 |
|
| 36 |
boolean verbose=false; |
| 37 |
int ziplevel=-1; |
| 38 |
String in1=null; |
| 39 |
String in2=null; |
| 40 |
long maxReads=-1; |
| 41 |
String outname1=null; |
| 42 |
String outname2=null; |
| 43 |
|
| 44 |
for(int i=0; i<args.length; i++){ |
| 45 |
final String arg=args[i]; |
| 46 |
final String[] split=arg.split("="); |
| 47 |
String a=split[0].toLowerCase(); |
| 48 |
String b=split.length>1 ? split[1] : null; |
| 49 |
|
| 50 |
if(Parser.parseCommonStatic(arg, a, b)){ |
| 51 |
|
| 52 |
}else if(Parser.parseZip(arg, a, b)){ |
| 53 |
|
| 54 |
}else if(Parser.parseQuality(arg, a, b)){ |
| 55 |
|
| 56 |
}else if(Parser.parseFasta(arg, a, b)){ |
| 57 |
|
| 58 |
}else if(a.equals("path") || a.equals("root") || a.equals("tempdir")){ |
| 59 |
Data.setPath(b); |
| 60 |
}else if(a.equals("fasta") || a.equals("in") || a.equals("input") || a.equals("in1") || a.equals("input1")){ |
| 61 |
in1=b; |
| 62 |
if(b.indexOf('#')>-1){ |
| 63 |
in1=b.replace("#", "1"); |
| 64 |
in2=b.replace("#", "2"); |
| 65 |
} |
| 66 |
}else if(a.equals("in2") || a.equals("input2")){ |
| 67 |
in2=b; |
| 68 |
}else if(a.startsWith("partition")){ |
| 69 |
partitions=Integer.parseInt(b); |
| 70 |
}else if(a.equals("append") || a.equals("app")){ |
| 71 |
append=ReadStats.append=Parse.parseBoolean(b); |
| 72 |
}else if(a.equals("overwrite") || a.equals("ow")){ |
| 73 |
overwrite=Parse.parseBoolean(b); |
| 74 |
System.out.println("Set overwrite to "+overwrite); |
| 75 |
}else if(a.equals("reads") || a.equals("maxreads")){ |
| 76 |
maxReads=Parse.parseKMG(b); |
| 77 |
}else if(a.equals("out") || a.equals("out1")){ |
| 78 |
if(b==null || b.equalsIgnoreCase("null") || b.equalsIgnoreCase("none") || split.length==1){ |
| 79 |
System.out.println("No output file."); |
| 80 |
outname1=null; |
| 81 |
}else{ |
| 82 |
outname1=b; |
| 83 |
assert(!outname1.equalsIgnoreCase(outname2)); |
| 84 |
} |
| 85 |
}else if(a.equals("out2")){ |
| 86 |
if(b==null || b.equalsIgnoreCase("null") || b.equalsIgnoreCase("none") || split.length==1){ |
| 87 |
outname2=null; |
| 88 |
}else{ |
| 89 |
outname2=b; |
| 90 |
assert(!outname2.equalsIgnoreCase(outname1)); |
| 91 |
} |
| 92 |
}else if(a.startsWith("verbose")){ |
| 93 |
verbose=Parse.parseBoolean(b); |
| 94 |
}else{ |
| 95 |
throw new RuntimeException("Unknown parameter: "+args[i]); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
{ |
| 100 |
Parser.processQuality(); |
| 101 |
} |
| 102 |
|
| 103 |
assert(FastaReadInputStream.settingsOK()); |
| 104 |
assert(outname1==null || outname1.indexOf('#')>=0 || partitions<2); |
| 105 |
assert(outname2==null || outname2.indexOf('#')>=0 || partitions<2); |
| 106 |
assert(outname1==null || !outname1.equalsIgnoreCase(outname2)); |
| 107 |
|
| 108 |
if(in1==null){throw new RuntimeException("Please specify input file.");} |
| 109 |
|
| 110 |
|
| 111 |
final ConcurrentReadInputStream cris; |
| 112 |
{ |
| 113 |
FileFormat ff1=FileFormat.testInput(in1, FileFormat.FASTQ, null, true, true); |
| 114 |
FileFormat ff2=FileFormat.testInput(in2, FileFormat.FASTQ, null, true, true); |
| 115 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ff1, ff2); |
| 116 |
if(verbose){System.err.println("Started cris");} |
| 117 |
|
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
TextStreamWriter[] tsw1=new TextStreamWriter[partitions]; |
| 123 |
TextStreamWriter[] tsw2=new TextStreamWriter[partitions]; |
| 124 |
|
| 125 |
FileFormat ff=FileFormat.testOutput(outname1, FileFormat.FASTQ, null, true, overwrite, append, false); |
| 126 |
fastq=ff.fastq(); |
| 127 |
fasta=ff.fasta(); |
| 128 |
bread=ff.bread(); |
| 129 |
|
| 130 |
for(int i=0; i<partitions; i++){ |
| 131 |
tsw1[i]=new TextStreamWriter(outname1.replaceFirst("#", ""+i), overwrite, false, true); |
| 132 |
if(outname2!=null){ |
| 133 |
tsw2[i]=new TextStreamWriter(outname2.replaceFirst("#", ""+i), overwrite, false, true); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
long reads=process(tsw1, tsw2, cris); |
| 138 |
t.stop(); |
| 139 |
System.out.println("Reads: \t"+reads); |
| 140 |
System.out.println("Time: \t"+t); |
| 141 |
} |
| 142 |
|
| 143 |
public static long process(TextStreamWriter[] tsw1, TextStreamWriter[] tsw2, ConcurrentReadInputStream cris){ |
| 144 |
for(TextStreamWriter tsw : tsw1){if(tsw!=null){tsw.start();}} |
| 145 |
for(TextStreamWriter tsw : tsw2){if(tsw!=null){tsw.start();}} |
| 146 |
cris.start(); |
| 147 |
|
| 148 |
ListNum<Read> ln=cris.nextList(); |
| 149 |
ArrayList<Read> readlist=ln.list; |
| 150 |
|
| 151 |
final boolean paired=cris.paired(); |
| 152 |
|
| 153 |
long x=0; |
| 154 |
final int div=tsw1.length; |
| 155 |
while(!readlist.isEmpty()){ |
| 156 |
|
| 157 |
|
| 158 |
for(int i=0; i<readlist.size(); i++){ |
| 159 |
Read r=readlist.get(i); |
| 160 |
if(r!=null){ |
| 161 |
final Read r2=r.mate; |
| 162 |
final int mod=(int)(x%div); |
| 163 |
|
| 164 |
ByteBuilder a=null, b=null; |
| 165 |
|
| 166 |
if(fastq){ |
| 167 |
a=r.toFastq(); |
| 168 |
if(paired){b=r2.toFastq();} |
| 169 |
}else if(fasta){ |
| 170 |
a=r.toFasta(); |
| 171 |
if(paired){b=r2.toFasta();} |
| 172 |
}else if(bread){ |
| 173 |
a=r.toText(true); |
| 174 |
if(paired){b=(r2==null ? new ByteBuilder(".") : r2.toText(true));} |
| 175 |
}else{ |
| 176 |
throw new RuntimeException("Unsupported output format."); |
| 177 |
} |
| 178 |
|
| 179 |
a.append('\n'); |
| 180 |
tsw1[mod].print(a); |
| 181 |
if(paired){ |
| 182 |
b.append('\n'); |
| 183 |
if(tsw2[i]!=null){tsw2[i].print(b);} |
| 184 |
else{tsw1[i].print(b);} |
| 185 |
} |
| 186 |
|
| 187 |
x++; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
cris.returnList(ln.id, readlist.isEmpty()); |
| 192 |
|
| 193 |
|
| 194 |
ln=cris.nextList(); |
| 195 |
readlist=ln.list; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
assert(readlist.isEmpty()); |
| 200 |
cris.returnList(ln.id, readlist.isEmpty()); |
| 201 |
|
| 202 |
|
| 203 |
for(TextStreamWriter tsw : tsw1){if(tsw!=null){tsw.poison();}} |
| 204 |
for(TextStreamWriter tsw : tsw2){if(tsw!=null){tsw.poison();}} |
| 205 |
ReadWrite.closeStream(cris); |
| 206 |
return x; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
public static boolean overwrite=true; |
| 211 |
|
| 212 |
public static boolean append=false; |
| 213 |
public static int partitions=2; |
| 214 |
public static boolean fastq=false; |
| 215 |
public static boolean fasta=false; |
| 216 |
public static boolean bread=false; |
| 217 |
|
| 218 |
} |