| 1 |
package var2; |
| 2 |
|
| 3 |
import java.io.PrintStream; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.LinkedHashMap; |
| 6 |
import java.util.Map.Entry; |
| 7 |
|
| 8 |
import fileIO.ByteFile; |
| 9 |
import fileIO.ByteFile1; |
| 10 |
import fileIO.ByteFile2; |
| 11 |
import fileIO.FileFormat; |
| 12 |
import fileIO.ReadWrite; |
| 13 |
import shared.Parse; |
| 14 |
import shared.Parser; |
| 15 |
import shared.PreParser; |
| 16 |
import shared.Shared; |
| 17 |
import shared.Timer; |
| 18 |
import shared.Tools; |
| 19 |
import stream.ConcurrentGenericReadInputStream; |
| 20 |
import stream.FastaReadInputStream; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public class VCFFile { |
| 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 |
outstream=pp.outstream; |
| 35 |
} |
| 36 |
|
| 37 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 38 |
ReadWrite.setZipThreads(Shared.threads()); |
| 39 |
VCFLine.AUTOCACHE=true; |
| 40 |
|
| 41 |
Timer t=new Timer(); |
| 42 |
String in=null; |
| 43 |
long maxLines=Long.MAX_VALUE; |
| 44 |
|
| 45 |
Parser parser=new Parser(); |
| 46 |
for(int i=0; i<args.length; i++){ |
| 47 |
String arg=args[i]; |
| 48 |
String[] split=arg.split("="); |
| 49 |
String a=split[0].toLowerCase(); |
| 50 |
String b=split.length>1 ? split[1] : null; |
| 51 |
|
| 52 |
if(parser.parse(arg, a, b)){ |
| 53 |
|
| 54 |
}else if(a.equals("lines")){ |
| 55 |
maxLines=Long.parseLong(b); |
| 56 |
if(maxLines<0){maxLines=Long.MAX_VALUE;} |
| 57 |
}else if(a.equals("verbose")){ |
| 58 |
verbose=Parse.parseBoolean(b); |
| 59 |
ByteFile1.verbose=verbose; |
| 60 |
ByteFile2.verbose=verbose; |
| 61 |
stream.FastaReadInputStream.verbose=verbose; |
| 62 |
ConcurrentGenericReadInputStream.verbose=verbose; |
| 63 |
stream.FastqReadInputStream.verbose=verbose; |
| 64 |
ReadWrite.verbose=verbose; |
| 65 |
}else{ |
| 66 |
outstream.println("Unknown parameter "+args[i]); |
| 67 |
assert(false) : "Unknown parameter "+args[i]; |
| 68 |
|
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
{ |
| 73 |
in=parser.in1; |
| 74 |
} |
| 75 |
|
| 76 |
assert(FastaReadInputStream.settingsOK()); |
| 77 |
|
| 78 |
if(in==null){throw new RuntimeException("Error - at least one input file is required.");} |
| 79 |
|
| 80 |
if(!ByteFile.FORCE_MODE_BF2){ |
| 81 |
ByteFile.FORCE_MODE_BF2=false; |
| 82 |
ByteFile.FORCE_MODE_BF1=true; |
| 83 |
} |
| 84 |
|
| 85 |
VCFFile vf=new VCFFile(in); |
| 86 |
t.stop(); |
| 87 |
vf.printTime(t); |
| 88 |
|
| 89 |
|
| 90 |
Shared.closeStream(outstream); |
| 91 |
} |
| 92 |
|
| 93 |
public VCFFile(String s){ |
| 94 |
in1=s; |
| 95 |
ffin1=FileFormat.testInput(in1, FileFormat.TEXT, null, true, false); |
| 96 |
load(); |
| 97 |
} |
| 98 |
|
| 99 |
public VCFFile(FileFormat ff){ |
| 100 |
in1=ff.name(); |
| 101 |
ffin1=ff; |
| 102 |
load(); |
| 103 |
} |
| 104 |
|
| 105 |
void load(){ |
| 106 |
|
| 107 |
ByteFile bf=ByteFile.makeByteFile(ffin1); |
| 108 |
|
| 109 |
byte[] line=bf.nextLine(); |
| 110 |
|
| 111 |
while(line!=null && (line.length==0 || line[0]=='#')){ |
| 112 |
if(line.length>0){ |
| 113 |
if(maxLines>0 && linesProcessed>=maxLines){break;} |
| 114 |
linesProcessed++; |
| 115 |
bytesProcessed+=line.length; |
| 116 |
header.add(line); |
| 117 |
if(Tools.startsWith(line, CHROM_POS)){ |
| 118 |
String[] split=new String(line).split("\t"); |
| 119 |
for(int i=9; i<split.length; i++){ |
| 120 |
sampleNames.add(split[i]); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
line=bf.nextLine(); |
| 125 |
} |
| 126 |
if(ScafMap.defaultScafMap()==null){ScafMap.setDefaultScafMap(toScafMap(null), in1);} |
| 127 |
assert(ScafMap.defaultScafMap().size()>0) : ScafMap.defaultScafMap()+"\n"+headerToString(); |
| 128 |
while(line!=null){ |
| 129 |
if(line.length>0){ |
| 130 |
if(maxLines>0 && linesProcessed>=maxLines){break;} |
| 131 |
linesProcessed++; |
| 132 |
bytesProcessed+=line.length; |
| 133 |
|
| 134 |
final boolean isHeader=(line[0]=='#'); |
| 135 |
|
| 136 |
if(isHeader){ |
| 137 |
header.add(line); |
| 138 |
if(Tools.startsWith(line, CHROM_POS)){ |
| 139 |
String[] split=new String(line).split("\t"); |
| 140 |
for(int i=9; i<split.length; i++){ |
| 141 |
sampleNames.add(split[i]); |
| 142 |
} |
| 143 |
} |
| 144 |
}else{ |
| 145 |
VCFLine vline=new VCFLine(line); |
| 146 |
map.put(vline, vline); |
| 147 |
} |
| 148 |
} |
| 149 |
line=bf.nextLine(); |
| 150 |
} |
| 151 |
|
| 152 |
errorState|=bf.close(); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
void printTime(Timer t){ |
| 168 |
outstream.println(Tools.timeLinesBytesProcessed(t, linesProcessed, bytesProcessed, 8)); |
| 169 |
|
| 170 |
outstream.println(); |
| 171 |
outstream.println("Header Lines: \t"+header.size()); |
| 172 |
outstream.println("Variant Lines: \t"+map.size()); |
| 173 |
} |
| 174 |
|
| 175 |
public ArrayList<VCFLine> lines(boolean simplify){ |
| 176 |
ArrayList<VCFLine> lines=new ArrayList<VCFLine>(map.size()); |
| 177 |
for(Entry<VCFLine, VCFLine> e : map.entrySet()){ |
| 178 |
VCFLine line=e.getValue(); |
| 179 |
if(simplify && (line.isMulti() || line.isComplex())){ |
| 180 |
ArrayList<VCFLine> list=line.split(true, true, false); |
| 181 |
lines.addAll(list); |
| 182 |
}else{ |
| 183 |
lines.add(line); |
| 184 |
} |
| 185 |
} |
| 186 |
return lines; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
public ScafMap toScafMap(ScafMap sm){ |
| 202 |
if(sm==null){sm=new ScafMap();} |
| 203 |
for(byte[] line : header){ |
| 204 |
if(Tools.startsWith(line, "##contig=<ID=")){ |
| 205 |
sm.addFromVcf(line); |
| 206 |
} |
| 207 |
} |
| 208 |
return sm; |
| 209 |
} |
| 210 |
|
| 211 |
public String headerToString(){ |
| 212 |
StringBuilder sb=new StringBuilder(); |
| 213 |
for(byte[] line : header){ |
| 214 |
for(byte b : line){ |
| 215 |
sb.append((char)b); |
| 216 |
} |
| 217 |
sb.append('\n'); |
| 218 |
} |
| 219 |
return sb.toString(); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public long linesProcessed() { |
| 225 |
return linesProcessed; |
| 226 |
} |
| 227 |
|
| 228 |
public long bytesProcessed() { |
| 229 |
return bytesProcessed; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
private long linesProcessed=0; |
| 235 |
private long bytesProcessed=0; |
| 236 |
|
| 237 |
private long maxLines=Long.MAX_VALUE; |
| 238 |
|
| 239 |
public ArrayList<byte[]> header=new ArrayList<byte[]>(); |
| 240 |
public ArrayList<String> sampleNames=new ArrayList<String>(); |
| 241 |
public LinkedHashMap<VCFLine, VCFLine> map=new LinkedHashMap<VCFLine, VCFLine>(); |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
private String in1=null; |
| 246 |
|
| 247 |
private final FileFormat ffin1; |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
public static final String CHROM_POS="#CHROM\tPOS\t"; |
| 253 |
|
| 254 |
private static PrintStream outstream=System.err; |
| 255 |
public static boolean verbose=false; |
| 256 |
public boolean errorState=false; |
| 257 |
|
| 258 |
} |