Class TableWriter<R>
- Type Parameters:
R
- the row record type.
- All Implemented Interfaces:
Closeable
,AutoCloseable
- Direct Known Subclasses:
AltSiteRecord.AltSiteRecordTableWriter
,ArtifactPrior.ArtifactPriorTableWriter
,CheckReferenceCompatibility.CheckReferenceCompatibilityTableWriter
,CompareReferences.CompareReferencesOutputTableWriter
,CompareReferences.FindSNPsOnlyTableWriter
,ConcordanceSummaryRecord.Writer
,GeneExpressionEvaluation.FragmentCountWriter
,InfoConcordanceRecord.InfoConcordanceWriter
,NormalArtifactRecord.NormalArtifactWriter
,PileupSummary.PileupSummaryTableWriter
The column (and they names) are passed in the constructor parameter along the output path
or writer
.
Extending classes must indicate how we can transcribe row record or type TableWriter
to the corresponding
record data-line in the output by overriding composeLine(R,DataLine)
.
Example:
public class Person { public final String name; public final int age; public final double netWorth; } public class PeopleTableWriter extends TableWriter<Person> { public MyRecordWriter(final File file) { super(file, new TableColumnCollection("name","age","net.worth")); } @Override protected void dataLine(final Person person, final DataLine dataLine) { dataLine.setAll(person.name, "" + person.age, "" + person.netWorth); } }
You must use the DataLine
instance passed and no other.
Instead of passing all the values as converted string in column order you may opt to use DataLine.set(java.lang.String, java.lang.String)
method family to set values one by one using the column index or column name like so:
Example (using the column index):
@Override protected void composeLine(final Person person, final DataLine dataLine) { dataLine .set(0,person.name) .set(1,person.age) .set(2,person.netWorth); }
Example (using column names):
@Override protected void composeLine(final Person person, final DataLine dataLine) { dataLine .set("name",person.name) .set("age",person.age) .set("net.worth",person.netWorth); }Notice that you don't need to explicitly convert neither the age nor the net-worth into a string thanks to
set
various overloads.
Alternatively, if you know the column order, that should quite often the case, you can avoid
indexing all together using append
operations instead:
@Override protected void composeLine(final Person person, final DataLine dataLine) { dataLine .append(person.name) .append(person.age) .append(person.netWorth); }
At any time the implementation can query the correspondence between column names and position within the data-line
by querying the TableColumnCollection
object directly that can be obtained from the dataLine's columns
field.
Example (using column names):
@Override protected void composeLine(final Person person, final DataLine dataLine) { dataLine .set("name",person.name) .set("age",person.age); if (dataLine.columns().contains("net.worth")) dataLine.set("net.worth",person.netWorth); }
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionTableWriter
(Writer writer, TableColumnCollection columns) Creates a new table writer given the destination writer and column names.TableWriter
(Path path, TableColumnCollection tableColumns) Creates a new table writer given the file and column names. -
Method Summary
Modifier and TypeMethodDescriptionfinal void
close()
protected abstract void
composeLine
(R record, DataLine dataLine) Composes the data-line to write into the output to represent a given recordfinal void
flush()
Pushes in-memory buffered content to the output stream.final void
writeAllRecords
(Iterable<R> records) Write all the records in aIterable
.final void
writeComment
(String comment) Writes a comment into the output.void
Writes the header if it has not been written already.final void
writeMetadata
(String key, String value) void
writeRecord
(R record) Writes a new record.
-
Field Details
-
METADATA_TAG
- See Also:
-
-
Constructor Details
-
TableWriter
Creates a new table writer given the file and column names.- Parameters:
path
- the destination path.tableColumns
- the table column names.- Throws:
IllegalArgumentException
- if eitherfile
ortableColumns
arenull
.IOException
- if one was raised when opening the the destination file for writing.
-
TableWriter
Creates a new table writer given the destination writer and column names.- Parameters:
writer
- the destination writer.columns
- the table column names.- Throws:
IllegalArgumentException
- if eitherwriter
orcolumns
arenull
.IOException
- if one was raised when opening the the destination file for writing.
-
-
Method Details
-
writeComment
Writes a comment into the output.This can be invoked at any time; comment lines can be present anywhere in the file.
Comments written before any record, will be output
- Parameters:
comment
- the comment to write out.- Throws:
IllegalArgumentException
- ifcomment
isnull
.IOException
- if any was raised by this operation.
-
writeMetadata
- Throws:
IOException
-
writeRecord
Writes a new record.- Parameters:
record
- the record to write.- Throws:
IOException
- if it was raised when writing the record.ClassCastException
- ifrecord
is of the correct type for this writer.IllegalArgumentException
- ifrecord
isnull
or it is not a valid record as per the implementation of this writer (seecomposeLine(R, org.broadinstitute.hellbender.utils.tsv.DataLine)
).
-
writeAllRecords
Write all the records in aIterable
.Records are written in the order they appear in the input
Iterable
.- Parameters:
records
- to write.- Throws:
IOException
- if any raised when writing any of the records.ClassCastException
- ifrecord
is of the correct type for this writer.IllegalArgumentException
- ifrecords
isnull
or it contains some values that would cause such an exception whenwriteRecord(R)
is call on that value. Previous record in the iterable would have been already written by then.
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
flush
Pushes in-memory buffered content to the output stream.- Throws:
IOException
-
writeHeaderIfApplies
Writes the header if it has not been written already.The header is written automatically before the first record is written or when the writer is closed and no record was written.
Comments written using
writeComment(java.lang.String)
before any record will precede the header unless you invoke your method first.Once the header line has been written, invoking this method does not have any effect.
- Throws:
IOException
- if any raised when writing into the destination writer.
-
composeLine
Composes the data-line to write into the output to represent a given recordAlso the first element cannot contain the
comment prefix
. If that is a genuine valid value for the first column you shall consider to re-order the columns or change the encoding of the first column to avoid this issue.Both inputs,
record
anddataLine
are guaranteed not to benull
s.- Parameters:
record
- the record to write into the data-line.dataLine
- the destination data-line object.- Throws:
ClassCastException
- ifrecord
is of the correct type for this writer.IllegalArgumentException
- if there is some conversion issue that does not allow the current write to generate a valid string array to encode the record.
-