Package htsjdk.samtools

# Class SAMFileWriterImpl

java.lang.Object
htsjdk.samtools.SAMFileWriterImpl

All Implemented Interfaces:
  `SAMFileWriter`, `Closeable`, `AutoCloseable`

Direct Known Subclasses:
  `BAMFileWriter`, `CRAMFileWriter`, `SAMTextWriter`

public abstract class SAMFileWriterImpl extends Object implements SAMFileWriter
Base class for implementing SAM writer with any underlying format. Mostly this manages accumulation invalid input: '&' sorting of SAMRecords when appropriate, and produces the text version of the header, since that seems to be a popular item in both text and binary file formats.

## Constructor Summary

Constructors
Constructor
Description
`SAMFileWriterImpl()`

## Method Summary

Modifier and Type
Method
Description
`void`
`addAlignment(SAMRecord alignment)`
Add an alignment record to be emitted by the writer.
`final void`
`close()`
Must be called or else file will likely be defective.
`protected abstract void`
`finish()`
Do any required flushing here.
`static int`
`getDefaultMaxRecordsInRam()`
When writing records that are not presorted, this number determines the number of records stored in RAM before spilling to disk.
`SAMFileHeader`
`getFileHeader()`
`protected abstract String`
`getFilename()`
For producing error messages.
`protected int`
`getMaxRecordsInRam()`
`protected SAMFileHeader.SortOrder`
`getSortOrder()`
Must be called after calling setHeader().
`protected File`
`getTempDirectory()`
`static void`
`setDefaultMaxRecordsInRam(int maxRecordsInRam)`
When writing records that are not presorted, specify the number of records stored in RAM before spilling to disk.
`void`
`setHeader(SAMFileHeader header)`
Must be called before addAlignment.
`protected void`
`setMaxRecordsInRam(int maxRecordsInRam)`
When writing records that are not presorted, specify the number of records stored in RAM before spilling to disk.
`void`
`setProgressLogger(ProgressLoggerInterface progress)`
Sets the progress logger used by this implementation.
`void`
`setSortOrder(SAMFileHeader.SortOrder sortOrder, boolean presorted)`
Must be called before calling setHeader().
`void`
`setSortOrderChecking(boolean check)`
If true writers that are writing pre-sorted records should check the order during writing.
`protected void`
`setTempDirectory(File tmpDir)`
When writing records that are not presorted, specify the path of the temporary directory for spilling to disk.
`protected abstract void`
`writeAlignment(SAMRecord alignment)`
Writes the record to disk.
`protected void`
`writeHeader(SAMFileHeader header)`
Write the header to disk.
`protected abstract void`
`writeHeader(String textHeader)`
Deprecated.
since 06/2018.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### SAMFileWriterImpl

public SAMFileWriterImpl()

## Method Details

### setDefaultMaxRecordsInRam

public static void setDefaultMaxRecordsInRam(int maxRecordsInRam)
When writing records that are not presorted, specify the number of records stored in RAM before spilling to disk. This method sets the default value for all SamFileWriterImpl instances. Must be called before the constructor is called.

Parameters:
  `maxRecordsInRam` -

### getDefaultMaxRecordsInRam

public static int getDefaultMaxRecordsInRam()
When writing records that are not presorted, this number determines the number of records stored in RAM before spilling to disk.

Returns:
  DEAFULT_MAX_RECORDS_IN_RAM

### setProgressLogger

public void setProgressLogger(ProgressLoggerInterface progress)
Sets the progress logger used by this implementation. Setting this lets this writer emit log messages as SAM records in a SortingCollection are being written to disk.

Specified by:
  `setProgressLogger` in interface `SAMFileWriter`

### setSortOrder

public void setSortOrder(SAMFileHeader.SortOrder sortOrder, boolean presorted)
Must be called before calling setHeader(). SortOrder value in the header passed to setHeader() is ignored. If setSortOrder is not called, default is SortOrder.unsorted.

### setSortOrderChecking

public void setSortOrderChecking(boolean check)
Description copied from interface: `SAMFileWriter`
If true writers that are writing pre-sorted records should check the order during writing.

Specified by:
  `setSortOrderChecking` in interface `SAMFileWriter`

### getSortOrder

protected SAMFileHeader.SortOrder getSortOrder()
Must be called after calling setHeader().

### setMaxRecordsInRam

protected void setMaxRecordsInRam(int maxRecordsInRam)
When writing records that are not presorted, specify the number of records stored in RAM before spilling to disk. Must be called before setHeader().

Parameters:
  `maxRecordsInRam` -

### getMaxRecordsInRam

protected int getMaxRecordsInRam()

### setTempDirectory

protected void setTempDirectory(File tmpDir)
When writing records that are not presorted, specify the path of the temporary directory for spilling to disk. Must be called before setHeader().

Parameters:
  `tmpDir` - path to the temporary directory

### getTempDirectory

protected File getTempDirectory()

### setHeader

public void setHeader(SAMFileHeader header)
Must be called before addAlignment. Header cannot be null.

### getFileHeader

public SAMFileHeader getFileHeader()

Specified by:
  `getFileHeader` in interface `SAMFileWriter`

### addAlignment

public void addAlignment(SAMRecord alignment)
Add an alignment record to be emitted by the writer.

Specified by:
  `addAlignment` in interface `SAMFileWriter`
Parameters:
  `alignment` - Must not be null. The record will be updated to use the header used by this writer, which will in turn cause any unresolved reference and mate reference indices to be resolved against the header's sequence dictionary.
Throws:
  `IllegalArgumentException` - if the record's reference or mate reference indices cannot be resolved against the writer's header using the current reference and mate reference names

### close

public final void close()
Must be called or else file will likely be defective.

Specified by:
  `close` in interface `AutoCloseable`
Specified by:
  `close` in interface `Closeable`
Specified by:
  `close` in interface `SAMFileWriter`

### writeAlignment

protected abstract void writeAlignment(SAMRecord alignment)
Writes the record to disk. Sort order has been taken care of by the time this method is called. The record must hava a non-null SAMFileHeader.

Parameters:
  `alignment` -

### writeHeader

@Deprecated protected abstract void writeHeader(String textHeader)
Deprecated.
since 06/2018. `writeHeader(SAMFileHeader)` is preferred for avoid String construction if not need it.
Write the header to disk. Header object is available via getHeader().

Parameters:
  `textHeader` - for convenience if the implementation needs it.

### writeHeader

protected void writeHeader(SAMFileHeader header)
Write the header to disk. Header object is available via getHeader().

IMPORTANT: this method will be abstract once `writeHeader(String)` is removed.

Note: default implementation uses `SAMTextHeaderCodec.encode(java.io.Writer, htsjdk.samtools.SAMFileHeader)` and calls `writeHeader(String)`.

Parameters:
  `header` - object to write.

### finish

protected abstract void finish()
Do any required flushing here.

### getFilename

protected abstract String getFilename()
For producing error messages.

Returns:
  Output filename, or null if there isn't one.