use of org.broadinstitute.hellbender.utils.tsv.TableColumnCollection in project gatk by broadinstitute.
the class ReadCountCollectionUtils method writerWithIntervals.
/**
* Creates a new table writer that will output the target intervals.
* @param writer where to output the table formatted content.
* @param countColumnNames list of count column names.
* @return never {@code null}.
* @throws IOException if there is some low level IO problem creating the writer.
* @throws IllegalArgumentException if {@code countColumnNames} is {@code null}, contains
* {@code null} or a non valid count column name (e.g. a reserved word).
*/
public static TableWriter<ReadCountRecord> writerWithIntervals(final Writer writer, final List<String> countColumnNames) throws IOException {
final List<String> columnNames = new ArrayList<>();
columnNames.add(TargetTableColumn.CONTIG.toString());
columnNames.add(TargetTableColumn.START.toString());
columnNames.add(TargetTableColumn.END.toString());
columnNames.add(TargetTableColumn.NAME.toString());
columnNames.addAll(Utils.nonNull(countColumnNames));
final TableColumnCollection columns = new TableColumnCollection(columnNames);
return new TableWriter<ReadCountRecord>(writer, columns) {
@Override
protected void composeLine(final ReadCountRecord record, final DataLine dataLine) {
final SimpleInterval interval = record.getTarget().getInterval();
if (interval == null) {
throw new IllegalStateException("invalid combination of targets with and without intervals defined");
}
dataLine.append(interval.getContig()).append(interval.getStart()).append(interval.getEnd()).append(record.getTarget().getName());
record.appendCountsTo(dataLine);
}
};
}
use of org.broadinstitute.hellbender.utils.tsv.TableColumnCollection in project gatk-protected by broadinstitute.
the class ReadCountRecordUnitTest method testAppendCountsTo.
@Test(dataProvider = "testData", dependsOnMethods = "testCreation")
public void testAppendCountsTo(@SuppressWarnings("unused") final String testName, final BiFunction<Target, double[], ? extends ReadCountRecord> constructor, final int size) {
final double[] counts = generateCounts(size);
final boolean round = testName.equals("long[]");
final ReadCountRecord record = constructor.apply(TEST_TARGET, counts);
final List<String> columnNames = Stream.concat(Stream.concat(IntStream.range(0, 10).mapToObj(i -> "pre-padding_" + i), IntStream.range(0, counts.length).mapToObj(i -> "column_" + i)), IntStream.range(0, 10).mapToObj(i -> "post-padding_" + i)).collect(Collectors.toList());
final TableColumnCollection columns = new TableColumnCollection(columnNames);
final DataLine dataLine = new DataLine(columns, RuntimeException::new);
final double[] copiedCounts = new double[counts.length + 20];
Arrays.fill(copiedCounts, -11);
for (int i = 0; i < 10 + 10 + counts.length; i++) {
dataLine.append("-11");
}
dataLine.seek(10);
record.appendCountsTo(dataLine);
// Check the copied values.
if (!round) {
for (int i = 0; i < counts.length; i++) {
Assert.assertEquals(dataLine.getDouble(10 + i), counts[i], 0.0);
}
} else {
for (int i = 0; i < counts.length; i++) {
Assert.assertEquals(dataLine.getDouble(10 + i), Math.round(counts[i]), 0.00001);
}
}
// Check that the padding remains intact:
for (int i = 0; i < 10; i++) {
Assert.assertEquals(dataLine.get(i), "-11");
}
for (int i = counts.length + 10; i < copiedCounts.length; i++) {
Assert.assertEquals(dataLine.get(i), "-11");
}
}
use of org.broadinstitute.hellbender.utils.tsv.TableColumnCollection in project gatk-protected by broadinstitute.
the class ReadCountRecordUnitTest method testAppendCountsToBeyondEnd.
@Test(dataProvider = "testNonZeroCountsData", dependsOnMethods = "testAppendCountsTo", expectedExceptions = IllegalStateException.class)
public void testAppendCountsToBeyondEnd(@SuppressWarnings("unused") final String testName, final BiFunction<Target, double[], ? extends ReadCountRecord> constructor, final int size) {
final double[] counts = generateCounts(size);
final ReadCountRecord record = constructor.apply(TEST_TARGET, counts);
final List<String> columnNames = Stream.concat(Stream.concat(IntStream.range(0, 10).mapToObj(i -> "pre-padding_" + i), IntStream.range(0, counts.length).mapToObj(i -> "column_" + i)), IntStream.range(0, 10).mapToObj(i -> "post-padding_" + i)).collect(Collectors.toList());
final TableColumnCollection columns = new TableColumnCollection(columnNames);
final DataLine dataLine = new DataLine(columns, RuntimeException::new);
final double[] copiedCounts = new double[counts.length + 20];
Arrays.fill(copiedCounts, -11);
dataLine.seek(columnNames.size());
record.appendCountsTo(dataLine);
}
use of org.broadinstitute.hellbender.utils.tsv.TableColumnCollection in project gatk-protected by broadinstitute.
the class CombineReadCountsIntegrationTest method createTargetFile.
private File createTargetFile(final List<Target> targets) throws IOException {
final File result = BaseTest.createTempFile("targets", ".tab");
final TableWriter<Target> writer = TableUtils.writer(result, new TableColumnCollection(TargetTableColumn.CONTIG, TargetTableColumn.START, TargetTableColumn.END, TargetTableColumn.NAME), (t, dataLine) -> {
final SimpleInterval interval = t.getInterval();
dataLine.append(interval.getContig()).append(interval.getStart()).append(interval.getEnd()).append(t.getName());
});
for (final Target target : targets) writer.writeRecord(target);
writer.close();
return result;
}
use of org.broadinstitute.hellbender.utils.tsv.TableColumnCollection in project gatk-protected by broadinstitute.
the class CombineReadCountsIntegrationTest method createCountFile.
private void createCountFile(final File output, List<Target> targets, final String sample, final double[] count, final boolean useName, final boolean useCoordinates) throws IOException {
final List<String> columnNames = new ArrayList<>();
if (useCoordinates) {
columnNames.add(TargetTableColumn.CONTIG.toString());
columnNames.add(TargetTableColumn.START.toString());
columnNames.add(TargetTableColumn.END.toString());
}
if (useName) {
columnNames.add(TargetTableColumn.NAME.toString());
}
columnNames.add(sample);
final TableColumnCollection columns = new TableColumnCollection(columnNames);
final TableWriter<Integer> writer = TableUtils.writer(output, columns, (i, d) -> {
final Target target = targets.get(i);
final SimpleInterval interval = target.getInterval();
if (useCoordinates)
d.append(interval.getContig()).append(interval.getStart()).append(interval.getEnd());
if (useName)
d.append(target.getName());
d.append(count[i]);
});
for (int i = 0; i < targets.size(); i++) writer.writeRecord(i);
writer.close();
}
Aggregations