| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
package edu.mit.compbio.ChromHMM; |
| 20 |
|
| 21 |
import htsjdk.samtools.*; |
| 22 |
import java.io.*; |
| 23 |
import java.util.*; |
| 24 |
import java.util.zip.GZIPOutputStream; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class Preprocessing |
| 32 |
{ |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
private static void windowSumGrid(int[][][] gridcontrol,int[][][] sumgridcontrol, int nflankwidthcontrol) |
| 38 |
{ |
| 39 |
|
| 40 |
for (int nchrom = 0; nchrom < gridcontrol.length; nchrom++) |
| 41 |
{ |
| 42 |
int[][] gridcontrol_nchrom = gridcontrol[nchrom]; |
| 43 |
int[][] sumgridcontrol_nchrom = sumgridcontrol[nchrom]; |
| 44 |
|
| 45 |
for (int nbin = 0; nbin < sumgridcontrol_nchrom.length; nbin++) |
| 46 |
{ |
| 47 |
int[] sumgridcontrol_nchrom_nbin = sumgridcontrol_nchrom[nbin]; |
| 48 |
int nstart = Math.max(0,nbin - nflankwidthcontrol); |
| 49 |
int nend = Math.min(nbin + nflankwidthcontrol,gridcontrol_nchrom.length-1); |
| 50 |
for (int nmark = 0; nmark < sumgridcontrol_nchrom_nbin.length; nmark++) |
| 51 |
{ |
| 52 |
int nsum = 0; |
| 53 |
for (int nrow = nstart; nrow <= nend; nrow++) |
| 54 |
{ |
| 55 |
int nval = gridcontrol_nchrom[nrow][nmark]; |
| 56 |
if (nval > 0) |
| 57 |
{ |
| 58 |
nsum += nval; |
| 59 |
} |
| 60 |
} |
| 61 |
sumgridcontrol_nchrom[nbin][nmark] = nsum; |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
private static void loadGrid(int[][][] grid,boolean[] bpresent, boolean[] bpresentmarks, String[] marks,int nshift, int nbinsize, |
| 89 |
boolean bcenterinterval,int noffsetleft, |
| 90 |
int noffsetright,HashMap hmfiles, String szcell, String szmarkdir,HashMap hmchrom, |
| 91 |
int ninitval, String szcolfields,boolean bpeaks,boolean bcontrol, boolean bbinarizebam, |
| 92 |
boolean bpairend,boolean bmixed) throws IOException |
| 93 |
{ |
| 94 |
int nummarks = grid[0][0].length; |
| 95 |
|
| 96 |
for (int nchrom = 0; nchrom < grid.length; nchrom++) |
| 97 |
{ |
| 98 |
int[][] grid_nchrom = grid[nchrom]; |
| 99 |
int numbins = grid_nchrom.length; |
| 100 |
for (int nbin = 0; nbin < numbins; nbin++) |
| 101 |
{ |
| 102 |
int[] grid_nchrom_nbin = grid_nchrom[nbin]; |
| 103 |
for (int nmark = 0; nmark < nummarks; nmark++) |
| 104 |
{ |
| 105 |
grid_nchrom_nbin[nmark] = ninitval; |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
int nchromcol=-1; |
| 112 |
int nbegincol=-1; |
| 113 |
int nendcol=-1; |
| 114 |
int nstrandcol= -1; |
| 115 |
int nmaxindex = -1; |
| 116 |
if (szcolfields!=null) |
| 117 |
{ |
| 118 |
StringTokenizer stcolfields = new StringTokenizer(szcolfields,","); |
| 119 |
|
| 120 |
int numtokens = stcolfields.countTokens(); |
| 121 |
if ((numtokens <3)||((numtokens<4)&&!bcenterinterval)) |
| 122 |
{ |
| 123 |
throw new IllegalArgumentException(" invalid number of column fields in "+szcolfields+" expecting 3 or 4 integers"); |
| 124 |
} |
| 125 |
nchromcol = Integer.parseInt(stcolfields.nextToken().trim()); |
| 126 |
nbegincol = Integer.parseInt(stcolfields.nextToken().trim()); |
| 127 |
nendcol = Integer.parseInt(stcolfields.nextToken().trim()); |
| 128 |
if (!bcenterinterval) |
| 129 |
{ |
| 130 |
nstrandcol = Integer.parseInt(stcolfields.nextToken().trim()); |
| 131 |
} |
| 132 |
|
| 133 |
nmaxindex = Math.max(nchromcol,Math.max(nbegincol,Math.max(nendcol, nstrandcol))); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
for (int nmark = 0; nmark < nummarks; nmark++) |
| 138 |
{ |
| 139 |
boolean bdatafound = false; |
| 140 |
|
| 141 |
ArrayList alfiles = (ArrayList) hmfiles.get(szcell+"\t"+marks[nmark]); |
| 142 |
|
| 143 |
if ((nummarks == 1)&&(bcontrol)) |
| 144 |
{ |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
int nfilemark = 1; |
| 149 |
while ((alfiles == null)&&(nfilemark < marks.length)) |
| 150 |
{ |
| 151 |
|
| 152 |
alfiles = (ArrayList) hmfiles.get(szcell+"\t"+marks[nfilemark]); |
| 153 |
nfilemark++; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
if (alfiles == null) |
| 158 |
{ |
| 159 |
if (bcontrol) |
| 160 |
{ |
| 161 |
System.out.println("Warning did not find control data for "+szcell+" "+marks[nmark]+" treating as missing"); |
| 162 |
} |
| 163 |
else |
| 164 |
{ |
| 165 |
System.out.println("Warning did not find data for "+szcell+" "+marks[nmark]+" treating as missing"); |
| 166 |
} |
| 167 |
|
| 168 |
bpresentmarks[nmark] = false; |
| 169 |
if (!bcontrol) |
| 170 |
{ |
| 171 |
|
| 172 |
for (int nchrom = 0; nchrom < grid.length; nchrom++) |
| 173 |
{ |
| 174 |
int[][] grid_nchrom = grid[nchrom]; |
| 175 |
int numbins = grid_nchrom.length; |
| 176 |
for (int nbin = 0; nbin < numbins; nbin++) |
| 177 |
{ |
| 178 |
grid_nchrom[nbin][nmark] = -1; |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
else |
| 184 |
{ |
| 185 |
|
| 186 |
bpresentmarks[nmark] = true; |
| 187 |
|
| 188 |
for (int nfile = 0; nfile < alfiles.size(); nfile++) |
| 189 |
{ |
| 190 |
String szfile = (String) alfiles.get(nfile); |
| 191 |
if (bbinarizebam) |
| 192 |
{ |
| 193 |
SamReaderFactory srf = SamReaderFactory.make(); |
| 194 |
srf.validationStringency(ValidationStringency.LENIENT); |
| 195 |
SamReader samReader = srf.open(new File(szmarkdir+"/"+szfile)); |
| 196 |
SAMRecordIterator iter = samReader.iterator(); |
| 197 |
|
| 198 |
if (bmixed) |
| 199 |
{ |
| 200 |
|
| 201 |
while (iter.hasNext()) |
| 202 |
{ |
| 203 |
SAMRecord rec= iter.next(); |
| 204 |
|
| 205 |
if (rec.getReadPairedFlag()) |
| 206 |
{ |
| 207 |
|
| 208 |
if ((rec.getProperPairFlag()) && rec.getFirstOfPairFlag()&&(!rec.getReadUnmappedFlag())) |
| 209 |
{ |
| 210 |
int nstartorig = rec.getAlignmentStart()-1; |
| 211 |
int nendorig = rec.getAlignmentEnd(); |
| 212 |
String szchrom = rec.getReferenceName(); |
| 213 |
boolean bnegstrand = rec.getReadNegativeStrandFlag(); |
| 214 |
int ninsertsize = rec.getInferredInsertSize(); |
| 215 |
|
| 216 |
Integer objInt = (Integer) hmchrom.get(szchrom); |
| 217 |
|
| 218 |
|
| 219 |
if (objInt != null) |
| 220 |
{ |
| 221 |
int nchrom = objInt.intValue(); |
| 222 |
int nbin; |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
if (bnegstrand) |
| 227 |
{ |
| 228 |
|
| 229 |
|
| 230 |
nbin = (nendorig-noffsetright+ninsertsize/2)/nbinsize; |
| 231 |
|
| 232 |
|
| 233 |
} |
| 234 |
else |
| 235 |
{ |
| 236 |
|
| 237 |
|
| 238 |
nbin = (nstartorig-noffsetleft+ninsertsize/2)/nbinsize; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
if ((nbin>=0)&&(nbin < grid[nchrom].length)) |
| 243 |
{ |
| 244 |
|
| 245 |
grid[nchrom][nbin][nmark]++; |
| 246 |
|
| 247 |
bpresent[nchrom] = true; |
| 248 |
bdatafound = true; |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
else |
| 254 |
{ |
| 255 |
|
| 256 |
int nstartorig = rec.getAlignmentStart()-1; |
| 257 |
int nendorig = rec.getAlignmentEnd(); |
| 258 |
String szchrom = rec.getReferenceName(); |
| 259 |
boolean bnegstrand = rec.getReadNegativeStrandFlag(); |
| 260 |
boolean bunmapped = rec.getReadUnmappedFlag(); |
| 261 |
|
| 262 |
if (bunmapped) |
| 263 |
{ |
| 264 |
continue; |
| 265 |
} |
| 266 |
|
| 267 |
Integer objInt = (Integer) hmchrom.get(szchrom); |
| 268 |
|
| 269 |
|
| 270 |
if (objInt != null) |
| 271 |
{ |
| 272 |
int nchrom = objInt.intValue(); |
| 273 |
int nbin; |
| 274 |
|
| 275 |
if (bpeaks) |
| 276 |
{ |
| 277 |
int nstart = Math.max(0,(nstartorig-noffsetleft)/nbinsize); |
| 278 |
int nend = Math.min(grid[nchrom].length-1, (nendorig-noffsetright)/nbinsize); |
| 279 |
|
| 280 |
for (nbin = nstart; nbin <= nend; nbin++) |
| 281 |
{ |
| 282 |
|
| 283 |
grid[nchrom][nbin][nmark]++; |
| 284 |
|
| 285 |
bpresent[nchrom] = true; |
| 286 |
bdatafound = true; |
| 287 |
} |
| 288 |
} |
| 289 |
else |
| 290 |
{ |
| 291 |
if (bcenterinterval) |
| 292 |
{ |
| 293 |
|
| 294 |
nbin = (nstartorig-noffsetleft+nendorig-noffsetright)/(2*nbinsize); |
| 295 |
} |
| 296 |
else |
| 297 |
{ |
| 298 |
if (bnegstrand) |
| 299 |
{ |
| 300 |
|
| 301 |
nbin = (nendorig-noffsetright-nshift)/nbinsize; |
| 302 |
|
| 303 |
} |
| 304 |
else |
| 305 |
{ |
| 306 |
|
| 307 |
nbin = (nstartorig-noffsetleft+nshift)/nbinsize; |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
if ((nbin>=0)&&(nbin < grid[nchrom].length)) |
| 312 |
{ |
| 313 |
|
| 314 |
grid[nchrom][nbin][nmark]++; |
| 315 |
|
| 316 |
bpresent[nchrom] = true; |
| 317 |
bdatafound = true; |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
else if (bpairend) |
| 325 |
{ |
| 326 |
while (iter.hasNext()) |
| 327 |
{ |
| 328 |
SAMRecord rec= iter.next(); |
| 329 |
|
| 330 |
if (!rec.getReadPairedFlag()) |
| 331 |
{ |
| 332 |
throw new IllegalArgumentException("The '-paired' option was specified, but found a read that was not paired end in "+szmarkdir+"/"+szfile); |
| 333 |
} |
| 334 |
|
| 335 |
if ((rec.getProperPairFlag()) && rec.getFirstOfPairFlag()&&(!rec.getReadUnmappedFlag())) |
| 336 |
{ |
| 337 |
int nstartorig = rec.getAlignmentStart()-1; |
| 338 |
int nendorig = rec.getAlignmentEnd(); |
| 339 |
String szchrom = rec.getReferenceName(); |
| 340 |
boolean bnegstrand = rec.getReadNegativeStrandFlag(); |
| 341 |
int ninsertsize = rec.getInferredInsertSize(); |
| 342 |
|
| 343 |
Integer objInt = (Integer) hmchrom.get(szchrom); |
| 344 |
|
| 345 |
|
| 346 |
if (objInt != null) |
| 347 |
{ |
| 348 |
int nchrom = objInt.intValue(); |
| 349 |
int nbin; |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
if (bnegstrand) |
| 354 |
{ |
| 355 |
|
| 356 |
|
| 357 |
nbin = (nendorig-noffsetright+ninsertsize/2)/nbinsize; |
| 358 |
|
| 359 |
|
| 360 |
} |
| 361 |
else |
| 362 |
{ |
| 363 |
|
| 364 |
|
| 365 |
nbin = (nstartorig-noffsetleft+ninsertsize/2)/nbinsize; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
if ((nbin>=0)&&(nbin < grid[nchrom].length)) |
| 370 |
{ |
| 371 |
|
| 372 |
grid[nchrom][nbin][nmark]++; |
| 373 |
|
| 374 |
bpresent[nchrom] = true; |
| 375 |
bdatafound = true; |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
else |
| 382 |
{ |
| 383 |
while (iter.hasNext()) |
| 384 |
{ |
| 385 |
SAMRecord rec= iter.next(); |
| 386 |
int nstartorig = rec.getAlignmentStart()-1; |
| 387 |
int nendorig = rec.getAlignmentEnd(); |
| 388 |
String szchrom = rec.getReferenceName(); |
| 389 |
boolean bnegstrand = rec.getReadNegativeStrandFlag(); |
| 390 |
boolean bunmapped = rec.getReadUnmappedFlag(); |
| 391 |
|
| 392 |
if (bunmapped) |
| 393 |
{ |
| 394 |
continue; |
| 395 |
} |
| 396 |
|
| 397 |
Integer objInt = (Integer) hmchrom.get(szchrom); |
| 398 |
|
| 399 |
|
| 400 |
if (objInt != null) |
| 401 |
{ |
| 402 |
int nchrom = objInt.intValue(); |
| 403 |
int nbin; |
| 404 |
|
| 405 |
if (bpeaks) |
| 406 |
{ |
| 407 |
int nstart = Math.max(0,(nstartorig-noffsetleft)/nbinsize); |
| 408 |
int nend = Math.min(grid[nchrom].length-1, (nendorig-noffsetright)/nbinsize); |
| 409 |
|
| 410 |
for (nbin = nstart; nbin <= nend; nbin++) |
| 411 |
{ |
| 412 |
|
| 413 |
grid[nchrom][nbin][nmark]++; |
| 414 |
|
| 415 |
bpresent[nchrom] = true; |
| 416 |
bdatafound = true; |
| 417 |
} |
| 418 |
} |
| 419 |
else |
| 420 |
{ |
| 421 |
if (bcenterinterval) |
| 422 |
{ |
| 423 |
|
| 424 |
nbin = (nstartorig-noffsetleft+nendorig-noffsetright)/(2*nbinsize); |
| 425 |
} |
| 426 |
else |
| 427 |
{ |
| 428 |
if (bnegstrand) |
| 429 |
{ |
| 430 |
|
| 431 |
nbin = (nendorig-noffsetright-nshift)/nbinsize; |
| 432 |
|
| 433 |
} |
| 434 |
else |
| 435 |
{ |
| 436 |
|
| 437 |
nbin = (nstartorig-noffsetleft+nshift)/nbinsize; |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
if ((nbin>=0)&&(nbin < grid[nchrom].length)) |
| 442 |
{ |
| 443 |
|
| 444 |
grid[nchrom][nbin][nmark]++; |
| 445 |
|
| 446 |
bpresent[nchrom] = true; |
| 447 |
bdatafound = true; |
| 448 |
} |
| 449 |
} |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
} |
| 454 |
else |
| 455 |
{ |
| 456 |
BufferedReader brbed = Util.getBufferedReader(szmarkdir+"/"+szfile); |
| 457 |
String szLine; |
| 458 |
|
| 459 |
if (szcolfields != null) |
| 460 |
{ |
| 461 |
while ((szLine = brbed.readLine())!= null) |
| 462 |
{ |
| 463 |
String[] szLineA = szLine.split("\\s+"); |
| 464 |
String szchrom = szLineA[nchromcol]; |
| 465 |
|
| 466 |
Integer objInt = (Integer) hmchrom.get(szchrom); |
| 467 |
|
| 468 |
if (nmaxindex >= szLineA.length) |
| 469 |
{ |
| 470 |
throw new IllegalArgumentException("Column index "+nmaxindex+" exceeds maximum index "+(szLineA.length-1)+" indicies are 0 based"); |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
if (objInt != null) |
| 475 |
{ |
| 476 |
int nchrom = objInt.intValue(); |
| 477 |
int nbin; |
| 478 |
if (bpeaks) |
| 479 |
{ |
| 480 |
int nstart = Math.max(0,(Integer.parseInt(szLineA[nbegincol])-noffsetleft)/nbinsize); |
| 481 |
int nend = Math.min(grid[nchrom].length-1, (Integer.parseInt(szLineA[nendcol])-noffsetright)/nbinsize); |
| 482 |
|
| 483 |
for (nbin = nstart; nbin <= nend; nbin++) |
| 484 |
{ |
| 485 |
|
| 486 |
grid[nchrom][nbin][nmark]++; |
| 487 |
|
| 488 |
bpresent[nchrom] = true; |
| 489 |
bdatafound = true; |
| 490 |
} |
| 491 |
} |
| 492 |
else |
| 493 |
{ |
| 494 |
if (bcenterinterval) |
| 495 |
{ |
| 496 |
|
| 497 |
nbin = (Integer.parseInt(szLineA[nbegincol])-noffsetleft+Integer.parseInt(szLineA[nendcol])-noffsetright)/(2*nbinsize); |
| 498 |
} |
| 499 |
else |
| 500 |
{ |
| 501 |
String szstrand = szLineA[nstrandcol]; |
| 502 |
|
| 503 |
if (szstrand.equals("+")) |
| 504 |
{ |
| 505 |
nbin = (Integer.parseInt(szLineA[nbegincol])-noffsetleft+nshift)/nbinsize; |
| 506 |
|
| 507 |
} |
| 508 |
else if (szstrand.equals("-")) |
| 509 |
{ |
| 510 |
nbin = (Integer.parseInt(szLineA[nendcol])-noffsetright-nshift)/nbinsize; |
| 511 |
} |
| 512 |
else |
| 513 |
{ |
| 514 |
throw new IllegalArgumentException(szstrand+" is an invalid strand!"); |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
if ((nbin>=0)&&(nbin < grid[nchrom].length)) |
| 519 |
{ |
| 520 |
|
| 521 |
grid[nchrom][nbin][nmark]++; |
| 522 |
|
| 523 |
bpresent[nchrom] = true; |
| 524 |
bdatafound = true; |
| 525 |
} |
| 526 |
} |
| 527 |
} |
| 528 |
} |
| 529 |
brbed.close(); |
| 530 |
} |
| 531 |
else |
| 532 |
{ |
| 533 |
while ((szLine = brbed.readLine())!= null) |
| 534 |
{ |
| 535 |
StringTokenizer st = new StringTokenizer(szLine,"\t "); |
| 536 |
if (!st.hasMoreTokens()) |
| 537 |
{ |
| 538 |
throw new IllegalArgumentException("Empty line found in "+szmarkdir+"/"+szfile); |
| 539 |
} |
| 540 |
String szchrom = st.nextToken(); |
| 541 |
Integer objInt = (Integer) hmchrom.get(szchrom); |
| 542 |
|
| 543 |
|
| 544 |
if (objInt != null) |
| 545 |
{ |
| 546 |
int nchrom = objInt.intValue(); |
| 547 |
if (!st.hasMoreTokens()) |
| 548 |
{ |
| 549 |
throw new IllegalArgumentException("Missing begin coordinate in "+szmarkdir+"/"+szfile); |
| 550 |
} |
| 551 |
|
| 552 |
String szbegin = st.nextToken(); |
| 553 |
|
| 554 |
if (!st.hasMoreTokens()) |
| 555 |
{ |
| 556 |
throw new IllegalArgumentException("Missing end coordinate in "+szmarkdir+"/"+szfile); |
| 557 |
} |
| 558 |
String szend = st.nextToken(); |
| 559 |
int nbin; |
| 560 |
|
| 561 |
if (bpeaks) |
| 562 |
{ |
| 563 |
int nstart = Math.max(0,(Integer.parseInt(szbegin)-noffsetleft)/nbinsize); |
| 564 |
int nend = Math.min(grid[nchrom].length-1, (Integer.parseInt(szend)-noffsetright)/nbinsize); |
| 565 |
|
| 566 |
for (nbin = nstart; nbin <= nend; nbin++) |
| 567 |
{ |
| 568 |
|
| 569 |
grid[nchrom][nbin][nmark]++; |
| 570 |
|
| 571 |
bpresent[nchrom] = true; |
| 572 |
bdatafound = true; |
| 573 |
} |
| 574 |
} |
| 575 |
else |
| 576 |
{ |
| 577 |
if (bcenterinterval) |
| 578 |
{ |
| 579 |
|
| 580 |
nbin = (Integer.parseInt(szbegin)-noffsetleft+Integer.parseInt(szend)-noffsetright)/(2*nbinsize); |
| 581 |
} |
| 582 |
else |
| 583 |
{ |
| 584 |
if (!st.hasMoreTokens()) |
| 585 |
{ |
| 586 |
throw new IllegalArgumentException("strand column expected, but not found in "+szmarkdir+"/"+szfile); |
| 587 |
} |
| 588 |
|
| 589 |
String szstrand = st.nextToken(); |
| 590 |
if (st.hasMoreTokens()) |
| 591 |
szstrand = st.nextToken(); |
| 592 |
if (st.hasMoreTokens()) |
| 593 |
szstrand = st.nextToken(); |
| 594 |
|
| 595 |
if (szstrand.equals("+")) |
| 596 |
{ |
| 597 |
nbin = (Integer.parseInt(szbegin)-noffsetleft+nshift)/nbinsize; |
| 598 |
|
| 599 |
} |
| 600 |
else if (szstrand.equals("-")) |
| 601 |
{ |
| 602 |
nbin = (Integer.parseInt(szend)-noffsetright-nshift)/nbinsize; |
| 603 |
} |
| 604 |
else |
| 605 |
{ |
| 606 |
throw new IllegalArgumentException(szstrand+" is an invalid strand!"); |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
if ((nbin>=0)&&(nbin < grid[nchrom].length)) |
| 611 |
{ |
| 612 |
|
| 613 |
grid[nchrom][nbin][nmark]++; |
| 614 |
|
| 615 |
bpresent[nchrom] = true; |
| 616 |
bdatafound = true; |
| 617 |
} |
| 618 |
} |
| 619 |
} |
| 620 |
} |
| 621 |
brbed.close(); |
| 622 |
} |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
if (!bdatafound) |
| 627 |
{ |
| 628 |
if (bcontrol) |
| 629 |
{ |
| 630 |
System.out.print("WARNING not able to load any control data for "+szcell+"\t"+marks[nmark]); |
| 631 |
} |
| 632 |
else |
| 633 |
{ |
| 634 |
System.out.print("WARNING not able to load any data for "+szcell+"\t"+marks[nmark]); |
| 635 |
} |
| 636 |
|
| 637 |
if (bbinarizebam) |
| 638 |
{ |
| 639 |
System.out.println(". Check if the chromosome naming is consistent between the chromosome length files and the bam files."); |
| 640 |
} |
| 641 |
else |
| 642 |
{ |
| 643 |
System.out.println(". Check if the chromosome naming is consistent between the chromosome length files and the bed files."); |
| 644 |
} |
| 645 |
} |
| 646 |
} |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
public static void makeBinaryDataFromPeaksSplit(String szchromlengthfile, String szmarkdir, String szoutputbinarydir, String szcellmarkfiletable, |
| 653 |
int nbinsize, boolean bgzip, int numsplitbins, int nsplit, int noffsetleft, int noffsetright, |
| 654 |
boolean bsplitcols,int nnummarksplit,int nmarksplitindex, boolean bstacked) throws IOException |
| 655 |
{ |
| 656 |
|
| 657 |
|
| 658 |
BufferedReader brchrom = Util.getBufferedReader(szchromlengthfile); |
| 659 |
String szLine; |
| 660 |
ArrayList allines = new ArrayList(); |
| 661 |
while ((szLine = brchrom.readLine())!=null) |
| 662 |
{ |
| 663 |
allines.add(szLine); |
| 664 |
} |
| 665 |
brchrom.close(); |
| 666 |
|
| 667 |
String[] chroms = new String[allines.size()]; |
| 668 |
int[] lengthbins = new int[chroms.length]; |
| 669 |
|
| 670 |
for (int ni = 0; ni < chroms.length; ni++) |
| 671 |
{ |
| 672 |
StringTokenizer st = new StringTokenizer((String) allines.get(ni),"\t "); |
| 673 |
if (!st.hasMoreTokens()) |
| 674 |
{ |
| 675 |
throw new IllegalArgumentException("empty line found in "+szchromlengthfile); |
| 676 |
} |
| 677 |
chroms[ni] = st.nextToken(); |
| 678 |
|
| 679 |
if (!st.hasMoreTokens()) |
| 680 |
{ |
| 681 |
throw new IllegalArgumentException("missing chromosome length for "+allines.get(ni)+" in "+szchromlengthfile); |
| 682 |
} |
| 683 |
int nlength = Integer.parseInt(st.nextToken()); |
| 684 |
int numbins = nlength/nbinsize; |
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
lengthbins[ni] = numbins; |
| 690 |
} |
| 691 |
|
| 692 |
BufferedReader brcellmark = Util.getBufferedReader(szcellmarkfiletable); |
| 693 |
HashSet hscells = new HashSet(); |
| 694 |
HashSet hsmarks = new HashSet(); |
| 695 |
HashMap hmfiles = new HashMap(); |
| 696 |
|
| 697 |
|
| 698 |
int ncellmarkentry = 0; |
| 699 |
int ncolsplitbegin = nnummarksplit*(nmarksplitindex); |
| 700 |
int ncolsplitend = nnummarksplit*(nmarksplitindex+1); |
| 701 |
|
| 702 |
while ((szLine = brcellmark.readLine())!=null) |
| 703 |
{ |
| 704 |
if (szLine.trim().equals("")) continue; |
| 705 |
|
| 706 |
if ((!bsplitcols) || ((ncolsplitbegin<=ncellmarkentry)&&(ncellmarkentry<ncolsplitend))) |
| 707 |
{ |
| 708 |
StringTokenizer st = new StringTokenizer(szLine,"\t"); |
| 709 |
|
| 710 |
if (st.countTokens() != 3) |
| 711 |
{ |
| 712 |
throw new IllegalArgumentException("In "+szcellmarkfiletable+" "+szLine+" does not have 3 columns, "+ |
| 713 |
"expecting 3 columns since peaks was specified"); |
| 714 |
} |
| 715 |
String szcell = st.nextToken().trim(); |
| 716 |
String szmark = st.nextToken().trim(); |
| 717 |
|
| 718 |
if (bstacked) |
| 719 |
{ |
| 720 |
szmark = szcell + "_" + szmark; |
| 721 |
szcell = "genome"; |
| 722 |
} |
| 723 |
|
| 724 |
String szfile = st.nextToken().trim(); |
| 725 |
|
| 726 |
hscells.add(szcell); |
| 727 |
hsmarks.add(szmark); |
| 728 |
|
| 729 |
ArrayList alfiles = (ArrayList) hmfiles.get(szcell+"\t"+szmark); |
| 730 |
if (alfiles == null) |
| 731 |
{ |
| 732 |
alfiles = new ArrayList(); |
| 733 |
hmfiles.put(szcell+"\t"+szmark,alfiles); |
| 734 |
} |
| 735 |
|
| 736 |
if (!alfiles.contains(szfile)) |
| 737 |
{ |
| 738 |
|
| 739 |
alfiles.add(szfile); |
| 740 |
} |
| 741 |
} |
| 742 |
ncellmarkentry++; |
| 743 |
} |
| 744 |
brcellmark.close(); |
| 745 |
|
| 746 |
|
| 747 |
int nummarks = hsmarks.size(); |
| 748 |
String[] marks = new String[nummarks]; |
| 749 |
int nmarkindex = 0; |
| 750 |
Iterator itrmarks = hsmarks.iterator(); |
| 751 |
while (itrmarks.hasNext()) |
| 752 |
{ |
| 753 |
marks[nmarkindex] = (String) itrmarks.next(); |
| 754 |
nmarkindex++; |
| 755 |
} |
| 756 |
Arrays.sort(marks); |
| 757 |
|
| 758 |
boolean[][] grid = new boolean[nummarks][numsplitbins]; |
| 759 |
Iterator itrcells = hscells.iterator(); |
| 760 |
while (itrcells.hasNext()) |
| 761 |
{ |
| 762 |
|
| 763 |
String szcell = (String) itrcells.next(); |
| 764 |
|
| 765 |
for (int nchrom = 0; nchrom < chroms.length; nchrom++) |
| 766 |
{ |
| 767 |
|
| 768 |
for (int nmark = 0; nmark < grid.length; nmark++) |
| 769 |
{ |
| 770 |
boolean[] grid_nmark = grid[nmark]; |
| 771 |
for (int nbin = 0; nbin < grid_nmark.length; nbin++) |
| 772 |
{ |
| 773 |
grid_nmark[nbin] = false; |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
|
| 778 |
String szcurrchrom = chroms[nchrom]; |
| 779 |
int nlengthbin = lengthbins[nchrom]; |
| 780 |
int nremainder = 0; |
| 781 |
int numsegments = nlengthbin/numsplitbins; |
| 782 |
if (nlengthbin % numsplitbins != 0) |
| 783 |
{ |
| 784 |
nremainder = nlengthbin % numsplitbins; |
| 785 |
numsegments++; |
| 786 |
} |
| 787 |
|
| 788 |
if (nsplit >= numsegments) |
| 789 |
continue; |
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
int nstartinterval = numsplitbins*nsplit; |
| 795 |
int nendinterval = nstartinterval + numsplitbins; |
| 796 |
|
| 797 |
for (int nmark = 0; nmark < marks.length; nmark++) |
| 798 |
{ |
| 799 |
ArrayList alfiles = (ArrayList) hmfiles.get(szcell+"\t"+marks[nmark]); |
| 800 |
boolean[] grid_nmark = grid[nmark]; |
| 801 |
for (int nfile = 0; nfile < alfiles.size(); nfile++) |
| 802 |
{ |
| 803 |
BufferedReader br = Util.getBufferedReader(szmarkdir+"/"+alfiles.get(nfile)); |
| 804 |
|
| 805 |
while ((szLine = br.readLine())!=null) |
| 806 |
{ |
| 807 |
StringTokenizer st = new StringTokenizer(szLine,"\t"); |
| 808 |
if (st.countTokens() == 0) continue; |
| 809 |
String szchrom = st.nextToken().trim(); |
| 810 |
if (szchrom.equals(szcurrchrom)) |
| 811 |
{ |
| 812 |
int nbegin = (Integer.parseInt(st.nextToken().trim())-noffsetleft)/nbinsize; |
| 813 |
int nend = (Integer.parseInt(st.nextToken().trim())-noffsetright)/nbinsize; |
| 814 |
|
| 815 |
|
| 816 |
int nactualbegin = nbegin; |
| 817 |
int nactualend = nend; |
| 818 |
|
| 819 |
if (nactualbegin < nstartinterval) |
| 820 |
{ |
| 821 |
nactualbegin = nstartinterval; |
| 822 |
} |
| 823 |
|
| 824 |
if (nactualend >= nendinterval) |
| 825 |
{ |
| 826 |
nactualend = nendinterval-1; |
| 827 |
} |
| 828 |
|
| 829 |
nactualbegin -= nstartinterval; |
| 830 |
nactualend -= nstartinterval; |
| 831 |
for (int nk = nactualbegin; nk <= nactualend; nk++) |
| 832 |
{ |
| 833 |
grid_nmark[nk] = true; |
| 834 |
} |
| 835 |
} |
| 836 |
} |
| 837 |
br.close(); |
| 838 |
} |
| 839 |
} |
| 840 |
|
| 841 |
|
| 842 |
int ncurrnumlines = numsplitbins; |
| 843 |
if ((nsplit == numsegments-1) && (nremainder >0)) |
| 844 |
{ |
| 845 |
ncurrnumlines = nremainder; |
| 846 |
} |
| 847 |
|
| 848 |
if (bgzip) |
| 849 |
{ |
| 850 |
GZIPOutputStream pwzip = new GZIPOutputStream(new FileOutputStream(szoutputbinarydir+"/"+szcell+"_"+szcurrchrom |
| 851 |
+"."+nsplit+"_binary.txt.gz")); |
| 852 |
StringBuffer sb = new StringBuffer(szcell+"\t"+szcurrchrom+"."+nsplit+"\n"); |
| 853 |
byte[] btformat = sb.toString().getBytes(); |
| 854 |
pwzip.write(btformat,0,btformat.length); |
| 855 |
|
| 856 |
sb = new StringBuffer(); |
| 857 |
for (int nmark = 0; nmark < nummarks; nmark++) |
| 858 |
{ |
| 859 |
sb.append(marks[nmark]); |
| 860 |
|
| 861 |
if (nmark < nummarks - 1) |
| 862 |
{ |
| 863 |
sb.append("\t"); |
| 864 |
} |
| 865 |
else |
| 866 |
{ |
| 867 |
sb.append("\n"); |
| 868 |
} |
| 869 |
} |
| 870 |
btformat = sb.toString().getBytes(); |
| 871 |
pwzip.write(btformat,0,btformat.length); |
| 872 |
|
| 873 |
for (int nline = 0; nline < ncurrnumlines; nline++) |
| 874 |
{ |
| 875 |
sb = new StringBuffer(); |
| 876 |
for (int nmark = 0; nmark < nummarks; nmark++) |
| 877 |
{ |
| 878 |
if (grid[nmark][nline]) |
| 879 |
{ |
| 880 |
sb.append("1"); |
| 881 |
} |
| 882 |
else |
| 883 |
{ |
| 884 |
sb.append("0"); |
| 885 |
} |
| 886 |
|
| 887 |
if (nmark < nummarks - 1) |
| 888 |
{ |
| 889 |
sb.append("\t"); |
| 890 |
} |
| 891 |
else |
| 892 |
{ |
| 893 |
sb.append("\n"); |
| 894 |
} |
| 895 |
} |
| 896 |
|
| 897 |
btformat = sb.toString().getBytes(); |
| 898 |
pwzip.write(btformat,0,btformat.length); |
| 899 |
} |
| 900 |
pwzip.finish(); |
| 901 |
pwzip.close(); |
| 902 |
} |
| 903 |
else |
| 904 |
{ |
| 905 |
PrintWriter pwout = new PrintWriter(new FileWriter(szoutputbinarydir+"/"+szcell+"_"+szcurrchrom |
| 906 |
+"."+nsplit+"_binary.txt")); |
| 907 |
pwout.println(szcell+"\t"+szcurrchrom+"."+nsplit); |
| 908 |
|
| 909 |
for (int nmark = 0; nmark < nummarks; nmark++) |
| 910 |
{ |
| 911 |
pwout.print(marks[nmark]); |
| 912 |
|
| 913 |
if (nmark < nummarks - 1) |
| 914 |
{ |
| 915 |
pwout.print("\t"); |
| 916 |
} |
| 917 |
else |
| 918 |
{ |
| 919 |
pwout.println(); |
| 920 |
} |
| 921 |
} |
| 922 |
|
| 923 |
for (int nline = 0; nline < ncurrnumlines; nline++) |
| 924 |
{ |
| 925 |
for (int nmark = 0; nmark < nummarks; nmark++) |
| 926 |
{ |
| 927 |
if (grid[nmark][nline]) |
| 928 |
{ |
| 929 |
pwout.print("1"); |
| 930 |
} |
| 931 |
else |
| 932 |
{ |
| 933 |
pwout.print("0"); |
| 934 |
} |
| 935 |
|
| 936 |
if (nmark < nummarks - 1) |
| 937 |
{ |
| 938 |
pwout.print("\t"); |
| 939 |
} |
| 940 |
else |
| 941 |
{ |
| 942 |
pwout.println(); |
| 943 |
} |
| 944 |
} |
| 945 |
} |
| 946 |
pwout.close(); |
| 947 |
} |
| 948 |
} |
| 949 |
} |
| 950 |
} |
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
public static void makeBinaryDataFromBed(String szchromlengthfile, String szmarkdir, String szcontroldir, int nflankwidthcontrol,String szcellmarkfiletable, |
| 983 |
int nshift, boolean bcenterinterval,int noffsetleft, int noffsetright, |
| 984 |
String szoutputsignaldir,String szoutputbinarydir, String szoutputcontroldir, |
| 985 |
double dpoissonthresh, double dfoldthresh,boolean bcontainsthresh, int npseudocountcontrol,int nbinsize, |
| 986 |
String szcolfields, boolean bpeaks, double dcountthresh, boolean bbinarizebam, boolean bpairend, |
| 987 |
boolean bgzip, boolean bsplit, int numsplitbins, |
| 988 |
boolean bsplitcols,int nnummarksplit,int nmarksplitindex, boolean bstacked, boolean bmixed |
| 989 |
) throws IOException |
| 990 |
{ |
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
BufferedReader brchrom = Util.getBufferedReader(szchromlengthfile); |
| 995 |
String szLine; |
| 996 |
ArrayList allines = new ArrayList(); |
| 997 |
while ((szLine = brchrom.readLine())!=null) |
| 998 |
{ |
| 999 |
allines.add(szLine); |
| 1000 |
} |
| 1001 |
brchrom.close(); |
| 1002 |
|
| 1003 |
String[] chroms = new String[allines.size()]; |
| 1004 |
int[] lengths = new int[chroms.length]; |
| 1005 |
HashMap hmchrom = new HashMap(); |
| 1006 |
for (int ni = 0; ni < chroms.length; ni++) |
| 1007 |
{ |
| 1008 |
StringTokenizer st = new StringTokenizer((String) allines.get(ni),"\t "); |
| 1009 |
if (!st.hasMoreTokens()) |
| 1010 |
{ |
| 1011 |
throw new IllegalArgumentException("empty line found in "+szchromlengthfile); |
| 1012 |
} |
| 1013 |
chroms[ni] = st.nextToken(); |
| 1014 |
hmchrom.put(chroms[ni], Integer.valueOf(ni)); |
| 1015 |
if (!st.hasMoreTokens()) |
| 1016 |
{ |
| 1017 |
throw new IllegalArgumentException("missing chromosome length for "+allines.get(ni)+" in "+szchromlengthfile); |
| 1018 |
} |
| 1019 |
lengths[ni] = Integer.parseInt(st.nextToken()); |
| 1020 |
} |
| 1021 |
|
| 1022 |
|
| 1023 |
BufferedReader brcellmark = Util.getBufferedReader(szcellmarkfiletable); |
| 1024 |
HashSet hscells = new HashSet(); |
| 1025 |
HashSet hsmarks = new HashSet(); |
| 1026 |
HashMap hmfiles = new HashMap(); |
| 1027 |
HashMap hmfilescontrol = new HashMap(); |
| 1028 |
HashMap hmfilescellcontrol = new HashMap(); |
| 1029 |
HashSet hscellnocontrol = new HashSet(); |
| 1030 |
|
| 1031 |
boolean bcontrol = false; |
| 1032 |
String szcontrolfile; |
| 1033 |
|
| 1034 |
int ncellmarkentry = 0; |
| 1035 |
int ncolsplitbegin = nnummarksplit*(nmarksplitindex); |
| 1036 |
int ncolsplitend = nnummarksplit*(nmarksplitindex+1); |
| 1037 |
|
| 1038 |
while ((szLine = brcellmark.readLine())!=null) |
| 1039 |
{ |
| 1040 |
if (szLine.trim().equals("")) continue; |
| 1041 |
|
| 1042 |
if ((!bsplitcols) || ((ncolsplitbegin<=ncellmarkentry)&&(ncellmarkentry<ncolsplitend))) |
| 1043 |
{ |
| 1044 |
StringTokenizer st = new StringTokenizer(szLine,"\t"); |
| 1045 |
if (st.countTokens() < 3) |
| 1046 |
{ |
| 1047 |
throw new IllegalArgumentException("In "+szcellmarkfiletable+" "+szLine+" has less than 3 columns, expecting at least 3!"); |
| 1048 |
} |
| 1049 |
String szcell = st.nextToken().trim(); |
| 1050 |
String szmark = st.nextToken().trim(); |
| 1051 |
|
| 1052 |
if (bstacked) |
| 1053 |
{ |
| 1054 |
szmark = szcell + "_" + szmark; |
| 1055 |
szcell = "genome"; |
| 1056 |
} |
| 1057 |
|
| 1058 |
String szfile = st.nextToken().trim(); |
| 1059 |
if (st.hasMoreTokens()) |
| 1060 |
{ |
| 1061 |
|
| 1062 |
szcontrolfile = st.nextToken().trim(); |
| 1063 |
bcontrol = true; |
| 1064 |
|
| 1065 |
|
| 1066 |
ArrayList alfilescontrol = (ArrayList) hmfilescontrol.get(szcell+"\t"+szmark); |
| 1067 |
if (alfilescontrol == null) |
| 1068 |
{ |
| 1069 |
alfilescontrol = new ArrayList(); |
| 1070 |
hmfilescontrol.put(szcell+"\t"+szmark,alfilescontrol); |
| 1071 |
} |
| 1072 |
|
| 1073 |
if (!alfilescontrol.contains(szcontrolfile)) |
| 1074 |
{ |
| 1075 |
|
| 1076 |
alfilescontrol.add(szcontrolfile); |
| 1077 |
} |
| 1078 |
|
| 1079 |
HashSet hscellcontrol = (HashSet) hmfilescellcontrol.get(szcell); |
| 1080 |
if (hscellcontrol == null) |
| 1081 |
{ |
| 1082 |
hscellcontrol = new HashSet(); |
| 1083 |
hmfilescellcontrol.put(szcell, hscellcontrol); |
| 1084 |
} |
| 1085 |
|
| 1086 |
hscellcontrol.add(szcontrolfile); |
| 1087 |
} |
| 1088 |
else |
| 1089 |
{ |
| 1090 |
hscellnocontrol.add(szcell); |
| 1091 |
} |
| 1092 |
hscells.add(szcell); |
| 1093 |
hsmarks.add(szmark); |
| 1094 |
|
| 1095 |
ArrayList alfiles = (ArrayList) hmfiles.get(szcell+"\t"+szmark); |
| 1096 |
if (alfiles == null) |
| 1097 |
{ |
| 1098 |
alfiles = new ArrayList(); |
| 1099 |
hmfiles.put(szcell+"\t"+szmark,alfiles); |
| 1100 |
} |
| 1101 |
|
| 1102 |
if (!alfiles.contains(szfile)) |
| 1103 |
{ |
| 1104 |
|
| 1105 |
alfiles.add(szfile); |
| 1106 |
} |
| 1107 |
} |
| 1108 |
ncellmarkentry++; |
| 1109 |
} |
| 1110 |
brcellmark.close(); |
| 1111 |
|
| 1112 |
|
| 1113 |
int nummarks = hsmarks.size(); |
| 1114 |
int numcontrolmarks=-1; |
| 1115 |
String[] marks = new String[nummarks]; |
| 1116 |
int nmarkindex = 0; |
| 1117 |
Iterator itrmarks = hsmarks.iterator(); |
| 1118 |
while (itrmarks.hasNext()) |
| 1119 |
{ |
| 1120 |
marks[nmarkindex] = (String) itrmarks.next(); |
| 1121 |
nmarkindex++; |
| 1122 |
} |
| 1123 |
Arrays.sort(marks); |
| 1124 |
|
| 1125 |
|
| 1126 |
int[][][] grid = new int[chroms.length][][]; |
| 1127 |
int[][][] gridcontrol = null; |
| 1128 |
int[][][] sumgridcontrol = null; |
| 1129 |
boolean[] bpresentcontrol = null; |
| 1130 |
boolean[] bpresentmarkscontrol = null; |
| 1131 |
|
| 1132 |
if (bcontrol) |
| 1133 |
{ |
| 1134 |
|
| 1135 |
gridcontrol = new int[chroms.length][][]; |
| 1136 |
sumgridcontrol = new int[chroms.length][][]; |
| 1137 |
bpresentcontrol = new boolean[chroms.length]; |
| 1138 |
bpresentmarkscontrol = new boolean[nummarks]; |
| 1139 |
} |
| 1140 |
|
| 1141 |
for (int ni = 0; ni < chroms.length; ni++) |
| 1142 |
{ |
| 1143 |
|
| 1144 |
grid[ni] = new int[lengths[ni]/nbinsize][nummarks]; |
| 1145 |
} |
| 1146 |
|
| 1147 |
boolean[] bpresent = new boolean[chroms.length]; |
| 1148 |
boolean[] bpresentmarks = new boolean[nummarks]; |
| 1149 |
|
| 1150 |
Iterator itrcells = hscells.iterator(); |
| 1151 |
while (itrcells.hasNext()) |
| 1152 |
{ |
| 1153 |
|
| 1154 |
|
| 1155 |
for (int nchromindex = 0; nchromindex < chroms.length; nchromindex++) |
| 1156 |
{ |
| 1157 |
bpresent[nchromindex] = false; |
| 1158 |
} |
| 1159 |
|
| 1160 |
String szcell = (String) itrcells.next(); |
| 1161 |
HashSet hscellcontrol = (HashSet) hmfilescellcontrol.get(szcell); |
| 1162 |
boolean bmissing = hscellnocontrol.contains(szcell); |
| 1163 |
|
| 1164 |
boolean bcontrolfile; |
| 1165 |
if (hscellcontrol == null) |
| 1166 |
{ |
| 1167 |
|
| 1168 |
bcontrolfile = false; |
| 1169 |
} |
| 1170 |
else |
| 1171 |
{ |
| 1172 |
bcontrolfile = true; |
| 1173 |
if ((hscellcontrol.size()==1) && (!bmissing)) |
| 1174 |
{ |
| 1175 |
|
| 1176 |
|
| 1177 |
numcontrolmarks = 1; |
| 1178 |
} |
| 1179 |
else |
| 1180 |
{ |
| 1181 |
|
| 1182 |
numcontrolmarks = nummarks; |
| 1183 |
} |
| 1184 |
} |
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
loadGrid(grid,bpresent,bpresentmarks,marks,nshift,nbinsize,bcenterinterval,noffsetleft, |
| 1189 |
noffsetright,hmfiles,szcell,szmarkdir,hmchrom,0,szcolfields,bpeaks,false,bbinarizebam, bpairend,bmixed); |
| 1190 |
if (bcontrolfile) |
| 1191 |
{ |
| 1192 |
if ((gridcontrol[0] == null)||(gridcontrol[0][0].length !=numcontrolmarks)) |
| 1193 |
{ |
| 1194 |
|
| 1195 |
|
| 1196 |
for (int ni = 0; ni < chroms.length; ni++) |
| 1197 |
{ |
| 1198 |
gridcontrol[ni] = new int[lengths[ni]/nbinsize][numcontrolmarks]; |
| 1199 |
sumgridcontrol[ni] = new int[lengths[ni]/nbinsize][numcontrolmarks]; |
| 1200 |
} |
| 1201 |
} |
| 1202 |
|
| 1203 |
|
| 1204 |
loadGrid(gridcontrol,bpresentcontrol,bpresentmarkscontrol,marks,nshift,nbinsize,bcenterinterval,noffsetleft,noffsetright, |
| 1205 |
hmfilescontrol,szcell,szcontroldir,hmchrom,npseudocountcontrol,szcolfields,bpeaks,true,bbinarizebam,bpairend,bmixed); |
| 1206 |
} |
| 1207 |
|
| 1208 |
|
| 1209 |
if (szoutputsignaldir!=null) |
| 1210 |
{ |
| 1211 |
|
| 1212 |
for (int nchrom = 0; nchrom < chroms.length; nchrom++) |
| 1213 |
{ |
| 1214 |
if (bpresent[nchrom]) |
| 1215 |
{ |
| 1216 |
|
| 1217 |
if (bgzip) |
| 1218 |
{ |
| 1219 |
if (bsplit) |
| 1220 |
{ |
| 1221 |
|
| 1222 |
int[][] grid_nchrom = grid[nchrom]; |
| 1223 |
int nsplit = 0; |
| 1224 |
GZIPOutputStream pwzip = null; |
| 1225 |
for (int nbin = 0; nbin < grid_nchrom.length; nbin++) |
| 1226 |
{ |
| 1227 |
if (nbin % numsplitbins == 0) |
| 1228 |
{ |
| 1229 |
String szfile = szoutputsignaldir+"/"+szcell+"_"+chroms[nchrom]+"."+nsplit+"_signal.txt.gz"; |
| 1230 |
System.out.println("Writing to file "+szfile); |
| 1231 |
|
| 1232 |
pwzip = new GZIPOutputStream(new FileOutputStream(szfile)); |
| 1233 |
|
| 1234 |
|
| 1235 |
String szout = szcell+"\t"+chroms[nchrom]+"."+nsplit +"\n"; |
| 1236 |
byte[] btformat = szout.getBytes(); |
| 1237 |
pwzip.write(btformat,0,btformat.length); |
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
StringBuffer sbout = new StringBuffer(); |
| 1242 |
for (int nmark = 0; nmark < marks.length-1; nmark++) |
| 1243 |
{ |
| 1244 |
sbout.append(marks[nmark]+"\t"); |
| 1245 |
|
| 1246 |
} |
| 1247 |
sbout.append(marks[marks.length-1]+"\n"); |
| 1248 |
btformat = sbout.toString().getBytes(); |
| 1249 |
pwzip.write(btformat,0,btformat.length); |
| 1250 |
nsplit++; |
| 1251 |
} |
| 1252 |
|
| 1253 |
|
| 1254 |
int[] grid_nchrom_nbin = grid_nchrom[nbin]; |
| 1255 |
|
| 1256 |
StringBuffer sbout = new StringBuffer(); |
| 1257 |
for (int nmark = 0; nmark < grid_nchrom_nbin.length-1; nmark++) |
| 1258 |
{ |
| 1259 |
sbout.append(grid_nchrom_nbin[nmark]+"\t"); |
| 1260 |
|
| 1261 |
} |
| 1262 |
sbout.append(grid_nchrom_nbin[marks.length-1]+"\n"); |
| 1263 |
byte[] btformat = sbout.toString().getBytes(); |
| 1264 |
pwzip.write(btformat,0,btformat.length); |
| 1265 |
|
| 1266 |
|
| 1267 |
if (((nbin + 1) == grid_nchrom.length)||((nbin+1) % numsplitbins == 0)) |
| 1268 |
{ |
| 1269 |
pwzip.finish(); |
| 1270 |
pwzip.close(); |
| 1271 |
} |
| 1272 |
} |
| 1273 |
} |
| 1274 |
else |
| 1275 |
{ |
| 1276 |
|
| 1277 |
|
| 1278 |
String szfile = szoutputsignaldir+"/"+szcell+"_"+chroms[nchrom]+"_signal.txt.gz"; |
| 1279 |
System.out.println("Writing to file "+szfile); |
| 1280 |
|
| 1281 |
GZIPOutputStream pwzip = new GZIPOutputStream(new FileOutputStream(szfile)); |
| 1282 |
|
| 1283 |
|
| 1284 |
String szout = szcell+"\t"+chroms[nchrom]+"\n"; |
| 1285 |
byte[] btformat = szout.getBytes(); |
| 1286 |
pwzip.write(btformat,0,btformat.length); |
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
StringBuffer sbout = new StringBuffer(); |
| 1291 |
for (int nmark = 0; nmark < marks.length-1; nmark++) |
| 1292 |
{ |
| 1293 |
sbout.append(marks[nmark]+"\t"); |
| 1294 |
|
| 1295 |
} |
| 1296 |
sbout.append(marks[marks.length-1]+"\n"); |
| 1297 |
btformat = sbout.toString().getBytes(); |
| 1298 |
pwzip.write(btformat,0,btformat.length); |
| 1299 |
|
| 1300 |
|
| 1301 |
int[][] grid_nchrom = grid[nchrom]; |
| 1302 |
for (int nbin = 0; nbin < grid_nchrom.length; nbin++) |
| 1303 |
{ |
| 1304 |
int[] grid_nchrom_nbin = grid_nchrom[nbin]; |
| 1305 |
sbout = new StringBuffer(); |
| 1306 |
for (int nmark = 0; nmark < grid_nchrom_nbin.length-1; nmark++) |
| 1307 |
{ |
| 1308 |
sbout.append(grid_nchrom_nbin[nmark]+"\t"); |
| 1309 |
|
| 1310 |
} |
| 1311 |
sbout.append(grid_nchrom_nbin[marks.length-1]+"\n"); |
| 1312 |
btformat = sbout.toString().getBytes(); |
| 1313 |
pwzip.write(btformat,0,btformat.length); |
| 1314 |
|
| 1315 |
} |
| 1316 |
pwzip.finish(); |
| 1317 |
pwzip.close(); |
| 1318 |
} |
| 1319 |
} |
| 1320 |
else |
| 1321 |
{ |
| 1322 |
if (bsplit) |
| 1323 |
{ |
| 1324 |
int[][] grid_nchrom = grid[nchrom]; |
| 1325 |
int nsplit = 0; |
| 1326 |
PrintWriter pw = null; |
| 1327 |
for (int nbin = 0; nbin < grid_nchrom.length; nbin++) |
| 1328 |
{ |
| 1329 |
if (nbin % numsplitbins == 0) |
| 1330 |
{ |
| 1331 |
String szfile = szoutputsignaldir+"/"+szcell+"_"+chroms[nchrom]+"."+nsplit+"_signal.txt"; |
| 1332 |
pw = new PrintWriter(szfile); |
| 1333 |
pw.println(szcell+"\t"+chroms[nchrom]+"."+nsplit); |
| 1334 |
|
| 1335 |
|
| 1336 |
for (int nmark = 0; nmark < marks.length-1; nmark++) |
| 1337 |
{ |
| 1338 |
pw.print(marks[nmark]+"\t"); |
| 1339 |
} |
| 1340 |
pw.println(marks[marks.length-1]); |
| 1341 |
|
| 1342 |
nsplit++; |
| 1343 |
} |
| 1344 |
|
| 1345 |
|
| 1346 |
int[] grid_nchrom_nbin = grid_nchrom[nbin]; |
| 1347 |
for (int nmark = 0; nmark < grid_nchrom_nbin.length-1; nmark++) |
| 1348 |
{ |
| 1349 |
pw.print(grid_nchrom_nbin[nmark]+"\t"); |
| 1350 |
} |
| 1351 |
pw.println(grid_nchrom_nbin[marks.length-1]); |
| 1352 |
|
| 1353 |
if (((nbin + 1) == grid_nchrom.length)||((nbin+1) % numsplitbins == 0)) |
| 1354 |
{ |
| 1355 |
pw.close(); |
| 1356 |
} |
| 1357 |
} |
| 1358 |
} |
| 1359 |
else |
| 1360 |
{ |
| 1361 |
String szfile = szoutputsignaldir+"/"+szcell+"_"+chroms[nchrom]+"_signal.txt"; |
| 1362 |
PrintWriter pw = new PrintWriter(szfile); |
| 1363 |
pw.println(szcell+"\t"+chroms[nchrom]); |
| 1364 |
|
| 1365 |
|
| 1366 |
for (int nmark = 0; nmark < marks.length-1; nmark++) |
| 1367 |
{ |
| 1368 |
pw.print(marks[nmark]+"\t"); |
| 1369 |
} |
| 1370 |
pw.println(marks[marks.length-1]); |
| 1371 |
|
| 1372 |
|
| 1373 |
int[][] grid_nchrom = grid[nchrom]; |
| 1374 |
for (int nbin = 0; nbin < grid_nchrom.length; nbin++) |
| 1375 |
{ |
| 1376 |
int[] grid_nchrom_nbin = grid_nchrom[nbin]; |
| 1377 |
for (int nmark = 0; nmark < grid_nchrom_nbin.length-1; nmark++) |
| 1378 |
{ |
| 1379 |
pw.print(grid_nchrom_nbin[nmark]+"\t"); |
| 1380 |
} |
| 1381 |
pw.println(grid_nchrom_nbin[marks.length-1]); |
| 1382 |
} |
| 1383 |
pw.close(); |
| 1384 |
} |
| 1385 |
} |
| 1386 |
} |
| 1387 |
} |
| 1388 |
} |
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
if ((bcontrolfile)&&(szoutputcontroldir!=null)) |
| 1393 |
{ |
| 1394 |
for (int nchrom = 0; nchrom < chroms.length; nchrom++) |
| 1395 |
{ |
| 1396 |
if (bpresent[nchrom]) |
| 1397 |
{ |
| 1398 |
if (bgzip) |
| 1399 |
{ |
| 1400 |
if (bsplit) |
| 1401 |
{ |
| 1402 |
|
| 1403 |
int[][] gridcontrol_nchrom = gridcontrol[nchrom]; |
| 1404 |
int nsplit = 0; |
| 1405 |
GZIPOutputStream pwzip = null; |
| 1406 |
for (int nbin = 0; nbin < gridcontrol_nchrom.length; nbin++) |
| 1407 |
{ |
| 1408 |
if (nbin % numsplitbins == 0) |
| 1409 |
{ |
| 1410 |
|
| 1411 |
String szfile = szoutputcontroldir+"/"+szcell+"_"+chroms[nchrom]+"."+nsplit+"_controlsignal.txt.gz"; |
| 1412 |
System.out.println("Writing to file "+szfile); |
| 1413 |
pwzip = new GZIPOutputStream(new FileOutputStream(szfile)); |
| 1414 |
|
| 1415 |
String szout = szcell+"\t"+chroms[nchrom]+"."+nsplit+"\n"; |
| 1416 |
byte[] btformat = szout.getBytes(); |
| 1417 |
pwzip.write(btformat,0,btformat.length); |
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
if (numcontrolmarks == 1) |
| 1422 |
{ |
| 1423 |
szout = "Control\n"; |
| 1424 |
btformat = szout.getBytes(); |
| 1425 |
pwzip.write(btformat,0,btformat.length); |
| 1426 |
|
| 1427 |
} |
| 1428 |
else |
| 1429 |
{ |
| 1430 |
StringBuffer sbout = new StringBuffer(); |
| 1431 |
for (int nmark = 0; nmark < marks.length-1; nmark++) |
| 1432 |
{ |
| 1433 |
sbout.append(marks[nmark]+"\t"); |
| 1434 |
|
| 1435 |
} |
| 1436 |
sbout.append(marks[marks.length-1]+"\n"); |
| 1437 |
|
| 1438 |
btformat = sbout.toString().getBytes(); |
| 1439 |
pwzip.write(btformat,0,btformat.length); |
| 1440 |
} |
| 1441 |
nsplit++; |
| 1442 |
} |
| 1443 |
|
| 1444 |
int[] gridcontrol_nchrom_nbin = gridcontrol_nchrom[nbin]; |
| 1445 |
if (gridcontrol_nchrom_nbin.length == 1) |
| 1446 |
{ |
| 1447 |
String szout = (gridcontrol_nchrom_nbin[0]-npseudocountcontrol)+"\n"; |
| 1448 |
|
| 1449 |
byte[] btformat = szout.getBytes(); |
| 1450 |
pwzip.write(btformat,0,btformat.length); |
| 1451 |
|
| 1452 |
} |
| 1453 |
else |
| 1454 |
{ |
| 1455 |
StringBuffer sbout = new StringBuffer(); |
| 1456 |
for (int nmark = 0; nmark < gridcontrol_nchrom_nbin.length-1; nmark++) |
| 1457 |
{ |
| 1458 |
sbout.append((gridcontrol_nchrom_nbin[nmark]-npseudocountcontrol)+"\t"); |
| 1459 |
|
| 1460 |
} |
| 1461 |
sbout.append((gridcontrol_nchrom_nbin[marks.length-1]-npseudocountcontrol)+"\n"); |
| 1462 |
byte[] btformat = sbout.toString().getBytes(); |
| 1463 |
pwzip.write(btformat,0,btformat.length); |
| 1464 |
|
| 1465 |
} |
| 1466 |
|
| 1467 |
if (((nbin + 1) == gridcontrol_nchrom.length)||((nbin+1) % numsplitbins == 0)) |
| 1468 |
{ |
| 1469 |
pwzip.finish(); |
| 1470 |
pwzip.close(); |
| 1471 |
} |
| 1472 |
} |
| 1473 |
} |
| 1474 |
else |
| 1475 |
{ |
| 1476 |
if (bsplit) |
| 1477 |
{ |
| 1478 |
|
| 1479 |
int[][] gridcontrol_nchrom = gridcontrol[nchrom]; |
| 1480 |
int nsplit = 0; |
| 1481 |
GZIPOutputStream pwzip = null; |
| 1482 |
for (int nbin = 0; nbin < gridcontrol_nchrom.length; nbin++) |
| 1483 |
{ |
| 1484 |
if (nbin % numsplitbins == 0) |
| 1485 |
{ |
| 1486 |
|
| 1487 |
String szfile = szoutputcontroldir+"/"+szcell+"_"+chroms[nchrom]+"."+nsplit+"_controlsignal.txt.gz"; |
| 1488 |
System.out.println("Writing to file "+szfile); |
| 1489 |
pwzip = new GZIPOutputStream(new FileOutputStream(szfile)); |
| 1490 |
|
| 1491 |
String szout = szcell+"\t"+chroms[nchrom]+"."+nsplit+"\n"; |
| 1492 |
byte[] btformat = szout.getBytes(); |
| 1493 |
pwzip.write(btformat,0,btformat.length); |
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
if (numcontrolmarks == 1) |
| 1498 |
{ |
| 1499 |
szout = "Control\n"; |
| 1500 |
btformat = szout.getBytes(); |
| 1501 |
pwzip.write(btformat,0,btformat.length); |
| 1502 |
|
| 1503 |
} |
| 1504 |
else |
| 1505 |
{ |
| 1506 |
StringBuffer sbout = new StringBuffer(); |
| 1507 |
for (int nmark = 0; nmark < marks.length-1; nmark++) |
| 1508 |
{ |
| 1509 |
sbout.append(marks[nmark]+"\t"); |
| 1510 |
|
| 1511 |
} |
| 1512 |
sbout.append(marks[marks.length-1]+"\n"); |
| 1513 |
|
| 1514 |
|
| 1515 |
btformat = sbout.toString().getBytes(); |
| 1516 |
pwzip.write(btformat,0,btformat.length); |
| 1517 |
} |
| 1518 |
nsplit++; |
| 1519 |
} |
| 1520 |
|
| 1521 |
|
| 1522 |
int[] gridcontrol_nchrom_nbin = gridcontrol_nchrom[nbin]; |
| 1523 |
if (gridcontrol_nchrom_nbin.length == 1) |
| 1524 |
{ |
| 1525 |
String szout = (gridcontrol_nchrom_nbin[0]-npseudocountcontrol)+"\n"; |
| 1526 |
|
| 1527 |
byte[] btformat = szout.getBytes(); |
| 1528 |
pwzip.write(btformat,0,btformat.length); |
| 1529 |
|
| 1530 |
} |
| 1531 |
else |
| 1532 |
{ |
| 1533 |
StringBuffer sbout = new StringBuffer(); |
| 1534 |
for (int nmark = 0; nmark < gridcontrol_nchrom_nbin.length-1; nmark++) |
| 1535 |
{ |
| 1536 |
sbout.append((gridcontrol_nchrom_nbin[nmark]-npseudocountcontrol)+"\t"); |
| 1537 |
|
| 1538 |
} |
| 1539 |
sbout.append((gridcontrol_nchrom_nbin[marks.length-1]-npseudocountcontrol)+"\n"); |
| 1540 |
byte[] btformat = sbout.toString().getBytes(); |
| 1541 |
pwzip.write(btformat,0,btformat.length); |
| 1542 |
|
| 1543 |
} |
| 1544 |
|
| 1545 |
if (((nbin + 1) == gridcontrol_nchrom.length)||((nbin+1) % numsplitbins == 0)) |
| 1546 |
{ |
| 1547 |
pwzip.finish(); |
| 1548 |
pwzip.close(); |
| 1549 |
} |
| 1550 |
} |
| 1551 |
} |
| 1552 |
else |
| 1553 |
{ |
| 1554 |
|
| 1555 |
String szfile = szoutputcontroldir+"/"+szcell+"_"+chroms[nchrom]+"_controlsignal.txt.gz"; |
| 1556 |
System.out.println("Writing to file "+szfile); |
| 1557 |
GZIPOutputStream pwzip = new GZIPOutputStream(new FileOutputStream(szfile)); |
| 1558 |
|
| 1559 |
String szout = szcell+"\t"+chroms[nchrom]+"\n"; |
| 1560 |
byte[] btformat = szout.getBytes(); |
| 1561 |
pwzip.write(btformat,0,btformat.length); |
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
if (numcontrolmarks == 1) |
| 1566 |
{ |
| 1567 |
szout = "Control\n"; |
| 1568 |
btformat = szout.getBytes(); |
| 1569 |
pwzip.write(btformat,0,btformat.length); |
| 1570 |
|
| 1571 |
} |
| 1572 |
else |
| 1573 |
{ |
| 1574 |
StringBuffer sbout = new StringBuffer(); |
| 1575 |
for (int nmark = 0; nmark < marks.length-1; nmark++) |
| 1576 |
{ |
| 1577 |
sbout.append(marks[nmark]+"\t"); |
| 1578 |
|
| 1579 |
} |
| 1580 |
sbout.append(marks[marks.length-1]+"\n"); |
| 1581 |
|
| 1582 |
|
| 1583 |
btformat = sbout.toString().getBytes(); |
| 1584 |
pwzip.write(btformat,0,btformat.length); |
| 1585 |
} |
| 1586 |
|
| 1587 |
|
| 1588 |
int[][] gridcontrol_nchrom = gridcontrol[nchrom]; |
| 1589 |
|
| 1590 |
for (int nbin = 0; nbin < gridcontrol_nchrom.length; nbin++) |
| 1591 |
{ |
| 1592 |
int[] gridcontrol_nchrom_nbin = gridcontrol_nchrom[nbin]; |
| 1593 |
if (gridcontrol_nchrom_nbin.length == 1) |
| 1594 |
{ |
| 1595 |
szout = (gridcontrol_nchrom_nbin[0]-npseudocountcontrol)+"\n"; |
| 1596 |
|
| 1597 |
btformat = szout.getBytes(); |
| 1598 |
pwzip.write(btformat,0,btformat.length); |
| 1599 |
|
| 1600 |
} |
| 1601 |
else |
| 1602 |
{ |
| 1603 |
StringBuffer sbout = new StringBuffer(); |
| 1604 |
for (int nmark = 0; nmark < gridcontrol_nchrom_nbin.length-1; nmark++) |
| 1605 |
{ |
| 1606 |
sbout.append((gridcontrol_nchrom_nbin[nmark]-npseudocountcontrol)+"\t"); |
| 1607 |
|
| 1608 |
} |
| 1609 |
sbout.append((gridcontrol_nchrom_nbin[marks.length-1]-npseudocountcontrol)+"\n"); |
| 1610 |
btformat = sbout.toString().getBytes(); |
| 1611 |
pwzip.write(btformat,0,btformat.length); |
| 1612 |
|
| 1613 |
} |
| 1614 |
} |
| 1615 |
pwzip.finish(); |
| 1616 |
pwzip.close(); |
| 1617 |
} |
| 1618 |
} |
| 1619 |
} |
| 1620 |
else |
| 1621 |
{ |
| 1622 |
if (bsplit) |
| 1623 |
{ |
| 1624 |
|
| 1625 |
int[][] gridcontrol_nchrom = gridcontrol[nchrom]; |
| 1626 |
int nsplit = 0; |
| 1627 |
PrintWriter pw = null; |
| 1628 |
for (int nbin = 0; nbin < gridcontrol_nchrom.length; nbin++) |
| 1629 |
{ |
| 1630 |
if (nbin % numsplitbins == 0) |
| 1631 |
{ |
| 1632 |
|
| 1633 |
String szfile = szoutputcontroldir+"/"+szcell+"_"+chroms[nchrom]+"."+nsplit+"_controlsignal.txt"; |
| 1634 |
System.out.println("Writing to file "+szfile); |
| 1635 |
pw = new PrintWriter(szfile); |
| 1636 |
pw.println(szcell+"\t"+chroms[nchrom]+"."+nsplit); |
| 1637 |
|
| 1638 |
if (numcontrolmarks == 1) |
| 1639 |
{ |
| 1640 |
pw.println("Control"); |
| 1641 |
} |
| 1642 |
else |
| 1643 |
{ |
| 1644 |
for (int nmark = 0; nmark < marks.length-1; nmark++) |
| 1645 |
{ |
| 1646 |
pw.print(marks[nmark]+"\t"); |
| 1647 |
} |
| 1648 |
pw.println(marks[marks.length-1]); |
| 1649 |
} |
| 1650 |
|
| 1651 |
nsplit++; |
| 1652 |
} |
| 1653 |
|
| 1654 |
int[] gridcontrol_nchrom_nbin = gridcontrol_nchrom[nbin]; |
| 1655 |
if (gridcontrol_nchrom_nbin.length == 1) |
| 1656 |
{ |
| 1657 |
pw.println(gridcontrol_nchrom_nbin[0]-npseudocountcontrol); |
| 1658 |
} |
| 1659 |
else |
| 1660 |
{ |
| 1661 |
for (int nmark = 0; nmark < gridcontrol_nchrom_nbin.length-1; nmark++) |
| 1662 |
{ |
| 1663 |
pw.print((gridcontrol_nchrom_nbin[nmark]-npseudocountcontrol)+"\t"); |
| 1664 |
} |
| 1665 |
pw.println(gridcontrol_nchrom_nbin[marks.length-1]-npseudocountcontrol); |
| 1666 |
} |
| 1667 |
|
| 1668 |
if (((nbin + 1) == gridcontrol_nchrom.length)||((nbin+1) % numsplitbins == 0)) |
| 1669 |
{ |
| 1670 |
pw.close(); |
| 1671 |
} |
| 1672 |
} |
| 1673 |
} |
| 1674 |
else |
| 1675 |
{ |
| 1676 |
|
| 1677 |
String szfile = szoutputcontroldir+"/"+szcell+"_"+chroms[nchrom]+"_controlsignal.txt"; |
| 1678 |
System.out.println("Writing to file "+szfile); |
| 1679 |
PrintWriter pw = new PrintWriter(szfile); |
| 1680 |
pw.println(szcell+"\t"+chroms[nchrom]); |
| 1681 |
|
| 1682 |
if (numcontrolmarks == 1) |
| 1683 |
{ |
| 1684 |
pw.println("Control"); |
| 1685 |
} |
| 1686 |
else |
| 1687 |
{ |
| 1688 |
for (int nmark = 0; nmark < marks.length-1; nmark++) |
| 1689 |
{ |
| 1690 |
pw.print(marks[nmark]+"\t"); |
| 1691 |
} |
| 1692 |
pw.println(marks[marks.length-1]); |
| 1693 |
} |
| 1694 |
|
| 1695 |
|
| 1696 |
int[][] gridcontrol_nchrom = gridcontrol[nchrom]; |
| 1697 |
for (int nbin = 0; nbin < gridcontrol_nchrom.length; nbin++) |
| 1698 |
{ |
| 1699 |
int[] gridcontrol_nchrom_nbin = gridcontrol_nchrom[nbin]; |
| 1700 |
if (gridcontrol_nchrom_nbin.length == 1) |
| 1701 |
{ |
| 1702 |
pw.println(gridcontrol_nchrom_nbin[0]-npseudocountcontrol); |
| 1703 |
} |
| 1704 |
else |
| 1705 |
{ |
| 1706 |
for (int nmark = 0; nmark < gridcontrol_nchrom_nbin.length-1; nmark++) |
| 1707 |
{ |
| 1708 |
pw.print((gridcontrol_nchrom_nbin[nmark]-npseudocountcontrol)+"\t"); |
| 1709 |
} |
| 1710 |
pw.println(gridcontrol_nchrom_nbin[marks.length-1]-npseudocountcontrol); |
| 1711 |
} |
| 1712 |
} |
| 1713 |
pw.close(); |
| 1714 |
} |
| 1715 |
} |
| 1716 |
} |
| 1717 |
} |
| 1718 |
} |
| 1719 |
|
| 1720 |
int nummarks_m1 = nummarks - 1; |
| 1721 |
|
| 1722 |
if (bcontrolfile) |
| 1723 |
{ |
| 1724 |
|
| 1725 |
|
| 1726 |
|
| 1727 |
windowSumGrid(gridcontrol,sumgridcontrol,nflankwidthcontrol); |
| 1728 |
|
| 1729 |
|
| 1730 |
int[][] thresholds = null; |
| 1731 |
|
| 1732 |
if (!bpeaks) |
| 1733 |
{ |
| 1734 |
thresholds = determineMarkThresholdsFromBinnedDataArrayAgainstControl(grid,sumgridcontrol, |
| 1735 |
bpresent,bpresentcontrol,dpoissonthresh,dfoldthresh,bcontainsthresh,dcountthresh); |
| 1736 |
} |
| 1737 |
|
| 1738 |
for (int nchrom = 0; nchrom < chroms.length; nchrom++) |
| 1739 |
{ |
| 1740 |
if ((bpresent[nchrom])&&(bpresentcontrol[nchrom])) |
| 1741 |
{ |
| 1742 |
|
| 1743 |
if (bgzip) |
| 1744 |
{ |
| 1745 |
if (bsplit) |
| 1746 |
{ |
| 1747 |
int[][] grid_nchrom = grid[nchrom]; |
| 1748 |
int[][] sumgridcontrol_nchrom = sumgridcontrol[nchrom]; |
| 1749 |
int nsplit = 0; |
| 1750 |
GZIPOutputStream pwzip = null; |
| 1751 |
|
| 1752 |
for (int nbin = 0; nbin < grid_nchrom.length; nbin++) |
| 1753 |
{ |
| 1754 |
if (nbin % numsplitbins == 0) |
| 1755 |
{ |
| 1756 |
String szfile = szoutputbinarydir+"/"+szcell+"_"+chroms[nchrom]+"."+nsplit+"_binary.txt.gz"; |
| 1757 |
System.out.println("Writing to file "+szfile); |
| 1758 |
pwzip = new GZIPOutputStream(new FileOutputStream(szfile)); |
| 1759 |
|
| 1760 |
|
| 1761 |
String szout = szcell+"\t"+chroms[nchrom]+"."+nsplit +"\n"; |
| 1762 |
byte[] btformat = szout.getBytes(); |
| 1763 |
pwzip.write(btformat,0,btformat.length); |
| 1764 |
|
| 1765 |
|
| 1766 |
StringBuffer sbout = new StringBuffer(); |
| 1767 |
for (int nmark = 0; nmark < nummarks_m1; nmark++) |
| 1768 |
{ |
| 1769 |
sbout.append(marks[nmark]+"\t"); |
| 1770 |
|
| 1771 |
} |
| 1772 |
sbout.append(marks[nummarks_m1]+"\n"); |
| 1773 |
|
| 1774 |
|
| 1775 |
btformat = sbout.toString().getBytes(); |
| 1776 |
pwzip.write(btformat,0,btformat.length); |
| 1777 |
nsplit++; |
| 1778 |
} |
| 1779 |
|
| 1780 |
|
| 1781 |
int[] grid_nchrom_nbin = grid_nchrom[nbin]; |
| 1782 |
int[] sumgrid_nchrom_nbin = sumgridcontrol_nchrom[nbin]; |
| 1783 |
|
| 1784 |
StringBuffer sbout = new StringBuffer(); |
| 1785 |
|
| 1786 |
for (int nmark = 0; nmark < nummarks_m1; nmark++) |
| 1787 |
{ |
| 1788 |
int ncontrolval; |
| 1789 |
if (numcontrolmarks == 1) |
| 1790 |
{ |
| 1791 |
ncontrolval = sumgrid_nchrom_nbin[0]; |
| 1792 |
} |
| 1793 |
else |
| 1794 |
{ |
| 1795 |
ncontrolval = sumgrid_nchrom_nbin[nmark]; |
| 1796 |
} |
| 1797 |
|
| 1798 |
|
| 1799 |
if (!bpresentmarks[nmark]) |
| 1800 |
{ |
| 1801 |
sbout.append("2\t"); |
| 1802 |
|
| 1803 |
} |
| 1804 |
else if ((bpeaks&&(grid_nchrom_nbin[nmark]>=1)) ||(!bpeaks&&(thresholds[nmark][ncontrolval] <= grid_nchrom_nbin[nmark]))) |
| 1805 |
{ |
| 1806 |
sbout.append("1\t"); |
| 1807 |
|
| 1808 |
} |
| 1809 |
else |
| 1810 |
{ |
| 1811 |
sbout.append("0\t"); |
| 1812 |
|
| 1813 |
} |
| 1814 |
} |
| 1815 |
|
| 1816 |
int ncontrolval; |
| 1817 |
if (numcontrolmarks == 1) |
| 1818 |
{ |
| 1819 |
ncontrolval = sumgrid_nchrom_nbin[0]; |
| 1820 |
} |
| 1821 |
else |
| 1822 |
{ |
| 1823 |
ncontrolval = sumgrid_nchrom_nbin[nummarks_m1]; |
| 1824 |
} |
| 1825 |
|
| 1826 |
if (!bpresentmarks[nummarks_m1]) |
| 1827 |
{ |
| 1828 |
sbout.append("2\n"); |
| 1829 |
|
| 1830 |
} |
| 1831 |
else if ((bpeaks&&(grid_nchrom_nbin[nummarks_m1]>=1))||(!bpeaks&&(thresholds[nummarks_m1][ncontrolval] <= grid_nchrom_nbin[nummarks_m1]))) |
| 1832 |
{ |
| 1833 |
sbout.append("1\n"); |
| 1834 |
|
| 1835 |
} |
| 1836 |
else |
| 1837 |
{ |
| 1838 |
sbout.append("0\n"); |
| 1839 |
|
| 1840 |
} |
| 1841 |
|
| 1842 |
byte[] btformat = sbout.toString().getBytes(); |
| 1843 |
pwzip.write(btformat,0,btformat.length); |
| 1844 |
|
| 1845 |
if (((nbin + 1) == grid_nchrom.length)||((nbin+1) % numsplitbins == 0)) |
| 1846 |
{ |
| 1847 |
pwzip.finish(); |
| 1848 |
pwzip.close(); |
| 1849 |
} |
| 1850 |
} |
| 1851 |
} |
| 1852 |
else |
| 1853 |
{ |
| 1854 |
String szfile = szoutputbinarydir+"/"+szcell+"_"+chroms[nchrom]+"_binary.txt.gz"; |
| 1855 |
System.out.println("Writing to file "+szfile); |
| 1856 |
GZIPOutputStream pwzip = new GZIPOutputStream(new FileOutputStream(szfile)); |
| 1857 |
|
| 1858 |
|
| 1859 |
String szout = szcell+"\t"+chroms[nchrom] +"\n"; |
| 1860 |
byte[] btformat = szout.getBytes(); |
| 1861 |
pwzip.write(btformat,0,btformat.length); |
| 1862 |
|
| 1863 |
|
| 1864 |
StringBuffer sbout = new StringBuffer(); |
| 1865 |
for (int nmark = 0; nmark < nummarks_m1; nmark++) |
| 1866 |
{ |
| 1867 |
sbout.append(marks[nmark]+"\t"); |
| 1868 |
|
| 1869 |
} |
| 1870 |
sbout.append(marks[nummarks_m1]+"\n"); |
| 1871 |
|
| 1872 |
|
| 1873 |
btformat = sbout.toString().getBytes(); |
| 1874 |
pwzip.write(btformat,0,btformat.length); |
| 1875 |
|
| 1876 |
int[][] grid_nchrom = grid[nchrom]; |
| 1877 |
int[][] sumgridcontrol_nchrom = sumgridcontrol[nchrom]; |
| 1878 |
for (int nbin = 0; nbin < grid_nchrom.length; nbin++) |
| 1879 |
{ |
| 1880 |
int[] grid_nchrom_nbin = grid_nchrom[nbin]; |
| 1881 |
int[] sumgrid_nchrom_nbin = sumgridcontrol_nchrom[nbin]; |
| 1882 |
|
| 1883 |
sbout = new StringBuffer(); |
| 1884 |
|
| 1885 |
for (int nmark = 0; nmark < nummarks_m1; nmark++) |
| 1886 |
{ |
| 1887 |
int ncontrolval; |
| 1888 |
if (numcontrolmarks == 1) |
| 1889 |
{ |
| 1890 |
ncontrolval = sumgrid_nchrom_nbin[0]; |
| 1891 |
} |
| 1892 |
else |
| 1893 |
{ |
| 1894 |
ncontrolval = sumgrid_nchrom_nbin[nmark]; |
| 1895 |
} |
| 1896 |
|
| 1897 |
|
| 1898 |
if (!bpresentmarks[nmark]) |
| 1899 |
{ |
| 1900 |
sbout.append("2\t"); |
| 1901 |
|
| 1902 |
} |
| 1903 |
else if ((bpeaks&&(grid_nchrom_nbin[nmark]>=1)) ||(!bpeaks&&(thresholds[nmark][ncontrolval] <= grid_nchrom_nbin[nmark]))) |
| 1904 |
{ |
| 1905 |
sbout.append("1\t"); |
| 1906 |
|
| 1907 |
} |
| 1908 |
else |
| 1909 |
{ |
| 1910 |
sbout.append("0\t"); |
| 1911 |
|
| 1912 |
} |
| 1913 |
} |
| 1914 |
|
| 1915 |
int ncontrolval; |
| 1916 |
if (numcontrolmarks == 1) |
| 1917 |
{ |
| 1918 |
ncontrolval = sumgrid_nchrom_nbin[0]; |
| 1919 |
} |
| 1920 |
else |
| 1921 |
{ |
| 1922 |
ncontrolval = sumgrid_nchrom_nbin[nummarks_m1]; |
| 1923 |
} |
| 1924 |
|
| 1925 |
if (!bpresentmarks[nummarks_m1]) |
| 1926 |
{ |
| 1927 |
sbout.append("2\n"); |
| 1928 |
|
| 1929 |
} |
| 1930 |
else if ((bpeaks&&(grid_nchrom_nbin[nummarks_m1]>=1))||(!bpeaks&&(thresholds[nummarks_m1][ncontrolval] <= grid_nchrom_nbin[nummarks_m1]))) |
| 1931 |
{ |
| 1932 |
sbout.append("1\n"); |
| 1933 |
|
| 1934 |
} |
| 1935 |
else |
| 1936 |
{ |
| 1937 |
sbout.append("0\n"); |
| 1938 |
|
| 1939 |
} |
| 1940 |
|
| 1941 |
btformat = sbout.toString().getBytes(); |
| 1942 |
pwzip.write(btformat,0,btformat.length); |
| 1943 |
} |
| 1944 |
|
| 1945 |
pwzip.finish(); |
| 1946 |
pwzip.close(); |
| 1947 |
} |
| 1948 |
} |
| 1949 |
else |
| 1950 |
{ |
| 1951 |
if (bsplit) |
| 1952 |
{ |
| 1953 |
int[][] grid_nchrom = grid[nchrom]; |
| 1954 |
int[][] sumgridcontrol_nchrom = sumgridcontrol[nchrom]; |
| 1955 |
int nsplit = 0; |
| 1956 |
PrintWriter pw = null; |
| 1957 |
for (int nbin = 0; nbin < grid_nchrom.length; nbin++) |
| 1958 |
{ |
| 1959 |
if (nbin % numsplitbins == 0) |
| 1960 |
{ |
| 1961 |
String szfile = szoutputbinarydir+"/"+szcell+"_"+chroms[nchrom]+"."+nsplit+"_binary.txt"; |
| 1962 |
System.out.println("Writing to file "+szfile); |
| 1963 |
pw = new PrintWriter(szfile); |
| 1964 |
|
| 1965 |
pw.println(szcell+"\t"+chroms[nchrom]+"."+nsplit); |
| 1966 |
|
| 1967 |
for (int nmark = 0; nmark < nummarks_m1; nmark++) |
| 1968 |
{ |
| 1969 |
pw.print(marks[nmark]+"\t"); |
| 1970 |
} |
| 1971 |
pw.println(marks[nummarks_m1]); |
| 1972 |
nsplit++; |
| 1973 |
} |
| 1974 |
|
| 1975 |
int[] grid_nchrom_nbin = grid_nchrom[nbin]; |
| 1976 |
int[] sumgrid_nchrom_nbin = sumgridcontrol_nchrom[nbin]; |
| 1977 |
|
| 1978 |
for (int nmark = 0; nmark < nummarks_m1; nmark++) |
| 1979 |
{ |
| 1980 |
int ncontrolval; |
| 1981 |
if (numcontrolmarks == 1) |
| 1982 |
{ |
| 1983 |
ncontrolval = sumgrid_nchrom_nbin[0]; |
| 1984 |
} |
| 1985 |
else |
| 1986 |
{ |
| 1987 |
ncontrolval = sumgrid_nchrom_nbin[nmark]; |
| 1988 |
} |
| 1989 |
|
| 1990 |
|
| 1991 |
if (!bpresentmarks[nmark]) |
| 1992 |
{ |
| 1993 |
pw.print("2\t"); |
| 1994 |
} |
| 1995 |
else if ((bpeaks&&(grid_nchrom_nbin[nmark]>=1)) ||(!bpeaks&&(thresholds[nmark][ncontrolval] <= grid_nchrom_nbin[nmark]))) |
| 1996 |
{ |
| 1997 |
pw.print("1\t"); |
| 1998 |
} |
| 1999 |
else |
| 2000 |
{ |
| 2001 |
pw.print("0\t"); |
| 2002 |
} |
| 2003 |
} |
| 2004 |
|
| 2005 |
int ncontrolval; |
| 2006 |
if (numcontrolmarks == 1) |
| 2007 |
{ |
| 2008 |
ncontrolval = sumgrid_nchrom_nbin[0]; |
| 2009 |
} |
| 2010 |
else |
| 2011 |
{ |
| 2012 |
ncontrolval = sumgrid_nchrom_nbin[nummarks_m1]; |
| 2013 |
} |
| 2014 |
|
| 2015 |
if (!bpresentmarks[nummarks_m1]) |
| 2016 |
{ |
| 2017 |
pw.println("2"); |
| 2018 |
} |
| 2019 |
else if ((bpeaks&&(grid_nchrom_nbin[nummarks_m1]>=1))|| |
| 2020 |
(!bpeaks&&(thresholds[nummarks_m1][ncontrolval] <= grid_nchrom_nbin[nummarks_m1]))) |
| 2021 |
{ |
| 2022 |
pw.println("1"); |
| 2023 |
} |
| 2024 |
else |
| 2025 |
{ |
| 2026 |
pw.println("0"); |
| 2027 |
} |
| 2028 |
|
| 2029 |
if (((nbin + 1) == grid_nchrom.length)||((nbin+1) % numsplitbins == 0)) |
| 2030 |
{ |
| 2031 |
pw.close(); |
| 2032 |
} |
| 2033 |
} |
| 2034 |
} |
| 2035 |
else |
| 2036 |
{ |
| 2037 |
String szfile = szoutputbinarydir+"/"+szcell+"_"+chroms[nchrom]+"_binary.txt"; |
| 2038 |
System.out.println("Writing to file "+szfile); |
| 2039 |
PrintWriter pw = new PrintWriter(szfile); |
| 2040 |
|
| 2041 |
pw.println(szcell+"\t"+chroms[nchrom]); |
| 2042 |
|
| 2043 |
for (int nmark = 0; nmark < nummarks_m1; nmark++) |
| 2044 |
{ |
| 2045 |
pw.print(marks[nmark]+"\t"); |
| 2046 |
} |
| 2047 |
pw.println(marks[nummarks_m1]); |
| 2048 |
|
| 2049 |
int[][] grid_nchrom = grid[nchrom]; |
| 2050 |
int[][] sumgridcontrol_nchrom = sumgridcontrol[nchrom]; |
| 2051 |
for (int nbin = 0; nbin < grid_nchrom.length; nbin++) |
| 2052 |
{ |
| 2053 |
int[] grid_nchrom_nbin = grid_nchrom[nbin]; |
| 2054 |
int[] sumgrid_nchrom_nbin = sumgridcontrol_nchrom[nbin]; |
| 2055 |
|
| 2056 |
for (int nmark = 0; nmark < nummarks_m1; nmark++) |
| 2057 |
{ |
| 2058 |
int ncontrolval; |
| 2059 |
if (numcontrolmarks == 1) |
| 2060 |
{ |
| 2061 |
ncontrolval = sumgrid_nchrom_nbin[0]; |
| 2062 |
} |
| 2063 |
else |
| 2064 |
{ |
| 2065 |
ncontrolval = sumgrid_nchrom_nbin[nmark]; |
| 2066 |
} |
| 2067 |
|
| 2068 |
|
| 2069 |
if (!bpresentmarks[nmark]) |
| 2070 |
{ |
| 2071 |
pw.print("2\t"); |
| 2072 |
} |
| 2073 |
else if ((bpeaks&&(grid_nchrom_nbin[nmark]>=1)) ||(!bpeaks&&(thresholds[nmark][ncontrolval] <= grid_nchrom_nbin[nmark]))) |
| 2074 |
{ |
| 2075 |
pw.print("1\t"); |
| 2076 |
} |
| 2077 |
else |
| 2078 |
{ |
| 2079 |
pw.print("0\t"); |
| 2080 |
} |
| 2081 |
} |
| 2082 |
|
| 2083 |
int ncontrolval; |
| 2084 |
if (numcontrolmarks == 1) |
| 2085 |
{ |
| 2086 |
ncontrolval = sumgrid_nchrom_nbin[0]; |
| 2087 |
} |
| 2088 |
else |
| 2089 |
{ |
| 2090 |
ncontrolval = sumgrid_nchrom_nbin[nummarks_m1]; |
| 2091 |
} |
| 2092 |
|
| 2093 |
|
| 2094 |
if (!bpresentmarks[nummarks_m1]) |
| 2095 |
{ |
| 2096 |
pw.println("2"); |
| 2097 |
} |
| 2098 |
else if ((bpeaks&&(grid_nchrom_nbin[nummarks_m1]>=1))||(!bpeaks&&(thresholds[nummarks_m1][ncontrolval] <= grid_nchrom_nbin[nummarks_m1]))) |
| 2099 |
{ |
| 2100 |
pw.println("1"); |
| 2101 |
} |
| 2102 |
else |
| 2103 |
{ |
| 2104 |
pw.println("0"); |
| 2105 |
} |
| 2106 |
} |
| 2107 |
pw.close(); |
| 2108 |
} |
| 2109 |
} |
| 2110 |
} |
| 2111 |
else |
| 2112 |
{ |
| 2113 |
if (bpresent[nchrom]&&!bpresentcontrol[nchrom]) |
| 2114 |
{ |
| 2115 |
System.out.println("WARNING for "+szcell+" "+chroms[nchrom]+" regular data is found but not control data"); |
| 2116 |
} |
| 2117 |
else if (!bpresent[nchrom]&&bpresentcontrol[nchrom]) |
| 2118 |
{ |
| 2119 |
System.out.println("WARNING for "+szcell+" "+chroms[nchrom]+" control data is found but not regular data"); |
| 2120 |
} |
| 2121 |
} |
| 2122 |
} |
| 2123 |
} |
| 2124 |
else |
| 2125 |
{ |
| 2126 |
|
| 2127 |
double[] thresholds = null; |
| 2128 |
|
| 2129 |
if (!bpeaks) |
| 2130 |
{ |
| 2131 |
thresholds = determineMarkThresholdsFromBinnedDataArray(grid,bpresent,dpoissonthresh,dfoldthresh,bcontainsthresh,dcountthresh); |
| 2132 |
} |
| 2133 |
|
| 2134 |
for (int nchrom = 0; nchrom < chroms.length; nchrom++) |
| 2135 |
{ |
| 2136 |
if (bpresent[nchrom]) |
| 2137 |
{ |
| 2138 |
if (bgzip) |
| 2139 |
{ |
| 2140 |
if (bsplit) |
| 2141 |
{ |
| 2142 |
int[][] grid_nchrom = grid[nchrom]; |
| 2143 |
int nsplit = 0; |
| 2144 |
GZIPOutputStream pwzip = null; |
| 2145 |
for (int nbin = 0; nbin < grid_nchrom.length; nbin++) |
| 2146 |
{ |
| 2147 |
if (nbin % numsplitbins == 0) |
| 2148 |
{ |
| 2149 |
String szfile = szoutputbinarydir+"/"+szcell+"_"+chroms[nchrom]+"."+nsplit+"_binary.txt.gz"; |
| 2150 |
System.out.println("Writing to file "+szfile); |
| 2151 |
pwzip = new GZIPOutputStream(new FileOutputStream(szfile)); |
| 2152 |
|
| 2153 |
|
| 2154 |
String szout = szcell+"\t"+chroms[nchrom]+"."+nsplit+"\n"; |
| 2155 |
byte[] btformat = szout.getBytes(); |
| 2156 |
pwzip.write(btformat,0,btformat.length); |
| 2157 |
|
| 2158 |
StringBuffer sbout = new StringBuffer(); |
| 2159 |
|
| 2160 |
for (int nmark = 0; nmark < marks.length-1; nmark++) |
| 2161 |
{ |
| 2162 |
sbout.append(marks[nmark]+"\t"); |
| 2163 |
|
| 2164 |
} |
| 2165 |
sbout.append(marks[nummarks_m1]+"\n"); |
| 2166 |
|
| 2167 |
|
| 2168 |
btformat = sbout.toString().getBytes(); |
| 2169 |
pwzip.write(btformat,0,btformat.length); |
| 2170 |
|
| 2171 |
nsplit++; |
| 2172 |
} |
| 2173 |
|
| 2174 |
StringBuffer sbout = new StringBuffer(); |
| 2175 |
|
| 2176 |
int[] grid_nchrom_nbin = grid_nchrom[nbin]; |
| 2177 |
for (int nmark = 0; nmark < nummarks_m1; nmark++) |
| 2178 |
{ |
| 2179 |
if (!bpresentmarks[nmark]) |
| 2180 |
{ |
| 2181 |
sbout.append("2\t"); |
| 2182 |
|
| 2183 |
} |
| 2184 |
else if ((bpeaks&&(grid_nchrom_nbin[nmark]>=1))||(!bpeaks&&(thresholds[nmark] <= grid_nchrom_nbin[nmark]))) |
| 2185 |
{ |
| 2186 |
sbout.append("1\t"); |
| 2187 |
|
| 2188 |
} |
| 2189 |
else |
| 2190 |
{ |
| 2191 |
sbout.append("0\t"); |
| 2192 |
|
| 2193 |
} |
| 2194 |
} |
| 2195 |
|
| 2196 |
if (!bpresentmarks[nummarks_m1]) |
| 2197 |
{ |
| 2198 |
sbout.append("2\n"); |
| 2199 |
|
| 2200 |
} |
| 2201 |
else if ((bpeaks&&(grid_nchrom_nbin[nummarks_m1]>=1))||(!bpeaks&&(thresholds[nummarks_m1] <= grid_nchrom_nbin[nummarks_m1]))) |
| 2202 |
{ |
| 2203 |
sbout.append("1\n"); |
| 2204 |
|
| 2205 |
} |
| 2206 |
else |
| 2207 |
{ |
| 2208 |
sbout.append("0\n"); |
| 2209 |
|
| 2210 |
} |
| 2211 |
|
| 2212 |
byte[] btformat = sbout.toString().getBytes(); |
| 2213 |
pwzip.write(btformat,0,btformat.length); |
| 2214 |
|
| 2215 |
if (((nbin + 1) == grid_nchrom.length)||((nbin+1) % numsplitbins == 0)) |
| 2216 |
{ |
| 2217 |
pwzip.finish(); |
| 2218 |
pwzip.close(); |
| 2219 |
} |
| 2220 |
} |
| 2221 |
} |
| 2222 |
else |
| 2223 |
{ |
| 2224 |
String szfile = szoutputbinarydir+"/"+szcell+"_"+chroms[nchrom]+"_binary.txt.gz"; |
| 2225 |
System.out.println("Writing to file "+szfile); |
| 2226 |
GZIPOutputStream pwzip = new GZIPOutputStream(new FileOutputStream(szfile)); |
| 2227 |
|
| 2228 |
|
| 2229 |
String szout = szcell+"\t"+chroms[nchrom]+"\n"; |
| 2230 |
byte[] btformat = szout.getBytes(); |
| 2231 |
pwzip.write(btformat,0,btformat.length); |
| 2232 |
|
| 2233 |
StringBuffer sbout = new StringBuffer(); |
| 2234 |
|
| 2235 |
for (int nmark = 0; nmark < marks.length-1; nmark++) |
| 2236 |
{ |
| 2237 |
sbout.append(marks[nmark]+"\t"); |
| 2238 |
|
| 2239 |
} |
| 2240 |
sbout.append(marks[nummarks_m1]+"\n"); |
| 2241 |
|
| 2242 |
|
| 2243 |
btformat = sbout.toString().getBytes(); |
| 2244 |
pwzip.write(btformat,0,btformat.length); |
| 2245 |
|
| 2246 |
int[][] grid_nchrom = grid[nchrom]; |
| 2247 |
for (int nbin = 0; nbin < grid_nchrom.length; nbin++) |
| 2248 |
{ |
| 2249 |
sbout = new StringBuffer(); |
| 2250 |
|
| 2251 |
int[] grid_nchrom_nbin = grid_nchrom[nbin]; |
| 2252 |
for (int nmark = 0; nmark < nummarks_m1; nmark++) |
| 2253 |
{ |
| 2254 |
if (!bpresentmarks[nmark]) |
| 2255 |
{ |
| 2256 |
sbout.append("2\t"); |
| 2257 |
|
| 2258 |
} |
| 2259 |
else if ((bpeaks&&(grid_nchrom_nbin[nmark]>=1))||(!bpeaks&&(thresholds[nmark] <= grid_nchrom_nbin[nmark]))) |
| 2260 |
{ |
| 2261 |
sbout.append("1\t"); |
| 2262 |
|
| 2263 |
} |
| 2264 |
else |
| 2265 |
{ |
| 2266 |
sbout.append("0\t"); |
| 2267 |
|
| 2268 |
} |
| 2269 |
} |
| 2270 |
|
| 2271 |
if (!bpresentmarks[nummarks_m1]) |
| 2272 |
{ |
| 2273 |
sbout.append("2\n"); |
| 2274 |
|
| 2275 |
} |
| 2276 |
else if ((bpeaks&&(grid_nchrom_nbin[nummarks_m1]>=1))||(!bpeaks&&(thresholds[nummarks_m1] <= grid_nchrom_nbin[nummarks_m1]))) |
| 2277 |
{ |
| 2278 |
sbout.append("1\n"); |
| 2279 |
|
| 2280 |
} |
| 2281 |
else |
| 2282 |
{ |
| 2283 |
sbout.append("0\n"); |
| 2284 |
|
| 2285 |
} |
| 2286 |
|
| 2287 |
btformat = sbout.toString().getBytes(); |
| 2288 |
pwzip.write(btformat,0,btformat.length); |
| 2289 |
} |
| 2290 |
pwzip.finish(); |
| 2291 |
pwzip.close(); |
| 2292 |
} |
| 2293 |
} |
| 2294 |
else |
| 2295 |
{ |
| 2296 |
if (bsplit) |
| 2297 |
{ |
| 2298 |
int[][] grid_nchrom = grid[nchrom]; |
| 2299 |
int nsplit = 0; |
| 2300 |
PrintWriter pw = null; |
| 2301 |
for (int nbin = 0; nbin < grid_nchrom.length; nbin++) |
| 2302 |
{ |
| 2303 |
if (nbin % numsplitbins == 0) |
| 2304 |
{ |
| 2305 |
String szfile = szoutputbinarydir+"/"+szcell+"_"+chroms[nchrom]+"."+nsplit+"_binary.txt"; |
| 2306 |
System.out.println("Writing to file "+szfile); |
| 2307 |
pw = new PrintWriter(szfile); |
| 2308 |
pw.println(szcell+"\t"+chroms[nchrom]+"."+nsplit); |
| 2309 |
for (int nmark = 0; nmark < marks.length-1; nmark++) |
| 2310 |
{ |
| 2311 |
pw.print(marks[nmark]+"\t"); |
| 2312 |
} |
| 2313 |
pw.println(marks[nummarks_m1]); |
| 2314 |
nsplit++; |
| 2315 |
} |
| 2316 |
|
| 2317 |
|
| 2318 |
int[] grid_nchrom_nbin = grid_nchrom[nbin]; |
| 2319 |
for (int nmark = 0; nmark < nummarks_m1; nmark++) |
| 2320 |
{ |
| 2321 |
if (!bpresentmarks[nmark]) |
| 2322 |
{ |
| 2323 |
pw.print("2\t"); |
| 2324 |
} |
| 2325 |
else if ((bpeaks&&(grid_nchrom_nbin[nmark]>=1))||(!bpeaks&&(thresholds[nmark] <= grid_nchrom_nbin[nmark]))) |
| 2326 |
{ |
| 2327 |
pw.print("1\t"); |
| 2328 |
} |
| 2329 |
else |
| 2330 |
{ |
| 2331 |
pw.print("0\t"); |
| 2332 |
} |
| 2333 |
} |
| 2334 |
|
| 2335 |
if (!bpresentmarks[nummarks_m1]) |
| 2336 |
{ |
| 2337 |
pw.println("2"); |
| 2338 |
} |
| 2339 |
else if ((bpeaks&&(grid_nchrom_nbin[nummarks_m1]>=1))||(!bpeaks&&(thresholds[nummarks_m1] <= grid_nchrom_nbin[nummarks_m1]))) |
| 2340 |
{ |
| 2341 |
pw.println("1"); |
| 2342 |
} |
| 2343 |
else |
| 2344 |
{ |
| 2345 |
pw.println("0"); |
| 2346 |
} |
| 2347 |
|
| 2348 |
if (((nbin + 1) == grid_nchrom.length)||((nbin+1) % numsplitbins == 0)) |
| 2349 |
{ |
| 2350 |
pw.close(); |
| 2351 |
} |
| 2352 |
} |
| 2353 |
} |
| 2354 |
else |
| 2355 |
{ |
| 2356 |
String szfile = szoutputbinarydir+"/"+szcell+"_"+chroms[nchrom]+"_binary.txt"; |
| 2357 |
System.out.println("Writing to file "+szfile); |
| 2358 |
PrintWriter pw = new PrintWriter(szfile); |
| 2359 |
pw.println(szcell+"\t"+chroms[nchrom]); |
| 2360 |
for (int nmark = 0; nmark < marks.length-1; nmark++) |
| 2361 |
{ |
| 2362 |
pw.print(marks[nmark]+"\t"); |
| 2363 |
} |
| 2364 |
pw.println(marks[nummarks_m1]); |
| 2365 |
|
| 2366 |
int[][] grid_nchrom = grid[nchrom]; |
| 2367 |
for (int nbin = 0; nbin < grid_nchrom.length; nbin++) |
| 2368 |
{ |
| 2369 |
|
| 2370 |
int[] grid_nchrom_nbin = grid_nchrom[nbin]; |
| 2371 |
for (int nmark = 0; nmark < nummarks_m1; nmark++) |
| 2372 |
{ |
| 2373 |
if (!bpresentmarks[nmark]) |
| 2374 |
{ |
| 2375 |
pw.print("2\t"); |
| 2376 |
} |
| 2377 |
else if ((bpeaks&&(grid_nchrom_nbin[nmark]>=1))||(!bpeaks&&(thresholds[nmark] <= grid_nchrom_nbin[nmark]))) |
| 2378 |
{ |
| 2379 |
pw.print("1\t"); |
| 2380 |
} |
| 2381 |
else |
| 2382 |
{ |
| 2383 |
pw.print("0\t"); |
| 2384 |
} |
| 2385 |
} |
| 2386 |
|
| 2387 |
if (!bpresentmarks[nummarks_m1]) |
| 2388 |
{ |
| 2389 |
pw.println("2"); |
| 2390 |
} |
| 2391 |
else if ((bpeaks&&(grid_nchrom_nbin[nummarks_m1]>=1))||(!bpeaks&&(thresholds[nummarks_m1] <= grid_nchrom_nbin[nummarks_m1]))) |
| 2392 |
{ |
| 2393 |
pw.println("1"); |
| 2394 |
} |
| 2395 |
else |
| 2396 |
{ |
| 2397 |
pw.println("0"); |
| 2398 |
} |
| 2399 |
} |
| 2400 |
pw.close(); |
| 2401 |
} |
| 2402 |
} |
| 2403 |
} |
| 2404 |
} |
| 2405 |
} |
| 2406 |
} |
| 2407 |
} |
| 2408 |
|
| 2409 |
|
| 2410 |
|
| 2411 |
|
| 2412 |
|
| 2413 |
|
| 2414 |
|
| 2415 |
|
| 2416 |
|
| 2417 |
|
| 2418 |
|
| 2419 |
|
| 2420 |
|
| 2421 |
|
| 2422 |
public static int[][] determineMarkThresholdsFromBinnedDataArrayAgainstControl(int[][][] grid, int[][][] gridcontrol, |
| 2423 |
boolean[] bpresent, boolean[] bpresentcontrol, |
| 2424 |
double dpoissonthresh, double dfoldthresh, |
| 2425 |
boolean bcontainsthresh, double dcountthresh) |
| 2426 |
{ |
| 2427 |
|
| 2428 |
double dcumthreshold = 1-dpoissonthresh; |
| 2429 |
|
| 2430 |
int nummarks= grid[0][0].length; |
| 2431 |
int numcontrolmarks = gridcontrol[0][0].length; |
| 2432 |
|
| 2433 |
|
| 2434 |
long[] sumtags = new long[nummarks]; |
| 2435 |
long[] sumtagscontrol = new long[nummarks]; |
| 2436 |
|
| 2437 |
|
| 2438 |
int[][] thresholds = new int[nummarks][]; |
| 2439 |
|
| 2440 |
|
| 2441 |
int[] maxcontrol = new int[nummarks]; |
| 2442 |
|
| 2443 |
|
| 2444 |
HashSet[] hscontrol = new HashSet[nummarks]; |
| 2445 |
for (int nmark = 0; nmark < nummarks; nmark++) |
| 2446 |
{ |
| 2447 |
hscontrol[nmark] = new HashSet(); |
| 2448 |
} |
| 2449 |
|
| 2450 |
|
| 2451 |
for (int nchrom = 0; nchrom < grid.length; nchrom++) |
| 2452 |
{ |
| 2453 |
if ((bpresent[nchrom])&&(bpresentcontrol[nchrom])) |
| 2454 |
{ |
| 2455 |
int[][] grid_nchrom = grid[nchrom]; |
| 2456 |
int[][] gridcontrol_nchrom = gridcontrol[nchrom]; |
| 2457 |
|
| 2458 |
for (int nbin = 0; nbin < grid_nchrom.length; nbin++) |
| 2459 |
{ |
| 2460 |
int[] grid_nchrom_nbin = grid_nchrom[nbin]; |
| 2461 |
int[] gridcontrol_nchrom_nbin = gridcontrol_nchrom[nbin]; |
| 2462 |
for (int nmark = 0; nmark < nummarks; nmark++) |
| 2463 |
{ |
| 2464 |
|
| 2465 |
int ncontrolval; |
| 2466 |
|
| 2467 |
if (numcontrolmarks == 1) |
| 2468 |
{ |
| 2469 |
ncontrolval = gridcontrol_nchrom_nbin[0]; |
| 2470 |
} |
| 2471 |
else |
| 2472 |
{ |
| 2473 |
ncontrolval = gridcontrol_nchrom_nbin[nmark]; |
| 2474 |
} |
| 2475 |
|
| 2476 |
if (ncontrolval > maxcontrol[nmark]) |
| 2477 |
{ |
| 2478 |
maxcontrol[nmark] = ncontrolval; |
| 2479 |
} |
| 2480 |
hscontrol[nmark].add(Integer.valueOf(ncontrolval)); |
| 2481 |
sumtags[nmark] += grid_nchrom_nbin[nmark]; |
| 2482 |
sumtagscontrol[nmark] += ncontrolval; |
| 2483 |
} |
| 2484 |
} |
| 2485 |
} |
| 2486 |
} |
| 2487 |
|
| 2488 |
for (int nmark = 0; nmark < sumtags.length; nmark++) |
| 2489 |
{ |
| 2490 |
|
| 2491 |
thresholds[nmark] = new int[maxcontrol[nmark]+1]; |
| 2492 |
int[] thresholds_nmark = thresholds[nmark]; |
| 2493 |
|
| 2494 |
|
| 2495 |
double davgratio = sumtags[nmark]/(double)sumtagscontrol[nmark]; |
| 2496 |
|
| 2497 |
|
| 2498 |
thresholds_nmark[0] = Math.max((int) Math.ceil(dcountthresh),1); |
| 2499 |
|
| 2500 |
|
| 2501 |
for (int nbackground = 1; nbackground <= maxcontrol[nmark]; nbackground++) |
| 2502 |
{ |
| 2503 |
|
| 2504 |
if(hscontrol[nmark].contains(Integer.valueOf(nbackground))) |
| 2505 |
{ |
| 2506 |
|
| 2507 |
|
| 2508 |
|
| 2509 |
|
| 2510 |
double dlambda = davgratio*nbackground; |
| 2511 |
|
| 2512 |
double dcum = 0; |
| 2513 |
short nthresh = 0; |
| 2514 |
double dlogfactorial = 0; |
| 2515 |
|
| 2516 |
while (dcum <= dcumthreshold) |
| 2517 |
{ |
| 2518 |
double dprob = Math.exp(Math.log(dlambda)*nthresh-dlambda- dlogfactorial); |
| 2519 |
dcum += dprob; |
| 2520 |
nthresh++; |
| 2521 |
dlogfactorial += Math.log(nthresh); |
| 2522 |
} |
| 2523 |
|
| 2524 |
if (bcontainsthresh) |
| 2525 |
{ |
| 2526 |
|
| 2527 |
nthresh--; |
| 2528 |
} |
| 2529 |
|
| 2530 |
thresholds_nmark[nbackground] = Math.max(Math.max(Math.max((int) Math.ceil(dfoldthresh*dlambda),nthresh),1),(int) Math.ceil(dcountthresh)); |
| 2531 |
|
| 2532 |
if (ChromHMM.BVERBOSE) |
| 2533 |
{ |
| 2534 |
System.out.println("Modification\t"+nmark+"\t"+nbackground+"\t"+maxcontrol[nmark]+"\t"+thresholds_nmark[nbackground]+"\t"+dlambda+"\t"+dcountthresh); |
| 2535 |
} |
| 2536 |
} |
| 2537 |
} |
| 2538 |
} |
| 2539 |
return thresholds; |
| 2540 |
} |
| 2541 |
|
| 2542 |
|
| 2543 |
|
| 2544 |
|
| 2545 |
|
| 2546 |
|
| 2547 |
|
| 2548 |
|
| 2549 |
|
| 2550 |
|
| 2551 |
|
| 2552 |
|
| 2553 |
public static double[] determineMarkThresholdsFromBinnedDataArray(int[][][] grid, boolean[] bpresent, |
| 2554 |
double dpoissonthresh, double dfoldthresh, |
| 2555 |
boolean bcontainsthresh, double dcountthresh) |
| 2556 |
{ |
| 2557 |
double dcumthreshold = 1-dpoissonthresh; |
| 2558 |
|
| 2559 |
double[] sumtags =null; |
| 2560 |
double[] thresholds = null; |
| 2561 |
int nummarks= grid[0][0].length; |
| 2562 |
String szcell; |
| 2563 |
|
| 2564 |
int ntotallocs = 0; |
| 2565 |
|
| 2566 |
sumtags = new double[nummarks]; |
| 2567 |
thresholds = new double[nummarks]; |
| 2568 |
|
| 2569 |
|
| 2570 |
|
| 2571 |
|
| 2572 |
for (int nchrom = 0; nchrom < grid.length; nchrom++) |
| 2573 |
{ |
| 2574 |
if (bpresent[nchrom]) |
| 2575 |
{ |
| 2576 |
int[][] grid_nchrom = grid[nchrom]; |
| 2577 |
for (int nbin = 0; nbin < grid_nchrom.length; nbin++) |
| 2578 |
{ |
| 2579 |
int[] grid_nchrom_nbin = grid[nchrom][nbin]; |
| 2580 |
for (int nmark = 0; nmark < nummarks; nmark++) |
| 2581 |
{ |
| 2582 |
sumtags[nmark] += grid_nchrom_nbin[nmark]; |
| 2583 |
} |
| 2584 |
} |
| 2585 |
ntotallocs += grid_nchrom.length; |
| 2586 |
} |
| 2587 |
} |
| 2588 |
|
| 2589 |
for (int nj = 0; nj < sumtags.length; nj++) |
| 2590 |
{ |
| 2591 |
|
| 2592 |
double dlambda = sumtags[nj]/(double) ntotallocs; |
| 2593 |
double dcum = 0; |
| 2594 |
short nthresh = 0; |
| 2595 |
|
| 2596 |
double dlogfactorial = 0; |
| 2597 |
while (dcum <= dcumthreshold) |
| 2598 |
{ |
| 2599 |
|
| 2600 |
|
| 2601 |
double dprob = Math.exp(Math.log(dlambda)*nthresh-dlambda-dlogfactorial); |
| 2602 |
dcum += dprob; |
| 2603 |
nthresh++; |
| 2604 |
dlogfactorial += Math.log(nthresh); |
| 2605 |
} |
| 2606 |
|
| 2607 |
if (bcontainsthresh) |
| 2608 |
{ |
| 2609 |
|
| 2610 |
nthresh--; |
| 2611 |
} |
| 2612 |
|
| 2613 |
|
| 2614 |
thresholds[nj] = Math.max(Math.max(Math.max((int) Math.ceil(dfoldthresh*dlambda),nthresh),1),(int) Math.ceil(dcountthresh)); |
| 2615 |
if (ChromHMM.BVERBOSE) |
| 2616 |
{ |
| 2617 |
System.out.println("Threshold\t"+nj+"\t"+thresholds[nj]+"\t"+sumtags[nj]+"\t"+((int) Math.ceil(dfoldthresh*dlambda))+"\t"+ntotallocs); |
| 2618 |
} |
| 2619 |
} |
| 2620 |
return thresholds; |
| 2621 |
} |
| 2622 |
|
| 2623 |
|
| 2624 |
|
| 2625 |
|
| 2626 |
|
| 2627 |
|
| 2628 |
|
| 2629 |
|
| 2630 |
|
| 2631 |
|
| 2632 |
|
| 2633 |
|
| 2634 |
|
| 2635 |
|
| 2636 |
|
| 2637 |
|
| 2638 |
|
| 2639 |
|
| 2640 |
public static void makeBinaryDataFromSignalAgainstControl(String szbinneddataDIR, String szcontrolDIR, String szoutputDIR, |
| 2641 |
double dpoissonthresh, double dfoldthresh, boolean bcontainsthresh, int nflankwidthcontrol, |
| 2642 |
int npseudocountcontrol, double dcountthresh, boolean bgzip, |
| 2643 |
boolean bsplit, int numsplitbins) throws IOException |
| 2644 |
{ |
| 2645 |
int nummarks=-1; |
| 2646 |
int nummarkscontrol=-1; |
| 2647 |
File dir = new File(szbinneddataDIR); |
| 2648 |
if (!dir.exists()) |
| 2649 |
{ |
| 2650 |
throw new IllegalArgumentException(szbinneddataDIR+" was not found!"); |
| 2651 |
} |
| 2652 |
|
| 2653 |
String[] allfiles = dir.list(); |
| 2654 |
int nfilecount = 0; |
| 2655 |
for (int nfile = 0; nfile < allfiles.length; nfile++) |
| 2656 |
{ |
| 2657 |
if ((allfiles[nfile].contains("_signal"))&&(!(new File(allfiles[nfile])).isHidden())) |
| 2658 |
{ |
| 2659 |
nfilecount++; |
| 2660 |
} |
| 2661 |
} |
| 2662 |
|
| 2663 |
if (nfilecount == 0) |
| 2664 |
{ |
| 2665 |
throw new IllegalArgumentException("No _signal files were found in directory "+szbinneddataDIR); |
| 2666 |
} |
| 2667 |
|
| 2668 |
String[] signalchromfiles = new String[nfilecount]; |
| 2669 |
int nfileindex = 0; |
| 2670 |
for (int nfile = 0; nfile < allfiles.length; nfile++) |
| 2671 |
{ |
| 2672 |
if ((allfiles[nfile].contains("_signal"))&&(!(new File(allfiles[nfile])).isHidden())) |
| 2673 |
{ |
| 2674 |
signalchromfiles[nfileindex] = allfiles[nfile]; |
| 2675 |
nfileindex++; |
| 2676 |
} |
| 2677 |
} |
| 2678 |
|
| 2679 |
|
| 2680 |
HashMap hmcontrol = new HashMap(); |
| 2681 |
|
| 2682 |
File dircontrol = new File(szcontrolDIR); |
| 2683 |
if (!dircontrol.exists()) |
| 2684 |
{ |
| 2685 |
throw new IllegalArgumentException(szcontrolDIR+" was not found!"); |
| 2686 |
} |
| 2687 |
|
| 2688 |
String[] allfilescontrol = dircontrol.list(); |
| 2689 |
for (int nfile = 0; nfile < allfilescontrol.length; nfile++) |
| 2690 |
{ |
| 2691 |
if ((allfilescontrol[nfile].contains("_controlsignal"))&&(!(new File(allfilescontrol[nfile])).isHidden())) |
| 2692 |
{ |
| 2693 |
BufferedReader brcontrol = Util.getBufferedReader(szcontrolDIR+"/"+allfilescontrol[nfile]); |
| 2694 |
String szheader = brcontrol.readLine(); |
| 2695 |
if (szheader == null) |
| 2696 |
{ |
| 2697 |
throw new IllegalArgumentException(szcontrolDIR+"/"+allfilescontrol[nfile]+" is empty!"); |
| 2698 |
} |
| 2699 |
StringTokenizer st = new StringTokenizer(szheader,"\t"); |
| 2700 |
if (st.countTokens() != 2) |
| 2701 |
{ |
| 2702 |
throw new IllegalArgumentException(szcontrolDIR+"/"+allfilescontrol[nfile]+" header line must have two columns delimited by a tab found only one in "+ |
| 2703 |
szheader); |
| 2704 |
} |
| 2705 |
String szkey = st.nextToken().trim()+"\t"+st.nextToken().trim(); |
| 2706 |
hmcontrol.put(szkey,allfilescontrol[nfile]); |
| 2707 |
brcontrol.close(); |
| 2708 |
} |
| 2709 |
} |
| 2710 |
|
| 2711 |
HashMap hmcellsToIndex = new HashMap(); |
| 2712 |
String szcell; |
| 2713 |
|
| 2714 |
|
| 2715 |
|
| 2716 |
|
| 2717 |
for (int nfile = 0; nfile < signalchromfiles.length; nfile++) |
| 2718 |
{ |
| 2719 |
BufferedReader br = Util.getBufferedReader(szbinneddataDIR+"/"+signalchromfiles[nfile]); |
| 2720 |
String szLine = br.readLine(); |
| 2721 |
String szcurrcell; |
| 2722 |
if (szLine == null) |
| 2723 |
{ |
| 2724 |
throw new IllegalArgumentException(signalchromfiles[nfile]+" is empty!"); |
| 2725 |
} |
| 2726 |
StringTokenizer st = new StringTokenizer(szLine,"\t"); |
| 2727 |
szcurrcell = st.nextToken().trim(); |
| 2728 |
br.close(); |
| 2729 |
|
| 2730 |
ArrayList al = (ArrayList) hmcellsToIndex.get(szcurrcell); |
| 2731 |
|
| 2732 |
if (al == null) |
| 2733 |
{ |
| 2734 |
al = new ArrayList(); |
| 2735 |
hmcellsToIndex.put(szcurrcell, al); |
| 2736 |
} |
| 2737 |
al.add(Integer.valueOf(nfile)); |
| 2738 |
} |
| 2739 |
|
| 2740 |
|
| 2741 |
Iterator itrcells = hmcellsToIndex.entrySet().iterator(); |
| 2742 |
|
| 2743 |
while (itrcells.hasNext()) |
| 2744 |
{ |
| 2745 |
Map.Entry pairs = (Map.Entry) itrcells.next(); |
| 2746 |
|
| 2747 |
szcell = (String) pairs.getKey(); |
| 2748 |
ArrayList al = (ArrayList) pairs.getValue(); |
| 2749 |
|
| 2750 |
int numchrompercell = al.size(); |
| 2751 |
int ntotallocs = 0; |
| 2752 |
|
| 2753 |
int[][][] grid = new int[numchrompercell][][]; |
| 2754 |
int[][][] gridcontrol = new int[numchrompercell][][]; |
| 2755 |
int[][][] sumgridcontrol = new int[numchrompercell][][]; |
| 2756 |
String[] chroms = new String[numchrompercell]; |
| 2757 |
String szHeaderLine2= null; |
| 2758 |
for (int nchrom = 0; nchrom < numchrompercell; nchrom++) |
| 2759 |
{ |
| 2760 |
|
| 2761 |
|
| 2762 |
String szfilename = signalchromfiles[((Integer) al.get(nchrom)).intValue()]; |
| 2763 |
BufferedReader br = Util.getBufferedReader(szbinneddataDIR+"/"+szfilename); |
| 2764 |
|
| 2765 |
String szLine; |
| 2766 |
String szHeaderLine1 = br.readLine(); |
| 2767 |
szHeaderLine2 = br.readLine(); |
| 2768 |
|
| 2769 |
if (szHeaderLine1 == null) |
| 2770 |
{ |
| 2771 |
throw new IllegalArgumentException(szcontrolDIR+"/"+szfilename+" is empty!"); |
| 2772 |
} |
| 2773 |
StringTokenizer stheader = new StringTokenizer(szHeaderLine1,"\t"); |
| 2774 |
stheader.nextToken(); |
| 2775 |
if (!stheader.hasMoreTokens()) |
| 2776 |
{ |
| 2777 |
throw new IllegalArgumentException("Only found one entry for line "+szHeaderLine1+" in file "+szbinneddataDIR+"/"+szfilename |
| 2778 |
+" expecting 2"); |
| 2779 |
} |
| 2780 |
String szchrom = stheader.nextToken().trim(); |
| 2781 |
chroms[nchrom] = szchrom; |
| 2782 |
String szcontrolfilename = (String) hmcontrol.get(szcell+"\t"+szchrom); |
| 2783 |
if (szcontrolfilename == null) |
| 2784 |
{ |
| 2785 |
throw new IllegalArgumentException("No control data found for "+szcell+"\t"+szchrom); |
| 2786 |
} |
| 2787 |
BufferedReader brcontrol = Util.getBufferedReader(szcontrolDIR+"/"+szcontrolfilename); |
| 2788 |
|
| 2789 |
brcontrol.readLine(); |
| 2790 |
String szHeaderLineControl2 = brcontrol.readLine(); |
| 2791 |
|
| 2792 |
StringTokenizer st = new StringTokenizer(szHeaderLine2,"\t"); |
| 2793 |
int ncurrnummarks = st.countTokens(); |
| 2794 |
|
| 2795 |
if (szHeaderLineControl2 == null) |
| 2796 |
{ |
| 2797 |
throw new IllegalArgumentException(szcontrolDIR+"/"+szcontrolfilename+" is missing lines!"); |
| 2798 |
} |
| 2799 |
StringTokenizer stcontrol = new StringTokenizer(szHeaderLineControl2,"\t"); |
| 2800 |
int ncurrnummarkscontrol = stcontrol.countTokens(); |
| 2801 |
if ((ncurrnummarkscontrol != 1) && (ncurrnummarkscontrol != ncurrnummarks)) |
| 2802 |
{ |
| 2803 |
throw new IllegalArgumentException("Number of marks in control file "+szcontrolDIR+"/"+szfilename+ |
| 2804 |
" is "+ncurrnummarkscontrol+" expecting "+ncurrnummarks+" or 1"); |
| 2805 |
} |
| 2806 |
|
| 2807 |
if (nchrom == 0) |
| 2808 |
{ |
| 2809 |
nummarks = ncurrnummarks; |
| 2810 |
nummarkscontrol = ncurrnummarkscontrol; |
| 2811 |
} |
| 2812 |
else |
| 2813 |
{ |
| 2814 |
if (ncurrnummarks != nummarks) |
| 2815 |
{ |
| 2816 |
throw new IllegalArgumentException("Number of marks ("+nummarks+ ") in "+szbinneddataDIR+"/"+ |
| 2817 |
szfilename+" is inconsistent with previously found # of "+nummarks); |
| 2818 |
} |
| 2819 |
else if (ncurrnummarkscontrol != nummarkscontrol) |
| 2820 |
{ |
| 2821 |
throw new IllegalArgumentException("Number of marks ("+nummarkscontrol+ ") in "+szcontrolDIR+"/"+szfilename+ |
| 2822 |
" is inconsistent with previously found # of "+nummarks); |
| 2823 |
} |
| 2824 |
} |
| 2825 |
|
| 2826 |
|
| 2827 |
|
| 2828 |
int nchromlocs = 0; |
| 2829 |
while ((szLine = br.readLine())!=null) |
| 2830 |
{ |
| 2831 |
nchromlocs++; |
| 2832 |
} |
| 2833 |
br.close(); |
| 2834 |
ntotallocs += nchromlocs; |
| 2835 |
|
| 2836 |
|
| 2837 |
grid[nchrom] = new int[nchromlocs][nummarks]; |
| 2838 |
gridcontrol[nchrom] = new int[nchromlocs][nummarkscontrol]; |
| 2839 |
sumgridcontrol[nchrom] = new int[nchromlocs][nummarkscontrol]; |
| 2840 |
|
| 2841 |
int nbin = 0; |
| 2842 |
|
| 2843 |
br = Util.getBufferedReader(szbinneddataDIR+"/"+szfilename); |
| 2844 |
br.readLine(); |
| 2845 |
br.readLine(); |
| 2846 |
|
| 2847 |
int[][] grid_nchrom = grid[nchrom]; |
| 2848 |
int[][] gridcontrol_nchrom = gridcontrol[nchrom]; |
| 2849 |
while ((szLine = br.readLine())!=null) |
| 2850 |
{ |
| 2851 |
int[] grid_nchrom_nbin = grid_nchrom[nbin]; |
| 2852 |
int[] gridcontrol_nchrom_nbin = gridcontrol_nchrom[nbin]; |
| 2853 |
st = new StringTokenizer(szLine,"\t "); |
| 2854 |
if (st.countTokens() != nummarks) |
| 2855 |
{ |
| 2856 |
throw new IllegalArgumentException("In "+szfilename+" did not find the expected "+nummarks+" marks in this line: "+szLine); |
| 2857 |
} |
| 2858 |
for (int nmark = 0; nmark < nummarks; nmark++) |
| 2859 |
{ |
| 2860 |
|
| 2861 |
grid_nchrom_nbin[nmark] = Integer.parseInt(st.nextToken()); |
| 2862 |
} |
| 2863 |
|
| 2864 |
String szLineControl = brcontrol.readLine(); |
| 2865 |
if (szLineControl == null) |
| 2866 |
{ |
| 2867 |
throw new IllegalArgumentException("The number of lines in the control file "+ |
| 2868 |
szcontrolDIR+"/"+szfilename+" does not match that in the signal file "+szbinneddataDIR+"/"+szcontrolfilename); |
| 2869 |
} |
| 2870 |
st = new StringTokenizer(szLineControl,"\t "); |
| 2871 |
for (int nmark = 0; nmark < nummarkscontrol; nmark++) |
| 2872 |
{ |
| 2873 |
|
| 2874 |
gridcontrol_nchrom_nbin[nmark] = Integer.parseInt(st.nextToken())+npseudocountcontrol; |
| 2875 |
} |
| 2876 |
nbin++; |
| 2877 |
} |
| 2878 |
br.close(); |
| 2879 |
brcontrol.close(); |
| 2880 |
} |
| 2881 |
|
| 2882 |
|
| 2883 |
|
| 2884 |
windowSumGrid(gridcontrol,sumgridcontrol,nflankwidthcontrol); |
| 2885 |
|
| 2886 |
boolean[] bpresent = new boolean[grid.length]; |
| 2887 |
boolean[] bpresentcontrol = new boolean[sumgridcontrol.length]; |
| 2888 |
|
| 2889 |
for (int ni = 0; ni < bpresent.length; ni++) |
| 2890 |
{ |
| 2891 |
|
| 2892 |
bpresent[ni] = true; |
| 2893 |
bpresentcontrol[ni] = true; |
| 2894 |
} |
| 2895 |
|
| 2896 |
|
| 2897 |
int[][] thresholds = determineMarkThresholdsFromBinnedDataArrayAgainstControl(grid,sumgridcontrol, |
| 2898 |
bpresent,bpresentcontrol,dpoissonthresh,dfoldthresh,bcontainsthresh,dcountthresh); |
| 2899 |
|
| 2900 |
for (int nchrom = 0; nchrom < grid.length; nchrom++) |
| 2901 |
{ |
| 2902 |
String szchrom = chroms[nchrom]; |
| 2903 |
|
| 2904 |
if (bgzip) |
| 2905 |
{ |
| 2906 |
if (bsplit) |
| 2907 |
{ |
| 2908 |
GZIPOutputStream pwzip = null; |
| 2909 |
int[][] grid_nchrom = grid[nchrom]; |
| 2910 |
int[][] sumgridcontrol_nchrom = sumgridcontrol[nchrom]; |
| 2911 |
int nsplit = 0; |
| 2912 |
|
| 2913 |
for (int nbin = 0; nbin < grid_nchrom.length; nbin++) |
| 2914 |
{ |
| 2915 |
if (nbin % numsplitbins == 0) |
| 2916 |
{ |
| 2917 |
String szfile = szoutputDIR+"/"+szcell+"_"+szchrom+"."+nsplit+"_binary.txt.gz"; |
| 2918 |
System.out.println("Writing to file "+szfile); |
| 2919 |
pwzip = new GZIPOutputStream(new FileOutputStream(szfile)); |
| 2920 |
|
| 2921 |
|
| 2922 |
|
| 2923 |
String szout = szcell+"\t"+szchrom+"."+nsplit+"\n"; |
| 2924 |
byte[] btformat = szout.getBytes(); |
| 2925 |
pwzip.write(btformat,0,btformat.length); |
| 2926 |
|
| 2927 |
|
| 2928 |
szout = szHeaderLine2+"\n"; |
| 2929 |
btformat = szout.getBytes(); |
| 2930 |
pwzip.write(btformat,0,btformat.length); |
| 2931 |
nsplit++; |
| 2932 |
} |
| 2933 |
|
| 2934 |
int[] grid_nchrom_nbin = grid_nchrom[nbin]; |
| 2935 |
int[] sumgridcontrol_nchrom_nbin = sumgridcontrol_nchrom[nbin]; |
| 2936 |
StringBuffer sbout = new StringBuffer(); |
| 2937 |
int nummarks_m1 = nummarks - 1; |
| 2938 |
for (int nmark = 0; nmark < nummarks_m1; nmark++) |
| 2939 |
{ |
| 2940 |
int ncontrolval; |
| 2941 |
if (nummarkscontrol == 1) |
| 2942 |
{ |
| 2943 |
|
| 2944 |
ncontrolval = sumgridcontrol_nchrom_nbin[0]; |
| 2945 |
} |
| 2946 |
else |
| 2947 |
{ |
| 2948 |
ncontrolval = sumgridcontrol_nchrom_nbin[nmark]; |
| 2949 |
} |
| 2950 |
|
| 2951 |
|
| 2952 |
if (grid_nchrom_nbin[nmark] == -1) |
| 2953 |
{ |
| 2954 |
sbout.append("2\t"); |
| 2955 |
|
| 2956 |
} |
| 2957 |
else if (thresholds[nmark][ncontrolval] <= grid_nchrom_nbin[nmark]) |
| 2958 |
{ |
| 2959 |
sbout.append("1\t"); |
| 2960 |
|
| 2961 |
} |
| 2962 |
else |
| 2963 |
{ |
| 2964 |
sbout.append("0\t"); |
| 2965 |
|
| 2966 |
} |
| 2967 |
} |
| 2968 |
|
| 2969 |
int ncontrolval; |
| 2970 |
if (nummarkscontrol == 1) |
| 2971 |
{ |
| 2972 |
ncontrolval = sumgridcontrol_nchrom_nbin[0]; |
| 2973 |
} |
| 2974 |
else |
| 2975 |
{ |
| 2976 |
ncontrolval = sumgridcontrol_nchrom_nbin[nummarks_m1]; |
| 2977 |
} |
| 2978 |
|
| 2979 |
if (grid_nchrom_nbin[nummarks_m1] == -1) |
| 2980 |
{ |
| 2981 |
sbout.append("2\n"); |
| 2982 |
|
| 2983 |
} |
| 2984 |
else if (thresholds[nummarks_m1][ncontrolval] <= grid_nchrom_nbin[nummarks_m1]) |
| 2985 |
{ |
| 2986 |
sbout.append("1\n"); |
| 2987 |
|
| 2988 |
} |
| 2989 |
else |
| 2990 |
{ |
| 2991 |
sbout.append("0\n"); |
| 2992 |
|
| 2993 |
} |
| 2994 |
|
| 2995 |
byte[] btformat = sbout.toString().getBytes(); |
| 2996 |
pwzip.write(btformat,0,btformat.length); |
| 2997 |
|
| 2998 |
if (((nbin + 1) == grid_nchrom.length)||((nbin+1) % numsplitbins == 0)) |
| 2999 |
{ |
| 3000 |
pwzip.finish(); |
| 3001 |
pwzip.close(); |
| 3002 |
} |
| 3003 |
} |
| 3004 |
} |
| 3005 |
else |
| 3006 |
{ |
| 3007 |
String szfile = szoutputDIR+"/"+szcell+"_"+szchrom+"_binary.txt.gz"; |
| 3008 |
System.out.println("Writing to file "+szfile); |
| 3009 |
GZIPOutputStream pwzip = new GZIPOutputStream(new FileOutputStream(szfile)); |
| 3010 |
|
| 3011 |
|
| 3012 |
int nummarks_m1 = nummarks - 1; |
| 3013 |
|
| 3014 |
String szout = szcell+"\t"+szchrom+"\n"; |
| 3015 |
byte[] btformat = szout.getBytes(); |
| 3016 |
pwzip.write(btformat,0,btformat.length); |
| 3017 |
|
| 3018 |
|
| 3019 |
szout = szHeaderLine2+"\n"; |
| 3020 |
btformat = szout.getBytes(); |
| 3021 |
pwzip.write(btformat,0,btformat.length); |
| 3022 |
|
| 3023 |
int[][] grid_nchrom = grid[nchrom]; |
| 3024 |
int[][] sumgridcontrol_nchrom = sumgridcontrol[nchrom]; |
| 3025 |
for (int nbin = 0; nbin < grid_nchrom.length; nbin++) |
| 3026 |
{ |
| 3027 |
int[] grid_nchrom_nbin = grid_nchrom[nbin]; |
| 3028 |
int[] sumgridcontrol_nchrom_nbin = sumgridcontrol_nchrom[nbin]; |
| 3029 |
StringBuffer sbout = new StringBuffer(); |
| 3030 |
|
| 3031 |
for (int nmark = 0; nmark < nummarks_m1; nmark++) |
| 3032 |
{ |
| 3033 |
int ncontrolval; |
| 3034 |
if (nummarkscontrol == 1) |
| 3035 |
{ |
| 3036 |
|
| 3037 |
ncontrolval = sumgridcontrol_nchrom_nbin[0]; |
| 3038 |
} |
| 3039 |
else |
| 3040 |
{ |
| 3041 |
ncontrolval = sumgridcontrol_nchrom_nbin[nmark]; |
| 3042 |
} |
| 3043 |
|
| 3044 |
|
| 3045 |
if (grid_nchrom_nbin[nmark] == -1) |
| 3046 |
{ |
| 3047 |
sbout.append("2\t"); |
| 3048 |
|
| 3049 |
} |
| 3050 |
else if (thresholds[nmark][ncontrolval] <= grid_nchrom_nbin[nmark]) |
| 3051 |
{ |
| 3052 |
sbout.append("1\t"); |
| 3053 |
|
| 3054 |
} |
| 3055 |
else |
| 3056 |
{ |
| 3057 |
sbout.append("0\t"); |
| 3058 |
|
| 3059 |
} |
| 3060 |
} |
| 3061 |
|
| 3062 |
int ncontrolval; |
| 3063 |
if (nummarkscontrol == 1) |
| 3064 |
{ |
| 3065 |
ncontrolval = sumgridcontrol_nchrom_nbin[0]; |
| 3066 |
} |
| 3067 |
else |
| 3068 |
{ |
| 3069 |
ncontrolval = sumgridcontrol_nchrom_nbin[nummarks_m1]; |
| 3070 |
} |
| 3071 |
|
| 3072 |
if (grid_nchrom_nbin[nummarks_m1] == -1) |
| 3073 |
{ |
| 3074 |
sbout.append("2\n"); |
| 3075 |
|
| 3076 |
} |
| 3077 |
else if (thresholds[nummarks_m1][ncontrolval] <= grid_nchrom_nbin[nummarks_m1]) |
| 3078 |
{ |
| 3079 |
sbout.append("1\n"); |
| 3080 |
|
| 3081 |
} |
| 3082 |
else |
| 3083 |
{ |
| 3084 |
sbout.append("0\n"); |
| 3085 |
|
| 3086 |
} |
| 3087 |
|
| 3088 |
btformat = sbout.toString().getBytes(); |
| 3089 |
pwzip.write(btformat,0,btformat.length); |
| 3090 |
} |
| 3091 |
pwzip.finish(); |
| 3092 |
pwzip.close(); |
| 3093 |
} |
| 3094 |
} |
| 3095 |
else |
| 3096 |
{ |
| 3097 |
if (bsplit) |
| 3098 |
{ |
| 3099 |
int[][] grid_nchrom = grid[nchrom]; |
| 3100 |
int[][] sumgridcontrol_nchrom = sumgridcontrol[nchrom]; |
| 3101 |
int nsplit = 0; |
| 3102 |
PrintWriter pw = null; |
| 3103 |
|
| 3104 |
for (int nbin = 0; nbin < grid_nchrom.length; nbin++) |
| 3105 |
{ |
| 3106 |
if (nbin % numsplitbins == 0) |
| 3107 |
{ |
| 3108 |
String szfile = szoutputDIR+"/"+szcell+"_"+szchrom+"."+nsplit+"_binary.txt"; |
| 3109 |
System.out.println("Writing to file "+szfile); |
| 3110 |
pw = new PrintWriter(new FileWriter(szfile)); |
| 3111 |
|
| 3112 |
pw.println(szcell+"\t"+szchrom+"."+nsplit); |
| 3113 |
pw.println(szHeaderLine2); |
| 3114 |
nsplit++; |
| 3115 |
} |
| 3116 |
|
| 3117 |
int[] grid_nchrom_nbin = grid_nchrom[nbin]; |
| 3118 |
int[] sumgridcontrol_nchrom_nbin = sumgridcontrol_nchrom[nbin]; |
| 3119 |
int nummarks_m1 = nummarks - 1; |
| 3120 |
|
| 3121 |
for (int nmark = 0; nmark < nummarks_m1; nmark++) |
| 3122 |
{ |
| 3123 |
int ncontrolval; |
| 3124 |
if (nummarkscontrol == 1) |
| 3125 |
{ |
| 3126 |
|
| 3127 |
ncontrolval = sumgridcontrol_nchrom_nbin[0]; |
| 3128 |
} |
| 3129 |
else |
| 3130 |
{ |
| 3131 |
ncontrolval = sumgridcontrol_nchrom_nbin[nmark]; |
| 3132 |
} |
| 3133 |
|
| 3134 |
|
| 3135 |
if (grid_nchrom_nbin[nmark] == -1) |
| 3136 |
{ |
| 3137 |
pw.print("2\t"); |
| 3138 |
} |
| 3139 |
else if (thresholds[nmark][ncontrolval] <= grid_nchrom_nbin[nmark]) |
| 3140 |
{ |
| 3141 |
pw.print("1\t"); |
| 3142 |
} |
| 3143 |
else |
| 3144 |
{ |
| 3145 |
pw.print("0\t"); |
| 3146 |
} |
| 3147 |
} |
| 3148 |
|
| 3149 |
int ncontrolval; |
| 3150 |
if (nummarkscontrol == 1) |
| 3151 |
{ |
| 3152 |
ncontrolval = sumgridcontrol_nchrom_nbin[0]; |
| 3153 |
} |
| 3154 |
else |
| 3155 |
{ |
| 3156 |
ncontrolval = sumgridcontrol_nchrom_nbin[nummarks_m1]; |
| 3157 |
} |
| 3158 |
|
| 3159 |
if (grid_nchrom_nbin[nummarks_m1] == -1) |
| 3160 |
{ |
| 3161 |
pw.println("2"); |
| 3162 |
} |
| 3163 |
else if (thresholds[nummarks_m1][ncontrolval] <= grid_nchrom_nbin[nummarks_m1]) |
| 3164 |
{ |
| 3165 |
pw.println("1"); |
| 3166 |
} |
| 3167 |
else |
| 3168 |
{ |
| 3169 |
pw.println("0"); |
| 3170 |
} |
| 3171 |
|
| 3172 |
if (((nbin + 1) == grid_nchrom.length)||((nbin+1) % numsplitbins == 0)) |
| 3173 |
{ |
| 3174 |
pw.close(); |
| 3175 |
} |
| 3176 |
} |
| 3177 |
} |
| 3178 |
else |
| 3179 |
{ |
| 3180 |
String szfile = szoutputDIR+"/"+szcell+"_"+szchrom+"_binary.txt"; |
| 3181 |
System.out.println("Writing to file "+szfile); |
| 3182 |
PrintWriter pw = new PrintWriter(new FileWriter(szfile)); |
| 3183 |
|
| 3184 |
int nummarks_m1 = nummarks - 1; |
| 3185 |
pw.println(szcell+"\t"+szchrom); |
| 3186 |
pw.println(szHeaderLine2); |
| 3187 |
|
| 3188 |
int[][] grid_nchrom = grid[nchrom]; |
| 3189 |
int[][] sumgridcontrol_nchrom = sumgridcontrol[nchrom]; |
| 3190 |
for (int nbin = 0; nbin < grid_nchrom.length; nbin++) |
| 3191 |
{ |
| 3192 |
int[] grid_nchrom_nbin = grid_nchrom[nbin]; |
| 3193 |
int[] sumgridcontrol_nchrom_nbin = sumgridcontrol_nchrom[nbin]; |
| 3194 |
|
| 3195 |
for (int nmark = 0; nmark < nummarks_m1; nmark++) |
| 3196 |
{ |
| 3197 |
int ncontrolval; |
| 3198 |
if (nummarkscontrol == 1) |
| 3199 |
{ |
| 3200 |
|
| 3201 |
ncontrolval = sumgridcontrol_nchrom_nbin[0]; |
| 3202 |
} |
| 3203 |
else |
| 3204 |
{ |
| 3205 |
ncontrolval = sumgridcontrol_nchrom_nbin[nmark]; |
| 3206 |
} |
| 3207 |
|
| 3208 |
|
| 3209 |
if (grid_nchrom_nbin[nmark] == -1) |
| 3210 |
{ |
| 3211 |
pw.print("2\t"); |
| 3212 |
} |
| 3213 |
else if (thresholds[nmark][ncontrolval] <= grid_nchrom_nbin[nmark]) |
| 3214 |
{ |
| 3215 |
pw.print("1\t"); |
| 3216 |
} |
| 3217 |
else |
| 3218 |
{ |
| 3219 |
pw.print("0\t"); |
| 3220 |
} |
| 3221 |
} |
| 3222 |
|
| 3223 |
int ncontrolval; |
| 3224 |
if (nummarkscontrol == 1) |
| 3225 |
{ |
| 3226 |
ncontrolval = sumgridcontrol_nchrom_nbin[0]; |
| 3227 |
} |
| 3228 |
else |
| 3229 |
{ |
| 3230 |
ncontrolval = sumgridcontrol_nchrom_nbin[nummarks_m1]; |
| 3231 |
} |
| 3232 |
|
| 3233 |
if (grid_nchrom_nbin[nummarks_m1] == -1) |
| 3234 |
{ |
| 3235 |
pw.println("2"); |
| 3236 |
} |
| 3237 |
else if (thresholds[nummarks_m1][ncontrolval] <= grid_nchrom_nbin[nummarks_m1]) |
| 3238 |
{ |
| 3239 |
pw.println("1"); |
| 3240 |
} |
| 3241 |
else |
| 3242 |
{ |
| 3243 |
pw.println("0"); |
| 3244 |
} |
| 3245 |
} |
| 3246 |
pw.close(); |
| 3247 |
} |
| 3248 |
} |
| 3249 |
} |
| 3250 |
} |
| 3251 |
} |
| 3252 |
|
| 3253 |
|
| 3254 |
|
| 3255 |
|
| 3256 |
|
| 3257 |
|
| 3258 |
|
| 3259 |
|
| 3260 |
|
| 3261 |
|
| 3262 |
|
| 3263 |
|
| 3264 |
|
| 3265 |
|
| 3266 |
public static void makeBinaryDataFromSignalUniform(String szbinneddataDIR, String szoutputDIR, |
| 3267 |
double dpoissonthresh, double dfoldthresh, |
| 3268 |
boolean bcontainsthresh, double dcountthresh, |
| 3269 |
boolean bgzip, boolean bsplit, int numsplitbins) throws IOException |
| 3270 |
{ |
| 3271 |
|
| 3272 |
|
| 3273 |
double dcumthreshold = 1-dpoissonthresh; |
| 3274 |
double[] sumtags =null; |
| 3275 |
int[] thresholds = null; |
| 3276 |
|
| 3277 |
int nummarks=-1; |
| 3278 |
File dir = new File(szbinneddataDIR); |
| 3279 |
if (!dir.exists()) |
| 3280 |
{ |
| 3281 |
throw new IllegalArgumentException(szbinneddataDIR+" was not found!"); |
| 3282 |
} |
| 3283 |
|
| 3284 |
|
| 3285 |
|
| 3286 |
String[] allfiles = dir.list(); |
| 3287 |
int nfilecount = 0; |
| 3288 |
for (int nfile = 0; nfile < allfiles.length; nfile++) |
| 3289 |
{ |
| 3290 |
if ((allfiles[nfile].contains("_signal"))&&(!(new File(allfiles[nfile])).isHidden())) |
| 3291 |
{ |
| 3292 |
nfilecount++; |
| 3293 |
} |
| 3294 |
} |
| 3295 |
|
| 3296 |
if (nfilecount == 0) |
| 3297 |
{ |
| 3298 |
throw new IllegalArgumentException("No _signal files were found in directory "+szbinneddataDIR); |
| 3299 |
} |
| 3300 |
|
| 3301 |
String[] signalchromfiles = new String[nfilecount]; |
| 3302 |
int nfileindex = 0; |
| 3303 |
for (int nfile = 0; nfile < allfiles.length; nfile++) |
| 3304 |
{ |
| 3305 |
if ((allfiles[nfile].contains("_signal"))&&(!(new File(allfiles[nfile])).isHidden())) |
| 3306 |
{ |
| 3307 |
signalchromfiles[nfileindex] = allfiles[nfile]; |
| 3308 |
nfileindex++; |
| 3309 |
} |
| 3310 |
} |
| 3311 |
HashMap hmcellsToIndex = new HashMap(); |
| 3312 |
String szcell; |
| 3313 |
|
| 3314 |
|
| 3315 |
|
| 3316 |
for (int nfile = 0; nfile < signalchromfiles.length; nfile++) |
| 3317 |
{ |
| 3318 |
BufferedReader br = Util.getBufferedReader(szbinneddataDIR+"/"+signalchromfiles[nfile]); |
| 3319 |
String szLine = br.readLine(); |
| 3320 |
if (szLine == null) |
| 3321 |
{ |
| 3322 |
throw new IllegalArgumentException(szbinneddataDIR+"/"+signalchromfiles[nfile]+" does not contain any data!"); |
| 3323 |
} |
| 3324 |
StringTokenizer st = new StringTokenizer(szLine,"\t"); |
| 3325 |
String szcurrcell = st.nextToken().trim(); |
| 3326 |
br.close(); |
| 3327 |
|
| 3328 |
ArrayList al = (ArrayList) hmcellsToIndex.get(szcurrcell); |
| 3329 |
|
| 3330 |
if (al == null) |
| 3331 |
{ |
| 3332 |
al = new ArrayList(); |
| 3333 |
hmcellsToIndex.put(szcurrcell, al); |
| 3334 |
} |
| 3335 |
al.add(Integer.valueOf(nfile)); |
| 3336 |
} |
| 3337 |
|
| 3338 |
Iterator itrcells = hmcellsToIndex.entrySet().iterator(); |
| 3339 |
|
| 3340 |
while (itrcells.hasNext()) |
| 3341 |
{ |
| 3342 |
Map.Entry pairs = (Map.Entry) itrcells.next(); |
| 3343 |
|
| 3344 |
szcell = (String) pairs.getKey(); |
| 3345 |
ArrayList al = (ArrayList) pairs.getValue(); |
| 3346 |
int numchrompercell = al.size(); |
| 3347 |
int ntotallocs = 0; |
| 3348 |
sumtags = null; |
| 3349 |
|
| 3350 |
for (int nindex = 0; nindex < numchrompercell; nindex++) |
| 3351 |
{ |
| 3352 |
|
| 3353 |
|
| 3354 |
String szfilename = signalchromfiles[((Integer) al.get(nindex)).intValue()]; |
| 3355 |
BufferedReader br = Util.getBufferedReader(szbinneddataDIR+"/"+szfilename); |
| 3356 |
br.readLine(); |
| 3357 |
String szLine = br.readLine(); |
| 3358 |
if (szLine == null) |
| 3359 |
{ |
| 3360 |
throw new IllegalArgumentException(szbinneddataDIR+"/"+szfilename+" is empty!!!"); |
| 3361 |
} |
| 3362 |
StringTokenizer st = new StringTokenizer(szLine,"\t"); |
| 3363 |
nummarks = st.countTokens(); |
| 3364 |
|
| 3365 |
if (sumtags == null) |
| 3366 |
{ |
| 3367 |
|
| 3368 |
sumtags = new double[nummarks]; |
| 3369 |
thresholds = new int[nummarks]; |
| 3370 |
} |
| 3371 |
else if (nummarks != sumtags.length) |
| 3372 |
{ |
| 3373 |
throw new IllegalArgumentException("Number of marks ("+nummarks+ ") in "+szfilename+" is inconsistent with previously found # of"+sumtags.length); |
| 3374 |
} |
| 3375 |
|
| 3376 |
|
| 3377 |
while ((szLine = br.readLine())!=null) |
| 3378 |
{ |
| 3379 |
ntotallocs++; |
| 3380 |
st = new StringTokenizer(szLine,"\t "); |
| 3381 |
if (st.countTokens() != nummarks) |
| 3382 |
{ |
| 3383 |
throw new IllegalArgumentException("In "+szfilename+" did not find the expected "+nummarks+" marks in this line: "+szLine); |
| 3384 |
} |
| 3385 |
|
| 3386 |
for (int nj = 0; nj < nummarks; nj++) |
| 3387 |
{ |
| 3388 |
double dval = Double.parseDouble(st.nextToken()); |
| 3389 |
sumtags[nj] += dval; |
| 3390 |
} |
| 3391 |
} |
| 3392 |
br.close(); |
| 3393 |
} |
| 3394 |
|
| 3395 |
for (int nj = 0; nj < sumtags.length; nj++) |
| 3396 |
{ |
| 3397 |
|
| 3398 |
double dlambda = sumtags[nj]/(double) ntotallocs; |
| 3399 |
|
| 3400 |
double dcum = 0; |
| 3401 |
short nthresh = 0; |
| 3402 |
double dlogfactorial = 0; |
| 3403 |
|
| 3404 |
while (dcum <= dcumthreshold) |
| 3405 |
{ |
| 3406 |
double dprob = Math.exp(Math.log(dlambda)*nthresh-dlambda-dlogfactorial); |
| 3407 |
dcum += dprob; |
| 3408 |
nthresh++; |
| 3409 |
dlogfactorial += Math.log(nthresh); |
| 3410 |
} |
| 3411 |
|
| 3412 |
if (bcontainsthresh) |
| 3413 |
{ |
| 3414 |
|
| 3415 |
nthresh--; |
| 3416 |
} |
| 3417 |
|
| 3418 |
thresholds[nj] = Math.max(Math.max(Math.max((int) Math.ceil(dfoldthresh*dlambda),nthresh),1), (int) Math.ceil(dcountthresh)); |
| 3419 |
if (ChromHMM.BVERBOSE) |
| 3420 |
{ |
| 3421 |
System.out.println("Modification\t"+szcell+"\t"+nj+"\t"+thresholds[nj]+"\t"+sumtags[nj]+ |
| 3422 |
"\t"+((int) Math.ceil(dfoldthresh*dlambda))+"\t"+ntotallocs); |
| 3423 |
} |
| 3424 |
} |
| 3425 |
|
| 3426 |
|
| 3427 |
for (int nindex = 0; nindex < numchrompercell; nindex++) |
| 3428 |
{ |
| 3429 |
|
| 3430 |
String szfilename = signalchromfiles[((Integer) al.get(nindex)).intValue()]; |
| 3431 |
BufferedReader br = Util.getBufferedReader(szbinneddataDIR+"/"+szfilename); |
| 3432 |
String szChromCellLine = br.readLine(); |
| 3433 |
String szMarkLine = br.readLine(); |
| 3434 |
if (szChromCellLine == null) |
| 3435 |
{ |
| 3436 |
throw new IllegalArgumentException(szbinneddataDIR+"/"+szfilename+" is empty!"); |
| 3437 |
} |
| 3438 |
StringTokenizer st = new StringTokenizer(szChromCellLine,"\t"); |
| 3439 |
if (st.countTokens() != 2) |
| 3440 |
{ |
| 3441 |
throw new IllegalArgumentException(szbinneddataDIR+"/"+szfilename+" header line must have a two columns delimited by a tab found "+ |
| 3442 |
szChromCellLine); |
| 3443 |
} |
| 3444 |
|
| 3445 |
if (bgzip) |
| 3446 |
{ |
| 3447 |
if (bsplit) |
| 3448 |
{ |
| 3449 |
|
| 3450 |
String szLine; |
| 3451 |
int nbin = 0; |
| 3452 |
int nsplit = 0; |
| 3453 |
String szcurrcell = st.nextToken().trim(); |
| 3454 |
String szchrom = st.nextToken().trim(); |
| 3455 |
boolean bopen = false; |
| 3456 |
GZIPOutputStream pwzip = null; |
| 3457 |
|
| 3458 |
while ((szLine = br.readLine())!=null) |
| 3459 |
{ |
| 3460 |
if (nbin % numsplitbins == 0) |
| 3461 |
{ |
| 3462 |
String szfile = szoutputDIR+"/"+szcurrcell+"_"+szchrom+"."+nsplit+"_binary.txt.gz"; |
| 3463 |
|
| 3464 |
System.out.println("Writing to file "+szfile); |
| 3465 |
|
| 3466 |
pwzip = new GZIPOutputStream(new FileOutputStream(szfile)); |
| 3467 |
|
| 3468 |
byte[] btformat = (szcurrcell+"\t"+szchrom+"."+nsplit+"\n").getBytes(); |
| 3469 |
pwzip.write(btformat,0,btformat.length); |
| 3470 |
|
| 3471 |
|
| 3472 |
btformat = (szMarkLine+"\n").getBytes(); |
| 3473 |
pwzip.write(btformat,0,btformat.length); |
| 3474 |
nsplit++; |
| 3475 |
bopen = true; |
| 3476 |
} |
| 3477 |
|
| 3478 |
st = new StringTokenizer(szLine,"\t "); |
| 3479 |
StringBuffer sbout = new StringBuffer(); |
| 3480 |
|
| 3481 |
for (int ncol = 0; ncol < nummarks-1; ncol++) |
| 3482 |
{ |
| 3483 |
double dval = Double.parseDouble(st.nextToken()); |
| 3484 |
if (dval == -1) |
| 3485 |
{ |
| 3486 |
sbout.append("2\t"); |
| 3487 |
|
| 3488 |
} |
| 3489 |
else if (thresholds[ncol] <= dval) |
| 3490 |
{ |
| 3491 |
sbout.append("1\t"); |
| 3492 |
|
| 3493 |
} |
| 3494 |
else |
| 3495 |
{ |
| 3496 |
sbout.append("0\t"); |
| 3497 |
|
| 3498 |
} |
| 3499 |
} |
| 3500 |
|
| 3501 |
double dval = Double.parseDouble(st.nextToken()); |
| 3502 |
if (dval == -1) |
| 3503 |
{ |
| 3504 |
sbout.append("2\n"); |
| 3505 |
|
| 3506 |
} |
| 3507 |
else if (thresholds[nummarks-1] <= dval) |
| 3508 |
{ |
| 3509 |
sbout.append("1\n"); |
| 3510 |
|
| 3511 |
} |
| 3512 |
else |
| 3513 |
{ |
| 3514 |
sbout.append("0\n"); |
| 3515 |
|
| 3516 |
} |
| 3517 |
|
| 3518 |
byte[] btformat = sbout.toString().getBytes(); |
| 3519 |
pwzip.write(btformat,0,btformat.length); |
| 3520 |
|
| 3521 |
if ((nbin+1) % numsplitbins == 0) |
| 3522 |
{ |
| 3523 |
bopen = false; |
| 3524 |
pwzip.finish(); |
| 3525 |
pwzip.close(); |
| 3526 |
} |
| 3527 |
nbin++; |
| 3528 |
} |
| 3529 |
br.close(); |
| 3530 |
|
| 3531 |
if (bopen) |
| 3532 |
{ |
| 3533 |
pwzip.finish(); |
| 3534 |
pwzip.close(); |
| 3535 |
} |
| 3536 |
} |
| 3537 |
else |
| 3538 |
{ |
| 3539 |
String szfile = szoutputDIR+"/"+st.nextToken().trim()+"_"+st.nextToken().trim()+"_binary.txt.gz"; |
| 3540 |
System.out.println("Writing to file "+szfile); |
| 3541 |
GZIPOutputStream pwzip = new GZIPOutputStream(new FileOutputStream(szfile)); |
| 3542 |
|
| 3543 |
byte[] btformat = (szChromCellLine+"\n").getBytes(); |
| 3544 |
pwzip.write(btformat,0,btformat.length); |
| 3545 |
|
| 3546 |
|
| 3547 |
btformat = (szMarkLine+"\n").getBytes(); |
| 3548 |
pwzip.write(btformat,0,btformat.length); |
| 3549 |
|
| 3550 |
|
| 3551 |
String szLine; |
| 3552 |
while ((szLine = br.readLine())!=null) |
| 3553 |
{ |
| 3554 |
st = new StringTokenizer(szLine,"\t "); |
| 3555 |
StringBuffer sbout = new StringBuffer(); |
| 3556 |
|
| 3557 |
for (int ncol = 0; ncol < nummarks-1; ncol++) |
| 3558 |
{ |
| 3559 |
double dval = Double.parseDouble(st.nextToken()); |
| 3560 |
if (dval == -1) |
| 3561 |
{ |
| 3562 |
sbout.append("2\t"); |
| 3563 |
|
| 3564 |
} |
| 3565 |
else if (thresholds[ncol] <= dval) |
| 3566 |
{ |
| 3567 |
sbout.append("1\t"); |
| 3568 |
|
| 3569 |
} |
| 3570 |
else |
| 3571 |
{ |
| 3572 |
sbout.append("0\t"); |
| 3573 |
|
| 3574 |
} |
| 3575 |
} |
| 3576 |
|
| 3577 |
double dval = Double.parseDouble(st.nextToken()); |
| 3578 |
if (dval == -1) |
| 3579 |
{ |
| 3580 |
sbout.append("2\n"); |
| 3581 |
|
| 3582 |
} |
| 3583 |
else if (thresholds[nummarks-1] <= dval) |
| 3584 |
{ |
| 3585 |
sbout.append("1\n"); |
| 3586 |
|
| 3587 |
} |
| 3588 |
else |
| 3589 |
{ |
| 3590 |
sbout.append("0\n"); |
| 3591 |
|
| 3592 |
} |
| 3593 |
|
| 3594 |
btformat = sbout.toString().getBytes(); |
| 3595 |
pwzip.write(btformat,0,btformat.length); |
| 3596 |
|
| 3597 |
} |
| 3598 |
br.close(); |
| 3599 |
pwzip.finish(); |
| 3600 |
pwzip.close(); |
| 3601 |
} |
| 3602 |
} |
| 3603 |
else |
| 3604 |
{ |
| 3605 |
if (bsplit) |
| 3606 |
{ |
| 3607 |
int nbin = 0; |
| 3608 |
int nsplit = 0; |
| 3609 |
String szcurrcell = st.nextToken().trim(); |
| 3610 |
String szchrom = st.nextToken().trim(); |
| 3611 |
boolean bopen = false; |
| 3612 |
PrintWriter pw = null; |
| 3613 |
String szLine; |
| 3614 |
while ((szLine = br.readLine())!=null) |
| 3615 |
{ |
| 3616 |
if (nbin % numsplitbins == 0) |
| 3617 |
{ |
| 3618 |
|
| 3619 |
String szfile = szoutputDIR+"/"+szcurrcell+"_"+szchrom+"."+nsplit+"_binary.txt"; |
| 3620 |
System.out.println("Writing to file "+szfile); |
| 3621 |
pw = new PrintWriter(new FileWriter(szfile)); |
| 3622 |
pw.println(szcurrcell+"\t"+szchrom+"."+nsplit); |
| 3623 |
pw.println(szMarkLine); |
| 3624 |
nsplit++; |
| 3625 |
bopen = true; |
| 3626 |
} |
| 3627 |
|
| 3628 |
st = new StringTokenizer(szLine,"\t "); |
| 3629 |
for (int ncol = 0; ncol < nummarks-1; ncol++) |
| 3630 |
{ |
| 3631 |
double dval = Double.parseDouble(st.nextToken()); |
| 3632 |
if (dval == -1) |
| 3633 |
{ |
| 3634 |
pw.print("2\t"); |
| 3635 |
} |
| 3636 |
else if (thresholds[ncol] <= dval) |
| 3637 |
{ |
| 3638 |
pw.print("1\t"); |
| 3639 |
} |
| 3640 |
else |
| 3641 |
{ |
| 3642 |
pw.print("0\t"); |
| 3643 |
} |
| 3644 |
} |
| 3645 |
|
| 3646 |
double dval = Double.parseDouble(st.nextToken()); |
| 3647 |
if (dval == -1) |
| 3648 |
{ |
| 3649 |
pw.println("2"); |
| 3650 |
} |
| 3651 |
else if (thresholds[nummarks-1] <= dval) |
| 3652 |
{ |
| 3653 |
pw.println("1"); |
| 3654 |
} |
| 3655 |
else |
| 3656 |
{ |
| 3657 |
pw.println("0"); |
| 3658 |
} |
| 3659 |
|
| 3660 |
if ((nbin+1)%numsplitbins == 0) |
| 3661 |
{ |
| 3662 |
pw.close(); |
| 3663 |
bopen = false; |
| 3664 |
} |
| 3665 |
|
| 3666 |
nbin++; |
| 3667 |
} |
| 3668 |
br.close(); |
| 3669 |
|
| 3670 |
if (bopen) |
| 3671 |
{ |
| 3672 |
pw.close(); |
| 3673 |
} |
| 3674 |
} |
| 3675 |
else |
| 3676 |
{ |
| 3677 |
String szfile = szoutputDIR+"/"+st.nextToken().trim()+"_"+st.nextToken().trim()+"_binary.txt"; |
| 3678 |
System.out.println("Writing to file "+szfile); |
| 3679 |
PrintWriter pw = new PrintWriter(new FileWriter(szfile)); |
| 3680 |
pw.println(szChromCellLine); |
| 3681 |
pw.println(szMarkLine); |
| 3682 |
String szLine; |
| 3683 |
while ((szLine = br.readLine())!=null) |
| 3684 |
{ |
| 3685 |
st = new StringTokenizer(szLine,"\t "); |
| 3686 |
for (int ncol = 0; ncol < nummarks-1; ncol++) |
| 3687 |
{ |
| 3688 |
double dval = Double.parseDouble(st.nextToken()); |
| 3689 |
if (dval == -1) |
| 3690 |
{ |
| 3691 |
pw.print("2\t"); |
| 3692 |
} |
| 3693 |
else if (thresholds[ncol] <= dval) |
| 3694 |
{ |
| 3695 |
pw.print("1\t"); |
| 3696 |
} |
| 3697 |
else |
| 3698 |
{ |
| 3699 |
pw.print("0\t"); |
| 3700 |
} |
| 3701 |
} |
| 3702 |
|
| 3703 |
double dval = Double.parseDouble(st.nextToken()); |
| 3704 |
if (dval == -1) |
| 3705 |
{ |
| 3706 |
pw.println("2"); |
| 3707 |
} |
| 3708 |
else if (thresholds[nummarks-1] <= dval) |
| 3709 |
{ |
| 3710 |
pw.println("1"); |
| 3711 |
} |
| 3712 |
else |
| 3713 |
{ |
| 3714 |
pw.println("0"); |
| 3715 |
} |
| 3716 |
} |
| 3717 |
br.close(); |
| 3718 |
pw.close(); |
| 3719 |
} |
| 3720 |
} |
| 3721 |
} |
| 3722 |
} |
| 3723 |
} |
| 3724 |
|
| 3725 |
|
| 3726 |
|
| 3727 |
|
| 3728 |
|
| 3729 |
|
| 3730 |
|
| 3731 |
public static void mergeBinarizedFiles(String szinputdir, String szoutputdir, String szdirlistfile, |
| 3732 |
boolean bsplit, int numsplitbins, boolean bgzip,String szfiletype) throws IOException |
| 3733 |
{ |
| 3734 |
ArrayList aldirs = new ArrayList(); |
| 3735 |
|
| 3736 |
if (szdirlistfile == null) |
| 3737 |
{ |
| 3738 |
|
| 3739 |
File dir = new File(szinputdir); |
| 3740 |
String[] subdirall = dir.list(); |
| 3741 |
if (subdirall == null) |
| 3742 |
{ |
| 3743 |
throw new IllegalArgumentException(szinputdir+" is not a valid directory!"); |
| 3744 |
} |
| 3745 |
for (int ndir = 0; ndir < subdirall.length; ndir++) |
| 3746 |
{ |
| 3747 |
File f = new File(szinputdir+"/"+subdirall[ndir]); |
| 3748 |
if ((f.isDirectory())&&(!f.isHidden())) |
| 3749 |
{ |
| 3750 |
aldirs.add(subdirall[ndir]); |
| 3751 |
} |
| 3752 |
} |
| 3753 |
|
| 3754 |
if (aldirs.size() == 0) |
| 3755 |
{ |
| 3756 |
throw new IllegalArgumentException("No sub-directories found in "+szinputdir+" that are not Hidden"); |
| 3757 |
} |
| 3758 |
} |
| 3759 |
else |
| 3760 |
{ |
| 3761 |
|
| 3762 |
BufferedReader brfiles = Util.getBufferedReader(szdirlistfile); |
| 3763 |
|
| 3764 |
String szLine; |
| 3765 |
while ((szLine = brfiles.readLine())!=null) |
| 3766 |
{ |
| 3767 |
aldirs.add(szLine); |
| 3768 |
} |
| 3769 |
brfiles.close(); |
| 3770 |
|
| 3771 |
} |
| 3772 |
|
| 3773 |
|
| 3774 |
|
| 3775 |
String[] subdirall = new String[aldirs.size()]; |
| 3776 |
for (int ndir = 0; ndir < subdirall.length; ndir++) |
| 3777 |
{ |
| 3778 |
subdirall[ndir] = (String) aldirs.get(ndir); |
| 3779 |
} |
| 3780 |
Arrays.sort(subdirall); |
| 3781 |
|
| 3782 |
HashSet hsfiles = new HashSet(); |
| 3783 |
HashMap[] hmbrA = new HashMap[subdirall.length]; |
| 3784 |
for (int na = 0; na < hmbrA.length; na++) |
| 3785 |
{ |
| 3786 |
hmbrA[na] = new HashMap(); |
| 3787 |
} |
| 3788 |
String[] szheader2A = new String[subdirall.length]; |
| 3789 |
|
| 3790 |
for (int ndir = 0; ndir < subdirall.length; ndir++) |
| 3791 |
{ |
| 3792 |
File currdir = new File(szinputdir+"/"+subdirall[ndir]); |
| 3793 |
String[] chromfilesall = currdir.list(); |
| 3794 |
if (chromfilesall == null) |
| 3795 |
{ |
| 3796 |
throw new IllegalArgumentException(szinputdir+"/"+subdirall[ndir]+" is not a valid directory!"); |
| 3797 |
} |
| 3798 |
|
| 3799 |
String szcurrpath = szinputdir+"/"+subdirall[ndir]; |
| 3800 |
|
| 3801 |
for (int nfile = 0; nfile < chromfilesall.length; nfile++) |
| 3802 |
{ |
| 3803 |
String szcurrfile = chromfilesall[nfile]; |
| 3804 |
|
| 3805 |
if ((szcurrfile.contains("_"+szfiletype))&&(!(new File(szcurrpath+"/"+szcurrfile)).isHidden())) |
| 3806 |
{ |
| 3807 |
|
| 3808 |
|
| 3809 |
|
| 3810 |
|
| 3811 |
String szpathfile = szcurrpath+"/"+szcurrfile; |
| 3812 |
|
| 3813 |
BufferedReader brfile = Util.getBufferedReader(szpathfile); |
| 3814 |
String szheader1 = brfile.readLine(); |
| 3815 |
if (szheader1 == null) |
| 3816 |
{ |
| 3817 |
throw new IllegalArgumentException("File "+szcurrpath+"/"+szcurrfile+" is empty!"); |
| 3818 |
} |
| 3819 |
StringTokenizer st =new StringTokenizer(szheader1,"\t"); |
| 3820 |
if (st.countTokens() != 2) |
| 3821 |
{ |
| 3822 |
throw new IllegalArgumentException("Invalid first line header of "+szheader1+" found in "+szcurrpath+"/"+szcurrfile); |
| 3823 |
} |
| 3824 |
|
| 3825 |
String szheader2 = brfile.readLine(); |
| 3826 |
brfile.close(); |
| 3827 |
|
| 3828 |
if (szheader2A[ndir] == null) |
| 3829 |
{ |
| 3830 |
szheader2A[ndir] = szheader2; |
| 3831 |
} |
| 3832 |
else if (!szheader2.equals(szheader2A[ndir])) |
| 3833 |
{ |
| 3834 |
throw new IllegalArgumentException("Inconsistent header lines in "+szcurrpath+" found "+szheader2+" and "+szheader2A[ndir]); |
| 3835 |
} |
| 3836 |
hmbrA[ndir].put(szheader1, szpathfile); |
| 3837 |
|
| 3838 |
hsfiles.add(szheader1); |
| 3839 |
} |
| 3840 |
} |
| 3841 |
|
| 3842 |
if (hmbrA[ndir].size() == 0) |
| 3843 |
{ |
| 3844 |
|
| 3845 |
throw new IllegalArgumentException("No files found in "+szcurrpath+" containing '_"+szfiletype+"' that are not Hidden"); |
| 3846 |
} |
| 3847 |
} |
| 3848 |
|
| 3849 |
|
| 3850 |
StringBuffer sbmergedheader = new StringBuffer(); |
| 3851 |
|
| 3852 |
String[] notpresent = new String[szheader2A.length]; |
| 3853 |
|
| 3854 |
|
| 3855 |
HashSet hsmarkpresent = new HashSet(); |
| 3856 |
|
| 3857 |
for (int ndir = 0; ndir < szheader2A.length; ndir++) |
| 3858 |
{ |
| 3859 |
StringTokenizer st = new StringTokenizer(szheader2A[ndir], "\t"); |
| 3860 |
int ntokens = st.countTokens(); |
| 3861 |
|
| 3862 |
|
| 3863 |
while (st.hasMoreTokens()) |
| 3864 |
{ |
| 3865 |
String szmarktoken = st.nextToken().trim(); |
| 3866 |
if (hsmarkpresent.contains(szmarktoken)) |
| 3867 |
{ |
| 3868 |
throw new IllegalArgumentException("Feature "+szmarktoken+" is not unique. Found in multiple files being merged"); |
| 3869 |
} |
| 3870 |
else |
| 3871 |
{ |
| 3872 |
hsmarkpresent.add(szmarktoken); |
| 3873 |
} |
| 3874 |
} |
| 3875 |
|
| 3876 |
|
| 3877 |
StringBuffer sbnotpresent = new StringBuffer(); |
| 3878 |
sbnotpresent.append("0"); |
| 3879 |
for (int na = 1; na <ntokens; na++) |
| 3880 |
{ |
| 3881 |
sbnotpresent.append("\t0"); |
| 3882 |
} |
| 3883 |
|
| 3884 |
notpresent[ndir] = sbnotpresent.toString(); |
| 3885 |
if (ndir == 0) |
| 3886 |
{ |
| 3887 |
sbmergedheader.append(szheader2A[ndir]); |
| 3888 |
} |
| 3889 |
else |
| 3890 |
{ |
| 3891 |
sbmergedheader.append("\t"+szheader2A[ndir]); |
| 3892 |
} |
| 3893 |
} |
| 3894 |
String szmergedheader = sbmergedheader.toString(); |
| 3895 |
|
| 3896 |
String[] chromfilescombine = new String[hsfiles.size()]; |
| 3897 |
|
| 3898 |
|
| 3899 |
|
| 3900 |
|
| 3901 |
Iterator hsfilesitr = (Iterator) hsfiles.iterator(); |
| 3902 |
for (int nfile = 0; nfile < chromfilescombine.length; nfile++) |
| 3903 |
{ |
| 3904 |
chromfilescombine[nfile] = (String) hsfilesitr.next(); |
| 3905 |
|
| 3906 |
|
| 3907 |
} |
| 3908 |
|
| 3909 |
Arrays.sort(chromfilescombine); |
| 3910 |
boolean[] bpresent = new boolean[subdirall.length]; |
| 3911 |
BufferedReader[] brfiles = new BufferedReader[subdirall.length]; |
| 3912 |
|
| 3913 |
for (int nfile = 0; nfile < chromfilescombine.length; nfile++) |
| 3914 |
{ |
| 3915 |
String szcurrfile = chromfilescombine[nfile]; |
| 3916 |
for (int ndir = 0; ndir < subdirall.length; ndir++) |
| 3917 |
{ |
| 3918 |
|
| 3919 |
String szpathfile = (String) hmbrA[ndir].get(szcurrfile); |
| 3920 |
|
| 3921 |
if (szpathfile == null) |
| 3922 |
{ |
| 3923 |
System.out.println("Warning directory "+szinputdir+"/"+subdirall[ndir]+" does not contain file "+szcurrfile+" going to treat as not present"); |
| 3924 |
bpresent[ndir] = false; |
| 3925 |
|
| 3926 |
} |
| 3927 |
else |
| 3928 |
{ |
| 3929 |
bpresent[ndir] = true; |
| 3930 |
brfiles[ndir] = Util.getBufferedReader(szpathfile); |
| 3931 |
brfiles[ndir].readLine(); |
| 3932 |
brfiles[ndir].readLine(); |
| 3933 |
} |
| 3934 |
} |
| 3935 |
|
| 3936 |
|
| 3937 |
StringTokenizer st = new StringTokenizer(chromfilescombine[nfile],"\t"); |
| 3938 |
String szcell = st.nextToken().trim(); |
| 3939 |
String szchrom = st.nextToken().trim(); |
| 3940 |
|
| 3941 |
|
| 3942 |
if (bsplit) |
| 3943 |
{ |
| 3944 |
int nsplit = 0; |
| 3945 |
boolean bnull = false; |
| 3946 |
while (!bnull) |
| 3947 |
{ |
| 3948 |
if (bgzip) |
| 3949 |
{ |
| 3950 |
|
| 3951 |
|
| 3952 |
GZIPOutputStream pwzip = new GZIPOutputStream(new FileOutputStream(szoutputdir+"/"+szcell+"_"+szchrom+"."+nsplit+"_"+szfiletype+".txt.gz")); |
| 3953 |
|
| 3954 |
|
| 3955 |
|
| 3956 |
String szout = szcell+"\t"+szchrom+"."+nsplit+"\n"; |
| 3957 |
byte[] btformat = szout.getBytes(); |
| 3958 |
pwzip.write(btformat,0,btformat.length); |
| 3959 |
|
| 3960 |
|
| 3961 |
btformat = (szmergedheader+"\n").getBytes(); |
| 3962 |
pwzip.write(btformat,0,btformat.length); |
| 3963 |
|
| 3964 |
|
| 3965 |
|
| 3966 |
int nline = 0; |
| 3967 |
while ((!bnull) && (nline < numsplitbins)) |
| 3968 |
{ |
| 3969 |
StringBuffer sbout = new StringBuffer(); |
| 3970 |
if (bpresent[0]) |
| 3971 |
{ |
| 3972 |
String szLine = brfiles[0].readLine(); |
| 3973 |
bnull = (szLine == null); |
| 3974 |
if (!bnull) |
| 3975 |
{ |
| 3976 |
sbout.append(szLine); |
| 3977 |
} |
| 3978 |
} |
| 3979 |
else |
| 3980 |
{ |
| 3981 |
sbout.append(notpresent[0]); |
| 3982 |
} |
| 3983 |
|
| 3984 |
int ndir = 1; |
| 3985 |
while ((ndir < subdirall.length) && (!bnull)) |
| 3986 |
{ |
| 3987 |
if (bpresent[ndir]) |
| 3988 |
{ |
| 3989 |
String szLine = brfiles[ndir].readLine(); |
| 3990 |
bnull = (szLine == null); |
| 3991 |
if (!bnull) |
| 3992 |
{ |
| 3993 |
sbout.append("\t"+szLine); |
| 3994 |
} |
| 3995 |
} |
| 3996 |
else |
| 3997 |
{ |
| 3998 |
sbout.append("\t"+notpresent[ndir]); |
| 3999 |
} |
| 4000 |
ndir++; |
| 4001 |
} |
| 4002 |
|
| 4003 |
if (!bnull) |
| 4004 |
{ |
| 4005 |
btformat = (sbout.toString()+"\n").getBytes(); |
| 4006 |
pwzip.write(btformat,0,btformat.length); |
| 4007 |
|
| 4008 |
} |
| 4009 |
nline++; |
| 4010 |
} |
| 4011 |
nsplit++; |
| 4012 |
pwzip.finish(); |
| 4013 |
pwzip.close(); |
| 4014 |
} |
| 4015 |
else |
| 4016 |
{ |
| 4017 |
|
| 4018 |
|
| 4019 |
|
| 4020 |
|
| 4021 |
|
| 4022 |
PrintWriter pw = new PrintWriter(new FileWriter(szoutputdir+"/"+szcell+"_"+szchrom+"."+nsplit+"_"+szfiletype+".txt")); |
| 4023 |
|
| 4024 |
pw.println(szcell+"\t"+szchrom+"."+nsplit); |
| 4025 |
pw.println(szmergedheader); |
| 4026 |
|
| 4027 |
|
| 4028 |
int nline = 0; |
| 4029 |
while ((!bnull) && (nline < numsplitbins)) |
| 4030 |
{ |
| 4031 |
StringBuffer sbout = new StringBuffer(); |
| 4032 |
if (bpresent[0]) |
| 4033 |
{ |
| 4034 |
String szLine = brfiles[0].readLine(); |
| 4035 |
bnull = (szLine == null); |
| 4036 |
|
| 4037 |
if (!bnull) |
| 4038 |
{ |
| 4039 |
sbout.append(szLine); |
| 4040 |
} |
| 4041 |
} |
| 4042 |
else |
| 4043 |
{ |
| 4044 |
sbout.append(notpresent[0]); |
| 4045 |
} |
| 4046 |
|
| 4047 |
int ndir = 1; |
| 4048 |
while ((ndir < subdirall.length) && (!bnull)) |
| 4049 |
{ |
| 4050 |
if (bpresent[ndir]) |
| 4051 |
{ |
| 4052 |
String szLine = brfiles[ndir].readLine(); |
| 4053 |
bnull = (szLine == null); |
| 4054 |
|
| 4055 |
if (!bnull) |
| 4056 |
{ |
| 4057 |
sbout.append("\t"+szLine); |
| 4058 |
} |
| 4059 |
} |
| 4060 |
else |
| 4061 |
{ |
| 4062 |
sbout.append("\t"+notpresent[ndir]); |
| 4063 |
} |
| 4064 |
ndir++; |
| 4065 |
} |
| 4066 |
|
| 4067 |
if (!bnull) |
| 4068 |
{ |
| 4069 |
pw.println(sbout.toString()); |
| 4070 |
} |
| 4071 |
nline++; |
| 4072 |
} |
| 4073 |
nsplit++; |
| 4074 |
pw.close(); |
| 4075 |
} |
| 4076 |
} |
| 4077 |
} |
| 4078 |
else |
| 4079 |
{ |
| 4080 |
if (bgzip) |
| 4081 |
{ |
| 4082 |
|
| 4083 |
GZIPOutputStream pwzip = new GZIPOutputStream(new FileOutputStream(szoutputdir+"/"+szcell+"_"+szchrom+"_"+szfiletype+".txt.gz")); |
| 4084 |
|
| 4085 |
|
| 4086 |
|
| 4087 |
String szout = szcell+"\t"+szchrom +"\n"; |
| 4088 |
byte[] btformat = szout.getBytes(); |
| 4089 |
pwzip.write(btformat,0,btformat.length); |
| 4090 |
|
| 4091 |
|
| 4092 |
btformat = (szmergedheader+"\n").getBytes(); |
| 4093 |
pwzip.write(btformat,0,btformat.length); |
| 4094 |
|
| 4095 |
|
| 4096 |
boolean bnull = false; |
| 4097 |
|
| 4098 |
while (!bnull) |
| 4099 |
{ |
| 4100 |
StringBuffer sbout = new StringBuffer(); |
| 4101 |
if (bpresent[0]) |
| 4102 |
{ |
| 4103 |
String szLine = brfiles[0].readLine(); |
| 4104 |
bnull = (szLine == null); |
| 4105 |
if (!bnull) |
| 4106 |
{ |
| 4107 |
sbout.append(szLine); |
| 4108 |
} |
| 4109 |
} |
| 4110 |
else |
| 4111 |
{ |
| 4112 |
sbout.append(notpresent[0]); |
| 4113 |
} |
| 4114 |
|
| 4115 |
int ndir = 1; |
| 4116 |
while ((ndir < subdirall.length) && (!bnull)) |
| 4117 |
{ |
| 4118 |
if (bpresent[ndir]) |
| 4119 |
{ |
| 4120 |
String szLine = brfiles[ndir].readLine(); |
| 4121 |
bnull = (szLine == null); |
| 4122 |
if (!bnull) |
| 4123 |
{ |
| 4124 |
sbout.append("\t"+szLine); |
| 4125 |
} |
| 4126 |
} |
| 4127 |
else |
| 4128 |
{ |
| 4129 |
sbout.append("\t"+notpresent[ndir]); |
| 4130 |
} |
| 4131 |
ndir++; |
| 4132 |
} |
| 4133 |
|
| 4134 |
if (!bnull) |
| 4135 |
{ |
| 4136 |
btformat = (sbout.toString()+"\n").getBytes(); |
| 4137 |
pwzip.write(btformat,0,btformat.length); |
| 4138 |
|
| 4139 |
} |
| 4140 |
} |
| 4141 |
pwzip.finish(); |
| 4142 |
pwzip.close(); |
| 4143 |
} |
| 4144 |
else |
| 4145 |
{ |
| 4146 |
|
| 4147 |
|
| 4148 |
|
| 4149 |
|
| 4150 |
PrintWriter pw = new PrintWriter(new FileWriter(szoutputdir+"/"+szcell+"_"+szchrom+"_"+szfiletype+".txt")); |
| 4151 |
|
| 4152 |
pw.println(szcell+"\t"+szchrom); |
| 4153 |
pw.println(szmergedheader); |
| 4154 |
|
| 4155 |
boolean bnull = false; |
| 4156 |
|
| 4157 |
while (!bnull) |
| 4158 |
{ |
| 4159 |
StringBuffer sbout = new StringBuffer(); |
| 4160 |
if (bpresent[0]) |
| 4161 |
{ |
| 4162 |
String szLine = brfiles[0].readLine(); |
| 4163 |
bnull = (szLine == null); |
| 4164 |
|
| 4165 |
if (!bnull) |
| 4166 |
{ |
| 4167 |
sbout.append(szLine); |
| 4168 |
} |
| 4169 |
} |
| 4170 |
else |
| 4171 |
{ |
| 4172 |
sbout.append(notpresent[0]); |
| 4173 |
} |
| 4174 |
|
| 4175 |
int ndir = 1; |
| 4176 |
while ((ndir < subdirall.length) && (!bnull)) |
| 4177 |
{ |
| 4178 |
if (bpresent[ndir]) |
| 4179 |
{ |
| 4180 |
String szLine = brfiles[ndir].readLine(); |
| 4181 |
bnull = (szLine == null); |
| 4182 |
|
| 4183 |
if (!bnull) |
| 4184 |
{ |
| 4185 |
sbout.append("\t"+szLine); |
| 4186 |
} |
| 4187 |
} |
| 4188 |
else |
| 4189 |
{ |
| 4190 |
sbout.append("\t"+notpresent[ndir]); |
| 4191 |
} |
| 4192 |
ndir++; |
| 4193 |
} |
| 4194 |
|
| 4195 |
if (!bnull) |
| 4196 |
{ |
| 4197 |
pw.println(sbout.toString()); |
| 4198 |
} |
| 4199 |
} |
| 4200 |
pw.close(); |
| 4201 |
} |
| 4202 |
} |
| 4203 |
|
| 4204 |
|
| 4205 |
|
| 4206 |
|
| 4207 |
|
| 4208 |
|
| 4209 |
|
| 4210 |
|
| 4211 |
|
| 4212 |
|
| 4213 |
|
| 4214 |
|
| 4215 |
|
| 4216 |
for (int ndir = 0; ndir < brfiles.length; ndir++) |
| 4217 |
{ |
| 4218 |
|
| 4219 |
if (brfiles[ndir] != null) |
| 4220 |
{ |
| 4221 |
brfiles[ndir].close(); |
| 4222 |
} |
| 4223 |
} |
| 4224 |
|
| 4225 |
} |
| 4226 |
|
| 4227 |
|
| 4228 |
|
| 4229 |
|
| 4230 |
|
| 4231 |
|
| 4232 |
|
| 4233 |
|
| 4234 |
|
| 4235 |
|
| 4236 |
} |
| 4237 |
|
| 4238 |
} |