1 package org.broadinstitute.hellbender.tools.walkers.mutect;
2 
3 import java.nio.file.Path;
4 import org.broadinstitute.hellbender.exceptions.UserException;
5 import org.broadinstitute.hellbender.utils.Utils;
6 import org.broadinstitute.hellbender.utils.io.IOUtils;
7 import org.broadinstitute.hellbender.utils.tsv.DataLine;
8 import org.broadinstitute.hellbender.utils.tsv.TableColumnCollection;
9 import org.broadinstitute.hellbender.utils.tsv.TableReader;
10 import org.broadinstitute.hellbender.utils.tsv.TableWriter;
11 
12 import java.io.File;
13 import java.io.IOException;
14 import java.util.List;
15 
16 public class MutectStats {
17     private String statistic;
18     private double value;
19 
MutectStats(final String statistic, final double value)20     public MutectStats(final String statistic, final double value) {
21         this.statistic = statistic;
22         this.value = value;
23     }
24 
getStatistic()25     public String getStatistic() { return statistic; }
26 
getValue()27     public double getValue() { return value; }
28 
29 
30     //----- The following two public static methods read and write contamination files
writeToFile(final List<MutectStats> records, final File outputTable)31     public static void writeToFile(final List<MutectStats> records, final File outputTable) {
32         try ( MutectStats.MutectStatsWriter writer = new MutectStats.MutectStatsWriter(
33             IOUtils.fileToPath(outputTable)) ) {
34             writer.writeAllRecords(records);
35         } catch (IOException e){
36             throw new UserException(String.format("Encountered an IO exception while writing to %s.", outputTable));
37         }
38     }
39 
readFromFile(final File tableFile)40     public static List<MutectStats> readFromFile(final File tableFile) {
41         try( MutectStats.MutectStatsReader reader = new MutectStats.MutectStatsReader(
42             IOUtils.fileToPath(tableFile)) ) {
43             return reader.toList();
44         } catch (IOException e){
45             throw new UserException(String.format("Encountered an IO exception while reading from %s.", tableFile));
46         }
47     }
48 
49     //-------- The following methods are boilerplate for reading and writing contamination tables
50     private static class MutectStatsWriter extends TableWriter<MutectStats> {
MutectStatsWriter(final Path output)51         private MutectStatsWriter(final Path output) throws IOException {
52             super(output, MutectStats.MutectStatsColumn.COLUMNS);
53         }
54 
55         @Override
composeLine(final MutectStats record, final DataLine dataLine)56         protected void composeLine(final MutectStats record, final DataLine dataLine) {
57             dataLine.set(MutectStats.MutectStatsColumn.STATISTIC.toString(), record.getStatistic())
58                     .set(MutectStats.MutectStatsColumn.VALUE.toString(), record.getValue());
59         }
60     }
61 
62     private static class MutectStatsReader extends TableReader<MutectStats> {
MutectStatsReader(final Path file)63         public MutectStatsReader(final Path file) throws IOException {
64             super(file);
65         }
66 
67         @Override
createRecord(final DataLine dataLine)68         protected MutectStats createRecord(final DataLine dataLine) {
69             final String sample = dataLine.get(MutectStats.MutectStatsColumn.STATISTIC);
70             final double contamination = dataLine.getDouble(MutectStats.MutectStatsColumn.VALUE);
71             return new MutectStats(sample, contamination);
72         }
73     }
74 
75     private enum MutectStatsColumn {
76         STATISTIC("statistic"),
77         VALUE("value");
78 
79         private final String columnName;
80 
MutectStatsColumn(final String columnName)81         MutectStatsColumn(final String columnName) {
82             this.columnName = Utils.nonNull(columnName);
83         }
84 
85         @Override
toString()86         public String toString() {
87             return columnName;
88         }
89 
90         public static final TableColumnCollection COLUMNS = new TableColumnCollection(STATISTIC, VALUE);
91     }
92 }
93