All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.broadinstitute.hellbender.tools.walkers.mutect.MutectStats Maven / Gradle / Ivy

The newest version!
package org.broadinstitute.hellbender.tools.walkers.mutect;

import java.nio.file.Path;
import org.broadinstitute.hellbender.exceptions.UserException;
import org.broadinstitute.hellbender.utils.Utils;
import org.broadinstitute.hellbender.utils.io.IOUtils;
import org.broadinstitute.hellbender.utils.tsv.DataLine;
import org.broadinstitute.hellbender.utils.tsv.TableColumnCollection;
import org.broadinstitute.hellbender.utils.tsv.TableReader;
import org.broadinstitute.hellbender.utils.tsv.TableWriter;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class MutectStats {
    private String statistic;
    private double value;

    public MutectStats(final String statistic, final double value) {
        this.statistic = statistic;
        this.value = value;
    }

    public String getStatistic() { return statistic; }

    public double getValue() { return value; }
    

    //----- The following two public static methods read and write contamination files
    public static void writeToFile(final List records, final File outputTable) {
        try ( MutectStats.MutectStatsWriter writer = new MutectStats.MutectStatsWriter(
            IOUtils.fileToPath(outputTable)) ) {
            writer.writeAllRecords(records);
        } catch (IOException e){
            throw new UserException(String.format("Encountered an IO exception while writing to %s.", outputTable));
        }
    }

    public static List readFromFile(final File tableFile) {
        try( MutectStats.MutectStatsReader reader = new MutectStats.MutectStatsReader(
            IOUtils.fileToPath(tableFile)) ) {
            return reader.toList();
        } catch (IOException e){
            throw new UserException(String.format("Encountered an IO exception while reading from %s.", tableFile));
        }
    }

    //-------- The following methods are boilerplate for reading and writing contamination tables
    private static class MutectStatsWriter extends TableWriter {
        private MutectStatsWriter(final Path output) throws IOException {
            super(output, MutectStats.MutectStatsColumn.COLUMNS);
        }

        @Override
        protected void composeLine(final MutectStats record, final DataLine dataLine) {
            dataLine.set(MutectStats.MutectStatsColumn.STATISTIC.toString(), record.getStatistic())
                    .set(MutectStats.MutectStatsColumn.VALUE.toString(), record.getValue());
        }
    }

    private static class MutectStatsReader extends TableReader {
        public MutectStatsReader(final Path file) throws IOException {
            super(file);
        }

        @Override
        protected MutectStats createRecord(final DataLine dataLine) {
            final String sample = dataLine.get(MutectStats.MutectStatsColumn.STATISTIC);
            final double contamination = dataLine.getDouble(MutectStats.MutectStatsColumn.VALUE);
            return new MutectStats(sample, contamination);
        }
    }

    private enum MutectStatsColumn {
        STATISTIC("statistic"),
        VALUE("value");

        private final String columnName;

        MutectStatsColumn(final String columnName) {
            this.columnName = Utils.nonNull(columnName);
        }

        @Override
        public String toString() {
            return columnName;
        }

        public static final TableColumnCollection COLUMNS = new TableColumnCollection(STATISTIC, VALUE);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy