Search in sources :

Example 6 with DataLine

use of org.broadinstitute.hellbender.utils.tsv.DataLine in project gatk-protected by broadinstitute.

the class XHMMSegmentCallerBaseIntegrationTest method writeChainInTempFile.

public static File writeChainInTempFile(final XHMMData chain) {
    final File result = createTempFile("chain-", ".tab");
    //final File result = new File("/tmp/input");
    final List<String> sampleNames = IntStream.range(0, chain.data.size()).mapToObj(a -> "SAMPLE_" + a).collect(Collectors.toList());
    final List<String> columnNames = new ArrayList<>(sampleNames.size() + 4);
    columnNames.addAll(Arrays.asList(TargetTableColumn.CONTIG.toString(), TargetTableColumn.START.toString(), TargetTableColumn.END.toString(), TargetTableColumn.NAME.toString()));
    columnNames.addAll(sampleNames);
    try (final TableWriter<Integer> writer = new TableWriter<Integer>(result, new TableColumnCollection(columnNames)) {

        @Override
        protected void composeLine(final Integer record, final DataLine dataLine) {
            dataLine.append(chain.targets.get(record).getContig()).append(chain.targets.get(record).getStart()).append(chain.targets.get(record).getEnd()).append(chain.targets.get(record).getName());
            for (final List<Double> sampleData : chain.data) {
                dataLine.append(sampleData.get(record));
            }
        }
    }) {
        writer.writeAllRecords(IntStream.range(0, chain.targets.size()).boxed().collect(Collectors.toList()));
    } catch (final IOException ex) {
        throw new UncheckedIOException(ex);
    }
    return result;
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) DataProvider(org.testng.annotations.DataProvider) org.broadinstitute.hellbender.tools.exome(org.broadinstitute.hellbender.tools.exome) Test(org.testng.annotations.Test) IOException(java.io.IOException) Random(java.util.Random) CommandLineProgramTest(org.broadinstitute.hellbender.CommandLineProgramTest) GATKException(org.broadinstitute.hellbender.exceptions.GATKException) Collectors(java.util.stream.Collectors) File(java.io.File) DataLine(org.broadinstitute.hellbender.utils.tsv.DataLine) ArrayList(java.util.ArrayList) TableWriter(org.broadinstitute.hellbender.utils.tsv.TableWriter) UncheckedIOException(java.io.UncheckedIOException) List(java.util.List) Stream(java.util.stream.Stream) CopyNumberTriState(org.broadinstitute.hellbender.tools.exome.germlinehmm.CopyNumberTriState) TableColumnCollection(org.broadinstitute.hellbender.utils.tsv.TableColumnCollection) DataLine(org.broadinstitute.hellbender.utils.tsv.DataLine) ArrayList(java.util.ArrayList) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) TableWriter(org.broadinstitute.hellbender.utils.tsv.TableWriter) File(java.io.File) TableColumnCollection(org.broadinstitute.hellbender.utils.tsv.TableColumnCollection)

Example 7 with DataLine

use of org.broadinstitute.hellbender.utils.tsv.DataLine in project gatk by broadinstitute.

the class XHMMSegmentCallerBaseIntegrationTest method writeChainInTempFile.

public static File writeChainInTempFile(final XHMMData chain) {
    final File result = createTempFile("chain-", ".tab");
    //final File result = new File("/tmp/input");
    final List<String> sampleNames = IntStream.range(0, chain.data.size()).mapToObj(a -> "SAMPLE_" + a).collect(Collectors.toList());
    final List<String> columnNames = new ArrayList<>(sampleNames.size() + 4);
    columnNames.addAll(Arrays.asList(TargetTableColumn.CONTIG.toString(), TargetTableColumn.START.toString(), TargetTableColumn.END.toString(), TargetTableColumn.NAME.toString()));
    columnNames.addAll(sampleNames);
    try (final TableWriter<Integer> writer = new TableWriter<Integer>(result, new TableColumnCollection(columnNames)) {

        @Override
        protected void composeLine(final Integer record, final DataLine dataLine) {
            dataLine.append(chain.targets.get(record).getContig()).append(chain.targets.get(record).getStart()).append(chain.targets.get(record).getEnd()).append(chain.targets.get(record).getName());
            for (final List<Double> sampleData : chain.data) {
                dataLine.append(sampleData.get(record));
            }
        }
    }) {
        writer.writeAllRecords(IntStream.range(0, chain.targets.size()).boxed().collect(Collectors.toList()));
    } catch (final IOException ex) {
        throw new UncheckedIOException(ex);
    }
    return result;
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) DataProvider(org.testng.annotations.DataProvider) org.broadinstitute.hellbender.tools.exome(org.broadinstitute.hellbender.tools.exome) Test(org.testng.annotations.Test) IOException(java.io.IOException) Random(java.util.Random) CommandLineProgramTest(org.broadinstitute.hellbender.CommandLineProgramTest) GATKException(org.broadinstitute.hellbender.exceptions.GATKException) Collectors(java.util.stream.Collectors) File(java.io.File) DataLine(org.broadinstitute.hellbender.utils.tsv.DataLine) ArrayList(java.util.ArrayList) TableWriter(org.broadinstitute.hellbender.utils.tsv.TableWriter) UncheckedIOException(java.io.UncheckedIOException) List(java.util.List) Stream(java.util.stream.Stream) CopyNumberTriState(org.broadinstitute.hellbender.tools.exome.germlinehmm.CopyNumberTriState) TableColumnCollection(org.broadinstitute.hellbender.utils.tsv.TableColumnCollection) DataLine(org.broadinstitute.hellbender.utils.tsv.DataLine) ArrayList(java.util.ArrayList) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) TableWriter(org.broadinstitute.hellbender.utils.tsv.TableWriter) File(java.io.File) TableColumnCollection(org.broadinstitute.hellbender.utils.tsv.TableColumnCollection)

Example 8 with DataLine

use of org.broadinstitute.hellbender.utils.tsv.DataLine 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);
        }
    };
}
Also used : TableWriter(org.broadinstitute.hellbender.utils.tsv.TableWriter) DataLine(org.broadinstitute.hellbender.utils.tsv.DataLine) SimpleInterval(org.broadinstitute.hellbender.utils.SimpleInterval) TableColumnCollection(org.broadinstitute.hellbender.utils.tsv.TableColumnCollection)

Example 9 with DataLine

use of org.broadinstitute.hellbender.utils.tsv.DataLine 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");
    }
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) DataProvider(org.testng.annotations.DataProvider) BiFunction(java.util.function.BiFunction) Test(org.testng.annotations.Test) Random(java.util.Random) Collectors(java.util.stream.Collectors) DataLine(org.broadinstitute.hellbender.utils.tsv.DataLine) ArrayList(java.util.ArrayList) List(java.util.List) Stream(java.util.stream.Stream) Assert(org.testng.Assert) TableColumnCollection(org.broadinstitute.hellbender.utils.tsv.TableColumnCollection) DataLine(org.broadinstitute.hellbender.utils.tsv.DataLine) TableColumnCollection(org.broadinstitute.hellbender.utils.tsv.TableColumnCollection) Test(org.testng.annotations.Test)

Example 10 with DataLine

use of org.broadinstitute.hellbender.utils.tsv.DataLine 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);
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) DataProvider(org.testng.annotations.DataProvider) BiFunction(java.util.function.BiFunction) Test(org.testng.annotations.Test) Random(java.util.Random) Collectors(java.util.stream.Collectors) DataLine(org.broadinstitute.hellbender.utils.tsv.DataLine) ArrayList(java.util.ArrayList) List(java.util.List) Stream(java.util.stream.Stream) Assert(org.testng.Assert) TableColumnCollection(org.broadinstitute.hellbender.utils.tsv.TableColumnCollection) DataLine(org.broadinstitute.hellbender.utils.tsv.DataLine) TableColumnCollection(org.broadinstitute.hellbender.utils.tsv.TableColumnCollection) Test(org.testng.annotations.Test)

Aggregations

DataLine (org.broadinstitute.hellbender.utils.tsv.DataLine)12 TableColumnCollection (org.broadinstitute.hellbender.utils.tsv.TableColumnCollection)12 ArrayList (java.util.ArrayList)6 Arrays (java.util.Arrays)6 List (java.util.List)6 Random (java.util.Random)6 Collectors (java.util.stream.Collectors)6 IntStream (java.util.stream.IntStream)6 Stream (java.util.stream.Stream)6 TableWriter (org.broadinstitute.hellbender.utils.tsv.TableWriter)6 DataProvider (org.testng.annotations.DataProvider)6 Test (org.testng.annotations.Test)6 BiFunction (java.util.function.BiFunction)4 SimpleInterval (org.broadinstitute.hellbender.utils.SimpleInterval)4 Assert (org.testng.Assert)4 File (java.io.File)2 IOException (java.io.IOException)2 UncheckedIOException (java.io.UncheckedIOException)2 CommandLineProgramTest (org.broadinstitute.hellbender.CommandLineProgramTest)2 GATKException (org.broadinstitute.hellbender.exceptions.GATKException)2