Package net.sf.cram.io

Examples of net.sf.cram.io.ExposedByteArrayOutputStream


  public static int writeContainer(Container c, OutputStream os)
      throws IOException {

    long time1 = System.nanoTime();
    ExposedByteArrayOutputStream baos = new ExposedByteArrayOutputStream();

    Block block = new CompressionHeaderBLock(c.h);
    block.write(baos);
    c.blockCount = 1;

    List<Integer> landmarks = new ArrayList<Integer>();
    SliceIO sio = new SliceIO();
    for (int i = 0; i < c.slices.length; i++) {
      Slice s = c.slices[i];
      landmarks.add(baos.size());
      sio.write(s, baos);
      c.blockCount++ ;
      c.blockCount++;
      if (s.embeddedRefBlock != null)
        c.blockCount++;
      c.blockCount += s.external.size();
    }
    c.landmarks = new int[landmarks.size()];
    for (int i = 0; i < c.landmarks.length; i++)
      c.landmarks[i] = landmarks.get(i);

    c.containerByteSize = baos.size();
    calculateSliceOffsetsAndSizes(c);

    ContainerHeaderIO chio = new ContainerHeaderIO();
    int len = chio.writeContainerHeader(c, os);
    os.write(baos.getBuffer(), 0, baos.size());
    len += baos.size();

    long time2 = System.nanoTime();

    log.debug("CONTAINER WRITTEN: " + c.toString());
    c.writeTime = time2 - time1;
View Full Code Here


    lastSlice.offset = c.landmarks[c.landmarks.length - 1];
    lastSlice.size = c.containerByteSize - lastSlice.offset;
  }

  private static byte[] toByteArray(SAMFileHeader samFileHeader) {
    ExposedByteArrayOutputStream headerBodyOS = new ExposedByteArrayOutputStream();
    OutputStreamWriter w = new OutputStreamWriter(headerBodyOS);
    new SAMTextHeaderCodec().encode(w, samFileHeader);
    try {
      w.close();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }

    ByteBuffer buf = ByteBuffer.allocate(4);
    buf.order(ByteOrder.LITTLE_ENDIAN);
    buf.putInt(headerBodyOS.size());
    buf.flip();
    byte[] bytes = new byte[buf.limit()];
    buf.get(bytes);

    ByteArrayOutputStream headerOS = new ExposedByteArrayOutputStream();
    try {
      headerOS.write(bytes);
      headerOS.write(headerBodyOS.getBuffer(), 0, headerBodyOS.size());
    } catch (IOException e) {
      throw new RuntimeException(e);
    }

    return headerOS.toByteArray();
  }
View Full Code Here

    c.bases = 0;
    c.globalRecordCounter = 0;
    c.nofRecords = 0;
    c.sequenceId = 0;

    ExposedByteArrayOutputStream baos = new ExposedByteArrayOutputStream();
    block.write(baos);
    c.containerByteSize = baos.size();

    ContainerHeaderIO chio = new ContainerHeaderIO();
    int len = chio.writeContainerHeader(c, os);
    os.write(baos.getBuffer(), 0, baos.size());

    return len + baos.size();
  }
View Full Code Here

  @Override
  public BitCodec<byte[]> buildCodec(Map<Integer, InputStream> inputMap,
      Map<Integer, ExposedByteArrayOutputStream> outputMap) {
    InputStream is = inputMap == null ? null : inputMap.get(externalId);
    ExposedByteArrayOutputStream os = outputMap == null ? null : outputMap
        .get(externalId);
    return new ByteArrayStopCodec(stopByte, is, os);
  }
View Full Code Here

  @Override
  public BitCodec<Long> buildCodec(Map<Integer, InputStream> inputMap,
      Map<Integer, ExposedByteArrayOutputStream> outputMap) {
    InputStream is = inputMap == null ? null : inputMap.get(contentId) ;
    ExposedByteArrayOutputStream os = outputMap == null ? null : outputMap.get(contentId) ;
    return (BitCodec) new ExternalLongCodec(os, is);
  }
View Full Code Here

  @Override
  public BitCodec<Integer> buildCodec(Map<Integer, InputStream> inputMap,
      Map<Integer, ExposedByteArrayOutputStream> outputMap) {
    InputStream is = inputMap == null ? null : inputMap.get(contentId) ;
    ExposedByteArrayOutputStream os = outputMap == null ? null : outputMap.get(contentId) ;
    return (BitCodec) new ExternalIntegerCodec(os, is);
  }
View Full Code Here

  @Override
  public BitCodec<byte[]> buildCodec(Map<Integer, InputStream> inputMap,
      Map<Integer, ExposedByteArrayOutputStream> outputMap) {
    InputStream is = inputMap == null ? null : inputMap.get(contentId);
    ExposedByteArrayOutputStream os = outputMap == null ? null : outputMap
        .get(contentId);
    return (BitCodec) new ExternalByteArrayCodec(os, is);
  }
View Full Code Here

  @Override
  public BitCodec<Byte> buildCodec(Map<Integer, InputStream> inputMap,
      Map<Integer, ExposedByteArrayOutputStream> outputMap) {
    InputStream is = inputMap == null ? null : inputMap.get(contentId) ;
    ExposedByteArrayOutputStream os = outputMap == null ? null : outputMap.get(contentId) ;
    return (BitCodec) new ExternalByteCodec(os, is);
  }
View Full Code Here

      CompressionHeader h, SAMFileHeader fileHeader)
      throws IllegalArgumentException, IllegalAccessException,
      IOException {
    Map<Integer, ExposedByteArrayOutputStream> map = new HashMap<Integer, ExposedByteArrayOutputStream>();
    for (int id : h.externalIds) {
      map.put(id, new ExposedByteArrayOutputStream());
    }

    DataWriterFactory f = new DataWriterFactory();
    ExposedByteArrayOutputStream bitBAOS = new ExposedByteArrayOutputStream();
    DefaultBitOutputStream bos = new DefaultBitOutputStream(bitBAOS);

    Slice slice = new Slice();
    slice.nofRecords = records.size();

    int[] seqIds = new int[fileHeader.getSequenceDictionary().size()];
    int minAlStart = Integer.MAX_VALUE;
    int maxAlEnd = SAMRecord.NO_ALIGNMENT_START;
    for (CramRecord r : records) {
      if (r.sequenceId != SAMRecord.NO_ALIGNMENT_REFERENCE_INDEX)
        seqIds[r.sequenceId]++;

      int alStart = r.getAlignmentStart();
      if (alStart != SAMRecord.NO_ALIGNMENT_START) {
        minAlStart = Math.min(alStart, minAlStart);
        maxAlEnd = Math.max(r.calcualteAlignmentEnd(), maxAlEnd);
      }
      slice.bases += r.getReadLength();
    }

    int seqId = SAMRecord.NO_ALIGNMENT_REFERENCE_INDEX;
    boolean singleSeqId = true;
    for (int i = 0; i < seqIds.length && singleSeqId; i++) {
      if (seqIds[i] > 0) {
        seqId = i++;
        for (; i < seqIds.length && singleSeqId; i++) {
          if (seqIds[i] > 0)
            singleSeqId = false;
        }
      }
    }

    if (!singleSeqId)
      throw new RuntimeException("Multiref slices are not supported.");

    slice.sequenceId = seqId;
    if (minAlStart == Integer.MAX_VALUE) {
      slice.alignmentStart = SAMRecord.NO_ALIGNMENT_START;
      slice.alignmentSpan = 0;
    } else {
      slice.alignmentStart = minAlStart;
      slice.alignmentSpan = maxAlEnd - minAlStart;
    }

    Writer writer = f.buildWriter(bos, map, h, slice.sequenceId);
    int prevAlStart = slice.alignmentStart;
    for (CramRecord r : records) {
      r.alignmentStartOffsetFromPreviousRecord = r.getAlignmentStart()
          - prevAlStart;
      prevAlStart = r.getAlignmentStart();
      writer.write(r);
    }

    slice.contentType = slice.alignmentSpan > -1 ? BlockContentType.MAPPED_SLICE
        : BlockContentType.RESERVED;

    bos.close();
    slice.coreBlock = new Block();
    slice.coreBlock.method = BlockCompressionMethod.RAW.ordinal();
    slice.coreBlock.setRawContent(bitBAOS.toByteArray());
    slice.coreBlock.contentType = BlockContentType.CORE;

    slice.external = new HashMap<Integer, Block>();
    for (Integer i : map.keySet()) {
      ExposedByteArrayOutputStream os = map.get(i);

      Block externalBlock = new Block();
      externalBlock.contentType = BlockContentType.EXTERNAL;
      externalBlock.method = BlockCompressionMethod.GZIP.ordinal();
      externalBlock.contentId = i;

      externalBlock.setRawContent(os.toByteArray());
      slice.external.put(i, externalBlock);
    }

    return slice;
  }
View Full Code Here

    for (int i = 0; i < records.size(); i++) {
      if (i < 10)
        System.out.println(records.get(i).toString());
    }

    ExposedByteArrayOutputStream baos = new ExposedByteArrayOutputStream(
        BYTES);
    ReadWrite.writeCramHeader(cramHeader, baos);
    byte[] b = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(b);
    CramHeader cramHeader2 = ReadWrite.readCramHeader(bais);
    assertEquals(toString(cramHeader.samFileHeader),
        toString(cramHeader2.samFileHeader));

    BLOCK_PROTO.recordsPerSlice = container.slices[0].nofRecords;

    Container container2 = BLOCK_PROTO.buildContainer(records,
        cramHeader.samFileHeader, true, 0, container.h.substitutionMatrix, true);
    for (int i = 0; i < container.slices.length; i++) {
      container2.slices[i].refMD5 = container.slices[i].refMD5;
    }

    ReadWrite.writeContainer(container2, baos);

    bais = new ByteArrayInputStream(baos.toByteArray());

    cramHeader2 = ReadWrite.readCramHeader(bais);
    assertNotNull(cramHeader);
    assertNotNull(cramHeader.samFileHeader);
    assertEquals(2, cramHeader.majorVersion);
    assertEquals(0, cramHeader.minorVersion);

    Container container3 = new Container();
    chio.readContainerHeader(container3, bais);
    assertNotNull(container3);
    System.out.println(container3);

    CompressionHeaderBLock chb3 = new CompressionHeaderBLock(bais);
    container3.h = chb3.getCompressionHeader();

    assertNotNull(container3.h);
    System.out.println(container3.h);

    container3.slices = new Slice[container3.landmarks.length];
    for (int s = 0; s < container3.landmarks.length; s++) {
      Slice slice = new Slice();
      sio.readSliceHeadBlock(slice, bais);
      sio.readSliceBlocks(slice, true, bais);
      container3.slices[s] = slice;
    }

    System.out.println(container3);

    ArrayList<CramRecord> records3 = new ArrayList<CramRecord>(
        container3.nofRecords);
    BLOCK_PROTO.getRecords(container3.h, container3,
        cramHeader.samFileHeader, records3);
    testALignmentSpan(container3, cramHeader, records3);

    for (int i = 0; i < records3.size(); i++) {
      System.out.println(records3.get(i).toString());
      if (i > 10)
        break;
    }

    assertEquals(container.alignmentStart, container3.alignmentStart);
    assertEquals(container.alignmentSpan, container3.alignmentSpan);
    assertEquals(container.bases, container3.bases);
    assertEquals(container.globalRecordCounter,
        container3.globalRecordCounter);
    assertEquals(container.landmarks.length, container3.landmarks.length);
    assertEquals(container.nofRecords, container3.nofRecords);
    assertEquals(container.sequenceId, container3.sequenceId);

    assertEquals(records.size(), records3.size());
    for (int i = 0; i < records.size(); i++) {
      CramRecord r1 = records.get(i);
      CramRecord r3 = records3.get(i);

      assertTrue(
          "Mismatch at " + i + ":\n" + r1.toString() + "\n"
              + r3.toString(), compare(r1, r3));
    }
   
    FileOutputStream fos = new FileOutputStream(new File("./src/test/resources/data/set1/small.cram2")) ;
    fos.write(baos.getBuffer(), 0, baos.size()) ;
    fos.close() ;
  }
View Full Code Here

TOP

Related Classes of net.sf.cram.io.ExposedByteArrayOutputStream

Copyright © 2018 www.massapicom. 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.