| 1 |
package var; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.util.ArrayList; |
| 5 |
|
| 6 |
import align2.IndexMaker4; |
| 7 |
import dna.ChromosomeArray; |
| 8 |
import dna.Data; |
| 9 |
import dna.FastaToChromArrays2; |
| 10 |
import fileIO.ReadWrite; |
| 11 |
import shared.Parse; |
| 12 |
import shared.PreParser; |
| 13 |
import shared.Shared; |
| 14 |
import shared.Timer; |
| 15 |
import shared.Tools; |
| 16 |
import tracker.ReadStats; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public class ApplyVarsToReference { |
| 24 |
|
| 25 |
public static void main(String[] args){ |
| 26 |
{ |
| 27 |
PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false); |
| 28 |
args=pp.args; |
| 29 |
|
| 30 |
} |
| 31 |
|
| 32 |
Timer t=new Timer(); |
| 33 |
|
| 34 |
String inPattern=args[0]; |
| 35 |
|
| 36 |
int minChrom=-1; |
| 37 |
int maxChrom=-1; |
| 38 |
int outgenome=-1; |
| 39 |
Data.GENOME_BUILD=-1; |
| 40 |
String name=null; |
| 41 |
|
| 42 |
for(int i=1; i<args.length; i++){ |
| 43 |
final String arg=args[i].toLowerCase(); |
| 44 |
String[] split=arg.split("="); |
| 45 |
String a=split[0]; |
| 46 |
String b=split.length>1 ? split[1] : null; |
| 47 |
|
| 48 |
if(a.equals("ingenome")){ |
| 49 |
Data.setGenome(Integer.parseInt(b)); |
| 50 |
if(minChrom==-1){minChrom=1;} |
| 51 |
if(maxChrom==-1){maxChrom=Data.numChroms;} |
| 52 |
}else if(a.equals("outgenome")){ |
| 53 |
outgenome=Integer.parseInt(b); |
| 54 |
}else if(a.equals("minchrom")){ |
| 55 |
minChrom=Integer.parseInt(b); |
| 56 |
}else if(a.equals("maxchrom")){ |
| 57 |
maxChrom=Integer.parseInt(b); |
| 58 |
}else if(a.equals("threads") || a.equals("t")){ |
| 59 |
THREADS=Integer.parseInt(b); |
| 60 |
}else if(a.equals("nblocksize")){ |
| 61 |
N_BLOCK_SIZE=Integer.parseInt(b); |
| 62 |
}else if(a.equals("nblocktrigger")){ |
| 63 |
N_BLOCK_TRIGGER=Integer.parseInt(b); |
| 64 |
}else if(a.equals("staynearref")){ |
| 65 |
STAY_NEAR_REF=Parse.parseBoolean(b); |
| 66 |
}else if(a.equals("append") || a.equals("app")){ |
| 67 |
append=ReadStats.append=Parse.parseBoolean(b); |
| 68 |
}else if(a.equals("overwrite") || a.equals("ow")){ |
| 69 |
overwrite=Parse.parseBoolean(b); |
| 70 |
}else if(a.startsWith("regen")){ |
| 71 |
REGEN_N_BLOCKS=Parse.parseBoolean(b); |
| 72 |
}else if(a.startsWith("name=")){ |
| 73 |
REGEN_N_BLOCKS=Parse.parseBoolean(b); |
| 74 |
}else{ |
| 75 |
System.err.println("Unknown argument "+arg); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
assert(Data.GENOME_BUILD>-1); |
| 80 |
assert(outgenome>-1); |
| 81 |
|
| 82 |
if(Data.GENOME_BUILD==outgenome){ |
| 83 |
System.out.println("Warning! Overwriting input genome "+outgenome); |
| 84 |
} |
| 85 |
|
| 86 |
String fname=Data.chromFname(minChrom, outgenome); |
| 87 |
File f=new File(fname.substring(0, fname.lastIndexOf('/'))); |
| 88 |
|
| 89 |
if(!f.exists()){f.mkdirs();} |
| 90 |
|
| 91 |
for(int chrom=minChrom; chrom<=maxChrom; chrom++){ |
| 92 |
String outName=Data.chromFname(chrom, outgenome); |
| 93 |
assert(overwrite || !new File(outName).exists()) : "Destination "+outName+" already exists."; |
| 94 |
|
| 95 |
process(inPattern.replaceFirst("#", ""+chrom), outName, chrom); |
| 96 |
} |
| 97 |
|
| 98 |
FastaToChromArrays2.writeInfo(outgenome, maxChrom, (name==null ? Data.name : name), ""+Data.GENOME_BUILD+"_plus_variations", false, false); |
| 99 |
|
| 100 |
t.stop(); |
| 101 |
|
| 102 |
{ |
| 103 |
String path=IndexMaker4.fname(1, 1, 12, 1); |
| 104 |
int lastSlash=path.lastIndexOf('/'); |
| 105 |
path=path.substring(0, lastSlash); |
| 106 |
File dir=new File(path); |
| 107 |
if(dir.exists()){ |
| 108 |
System.out.println("Deleting old index for "+outgenome); |
| 109 |
for(File f2 : dir.listFiles()){ |
| 110 |
if(f2.isFile() && (f2.getName().contains(".int2d") || f2.getName().endsWith(".txt"))){ |
| 111 |
f2.delete(); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
System.out.println(); |
| 120 |
System.out.println("Time: \t"+t); |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
public static void process(String inVarsName, String outChromName, int chrom) { |
| 125 |
ArrayList<Varlet> vars=Varlet.fromTextFile(inVarsName); |
| 126 |
ChromosomeArray cha=Data.getChromosome(chrom); |
| 127 |
ChromosomeArray chb=new ChromosomeArray(chrom, Shared.PLUS); |
| 128 |
|
| 129 |
|
| 130 |
int aloc=0; |
| 131 |
|
| 132 |
int bloc=0; |
| 133 |
|
| 134 |
for(int i=0; i<vars.size(); i++){ |
| 135 |
|
| 136 |
Varlet v=vars.get(i); |
| 137 |
assert(v.beginLoc>=aloc) : i+"\n"+vars.get(i-1)+"\n"+v+"\n"; |
| 138 |
|
| 139 |
while(v.beginLoc<aloc){ |
| 140 |
System.err.print("e"); |
| 141 |
i++; |
| 142 |
if(i>=vars.size()){break;} |
| 143 |
v=vars.get(i); |
| 144 |
} |
| 145 |
|
| 146 |
if(STAY_NEAR_REF && Tools.absdif(aloc, bloc)>=REF_LIMIT){ |
| 147 |
int dif=v.lengthDif(); |
| 148 |
|
| 149 |
if(aloc<bloc){ |
| 150 |
while(dif>0){ |
| 151 |
|
| 152 |
i++; |
| 153 |
if(i>=vars.size()){break;} |
| 154 |
v=vars.get(i); |
| 155 |
dif=v.lengthDif(); |
| 156 |
} |
| 157 |
}else{ |
| 158 |
while(dif<0){ |
| 159 |
|
| 160 |
i++; |
| 161 |
if(i>=vars.size()){break;} |
| 162 |
v=vars.get(i); |
| 163 |
dif=v.lengthDif(); |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
while(aloc<v.beginLoc){ |
| 170 |
byte b=cha.get(aloc); |
| 171 |
chb.set(bloc, b); |
| 172 |
aloc++; |
| 173 |
bloc++; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
if(v.varType==Variation.SNP){ |
| 178 |
String call=v.call; |
| 179 |
String ref=v.ref; |
| 180 |
if(ref!=null && ref.equals("=")){ref=null;} |
| 181 |
for(int j=0; j<call.length(); j++){ |
| 182 |
char c=call.charAt(j); |
| 183 |
if(ref!=null){ |
| 184 |
assert(ref.charAt(j)==cha.get(aloc)) : "\n"+i+", "+v; |
| 185 |
} |
| 186 |
chb.set(bloc, c); |
| 187 |
aloc++; |
| 188 |
bloc++; |
| 189 |
} |
| 190 |
}else if(v.varType==Variation.DELINS){ |
| 191 |
String call=v.call; |
| 192 |
for(int j=0; j<call.length(); j++){ |
| 193 |
char c=call.charAt(j); |
| 194 |
chb.set(bloc, c); |
| 195 |
bloc++; |
| 196 |
} |
| 197 |
aloc+=v.lengthRef(); |
| 198 |
}else if(v.varType==Variation.NOCALL){ |
| 199 |
|
| 200 |
if(!foundNocall){ |
| 201 |
System.err.println("*** Warning - found a nocall in input variations ***"); |
| 202 |
foundNocall=true; |
| 203 |
} |
| 204 |
}else if(v.varType==Variation.NOREF){ |
| 205 |
String call=v.call; |
| 206 |
for(int j=0; j<call.length(); j++){ |
| 207 |
char c=call.charAt(j); |
| 208 |
assert(cha.get(aloc)=='N') : cha.get(aloc); |
| 209 |
chb.set(bloc, c); |
| 210 |
aloc++; |
| 211 |
bloc++; |
| 212 |
} |
| 213 |
}else if(v.varType==Variation.INS){ |
| 214 |
String call=v.call; |
| 215 |
for(int j=0; j<call.length(); j++){ |
| 216 |
char c=call.charAt(j); |
| 217 |
chb.set(bloc, c); |
| 218 |
bloc++; |
| 219 |
} |
| 220 |
}else if(v.varType==Variation.DEL){ |
| 221 |
int len=v.lengthRef(); |
| 222 |
assert(len>0); |
| 223 |
aloc+=len; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
while(aloc<cha.array.length || aloc<=cha.maxIndex){ |
| 229 |
byte c=cha.get(aloc); |
| 230 |
chb.set(bloc, c); |
| 231 |
aloc++; |
| 232 |
bloc++; |
| 233 |
} |
| 234 |
|
| 235 |
System.out.println("Length Shift for chr"+chrom+": \t"+(bloc-aloc)); |
| 236 |
|
| 237 |
Data.unload(chrom, true); |
| 238 |
cha=null; |
| 239 |
|
| 240 |
if(REGEN_N_BLOCKS){ |
| 241 |
chb=regenNBlocks(chb, N_BLOCK_SIZE, N_BLOCK_TRIGGER, N_BLOCK_END_SIZE); |
| 242 |
} |
| 243 |
|
| 244 |
chb.resize(chb.maxIndex+1); |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
ReadWrite.write(chb, outChromName, false); |
| 251 |
} |
| 252 |
|
| 253 |
public static ChromosomeArray regenNBlocks(ChromosomeArray cha, int blocksize, int trigger, int endsize){ |
| 254 |
ChromosomeArray chb=new ChromosomeArray(cha.chromosome, cha.strand, cha.minIndex, cha.maxIndex); |
| 255 |
chb.maxIndex=-1; |
| 256 |
|
| 257 |
int aloc=0; |
| 258 |
int bloc=0; |
| 259 |
int ns=0; |
| 260 |
|
| 261 |
|
| 262 |
while(cha.get(aloc)=='N'){ |
| 263 |
chb.set(bloc, 'N'); |
| 264 |
ns++; |
| 265 |
aloc++; |
| 266 |
bloc++; |
| 267 |
} |
| 268 |
while(ns<endsize){ |
| 269 |
chb.set(bloc, 'N'); |
| 270 |
ns++; |
| 271 |
bloc++; |
| 272 |
} |
| 273 |
ns=0; |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
while(aloc<=cha.maxIndex){ |
| 278 |
byte b=cha.get(aloc); |
| 279 |
if(b=='N'){ |
| 280 |
ns++; |
| 281 |
}else{ |
| 282 |
if(ns>=trigger){ |
| 283 |
while(ns<blocksize){ |
| 284 |
chb.set(bloc, 'N'); |
| 285 |
bloc++; |
| 286 |
ns++; |
| 287 |
} |
| 288 |
} |
| 289 |
ns=0; |
| 290 |
} |
| 291 |
chb.set(bloc, b); |
| 292 |
aloc++; |
| 293 |
bloc++; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
ns=0; |
| 299 |
for(int i=chb.maxIndex; i>=0; i--){ |
| 300 |
if(chb.get(i)!='N'){break;} |
| 301 |
} |
| 302 |
while(ns<endsize){ |
| 303 |
chb.set(chb.maxIndex+1, 'N'); |
| 304 |
ns++; |
| 305 |
} |
| 306 |
|
| 307 |
return chb; |
| 308 |
} |
| 309 |
|
| 310 |
public static int THREADS=1; |
| 311 |
|
| 312 |
private static boolean foundNocall=false; |
| 313 |
private static boolean STAY_NEAR_REF=false; |
| 314 |
private static final int REF_LIMIT=20; |
| 315 |
public static boolean REGEN_N_BLOCKS=true; |
| 316 |
public static int N_BLOCK_END_SIZE=2000; |
| 317 |
public static int N_BLOCK_SIZE=300; |
| 318 |
public static int N_BLOCK_TRIGGER=80; |
| 319 |
|
| 320 |
public static boolean overwrite=true; |
| 321 |
|
| 322 |
public static boolean append=false; |
| 323 |
|
| 324 |
} |