Ejemplo n.º 1
0
  /**
   * Create a BAMFileWriter that is ready to receive SAMRecords.
   *
   * @param header entire header. Sort order is determined by the sortOrder property of this arg.
   * @param presorted if true, SAMRecords must be added to the SAMFileWriter in order that agrees
   *     with header.sortOrder.
   * @param outputFile where to write the output.
   * @param compressionLevel Override default compression level with the given value, between 0
   *     (fastest) and 9 (smallest).
   */
  public SAMFileWriter makeBAMWriter(
      final SAMFileHeader header,
      final boolean presorted,
      final File outputFile,
      final int compressionLevel) {
    try {
      final boolean createMd5File = this.createMd5File && IOUtil.isRegularPath(outputFile);
      if (this.createMd5File && !createMd5File) {
        System.err.println(
            "Cannot create MD5 file for BAM because output file is not a regular file: "
                + outputFile.getAbsolutePath());
      }
      OutputStream os =
          IOUtil.maybeBufferOutputStream(new FileOutputStream(outputFile, false), bufferSize);
      if (createMd5File)
        os = new Md5CalculatingOutputStream(os, new File(outputFile.getAbsolutePath() + ".md5"));
      final BAMFileWriter ret = new BAMFileWriter(os, outputFile, compressionLevel);
      final boolean createIndex = this.createIndex && IOUtil.isRegularPath(outputFile);
      if (this.createIndex && !createIndex) {
        System.err.println(
            "Cannot create index for BAM because output file is not a regular file: "
                + outputFile.getAbsolutePath());
      }
      if (this.tmpDir != null) ret.setTempDirectory(this.tmpDir);
      initializeBAMWriter(ret, header, presorted, createIndex);

      if (this.useAsyncIo) return new AsyncSAMFileWriter(ret, this.asyncOutputBufferSize);
      else return ret;
    } catch (final IOException ioe) {
      throw new RuntimeIOException("Error opening file: " + outputFile.getAbsolutePath());
    }
  }