| 1 |
package var2; |
| 2 |
|
| 3 |
import java.io.PrintStream; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.HashSet; |
| 6 |
import java.util.Map.Entry; |
| 7 |
|
| 8 |
import fileIO.ByteFile; |
| 9 |
import fileIO.ByteFile1; |
| 10 |
import fileIO.ByteFile2; |
| 11 |
import fileIO.ByteStreamWriter; |
| 12 |
import fileIO.FileFormat; |
| 13 |
import fileIO.ReadWrite; |
| 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.FastaReadInputStream; |
| 22 |
import structures.ByteBuilder; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class CompareVCF { |
| 30 |
|
| 31 |
public static void main(String[] args){ |
| 32 |
Timer t=new Timer(); |
| 33 |
CompareVCF x=new CompareVCF(args); |
| 34 |
x.process(t); |
| 35 |
|
| 36 |
|
| 37 |
Shared.closeStream(x.outstream); |
| 38 |
} |
| 39 |
|
| 40 |
public CompareVCF(String[] args){ |
| 41 |
|
| 42 |
{ |
| 43 |
PreParser pp=new PreParser(args, getClass(), false); |
| 44 |
args=pp.args; |
| 45 |
outstream=pp.outstream; |
| 46 |
} |
| 47 |
|
| 48 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 49 |
ReadWrite.setZipThreads(Shared.threads()); |
| 50 |
|
| 51 |
int mode_=DIFFERENCE; |
| 52 |
|
| 53 |
Parser parser=new Parser(); |
| 54 |
for(int i=0; i<args.length; i++){ |
| 55 |
String arg=args[i]; |
| 56 |
String[] split=arg.split("="); |
| 57 |
String a=split[0].toLowerCase(); |
| 58 |
String b=split.length>1 ? split[1] : null; |
| 59 |
|
| 60 |
if(a.equals("lines")){ |
| 61 |
maxLines=Long.parseLong(b); |
| 62 |
if(maxLines<0){maxLines=Long.MAX_VALUE;} |
| 63 |
}else if(a.equals("difference") || a.equals("minus") || a.equals("dif") || a.equals("diff") || a.equals("subtraction") || a.equals("subtract")){ |
| 64 |
mode_=DIFFERENCE; |
| 65 |
}else if(a.equals("union") || a.equals("plus")){ |
| 66 |
mode_=UNION; |
| 67 |
}else if(a.equals("intersection") || a.equals("shared")){ |
| 68 |
mode_=INTERSECTION; |
| 69 |
}else if(a.equals("addsamples")){ |
| 70 |
addSamples=Parse.parseBoolean(b); |
| 71 |
}else if(a.equals("verbose")){ |
| 72 |
verbose=Parse.parseBoolean(b); |
| 73 |
ByteFile1.verbose=verbose; |
| 74 |
ByteFile2.verbose=verbose; |
| 75 |
stream.FastaReadInputStream.verbose=verbose; |
| 76 |
ConcurrentGenericReadInputStream.verbose=verbose; |
| 77 |
stream.FastqReadInputStream.verbose=verbose; |
| 78 |
ReadWrite.verbose=verbose; |
| 79 |
}else if(a.equals("splitalleles")) { |
| 80 |
splitAlleles=Parse.parseBoolean(b); |
| 81 |
}else if(a.equals("splitsubs") || a.equals("splitsnps")) { |
| 82 |
splitSubs=Parse.parseBoolean(b); |
| 83 |
}else if(a.equals("splitcomplex")) { |
| 84 |
splitComplex=Parse.parseBoolean(b); |
| 85 |
}else if(a.equals("sass") || a.equals("split")) { |
| 86 |
splitAlleles=splitSubs=Parse.parseBoolean(b); |
| 87 |
}else if(a.equals("splitall") || a.equals("sascsss")) { |
| 88 |
splitAlleles=splitComplex=splitSubs=Parse.parseBoolean(b); |
| 89 |
}else if(a.equals("minscore") || a.equals("minqual") || a.equals("minq")) { |
| 90 |
minScore=Double.parseDouble(b); |
| 91 |
}else if(a.equals("trimtocanonical") || a.equals("canonicalize") || a.equals("canonicize") || a.equals("canonize")){ |
| 92 |
VCFLine.TRIM_TO_CANONICAL=Parse.parseBoolean(b); |
| 93 |
}else if(a.equals("ref")){ |
| 94 |
ref=b; |
| 95 |
}else if(parser.parse(arg, a, b)){ |
| 96 |
|
| 97 |
}else{ |
| 98 |
outstream.println("Unknown parameter "+args[i]); |
| 99 |
assert(false) : "Unknown parameter "+args[i]; |
| 100 |
|
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
mode=mode_; |
| 105 |
{ |
| 106 |
in1=(parser.in1==null ? null : parser.in1.split(",")); |
| 107 |
out1=parser.out1; |
| 108 |
overwrite=parser.overwrite; |
| 109 |
append=parser.append; |
| 110 |
} |
| 111 |
|
| 112 |
assert(FastaReadInputStream.settingsOK()); |
| 113 |
|
| 114 |
if(in1==null || in1.length<2){throw new RuntimeException("Error - at least two input files are required.");} |
| 115 |
|
| 116 |
if(!ByteFile.FORCE_MODE_BF2){ |
| 117 |
ByteFile.FORCE_MODE_BF2=false; |
| 118 |
ByteFile.FORCE_MODE_BF1=true; |
| 119 |
} |
| 120 |
ffin1=new FileFormat[in1.length]; |
| 121 |
for(int i=0; i<in1.length; i++){ |
| 122 |
ffin1[i]=FileFormat.testInput(in1[i], FileFormat.VCF, null, true, true); |
| 123 |
} |
| 124 |
|
| 125 |
ffout1=FileFormat.testOutput(out1, FileFormat.VCF, null, true, overwrite, append, false); |
| 126 |
if(ffout1!=null && ffout1.type()==FileFormat.VAR){outputVar=true;} |
| 127 |
if(ref!=null){ScafMap.loadReference(ref, null, null, true);} |
| 128 |
} |
| 129 |
|
| 130 |
public HashSet<VCFLine> getSet(FileFormat ff, HashSet<VCFLine> set){ |
| 131 |
if(set==null){set=new HashSet<VCFLine>();} |
| 132 |
VCFFile vfile=new VCFFile(ff); |
| 133 |
samples.addAll(vfile.sampleNames); |
| 134 |
if(header==null){ |
| 135 |
header=vfile.header; |
| 136 |
if(ScafMap.defaultScafMap()==null){ |
| 137 |
ScafMap.setDefaultScafMap(vfile.toScafMap(null), ff.name()); |
| 138 |
} |
| 139 |
} |
| 140 |
for(Entry<VCFLine, VCFLine> e : vfile.map.entrySet()){ |
| 141 |
VCFLine v=e.getValue(); |
| 142 |
ArrayList<VCFLine> list=null; |
| 143 |
if(splitAlleles || splitComplex || splitSubs){list=v.split(splitAlleles, splitComplex, splitSubs);} |
| 144 |
if(list==null || list.isEmpty()){ |
| 145 |
if(!set.contains(v) && v.qual>=minScore){set.add(v);} |
| 146 |
}else{ |
| 147 |
for(VCFLine line : list){ |
| 148 |
if(!set.contains(line) && v.qual>=minScore){set.add(line);} |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
linesProcessed+=vfile.linesProcessed(); |
| 154 |
headerLinesProcessed+=vfile.header.size(); |
| 155 |
variantLinesProcessed+=vfile.map.size(); |
| 156 |
bytesProcessed+=vfile.bytesProcessed(); |
| 157 |
|
| 158 |
errorState|=vfile.errorState; |
| 159 |
return set; |
| 160 |
} |
| 161 |
|
| 162 |
public HashSet<VCFLine> union(){ |
| 163 |
final HashSet<VCFLine> set=new HashSet<VCFLine>(); |
| 164 |
for(FileFormat ff : ffin1){ |
| 165 |
getSet(ff, set); |
| 166 |
} |
| 167 |
return set; |
| 168 |
} |
| 169 |
|
| 170 |
public HashSet<VCFLine> intersection(){ |
| 171 |
HashSet<VCFLine> set0=null; |
| 172 |
for(FileFormat ff : ffin1){ |
| 173 |
HashSet<VCFLine> set=getSet(ff, null); |
| 174 |
if(set0==null){set0=set;} |
| 175 |
else{set0.retainAll(set);} |
| 176 |
} |
| 177 |
return set0; |
| 178 |
} |
| 179 |
|
| 180 |
public HashSet<VCFLine> difference(){ |
| 181 |
HashSet<VCFLine> set0=null; |
| 182 |
for(FileFormat ff : ffin1){ |
| 183 |
HashSet<VCFLine> set=getSet(ff, null); |
| 184 |
if(set0==null){set0=set;} |
| 185 |
else{set0.removeAll(set);} |
| 186 |
} |
| 187 |
return set0; |
| 188 |
} |
| 189 |
|
| 190 |
ArrayList<VCFLine> toList(){ |
| 191 |
final HashSet<VCFLine> set; |
| 192 |
if(mode==DIFFERENCE){ |
| 193 |
set=difference(); |
| 194 |
}else if(mode==UNION){ |
| 195 |
set=union(); |
| 196 |
}else if(mode==INTERSECTION){ |
| 197 |
set=intersection(); |
| 198 |
}else{ |
| 199 |
throw new RuntimeException("Unknown mode "+mode); |
| 200 |
} |
| 201 |
ArrayList<VCFLine> list=new ArrayList<VCFLine>(set.size()); |
| 202 |
list.addAll(set); |
| 203 |
Shared.sort(list); |
| 204 |
return list; |
| 205 |
} |
| 206 |
|
| 207 |
void process(Timer t){ |
| 208 |
|
| 209 |
ArrayList<VCFLine> list=toList(); |
| 210 |
|
| 211 |
ByteStreamWriter bsw=null; |
| 212 |
if(ffout1!=null){ |
| 213 |
bsw=new ByteStreamWriter(ffout1); |
| 214 |
bsw.start(); |
| 215 |
} |
| 216 |
|
| 217 |
{ |
| 218 |
for(byte[] line : header){ |
| 219 |
headerLinesOut++; |
| 220 |
if(bsw!=null){bsw.println(line);} |
| 221 |
} |
| 222 |
ByteBuilder bb=new ByteBuilder(33000); |
| 223 |
for(VCFLine line : list){ |
| 224 |
variantLinesOut++; |
| 225 |
if(outputVar){ |
| 226 |
assert(false) : "TODO"; |
| 227 |
|
| 228 |
|
| 229 |
}else{ |
| 230 |
line.toText(bb); |
| 231 |
} |
| 232 |
bb.nl(); |
| 233 |
if(bb.length>=32000){ |
| 234 |
if(bsw!=null){bsw.print(bb);} |
| 235 |
bb.clear(); |
| 236 |
} |
| 237 |
} |
| 238 |
if(bb.length>0){ |
| 239 |
if(bsw!=null){bsw.print(bb);} |
| 240 |
bb.clear(); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
if(bsw!=null){errorState|=bsw.poisonAndWait();} |
| 245 |
|
| 246 |
t.stop(); |
| 247 |
outstream.println(Tools.timeLinesBytesProcessed(t, linesProcessed, bytesProcessed, 8)); |
| 248 |
|
| 249 |
outstream.println(); |
| 250 |
outstream.println("Header Lines In: \t"+headerLinesProcessed); |
| 251 |
outstream.println("Variant Lines In: \t"+variantLinesProcessed); |
| 252 |
outstream.println("Header Lines Out: \t"+headerLinesOut); |
| 253 |
outstream.println("Variant Lines Out: \t"+variantLinesOut); |
| 254 |
|
| 255 |
if(errorState){ |
| 256 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
private long linesProcessed=0; |
| 265 |
private long headerLinesProcessed=0; |
| 266 |
private long variantLinesProcessed=0; |
| 267 |
private long headerLinesOut=0; |
| 268 |
private long variantLinesOut=0; |
| 269 |
private long bytesProcessed=0; |
| 270 |
|
| 271 |
private long maxLines=Long.MAX_VALUE; |
| 272 |
|
| 273 |
public ArrayList<byte[]> header=null; |
| 274 |
public ArrayList<String> samples=new ArrayList<String>(); |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
private String in1[]=null; |
| 279 |
private String out1=null; |
| 280 |
private String ref=null; |
| 281 |
|
| 282 |
private final FileFormat ffin1[]; |
| 283 |
private final FileFormat ffout1; |
| 284 |
|
| 285 |
public final int mode; |
| 286 |
|
| 287 |
public boolean addSamples=true; |
| 288 |
private boolean outputVar=false; |
| 289 |
|
| 290 |
boolean splitAlleles=false; |
| 291 |
boolean splitSubs=false; |
| 292 |
boolean splitComplex=false; |
| 293 |
double minScore=-99999; |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
public static int DIFFERENCE=0, UNION=1, INTERSECTION=2; |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
private PrintStream outstream=System.err; |
| 302 |
public static boolean verbose=false; |
| 303 |
public boolean errorState=false; |
| 304 |
private boolean overwrite=true; |
| 305 |
private boolean append=false; |
| 306 |
|
| 307 |
} |