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 & 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
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddAlignment(SAMRecord alignment)Add an alignment record to be emitted by the writer.voidclose()Must be called or else file will likely be defective.protected abstract voidfinish()Do any required flushing here.static intgetDefaultMaxRecordsInRam()When writing records that are not presorted, this number determines the number of records stored in RAM before spilling to disk.SAMFileHeadergetFileHeader()protected abstract StringgetFilename()For producing error messages.protected intgetMaxRecordsInRam()protected SAMFileHeader.SortOrdergetSortOrder()Must be called after calling setHeader().protected FilegetTempDirectory()static 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().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.
-
-
-
Method Detail
-
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:
setProgressLoggerin interfaceSAMFileWriter
-
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.
-
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:
getFileHeaderin interfaceSAMFileWriter
-
addAlignment
public void addAlignment(SAMRecord alignment)
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
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 callswriteHeader(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.
-
-