Package htsjdk.samtools
Class SAMFileWriterImpl
java.lang.Object
htsjdk.samtools.SAMFileWriterImpl
- All Implemented Interfaces:
SAMFileWriter
,Closeable
,AutoCloseable
- Direct Known Subclasses:
BAMFileWriter
,CRAMFileWriter
,SAMTextWriter
Base class for implementing SAM writer with any underlying format.
Mostly this manages accumulation & 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
-
Method Summary
Modifier and TypeMethodDescriptionvoid
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
When writing records that are not presorted, this number determines the number of records stored in RAM before spilling to disk.protected abstract String
For producing error messages.protected int
protected SAMFileHeader.SortOrder
Must be called after calling setHeader().protected File
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.
-
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
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 interfaceSAMFileWriter
-
setSortOrder
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 interfaceSAMFileWriter
-
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
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
-
setHeader
Must be called before addAlignment. Header cannot be null. -
getFileHeader
- Specified by:
getFileHeader
in interfaceSAMFileWriter
-
addAlignment
Add an alignment record to be emitted by the writer.- Specified by:
addAlignment
in interfaceSAMFileWriter
- 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 interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceSAMFileWriter
-
writeAlignment
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.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
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 callswriteHeader(String)
.- Parameters:
header
- object to write.
-
finish
protected abstract void finish()Do any required flushing here. -
getFilename
For producing error messages.- Returns:
- Output filename, or null if there isn't one.
-