Class SAMFileWriterFactory

java.lang.Object
htsjdk.samtools.SAMFileWriterFactory
All Implemented Interfaces:
Cloneable

public class SAMFileWriterFactory extends Object implements Cloneable
Create a writer for writing SAM, BAM, or CRAM files.
  • Constructor Details

    • SAMFileWriterFactory

      public SAMFileWriterFactory()
      simple constructor
    • SAMFileWriterFactory

      public SAMFileWriterFactory(SAMFileWriterFactory other)
      copy constructor
  • Method Details

    • clone

      public SAMFileWriterFactory clone()
      Overrides:
      clone in class Object
    • setDefaultCreateMd5File

      public static void setDefaultCreateMd5File(boolean createMd5File)
      Sets the default for whether to create md5Files for BAM files this factory.
    • getDefaultCreateMd5File

      public static boolean getDefaultCreateMd5File()
      Gets the default for whether to create md5Files for BAM files this factory.
      See Also:
    • setCreateMd5File

      public SAMFileWriterFactory setCreateMd5File(boolean createMd5File)
      Sets whether to create md5Files for BAMs from this factory.
    • setDeflaterFactory

      public SAMFileWriterFactory setDeflaterFactory(DeflaterFactory deflaterFactory)
      Set the deflater factory used by BAM writers created by this writer factory. Must not be null. If this method is not called, the default DeflaterFactory is used which creates the default JDK Deflater. This method returns the SAMFileWriterFactory itself.
    • setCompressionLevel

      public SAMFileWriterFactory setCompressionLevel(int compressionLevel)
      set compression level 0!none 9: max
    • getCompressionLevel

      public int getCompressionLevel()
    • setDefaultCreateIndexWhileWriting

      public static void setDefaultCreateIndexWhileWriting(boolean setting)
      Sets the default for subsequent SAMFileWriterFactories that do not specify whether to create an index. If a BAM (not SAM) file is created, the setting is true, and the file header specifies coordinate order, then a BAM index file will be written along with the BAM file.
      Parameters:
      setting - whether to attempt to create a BAM index while creating the BAM file
    • getDefaultCreateIndexWhileWriting

      public static boolean getDefaultCreateIndexWhileWriting()
      Gets the default for subsequent SAMFileWriterFactories that do not specify whether to create an index.
      See Also:
    • setCreateIndex

      public SAMFileWriterFactory setCreateIndex(boolean setting)
      Convenience method allowing newSAMFileWriterFactory().setCreateIndex(true); Equivalent to SAMFileWriterFactory.setDefaultCreateIndexWhileWriting(true); newSAMFileWriterFactory(); If a BAM or CRAM (not SAM) file is created, the setting is true, and the file header specifies coordinate order, then a BAM index file will be written along with the BAM file.
      Parameters:
      setting - whether to attempt to create a BAM index while creating the BAM file.
      Returns:
      this factory object
    • setMaxRecordsInRam

      public SAMFileWriterFactory setMaxRecordsInRam(int maxRecordsInRam)
      Before creating a writer that is not presorted, this method may be called in order to override the default number of SAMRecords stored in RAM before spilling to disk (c.f. SAMFileWriterImpl.MAX_RECORDS_IN_RAM). When writing very large sorted SAM files, you may need call this method in order to avoid running out of file handles. The RAM available to the JVM may need to be increased in order to hold the specified number of records in RAM. This value affects the number of records stored in subsequent calls to one of the make...() methods.
      Parameters:
      maxRecordsInRam - Number of records to store in RAM before spilling to temporary file when creating a sorted SAM or BAM file.
    • getMaxRecordsInRam

      public int getMaxRecordsInRam()
      Gets the maximum number of records held in RAM before spilling to disk during sorting.
      See Also:
    • setUseAsyncIo

      public SAMFileWriterFactory setUseAsyncIo(boolean useAsyncIo)
      Turn on or off the use of asynchronous IO for writing output SAM and BAM files. If true then each SAMFileWriter creates a dedicated thread which is used for compression and IO activities.
    • setAsyncOutputBufferSize

      public SAMFileWriterFactory setAsyncOutputBufferSize(int asyncOutputBufferSize)
      If and only if using asynchronous IO then sets the maximum number of records that can be buffered per SAMFileWriter before producers will block when trying to write another SAMRecord.
    • setBufferSize

      public SAMFileWriterFactory setBufferSize(int bufferSize)
      Controls size of write buffer. Default value: [[htsjdk.samtools.Defaults#BUFFER_SIZE]]
    • setTempDirectory

      public SAMFileWriterFactory setTempDirectory(File tmpDir)
      Set the temporary directory to use when sort data.
      Parameters:
      tmpDir - Path to the temporary directory
    • getTempDirectory

      public File getTempDirectory()
      Gets the temporary directory that will be used when sorting data.
      See Also:
    • setSamFlagFieldOutput

      public SAMFileWriterFactory setSamFlagFieldOutput(SamFlagField samFlagFieldOutput)
      Set the flag output format only when writing text. Default value: [[htsjdk.samtools.SAMTextWriter.samFlagFieldOutput.DECIMAL]]
    • makeBAMWriter

      public SAMFileWriter makeBAMWriter(SAMFileHeader header, boolean presorted, File outputFile)
      Create a BAMFileWriter that is ready to receive SAMRecords. Uses default compression level.
      Parameters:
      header - entire header. Sort order is determined by the sortOrder property of this arg.
      presorted - if true, SAMRecords must be added to the SAMFileWriter in order that agrees with header.sortOrder.
      outputFile - where to write the output.
    • makeBAMWriter

      public SAMFileWriter makeBAMWriter(SAMFileHeader header, boolean presorted, Path outputPath)
      Create a BAMFileWriter that is ready to receive SAMRecords. Uses default compression level.
      Parameters:
      header - entire header. Sort order is determined by the sortOrder property of this arg.
      presorted - if true, SAMRecords must be added to the SAMFileWriter in order that agrees with header.sortOrder.
      outputPath - where to write the output.
    • makeBAMWriter

      public SAMFileWriter makeBAMWriter(SAMFileHeader header, boolean presorted, File outputFile, int compressionLevel)
      Create a BAMFileWriter that is ready to receive SAMRecords.
      Parameters:
      header - entire header. Sort order is determined by the sortOrder property of this arg.
      presorted - if true, SAMRecords must be added to the SAMFileWriter in order that agrees with header.sortOrder.
      outputFile - where to write the output.
      compressionLevel - Override default compression level with the given value, between 0 (fastest) and 9 (smallest).
    • makeBAMWriter

      public SAMFileWriter makeBAMWriter(SAMFileHeader header, boolean presorted, Path outputPath, int compressionLevel)
      Create a BAMFileWriter that is ready to receive SAMRecords.
      Parameters:
      header - entire header. Sort order is determined by the sortOrder property of this arg.
      presorted - if true, SAMRecords must be added to the SAMFileWriter in order that agrees with header.sortOrder.
      outputPath - where to write the output.
      compressionLevel - Override default compression level with the given value, between 0 (fastest) and 9 (smallest).
    • makeSAMWriter

      public SAMFileWriter makeSAMWriter(SAMFileHeader header, boolean presorted, File outputFile)
      Create a SAMTextWriter that is ready to receive SAMRecords.
      Parameters:
      header - entire header. Sort order is determined by the sortOrder property of this arg.
      presorted - if true, SAMRecords must be added to the SAMFileWriter in order that agrees with header.sortOrder.
      outputFile - where to write the output.
    • makeSAMWriter

      public SAMFileWriter makeSAMWriter(SAMFileHeader header, boolean presorted, Path outputPath)
      Create a SAMTextWriter that is ready to receive SAMRecords.
      Parameters:
      header - entire header. Sort order is determined by the sortOrder property of this arg.
      presorted - if true, SAMRecords must be added to the SAMFileWriter in order that agrees with header.sortOrder.
      outputPath - where to write the output.
    • makeSAMWriter

      public SAMFileWriter makeSAMWriter(SAMFileHeader header, boolean presorted, OutputStream stream)
      Create a SAMTextWriter for writing to a stream that is ready to receive SAMRecords. This method does not support the creation of an MD5 file
      Parameters:
      header - entire header. Sort order is determined by the sortOrder property of this arg.
      presorted - if true, SAMRecords must be added to the SAMFileWriter in order that agrees with header.sortOrder.
      stream - the stream to write records to. Note that this method does not buffer the stream, so the caller must buffer if desired. Note that PrintStream is buffered.
    • makeBAMWriter

      public SAMFileWriter makeBAMWriter(SAMFileHeader header, boolean presorted, OutputStream stream)
      Create a BAMFileWriter for writing to a stream that is ready to receive SAMRecords. This method does not support the creation of an MD5 file
      Parameters:
      header - entire header. Sort order is determined by the sortOrder property of this arg.
      presorted - if true, SAMRecords must be added to the SAMFileWriter in order that agrees with header.sortOrder.
      stream - the stream to write records to. Note that this method does not buffer the stream, so the caller must buffer if desired. Note that PrintStream is buffered.
    • makeSAMOrBAMWriter

      public SAMFileWriter makeSAMOrBAMWriter(SAMFileHeader header, boolean presorted, File outputFile)
      Create either a SAM or a BAM writer based on examination of the outputFile extension, defaults to BAM writer.
      Parameters:
      header - entire header. Sort order is determined by the sortOrder property of this arg.
      presorted - presorted if true, SAMRecords must be added to the SAMFileWriter in order that agrees with header.sortOrder.
      outputFile - where to write the output. Must end with .sam or .bam.
      Returns:
      SAM or BAM writer based on file extension of outputFile.
    • makeSAMOrBAMWriter

      public SAMFileWriter makeSAMOrBAMWriter(SAMFileHeader header, boolean presorted, Path outputPath)
      Create either a SAM or a BAM writer based on examination of the outputPath extension.
      Parameters:
      header - entire header. Sort order is determined by the sortOrder property of this arg.
      presorted - presorted if true, SAMRecords must be added to the SAMFileWriter in order that agrees with header.sortOrder.
      outputPath - where to write the output. Must end with .sam or .bam.
      Returns:
      SAM or BAM writer based on file extension of outputPath.
    • makeWriter

      public SAMFileWriter makeWriter(SAMFileHeader header, boolean presorted, File outputFile, File referenceFasta)
      Create a SAM, BAM or CRAM writer based on examination of the outputFile extension. The method assumes BAM file format for unknown file extensions.
      Parameters:
      header - header. Sort order is determined by the sortOrder property of this arg.
      presorted - if true, SAMRecords must be added to the SAMFileWriter in order that agrees with header.sortOrder.
      outputFile - where to write the output. Should end with .sam, .bam or .cram.
      referenceFasta - reference sequence file
      Returns:
      SAMFileWriter appropriate for SAM and CRAM file types specified in outputFile, or a BAM writer for all other types
    • makeWriter

      @Deprecated public SAMFileWriter makeWriter(SAMFileHeader header, boolean presorted, Path outputPath, File referenceFasta)
      Deprecated.
      Create a SAM, BAM or CRAM writer based on examination of the outputPath extension.
      Parameters:
      header - header. Sort order is determined by the sortOrder property of this arg.
      presorted - if true, SAMRecords must be added to the SAMFileWriter in order that agrees with header.sortOrder.
      outputPath - where to write the output. Must end with .sam, .bam or .cram.
      referenceFasta - reference sequence file
      Returns:
      SAMFileWriter appropriate for the file type specified in outputPath
    • makeWriter

      public SAMFileWriter makeWriter(SAMFileHeader header, boolean presorted, Path outputPath, Path referenceFasta)
      Create a SAM, BAM or CRAM writer based on examination of the outputPath extension.
      Parameters:
      header - header. Sort order is determined by the sortOrder property of this arg.
      presorted - if true, SAMRecords must be added to the SAMFileWriter in order that agrees with header.sortOrder.
      outputPath - where to write the output. Must end with .sam, .bam or .cram.
      referenceFasta - reference sequence file
      Returns:
      SAMFileWriter appropriate for the file type specified in outputPath
    • makeCRAMWriter

      public CRAMFileWriter makeCRAMWriter(SAMFileHeader header, OutputStream stream, File referenceFasta)
      Create a CRAMFileWriter on an output stream. Requires the input to be presorted to match the sort order defined by the input header. Note: does not honor factory settings for CREATE_MD5, CREATE_INDEX, USE_ASYNC_IO.
      Parameters:
      header - entire header. Sort order is determined by the sortOrder property of this arg.
      stream - where to write the output.
      referenceFasta - reference sequence file
      Returns:
      CRAMFileWriter
    • makeCRAMWriter

      public CRAMFileWriter makeCRAMWriter(SAMFileHeader header, OutputStream stream, Path referenceFasta)
      Create a CRAMFileWriter on an output stream. Requires the input to be presorted to match the sort order defined by the input header. Note: does not honor factory settings for CREATE_MD5, CREATE_INDEX, USE_ASYNC_IO.
      Parameters:
      header - entire header. Sort order is determined by the sortOrder property of this arg.
      stream - where to write the output.
      referenceFasta - reference sequence file
      Returns:
      CRAMFileWriter
    • makeCRAMWriter

      public CRAMFileWriter makeCRAMWriter(SAMFileHeader header, File outputFile, File referenceFasta)
      Create a CRAMFileWriter on an output file. Requires input record to be presorted to match the sort order defined by the input header. Note: does not honor factory settings for USE_ASYNC_IO.
      Parameters:
      header - entire header. Sort order is determined by the sortOrder property of this arg.
      outputFile - where to write the output. Must end with .sam, .bam or .cram.
      referenceFasta - reference sequence file
      Returns:
      CRAMFileWriter
    • makeCRAMWriter

      @Deprecated public CRAMFileWriter makeCRAMWriter(SAMFileHeader header, Path outputPath, File referenceFasta)
      Deprecated.
      since 6/18, prefer makeWriter(SAMFileHeader, boolean, Path, Path) for creating bam/cram writers however makeCRAMWriter(SAMFileHeader, boolean, Path, Path) is the direct replacement for this method
      Create a CRAMFileWriter on an output file. Requires input record to be presorted to match the sort order defined by the input header. Note: does not honor factory settings for USE_ASYNC_IO.
      Parameters:
      header - entire header. Sort order is determined by the sortOrder property of this arg.
      outputPath - where to write the output. Must end with .sam, .bam or .cram.
      referenceFasta - reference sequence file
      Returns:
      CRAMFileWriter
    • makeCRAMWriter

      public CRAMFileWriter makeCRAMWriter(SAMFileHeader header, boolean presorted, File outputFile, File referenceFasta)
      Create a CRAMFileWriter on an output file. Note: does not honor factory setting for USE_ASYNC_IO.
      Parameters:
      header - entire header. Sort order is determined by the sortOrder property of this arg.
      presorted - if true, SAMRecords must be added to the SAMFileWriter in order that agrees with header.sortOrder.
      outputFile - where to write the output. Must end with .sam, .bam or .cram.
      referenceFasta - reference sequence file
      Returns:
      CRAMFileWriter
    • makeCRAMWriter

      @Deprecated public CRAMFileWriter makeCRAMWriter(SAMFileHeader header, boolean presorted, Path output, File referenceFasta)
      Deprecated.
      since 6/18, prefer makeWriter(SAMFileHeader, boolean, Path, Path) for creating bam/cram writers however makeCRAMWriter(SAMFileHeader, boolean, Path, Path) is the direct replacement for this method
      Create a CRAMFileWriter on an output file. Note: does not honor factory setting for USE_ASYNC_IO.
      Parameters:
      header - entire header. Sort order is determined by the sortOrder property of this arg.
      presorted - if true, SAMRecords must be added to the SAMFileWriter in order that agrees with header.sortOrder.
      output - where to write the output. Must end with .sam, .bam or .cram.
      referenceFasta - reference sequence file
      Returns:
      CRAMFileWriter
    • makeCRAMWriter

      public CRAMFileWriter makeCRAMWriter(SAMFileHeader header, boolean presorted, Path output, Path referenceFasta)
      Create a CRAMFileWriter on an output file. Note: does not honor factory setting for USE_ASYNC_IO.
      Parameters:
      header - entire header. Sort order is determined by the sortOrder property of this arg.
      presorted - if true, SAMRecords must be added to the SAMFileWriter in order that agrees with header.sortOrder.
      output - where to write the output. Must end with .sam, .bam or .cram.
      referenceFasta - reference sequence file
      Returns:
      CRAMFileWriter
    • toString

      public String toString()
      Overrides:
      toString in class Object