VcfWriter { public static void writeRecord(final List<VcfSample> samples, final VcfRecord record, final PrintWriter writer) { checkNotNull(samples); checkNotNull(record); checkNotNull(writer); StringBuilder sb = new StringBuilder(); sb.append(record.getChrom()); sb.append("\t"); sb.append(record.getPos()); sb.append("\t"); if (record.getId().length == 0) { sb.append("."); } else { sb.append(Joiner.on(";").join(record.getId())); } sb.append("\t"); sb.append(record.getRef()); sb.append("\t"); sb.append(Joiner.on(",").join(record.getAlt())); sb.append("\t"); if (Double.isNaN(record.getQual())) { sb.append("."); } else { sb.append((int) record.getQual()); } sb.append("\t"); sb.append(Joiner.on(";").join(record.getFilter())); sb.append("\t"); if (record.getInfo().isEmpty()) { sb.append("."); } else { sb.append(Joiner.on(";").withKeyValueSeparator("=").join(record.getInfo().asMap())); } if (!samples.isEmpty()) { sb.append("\t"); sb.append(Joiner.on(":").join(record.getFormat())); for (VcfSample sample : samples) { sb.append("\t"); List<String> values = new ArrayList<String>(); for (String formatId : record.getFormat()) { List<String> fieldValues = record.getGenotypes().get(sample.getId()).getFields().get(formatId); values.add(fieldValues.isEmpty() ? "." : Joiner.on(",").join(fieldValues)); } sb.append(Joiner.on(":").join(values)); } } writer.println(sb.toString()); } private  VcfWriter(); static void write(final VcfHeader header,
                             final List<VcfSample> samples,
                             final List<VcfRecord> records,
                             final PrintWriter writer); static void writeHeader(final VcfHeader header, final PrintWriter writer); static void writeColumnHeader(final List<VcfSample> samples, final PrintWriter writer); static void writeRecords(final List<VcfSample> samples,
                                    final List<VcfRecord> records,
                                    final PrintWriter writer); static void writeRecord(final List<VcfSample> samples, final VcfRecord record, final PrintWriter writer);  }
@Test(expected=NullPointerException.class) public void testWriteRecordNullSamples() throws Exception { writeRecord(null, record, writer); }
@Test(expected=NullPointerException.class) public void testWriteRecordNullRecord() throws Exception { writeRecord(samples, null, writer); }
@Test(expected=NullPointerException.class) public void testWriteRecordNullWriter() throws Exception { writeRecord(samples, record, null); }
@Test public void testWriteRecord() throws Exception { writeRecord(samples, record, writer); writer.close(); assertEquals("22\t16140370\trs2096606\tA\tG\t100\tPASS\t.\tGT\t1|1" + System.lineSeparator(), outputStream.toString()); }
@Test public void testWriteRecordMissingQual() throws Exception { VcfRecord missingQual = new VcfRecord(3L, "22", 16140370L, new String[] { "rs2096606" }, "A", new String[] { "G" }, Double.NaN, new String[] { "PASS" }, info, new String[] { "GT" }, genotypes); writeRecord(samples, missingQual, writer); writer.close(); assertEquals("22\t16140370\trs2096606\tA\tG\t.\tPASS\t.\tGT\t1|1" + System.lineSeparator(), outputStream.toString()); }
@Test public void testWriteRecordMissingId() throws Exception { VcfRecord missingId = new VcfRecord(3L, "22", 16140370L, new String[0], "A", new String[] { "G" }, Double.NaN, new String[] { "PASS" }, info, new String[] { "GT" }, genotypes); writeRecord(samples, missingId, writer); writer.close(); assertEquals("22\t16140370\t.\tA\tG\t.\tPASS\t.\tGT\t1|1" + System.lineSeparator(), outputStream.toString()); }
@Test public void testWriteRecordGenotypeFields() throws Exception { Map<String, VcfGenotype> genotypes = new HashMap<String, VcfGenotype>(); VcfGenotype genotype = VcfGenotype.builder().withField("GT", "1|1").withField("DS", "0.000").withField("GL", "-0.02", "-1.38", "-5.00").build(); genotypes.put("NA19131", genotype); VcfRecord genotypeFields = new VcfRecord(3L, "22", 16140370L, new String[] { "rs2096606" }, "A", new String[] { "G" }, Double.NaN, new String[] { "PASS" }, info, new String[] { "GT", "DS", "GL" }, genotypes); writeRecord(samples, genotypeFields, writer); writer.close(); assertEquals("22\t16140370\trs2096606\tA\tG\t.\tPASS\t.\tGT:DS:GL\t1|1:0.000:-0.02,-1.38,-5.00" + System.lineSeparator(), outputStream.toString()); }
@Test public void testWriteRecordMissingGenotypeField() throws Exception { Map<String, VcfGenotype> genotypes = new HashMap<String, VcfGenotype>(); VcfGenotype genotype = VcfGenotype.builder().withField("GT", "1|1").withField("GL", "-0.02", "-1.38", "-5.00").build(); genotypes.put("NA19131", genotype); VcfRecord genotypeFields = new VcfRecord(3L, "22", 16140370L, new String[] { "rs2096606" }, "A", new String[] { "G" }, Double.NaN, new String[] { "PASS" }, info, new String[] { "GT", "DS", "GL" }, genotypes); writeRecord(samples, genotypeFields, writer); writer.close(); assertEquals("22\t16140370\trs2096606\tA\tG\t.\tPASS\t.\tGT:DS:GL\t1|1:.:-0.02,-1.38,-5.00" + System.lineSeparator(), outputStream.toString()); }