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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAlignment(SAMRecord alignment) Add an alignment record to be emitted by the writer.final voidclose()Must be called or else file will likely be defective.protected abstract voidfinish()Do any required flushing here.static intWhen writing records that are not presorted, this number determines the number of records stored in RAM before spilling to disk.protected abstract StringFor producing error messages.protected intprotected SAMFileHeader.SortOrderMust be called after calling setHeader().protected Filestatic voidsetDefaultMaxRecordsInRam(int maxRecordsInRam) When writing records that are not presorted, specify the number of records stored in RAM before spilling to disk.voidsetHeader(SAMFileHeader header) Must be called before addAlignment.protected voidsetMaxRecordsInRam(int maxRecordsInRam) When writing records that are not presorted, specify the number of records stored in RAM before spilling to disk.voidsetProgressLogger(ProgressLoggerInterface progress) Sets the progress logger used by this implementation.voidsetSortOrder(SAMFileHeader.SortOrder sortOrder, boolean presorted) Must be called before calling setHeader().voidsetSortOrderChecking(boolean check) If true writers that are writing pre-sorted records should check the order during writing.protected voidsetTempDirectory(File tmpDir) When writing records that are not presorted, specify the path of the temporary directory for spilling to disk.protected abstract voidwriteAlignment(SAMRecord alignment) Writes the record to disk.protected voidwriteHeader(SAMFileHeader header) Write the header to disk.protected abstract voidwriteHeader(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:
setProgressLoggerin 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:SAMFileWriterIf true writers that are writing pre-sorted records should check the order during writing.- Specified by:
setSortOrderCheckingin 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:
getFileHeaderin interfaceSAMFileWriter
-
addAlignment
Add an alignment record to be emitted by the writer.- Specified by:
addAlignmentin 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:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein 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.
-