private void syncAndClose(Bin bin) {
   try {
     bin.sync();
   } finally {
     bin.close();
   }
 }
 /**
  * Gets the level associated with the given bin number.
  *
  * @param bin The bin for which to determine the level.
  * @return the level associated with the given bin number.
  */
 public int getLevelForBin(final Bin bin) {
   if (bin.getBinNumber() >= GenomicIndexUtil.MAX_BINS)
     throw new SAMException("Tried to get level for invalid bin.");
   for (int i = getNumIndexLevels() - 1; i >= 0; i--) {
     if (bin.getBinNumber() >= GenomicIndexUtil.LEVEL_STARTS[i]) return i;
   }
   throw new SAMException("Unable to find correct bin for bin " + bin);
 }
Example #3
0
  private void updateGaps(Bin<T> prev, Bin<T> next) {
    Gap<T> newGap = new Gap<T>(prev, next, gapWeight(prev, next));

    Gap<T> prevGap = _binsToGaps.get(prev.getMean());
    if (prevGap != null) {
      _gaps.remove(prevGap);
    }

    _binsToGaps.put(prev.getMean(), newGap);
    _gaps.add(newGap);
  }
Example #4
0
  private void updateGaps(Bin<T> newBin) {
    Bin<T> prev = lower(newBin.getMean());
    if (prev != null) {
      updateGaps(prev, newBin);
    }

    Bin<T> next = higher(newBin.getMean());
    if (next != null) {
      updateGaps(newBin, next);
    }
  }
 private void writeToLockFile(int lockSignal) {
   ByteArrayBuffer lockBuffer = newLockFileBuffer();
   lockBuffer.writeInt(lockSignal);
   lockBuffer.writeInt(lockSignal);
   write(_lockFile, lockBuffer);
   _lockFile.sync();
 }
  @Override
  public void applySlotChanges(
      Visitable<SlotChange> slotChangeTree, int slotChangeCount, Slot reservedSlot) {
    if (slotChangeCount < 1) {
      return;
    }

    Runnable commitHook = _container.commitHook();
    flushDatabaseFile();

    ensureLogAndLock();
    int length = transactionLogSlotLength(slotChangeCount);
    ByteArrayBuffer logBuffer = new ByteArrayBuffer(length);
    logBuffer.writeInt(length);
    logBuffer.writeInt(slotChangeCount);

    appendSlotChanges(logBuffer, slotChangeTree);
    write(_logFile, logBuffer);
    _logFile.sync();

    writeToLockFile(LOCK_INT);

    writeSlots(slotChangeTree);
    commitHook.run();
    flushDatabaseFile();
    writeToLockFile(0);
  }
 /**
  * Gets the last locus that this bin can index into.
  *
  * @param bin The bin to test.
  * @return The last position that the given bin can represent.
  */
 public int getLastLocusInBin(final Bin bin) {
   final int level = getLevelForBin(bin);
   final int levelStart = GenomicIndexUtil.LEVEL_STARTS[level];
   final int levelSize =
       ((level == getNumIndexLevels() - 1)
               ? GenomicIndexUtil.MAX_BINS - 1
               : GenomicIndexUtil.LEVEL_STARTS[level + 1])
           - levelStart;
   return (bin.getBinNumber() - levelStart + 1) * (GenomicIndexUtil.BIN_GENOMIC_SPAN / levelSize);
 }
Example #8
0
  @Override
  public void merge() {
    while (_bins.size() > getMaxBins()) {
      Gap<T> smallestGap = _gaps.pollFirst();
      Bin<T> newBin = smallestGap.getStartBin().combine(smallestGap.getEndBin());

      Gap<T> followingGap = _binsToGaps.get(smallestGap.getEndBin().getMean());
      if (followingGap != null) {
        _gaps.remove(followingGap);
      }

      _bins.remove(smallestGap.getStartBin().getMean());
      _bins.remove(smallestGap.getEndBin().getMean());
      _binsToGaps.remove(smallestGap.getStartBin().getMean());
      _binsToGaps.remove(smallestGap.getEndBin().getMean());

      updateGaps(newBin);
      _bins.put(newBin.getMean(), newBin);
    }
  }
  public static Solution fit(Solution sol, List<Integer> sticks) {
    Collections.sort(sticks);
    Collections.reverse(sticks);
    Set<Bin> set = sol.getBins();
    List<Bin> bins = new ArrayList<>(set);
    Collections.sort(bins);
    Collections.reverse(bins);

    boolean added = false;
    for (Integer i : sticks) {
      added = false;
      for (Bin bin : bins) {
        if (bin.add(i)) {
          added = true;
          break;
        }
      }
      if (!added) {
        bins.add(new Bin(i, sol.getCapacity()));
      }
    }
    return new Solution(new HashSet<>(bins), sol.getCapacity());
  }
 private void write(Bin storage, ByteArrayBuffer buffer) {
   storage.write(0, buffer._buffer, buffer.length());
 }
 private void read(Bin storage, ByteArrayBuffer buffer) {
   storage.read(0, buffer._buffer, buffer.length());
 }
Example #12
0
 /**
  * Constructor.
  *
  * @param bin base64 input
  * @param ii input info
  * @throws QueryException query exception
  */
 public B64(final Bin bin, final InputInfo ii) throws QueryException {
   this(bin.binary(ii));
 }
  protected BAMIndexContent query(
      final int referenceSequence, final int startPos, final int endPos) {
    seek(4);

    final List<Chunk> metaDataChunks = new ArrayList<Chunk>();

    final int sequenceCount = readInteger();

    if (referenceSequence >= sequenceCount) {
      return null;
    }

    final BitSet regionBins = GenomicIndexUtil.regionToBins(startPos, endPos);
    if (regionBins == null) {
      return null;
    }

    skipToSequence(referenceSequence);

    final int binCount = readInteger();
    boolean metaDataSeen = false;
    final Bin[] bins = new Bin[getMaxBinNumberForReference(referenceSequence) + 1];
    for (int binNumber = 0; binNumber < binCount; binNumber++) {
      final int indexBin = readInteger();
      final int nChunks = readInteger();
      List<Chunk> chunks = null;
      // System.out.println("# bin[" + i + "] = " + indexBin + ", nChunks = " + nChunks);
      Chunk lastChunk = null;
      if (regionBins.get(indexBin)) {
        chunks = new ArrayList<Chunk>(nChunks);
        for (int ci = 0; ci < nChunks; ci++) {
          final long chunkBegin = readLong();
          final long chunkEnd = readLong();
          lastChunk = new Chunk(chunkBegin, chunkEnd);
          chunks.add(lastChunk);
        }
      } else if (indexBin == GenomicIndexUtil.MAX_BINS) {
        // meta data - build the bin so that the count of bins is correct;
        // but don't attach meta chunks to the bin, or normal queries will be off
        for (int ci = 0; ci < nChunks; ci++) {
          final long chunkBegin = readLong();
          final long chunkEnd = readLong();
          lastChunk = new Chunk(chunkBegin, chunkEnd);
          metaDataChunks.add(lastChunk);
        }
        metaDataSeen = true;
        continue; // don't create a Bin
      } else {
        skipBytes(16 * nChunks);
        chunks = Collections.emptyList();
      }
      final Bin bin = new Bin(referenceSequence, indexBin);
      bin.setChunkList(chunks);
      bin.setLastChunk(lastChunk);
      bins[indexBin] = bin;
    }

    final int nLinearBins = readInteger();

    final int regionLinearBinStart = LinearIndex.convertToLinearIndexOffset(startPos);
    final int regionLinearBinStop =
        endPos > 0 ? LinearIndex.convertToLinearIndexOffset(endPos) : nLinearBins - 1;
    final int actualStop = Math.min(regionLinearBinStop, nLinearBins - 1);

    long[] linearIndexEntries = new long[0];
    if (regionLinearBinStart < nLinearBins) {
      linearIndexEntries = new long[actualStop - regionLinearBinStart + 1];
      skipBytes(8 * regionLinearBinStart);
      for (int linearBin = regionLinearBinStart; linearBin <= actualStop; linearBin++)
        linearIndexEntries[linearBin - regionLinearBinStart] = readLong();
    }

    final LinearIndex linearIndex =
        new LinearIndex(referenceSequence, regionLinearBinStart, linearIndexEntries);

    return new BAMIndexContent(
        referenceSequence,
        bins,
        binCount - (metaDataSeen ? 1 : 0),
        new BAMIndexMetaData(metaDataChunks),
        linearIndex);
  }
Example #14
0
 @Override
 public void insert(Bin<T> bin) {
   addTotalCount(bin);
   if (isFrozen() && getBins().size() == getMaxBins()) {
     Double floorDiff = Double.MAX_VALUE;
     Bin<T> floorBin = floor(bin.getMean());
     if (floorBin != null) {
       floorDiff = Math.abs(floorBin.getMean() - bin.getMean());
     }
     Double ceilDiff = Double.MAX_VALUE;
     Bin<T> ceilBin = ceiling(bin.getMean());
     if (ceilBin != null) {
       ceilDiff = Math.abs(ceilBin.getMean() - bin.getMean());
     }
     if (floorDiff <= ceilDiff) {
       floorBin.sumUpdate(bin);
     } else {
       ceilBin.sumUpdate(bin);
     }
   } else {
     Bin<T> existingBin = get(bin.getMean());
     if (existingBin != null) {
       existingBin.sumUpdate(bin);
       if (isWeightGaps()) {
         updateGaps(existingBin);
       }
     } else {
       updateGaps(bin);
       _bins.put(bin.getMean(), bin);
     }
   }
 }
Example #15
0
 @Override
 public IBin binFromNameRecurseUp(String name) {
   Pointer p = gst_bin_get_by_name_recurse_up(ptr, name);
   if (p == null || p == Pointer.NULL) return null;
   return Bin.from(p);
 }