/**
   * Searches for toFind in sequence. It is assumed toFind will be at the end of sequence and that
   * the beginning of toFind is the most important part.
   *
   * @param sequence the sequence to search
   * @param toFind the string to find
   * @param barcodeIndex the index of the barcode within all of the barcodes
   * @param minMatchLength the minimum match length to consider
   * @return returns the number of differences between sequence and toFind
   */
  @Override
  BarcodeMatcherResult bestMatch(
      final MutableString sequence,
      final MutableString toFind,
      final int barcodeIndex,
      final int minMatchLength) {
    final OverlapResult overlapResult = overlapPortion(sequence, toFind);
    int leastNumDiffs = Integer.MAX_VALUE;
    int leastNumDiffsBarcodeStartPos = 0;
    int pos = 0;
    for (int matchLength = overlapResult.length;
        matchLength >= minMatchLength;
        matchLength--, pos++) {
      final int numDiffs =
          numDifferences(sequence, toFind, overlapResult.start + pos, 0, matchLength);
      if (numDiffs <= leastNumDiffs) {
        leastNumDiffs = numDiffs;
        leastNumDiffsBarcodeStartPos = sequence.length() - matchLength;
        if (leastNumDiffs == 0) {
          break;
        }
      }
    }
    final int preMatchSequenceLength = sequence.length();
    final int barcodeMatchedLength = preMatchSequenceLength - leastNumDiffsBarcodeStartPos;
    final int actualSequenceLength = preMatchSequenceLength - barcodeMatchedLength;

    return new BarcodeMatcherResult(
        barcodeIndex,
        leastNumDiffs,
        0,
        actualSequenceLength,
        leastNumDiffsBarcodeStartPos,
        barcodeMatchedLength);
  }
 /**
  * Determine the overlap portion of the two strings given their lengths.
  *
  * @param sequence the string we are searching
  * @param toFind the string we are looking for (at the end of search)
  * @return OverlapResult which specifies start and length
  */
 OverlapResult overlapPortion(final MutableString sequence, final MutableString toFind) {
   final OverlapResult overlapResult = new OverlapResult();
   final int sequenceLength = sequence.length();
   final int toFindLength = toFind.length();
   if (sequenceLength >= toFindLength) {
     overlapResult.start = (sequenceLength - toFindLength);
     overlapResult.length = toFindLength;
   } else if (sequenceLength < toFindLength) {
     overlapResult.start = 0;
     overlapResult.length = sequenceLength;
   }
   return overlapResult;
 }
示例#3
0
  public int encode(final CharSequence s, final OutputStream os) {

    final ByteCountOutputStream bcos = new ByteCountOutputStream(os);

    // Wrap with Writer using encoder.
    final OutputStreamWriter w = new OutputStreamWriter(bcos, cs);

    try {

      if (s instanceof MutableString) {
        // Efficient: tunnels to the backing char[].
        final MutableString t = (MutableString) s;
        w.write(t.array(), 0, t.length());
      } else if (s instanceof String) {
        w.write((String) s);
      } else {
        // TODO optimize for CharBuffer, StringBuilder
        w.write(s.toString());
      }

      w.flush();

      w.close();

      return bcos.getNWritten();

    } catch (IOException ex) {

      throw new RuntimeException(ex);
    }
  }