Examples of EncodingParams


Examples of net.sf.cram.EncodingParams

    int[] values = c.values();
    int[] lens = c.bitLens();
    System.out.println(Arrays.toString(values));
    System.out.println(Arrays.toString(lens));

    EncodingParams params = HuffmanIntegerEncoding.toParam(values, lens);
    HuffmanIntegerEncoding e = new HuffmanIntegerEncoding();
    e.fromByteArray(params.params);

    BitCodec<Integer> codec = e.buildCodec(null, null);
View Full Code Here

Examples of net.sf.cram.EncodingParams

        EncodingID id = EncodingID.values()[buf.get()];
        int paramLen = ByteBufferUtils.readUnsignedITF8(buf);
        byte[] paramBytes = new byte[paramLen];
        buf.get(paramBytes);

        eMap.put(eKey, new EncodingParams(id, paramBytes));

        log.debug(String.format("FOUND ENCODING: %s, %s, %s.",
            eKey.name(), id.name(),
            Arrays.toString(Arrays.copyOf(paramBytes, 20))));
      }
    }

    { // tag encoding map:
      int byteSize = ByteBufferUtils.readUnsignedITF8(is);
      byte[] bytes = new byte[byteSize];
      ByteBufferUtils.readFully(bytes, is);
      ByteBuffer buf = ByteBuffer.wrap(bytes);

      int mapSize = ByteBufferUtils.readUnsignedITF8(buf);
      tMap = new TreeMap<Integer, EncodingParams>();
      for (int i = 0; i < mapSize; i++) {
        int key = ByteBufferUtils.readUnsignedITF8(buf);

        EncodingID id = EncodingID.values()[buf.get()];
        int paramLen = ByteBufferUtils.readUnsignedITF8(buf);
        byte[] paramBytes = new byte[paramLen];
        buf.get(paramBytes);

        tMap.put(key, new EncodingParams(id, paramBytes));
      }
    }
  }
View Full Code Here

Examples of net.sf.cram.EncodingParams

          continue;

        mapBuf.put((byte) eKey.name().charAt(0));
        mapBuf.put((byte) eKey.name().charAt(1));

        EncodingParams params = eMap.get(eKey);
        mapBuf.put((byte) (0xFF & params.id.ordinal()));
        ByteBufferUtils.writeUnsignedITF8(params.params.length, mapBuf);
        mapBuf.put(params.params);
      }
      mapBuf.flip();
      byte[] mapBytes = new byte[mapBuf.limit()];
      mapBuf.get(mapBytes);

      ByteBufferUtils.writeUnsignedITF8(mapBytes.length, os);
      os.write(mapBytes);
    }

    { // tag encoding map:
      ByteBuffer mapBuf = ByteBuffer.allocate(1024 * 100);
      ByteBufferUtils.writeUnsignedITF8(tMap.size(), mapBuf);
      for (Integer eKey : tMap.keySet()) {
        ByteBufferUtils.writeUnsignedITF8(eKey, mapBuf);

        EncodingParams params = tMap.get(eKey);
        mapBuf.put((byte) (0xFF & params.id.ordinal()));
        ByteBufferUtils.writeUnsignedITF8(params.params.length, mapBuf);
        mapBuf.put(params.params);
      }
      mapBuf.flip();
View Full Code Here

Examples of net.sf.cram.EncodingParams

      }

      Encoding<Integer> bestEncoding = calc.getBestEncoding();
      h.eMap.put(
          EncodingKey.AP_AlignmentPositionOffset,
          new EncodingParams(bestEncoding.id(), bestEncoding
              .toByteArray()));
    }

    { // read group
      HuffmanParamsCalculator calculator = new HuffmanParamsCalculator();
      for (CramRecord r : records)
        calculator.add(r.getReadGroupID());
      calculator.calculate();

      h.eMap.put(EncodingKey.RG_ReadGroup, HuffmanIntegerEncoding
          .toParam(calculator.values(), calculator.bitLens()));
    }

    { // read name encoding:
      HuffmanParamsCalculator calculator = new HuffmanParamsCalculator();
      for (CramRecord r : records)
        calculator.add(r.getReadName().length());
      calculator.calculate();

      h.eMap.put(EncodingKey.RN_ReadName, ByteArrayLenEncoding.toParam(
          HuffmanIntegerEncoding.toParam(calculator.values(),
              calculator.bitLens()), ExternalByteArrayEncoding
              .toParam(readNameID)));
      // h.eMap.put(EncodingKey.RN_ReadName,
      // ByteArrayStopEncoding.toParam((byte) 0, readNameID));
    }

    { // records to next fragment
      IntegerEncodingCalculator calc = new IntegerEncodingCalculator(
          "records to next fragment");
      for (CramRecord r : records)
        calc.addValue(r.getRecordsToNextFragment());

      Encoding<Integer> bestEncoding = calc.getBestEncoding();
      h.eMap.put(
          EncodingKey.NF_RecordsToNextFragment,
          new EncodingParams(bestEncoding.id(), bestEncoding
              .toByteArray()));
    }

    { // tag count
      HuffmanParamsCalculator calculator = new HuffmanParamsCalculator();
      for (CramRecord r : records)
        calculator.add(r.tags == null ? 0 : r.tags.length);
      calculator.calculate();

      h.eMap.put(EncodingKey.TC_TagCount, HuffmanIntegerEncoding.toParam(
          calculator.values(), calculator.bitLens()));
    }

    { // tag name and type
      HuffmanParamsCalculator calculator = new HuffmanParamsCalculator();
      for (CramRecord r : records) {
        if (r.tags == null)
          continue;
        for (ReadTag tag : r.tags)
          calculator.add(tag.keyType3BytesAsInt);
      }
      calculator.calculate();

      h.eMap.put(EncodingKey.TN_TagNameAndType, HuffmanIntegerEncoding
          .toParam(calculator.values(), calculator.bitLens()));
    }

    {

      Comparator<ReadTag> comparator = new Comparator<ReadTag>() {

        @Override
        public int compare(ReadTag o1, ReadTag o2) {
          return o1.keyType3BytesAsInt - o2.keyType3BytesAsInt;
        }
      };

      Comparator<byte[]> baComparator = new Comparator<byte[]>() {

        @Override
        public int compare(byte[] o1, byte[] o2) {
          if (o1.length - o2.length != 0)
            return o1.length - o2.length;

          for (int i = 0; i < o1.length; i++)
            if (o1[i] != o2[i])
              return o1[i] - o2[i];

          return 0;
        }
      };

      Map<byte[], MutableInt> map = new TreeMap<byte[], MutableInt>(
          baComparator);
      MutableInt noTagCounter = new MutableInt();
      map.put(new byte[0], noTagCounter);
      for (CramRecord r : records) {
        if (r.tags == null) {
          noTagCounter.value++;
          r.tagIdsIndex = noTagCounter;
          continue;
        }

        Arrays.sort(r.tags, comparator);
        r.tagIds = new byte[r.tags.length * 3];

        int tagIndex = 0;
        for (int i = 0; i < r.tags.length; i++) {
          r.tagIds[i * 3] = (byte) r.tags[tagIndex].keyType3Bytes
              .charAt(0);
          r.tagIds[i * 3 + 1] = (byte) r.tags[tagIndex].keyType3Bytes
              .charAt(1);
          r.tagIds[i * 3 + 2] = (byte) r.tags[tagIndex].keyType3Bytes
              .charAt(2);
          tagIndex++;
        }

        MutableInt count = map.get(r.tagIds);
        if (count == null) {
          count = new MutableInt();
          map.put(r.tagIds, count);
        }
        count.value++;
        r.tagIdsIndex = count;
      }

      byte[][][] dic = new byte[map.size()][][];
      int i = 0;
      HuffmanParamsCalculator calculator = new HuffmanParamsCalculator();
      for (byte[] idsAsBytes : map.keySet()) {
        int nofIds = idsAsBytes.length / 3;
        dic[i] = new byte[nofIds][];
        for (int j = 0; j < idsAsBytes.length;) {
          int idIndex = j / 3;
          dic[i][idIndex] = new byte[3];
          dic[i][idIndex][0] = idsAsBytes[j++];
          dic[i][idIndex][1] = idsAsBytes[j++];
          dic[i][idIndex][2] = idsAsBytes[j++];
        }
        calculator.add(i, map.get(idsAsBytes).value);
        map.get(idsAsBytes).value = i++;
      }

      calculator.calculate();
      h.eMap.put(EncodingKey.TL_TagIdList, HuffmanIntegerEncoding
          .toParam(calculator.values(), calculator.bitLens()));
      h.dictionary = dic;
    }

    { // tag values
      Map<Integer, HuffmanParamsCalculator> cc = new TreeMap<Integer, HuffmanParamsCalculator>();

      for (CramRecord r : records) {
        if (r.tags == null)
          continue;

        for (ReadTag tag : r.tags) {
          switch (tag.keyType3BytesAsInt) {
          // case ReadTag.OQZ:
          // case ReadTag.BQZ:
          // EncodingParams params = h.tMap
          // .get(tag.keyType3BytesAsInt);
          // if (params == null) {
          // h.tMap.put(tag.keyType3BytesAsInt,
          // ByteArrayStopEncoding.toParam((byte) 1,
          // tagValueExtID));
          // }
          // break;

          default:
            HuffmanParamsCalculator c = cc
                .get(tag.keyType3BytesAsInt);
            if (c == null) {
              c = new HuffmanParamsCalculator();
              cc.put(tag.keyType3BytesAsInt, c);
            }
            c.add(tag.getValueAsByteArray().length);
            break;
          }
        }
      }

      if (!cc.isEmpty())
        for (Integer key : cc.keySet()) {
          HuffmanParamsCalculator c = cc.get(key);
          c.calculate();

          h.tMap.put(key, ByteArrayLenEncoding.toParam(
              HuffmanIntegerEncoding.toParam(c.values(),
                  c.bitLens()),
              ExternalByteArrayEncoding.toParam(tagValueExtID)));
        }

      for (Integer key : h.tMap.keySet()) {
        log.debug(String.format("TAG ENCODING: %d, %s", key,
            h.tMap.get(key)));
      }

      // for (CramRecord r : records) {
      // if (r.tags == null || r.tags.isEmpty())
      // continue;
      // for (ReadTag tag : r.tags) {
      // EncodingParams params = h.tMap.get(tag.keyType3BytesAsInt);
      // if (params == null) {
      // h.tMap.put(tag.keyType3BytesAsInt,
      // ByteArrayStopEncoding.toParam((byte) 0,
      // tagValueExtID));
      // }
      // }
      // }
    }

    { // number of read features
      HuffmanParamsCalculator calculator = new HuffmanParamsCalculator();
      for (CramRecord r : records)
        calculator.add(r.getReadFeatures() == null ? 0 : r
            .getReadFeatures().size());
      calculator.calculate();

      h.eMap.put(EncodingKey.FN_NumberOfReadFeatures,
          HuffmanIntegerEncoding.toParam(calculator.values(),
              calculator.bitLens()));
    }

    { // feature position
      IntegerEncodingCalculator calc = new IntegerEncodingCalculator(
          "read feature position");
      for (CramRecord r : records) {
        int prevPos = 0;
        if (r.getReadFeatures() == null)
          continue;
        for (ReadFeature rf : r.getReadFeatures()) {
          calc.addValue(rf.getPosition() - prevPos);
          prevPos = rf.getPosition();
        }
      }

      Encoding<Integer> bestEncoding = calc.getBestEncoding();
      h.eMap.put(EncodingKey.FP_FeaturePosition, new EncodingParams(
          bestEncoding.id(), bestEncoding.toByteArray()));
    }

    { // feature code
      HuffmanParamsCalculator calculator = new HuffmanParamsCalculator();
View Full Code Here

Examples of net.sf.cram.EncodingParams

          for (Integer value : dictionary.keySet())
            c.add(value, dictionary.get(value).value);

          c.calculate();

          EncodingParams param = HuffmanIntegerEncoding.toParam(
              c.values(), c.bitLens());
          HuffmanIntegerEncoding he = new HuffmanIntegerEncoding();
          he.fromByteArray(param.params);
          EncodingLengthCalculator lc = new EncodingLengthCalculator(
              he);
View Full Code Here

Examples of net.sf.cram.EncodingParams

  }

  public static EncodingParams toParam(byte stopByte, int externalId) {
    ByteArrayStopEncoding e = new ByteArrayStopEncoding(stopByte,
        externalId);
    EncodingParams params = new EncodingParams(ID, e.toByteArray());
    return params;
  }
View Full Code Here

Examples of net.sf.cram.EncodingParams

  }
 
  public static EncodingParams toParam(int contentId) {
    ExternalLongEncoding e = new ExternalLongEncoding() ;
    e.contentId = contentId ;
    return new EncodingParams(encodingId, e.toByteArray()) ;
  }
View Full Code Here

Examples of net.sf.cram.EncodingParams

  public static EncodingParams toParam(int m) {
    GolombIntegerEncoding e = new GolombIntegerEncoding();
    e.m = m;
    e.offset = 0 ;
    return new EncodingParams(ENCODING_ID, e.toByteArray());
  }
View Full Code Here

Examples of net.sf.cram.EncodingParams

 
  public static EncodingParams toParam(int m, int offset) {
    GolombIntegerEncoding e = new GolombIntegerEncoding();
    e.m = m;
    e.offset = offset ;
    return new EncodingParams(ENCODING_ID, e.toByteArray());
  }
View Full Code Here

Examples of net.sf.cram.EncodingParams

  }
 
  public static EncodingParams toParam(int contentId) {
    ExternalIntegerEncoding e = new ExternalIntegerEncoding() ;
    e.contentId = contentId ;
    return new EncodingParams(encodingId, e.toByteArray()) ;
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.