| 1 |
package dna; |
| 2 |
import java.io.Serializable; |
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.Arrays; |
| 5 |
|
| 6 |
import fileIO.ReadWrite; |
| 7 |
import jgi.AssemblyStats2; |
| 8 |
import shared.KillSwitch; |
| 9 |
import shared.Shared; |
| 10 |
import shared.Tools; |
| 11 |
import structures.ByteBuilder; |
| 12 |
import structures.Range; |
| 13 |
|
| 14 |
|
| 15 |
public class ChromosomeArray implements Serializable { |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
private static final long serialVersionUID = 3199182397853127842L; |
| 22 |
|
| 23 |
public static void main(String[] args){ |
| 24 |
translateFile(args[1], Byte.parseByte(args[0])); |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
private static void translateFile(String fname, int chrom){ |
| 29 |
|
| 30 |
long time1=System.nanoTime(); |
| 31 |
|
| 32 |
ChromosomeArray cha=read(fname, chrom); |
| 33 |
cha.chromosome=chrom; |
| 34 |
long time2=System.nanoTime(); |
| 35 |
|
| 36 |
int dot=fname.lastIndexOf(".fa"); |
| 37 |
String outfile=fname.substring(0,dot).replace("hs_ref_", "")+".chrom"; |
| 38 |
|
| 39 |
System.out.println("Writing to "+outfile); |
| 40 |
|
| 41 |
System.out.println("minIndex="+cha.minIndex+", maxIndex="+cha.maxIndex+", length="+cha.array.length+ |
| 42 |
"; time="+Tools.format("%.3f seconds", (time2-time1)/1000000000d)); |
| 43 |
|
| 44 |
long time3=System.nanoTime(); |
| 45 |
ReadWrite.write(cha, outfile, false); |
| 46 |
cha=null; |
| 47 |
System.gc(); |
| 48 |
cha=read(outfile); |
| 49 |
long time4=System.nanoTime(); |
| 50 |
|
| 51 |
System.out.println("minIndex="+cha.minIndex+", maxIndex="+cha.maxIndex+", length="+cha.array.length+ |
| 52 |
"; time="+Tools.format("%.3f seconds", (time4-time3)/1000000000d)); |
| 53 |
} |
| 54 |
|
| 55 |
public static ChromosomeArray read(String fname, int chrom){ |
| 56 |
ChromosomeArray cha=read(fname); |
| 57 |
assert(cha.chromosome<1); |
| 58 |
cha.chromosome=chrom; |
| 59 |
return cha; |
| 60 |
} |
| 61 |
|
| 62 |
public static ChromosomeArray read(String fname){ |
| 63 |
|
| 64 |
|
| 65 |
ChromosomeArray ca=ReadWrite.read(ChromosomeArray.class, fname, false); |
| 66 |
if(CHANGE_UNDEFINED_TO_N_ON_READ){ |
| 67 |
ca.changeUndefinedToN(); |
| 68 |
} |
| 69 |
return ca; |
| 70 |
} |
| 71 |
|
| 72 |
public void changeUndefinedToN(){ |
| 73 |
for(int i=0; i<array.length; i++){ |
| 74 |
|
| 75 |
if(!AminoAcid.isACGTN(array[i])){array[i]='N';} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
public ChromosomeArray(){ |
| 80 |
this((byte)-1, Shared.PLUS); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
public ChromosomeArray complement(){ |
| 85 |
byte otherStrand=(strand==Shared.MINUS ? Shared.PLUS : Shared.MINUS); |
| 86 |
ChromosomeArray ca=new ChromosomeArray(chromosome, otherStrand, 0, maxIndex); |
| 87 |
for(int i=0; i<=maxIndex; i++){ |
| 88 |
int pos=maxIndex-i; |
| 89 |
byte b=AminoAcid.baseToComplementExtended[array[i]]; |
| 90 |
ca.array[pos]=b; |
| 91 |
} |
| 92 |
return ca; |
| 93 |
} |
| 94 |
|
| 95 |
public ChromosomeArray(int chrom, byte strnd){ |
| 96 |
this(chrom, strnd, Integer.MAX_VALUE, -1); |
| 97 |
} |
| 98 |
|
| 99 |
public ChromosomeArray(int chrom, byte strnd, int min, int max){ |
| 100 |
chromosome=chrom; |
| 101 |
strand=strnd; |
| 102 |
array=KillSwitch.allocByte1D(Tools.max(1000, max+1)); |
| 103 |
minIndex=min; |
| 104 |
maxIndex=max; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
public void set(int loc, int val){ |
| 109 |
|
| 110 |
if(loc>=array.length){ |
| 111 |
int newlen=(int)(1+(3L*max(array.length, loc))/2); |
| 112 |
assert(newlen>loc) : newlen+", "+loc+", "+array.length; |
| 113 |
resize(newlen); |
| 114 |
assert(array.length==newlen); |
| 115 |
|
| 116 |
} |
| 117 |
if(CHANGE_U_TO_T && CHANGE_DEGENERATE_TO_N){ |
| 118 |
val=AminoAcid.baseToACGTN[val]; |
| 119 |
}else{ |
| 120 |
val=Tools.toUpperCase((char)val); |
| 121 |
if(AminoAcid.baseToNumberExtended[val]<0){val='N';} |
| 122 |
} |
| 123 |
array[loc]=(val>Byte.MAX_VALUE ? Byte.MAX_VALUE : (byte)val); |
| 124 |
minIndex=min(loc, minIndex); |
| 125 |
maxIndex=max(loc, maxIndex); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
public void set(int loc, CharSequence s){ |
| 130 |
int loc2=loc+s.length(); |
| 131 |
if(loc2>array.length){ |
| 132 |
int newlen=(int)(1+(3L*max(array.length, loc2))/2); |
| 133 |
assert(newlen>loc2) : newlen+", "+loc2+", "+array.length; |
| 134 |
resize(newlen); |
| 135 |
assert(array.length==newlen); |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
if(CHANGE_U_TO_T && CHANGE_DEGENERATE_TO_N){ |
| 140 |
for(int i=0; i<s.length(); i++, loc++){ |
| 141 |
array[loc]=AminoAcid.baseToACGTN[s.charAt(i)]; |
| 142 |
} |
| 143 |
}else{ |
| 144 |
for(int i=0; i<s.length(); i++, loc++){ |
| 145 |
char c=Tools.toUpperCase(s.charAt(i)); |
| 146 |
if(AminoAcid.baseToNumberExtended[c]<0){c='N';} |
| 147 |
assert(Tools.isLetter(c)); |
| 148 |
assert(c<=Byte.MAX_VALUE); |
| 149 |
array[loc]=(byte)c; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
loc--; |
| 154 |
assert(loc==loc2-1) : "loc="+loc+", loc2="+loc2+", s.len="+s.length(); |
| 155 |
minIndex=min(loc, minIndex); |
| 156 |
maxIndex=max(loc, maxIndex); |
| 157 |
} |
| 158 |
|
| 159 |
public void set(int loc, byte[] s){ |
| 160 |
set(loc, s, s.length); |
| 161 |
} |
| 162 |
|
| 163 |
public void set(int loc, ByteBuilder bb){ |
| 164 |
set(loc, bb.array, bb.length()); |
| 165 |
} |
| 166 |
|
| 167 |
public void set(int loc, byte[] s, final int slen){ |
| 168 |
assert(slen<=s.length && slen>=0); |
| 169 |
int loc2=loc+slen; |
| 170 |
if(loc2>array.length){ |
| 171 |
int newlen=(int)(1+(3L*max(array.length, loc2))/2); |
| 172 |
assert(newlen>loc2) : newlen+", "+loc2+", "+array.length; |
| 173 |
resize(newlen); |
| 174 |
assert(array.length==newlen); |
| 175 |
|
| 176 |
} |
| 177 |
|
| 178 |
if(CHANGE_U_TO_T && CHANGE_DEGENERATE_TO_N){ |
| 179 |
for(int i=0; i<slen; i++, loc++){ |
| 180 |
byte b=(byte)Tools.max(0, s[i]); |
| 181 |
array[loc]=AminoAcid.baseToACGTN[b]; |
| 182 |
} |
| 183 |
}else{ |
| 184 |
for(int i=0; i<slen; i++, loc++){ |
| 185 |
char c=Tools.max((char)0, Tools.toUpperCase((char)s[i])); |
| 186 |
if(AminoAcid.baseToNumberExtended[c]<0){c='N';} |
| 187 |
assert(Tools.isLetter(c)); |
| 188 |
assert(c<=Byte.MAX_VALUE); |
| 189 |
array[loc]=(byte)c; |
| 190 |
} |
| 191 |
} |
| 192 |
loc--; |
| 193 |
assert(loc==loc2-1) : "loc="+loc+", loc2="+loc2+", s.len="+slen; |
| 194 |
minIndex=min(loc, minIndex); |
| 195 |
maxIndex=max(loc, maxIndex); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public float calcGC(int loc, int length, int[] counts) { |
| 205 |
counts=countACGTINOC(loc, length, counts); |
| 206 |
long at=counts[0]+counts[3]; |
| 207 |
long gc=counts[1]+counts[2]; |
| 208 |
return gc/(float)Tools.max(at+gc, 1); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public int[] countACGTINOC(final int loc, final int length, int[] counts) { |
| 217 |
final int lim=loc+length; |
| 218 |
assert(loc>=0 && lim<=maxIndex+1 && loc<=lim); |
| 219 |
if(counts==null){counts=new int[8];} |
| 220 |
else{Arrays.fill(counts, 0);} |
| 221 |
assert(counts.length==8); |
| 222 |
for(int i=loc; i<lim; i++){ |
| 223 |
byte b=get(i); |
| 224 |
int num=charToNum[b]; |
| 225 |
counts[num]++; |
| 226 |
} |
| 227 |
return counts; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
public byte get(int loc){ |
| 233 |
return loc<minIndex || loc>=maxIndex ? (byte)'N' : array[loc]; |
| 234 |
} |
| 235 |
|
| 236 |
public String getString(int a, int b){ |
| 237 |
StringBuilder sb=new StringBuilder(b-a+1); |
| 238 |
for(int i=a; i<=b; i++){ |
| 239 |
sb.append((char)get(i)); |
| 240 |
} |
| 241 |
return sb.toString(); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
public byte[] getBytes(int a, int b){ |
| 246 |
byte[] out=KillSwitch.copyOfRange(array, a, b+1); |
| 247 |
|
| 248 |
if(a<minIndex || b>maxIndex){ |
| 249 |
for(int i=0; i<out.length; i++){ |
| 250 |
if(out[i]==0){out[i]='N';} |
| 251 |
} |
| 252 |
} |
| 253 |
return out; |
| 254 |
} |
| 255 |
|
| 256 |
public byte getNumberACGTN(int loc){ |
| 257 |
return AminoAcid.baseToNumberACGTN[array[loc]]; |
| 258 |
} |
| 259 |
|
| 260 |
public byte getNumber(int loc){ |
| 261 |
return AminoAcid.baseToNumber[array[loc]]; |
| 262 |
} |
| 263 |
|
| 264 |
public boolean isFullyDefined(int a, int b){ |
| 265 |
for(int i=a; i<=b; i++){ |
| 266 |
int x=AminoAcid.baseToNumber[array[i]]; |
| 267 |
if(x<0){return false;} |
| 268 |
} |
| 269 |
return true; |
| 270 |
} |
| 271 |
|
| 272 |
public boolean isFullyUndefined(int a, int b){ |
| 273 |
for(int i=a; i<=b; i++){ |
| 274 |
int x=AminoAcid.baseToNumber[array[i]]; |
| 275 |
if(x>=0){return false;} |
| 276 |
} |
| 277 |
return true; |
| 278 |
} |
| 279 |
|
| 280 |
public int countDefinedBases(){ |
| 281 |
return countDefinedBases(minIndex, maxIndex); |
| 282 |
} |
| 283 |
|
| 284 |
public int countDefinedBases(int a, int b){ |
| 285 |
int sum=0; |
| 286 |
for(int i=a; i<=b; i++){ |
| 287 |
int x=AminoAcid.baseToNumber[array[i]]; |
| 288 |
if(x>=0){sum++;} |
| 289 |
} |
| 290 |
return sum; |
| 291 |
} |
| 292 |
|
| 293 |
public int getNumber(int a, int b){ |
| 294 |
return toNumber(a, b, array); |
| 295 |
} |
| 296 |
|
| 297 |
public static int toNumber(int a, int b, byte[] bases){ |
| 298 |
assert(b>=a); |
| 299 |
assert(b-a<17); |
| 300 |
int out=0; |
| 301 |
for(int i=a; i<=b; i++){ |
| 302 |
int x=AminoAcid.baseToNumber[bases[i]]; |
| 303 |
if(x<0){return -1;} |
| 304 |
out=((out<<2)|x); |
| 305 |
} |
| 306 |
return out; |
| 307 |
} |
| 308 |
|
| 309 |
public static int toNumber(int a, int b, String bases){ |
| 310 |
int out=0; |
| 311 |
for(int i=a; i<=b; i++){ |
| 312 |
int x=AminoAcid.baseToNumber[bases.charAt(i)]; |
| 313 |
if(x<0){return -1;} |
| 314 |
out=((out<<2)|x); |
| 315 |
} |
| 316 |
return out; |
| 317 |
} |
| 318 |
|
| 319 |
public void resize(int newlen){ |
| 320 |
byte[] temp=KillSwitch.allocByte1D(newlen); |
| 321 |
int lim=min(array.length, newlen); |
| 322 |
assert(lim>=maxIndex) : lim+","+maxIndex; |
| 323 |
for(int i=0; i<lim; i++){ |
| 324 |
temp[i]=array[i]; |
| 325 |
} |
| 326 |
array=temp; |
| 327 |
} |
| 328 |
|
| 329 |
public String toBaseString(){ |
| 330 |
String s=new String(array); |
| 331 |
return s; |
| 332 |
} |
| 333 |
|
| 334 |
public char[] nearestDefinedBase(){ |
| 335 |
char[] r=new char[array.length]; |
| 336 |
final char max=Character.MAX_VALUE; |
| 337 |
|
| 338 |
char dist=max; |
| 339 |
for(int i=0; i<r.length; i++){ |
| 340 |
byte b=array[i]; |
| 341 |
if(b=='A' || b=='C' || b=='G' || b=='T'){ |
| 342 |
dist=0; |
| 343 |
}else{ |
| 344 |
dist=(dist==max ? max : (char)(dist+1)); |
| 345 |
} |
| 346 |
r[i]=dist; |
| 347 |
} |
| 348 |
|
| 349 |
dist=r[r.length-1]; |
| 350 |
for(int i=r.length-1; i>=0; i--){ |
| 351 |
byte b=array[i]; |
| 352 |
if(b=='A' || b=='C' || b=='G' || b=='T'){ |
| 353 |
dist=0; |
| 354 |
}else{ |
| 355 |
dist=(dist==max ? max : (char)(dist+1)); |
| 356 |
} |
| 357 |
r[i]=Tools.min(dist, r[i]); |
| 358 |
} |
| 359 |
return r; |
| 360 |
} |
| 361 |
|
| 362 |
public ArrayList<Range> toContigRanges(final int nBlockSize){ |
| 363 |
assert(nBlockSize>0); |
| 364 |
ArrayList<Range> list=new ArrayList<Range>(); |
| 365 |
|
| 366 |
int start=-1; |
| 367 |
int stop=-1; |
| 368 |
int ns=nBlockSize+1; |
| 369 |
|
| 370 |
boolean contig=false; |
| 371 |
|
| 372 |
for(int i=minIndex; i<=maxIndex; i++){ |
| 373 |
byte b=array[i]; |
| 374 |
if(b=='N' || b=='X'){ |
| 375 |
ns++; |
| 376 |
if(contig && (b=='X' || ns>=nBlockSize)){ |
| 377 |
Range r=new Range(start, stop); |
| 378 |
list.add(r); |
| 379 |
contig=false; |
| 380 |
} |
| 381 |
}else{ |
| 382 |
ns=0; |
| 383 |
if(!contig){start=i;} |
| 384 |
contig=true; |
| 385 |
stop=i; |
| 386 |
} |
| 387 |
} |
| 388 |
if(contig){ |
| 389 |
Range r=new Range(start, stop); |
| 390 |
list.add(r); |
| 391 |
} |
| 392 |
return list; |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
public boolean equalsIgnoreCase(ChromosomeArray other){ |
| 397 |
if(minIndex!=other.minIndex){System.err.println("a");return false;} |
| 398 |
if(maxIndex!=other.maxIndex){System.err.println("b");return false;} |
| 399 |
if(chromosome!=other.chromosome){System.err.println("c");return false;} |
| 400 |
if(array.length!=other.array.length){System.err.println("d");return false;} |
| 401 |
for(int i=minIndex; i<=maxIndex; i++){ |
| 402 |
if(Tools.toLowerCase(array[i])!=Tools.toLowerCase(other.array[i])){ |
| 403 |
System.err.println("e"); |
| 404 |
return false; |
| 405 |
} |
| 406 |
} |
| 407 |
return true; |
| 408 |
} |
| 409 |
|
| 410 |
private static final long min(long x, long y){return x<y ? x : y;} |
| 411 |
private static final long max(long x, long y){return x>y ? x : y;} |
| 412 |
private static final int min(int x, int y){return x<y ? x : y;} |
| 413 |
private static final int max(int x, int y){return x>y ? x : y;} |
| 414 |
|
| 415 |
public final byte strand; |
| 416 |
public int chromosome; |
| 417 |
public byte[] array; |
| 418 |
public int maxIndex=-1; |
| 419 |
public int minIndex=Integer.MAX_VALUE; |
| 420 |
|
| 421 |
public static boolean CHANGE_UNDEFINED_TO_N_ON_READ=false; |
| 422 |
public static boolean CHANGE_U_TO_T=true; |
| 423 |
public static boolean CHANGE_DEGENERATE_TO_N=true; |
| 424 |
|
| 425 |
|
| 426 |
private static final byte[] charToNum=AssemblyStats2.makeCharToNum(); |
| 427 |
|
| 428 |
|
| 429 |
} |