/* * Copyright (c) 2012 The Broad Institute * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package org.broadinstitute.variant.vcf; import org.broad.tribble.readers.AsciiLineReader; import org.broad.tribble.readers.PositionalBufferedStream; import org.broadinstitute.variant.VariantBaseTest; import org.testng.Assert; import org.testng.annotations.Test; import java.io.*; import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /** * Created by IntelliJ IDEA. * User: aaron * Date: Jun 30, 2010 * Time: 3:32:08 PM * To change this template use File | Settings | File Templates. */ public class VCFHeaderUnitTest extends VariantBaseTest { private VCFHeader createHeader(String headerStr) { VCFCodec codec = new VCFCodec(); VCFHeader header = (VCFHeader)codec.readHeader(new AsciiLineReader(new PositionalBufferedStream(new StringBufferInputStream(headerStr)))); Assert.assertEquals(header.getMetaDataInInputOrder().size(), VCF4headerStringCount); return header; } @Test public void testVCF4ToVCF4() { VCFHeader header = createHeader(VCF4headerStrings); checkMD5ofHeaderFile(header, "f05a57053a0c6a5bac15dba566f7f7ff"); } @Test public void testVCF4ToVCF4_alternate() { VCFHeader header = createHeader(VCF4headerStrings_with_negativeOne); checkMD5ofHeaderFile(header, "b1d71cc94261053131f8d239d65a8c9f"); } /** * a little utility function for all tests to md5sum a file * Shameless taken from: * * http://www.javalobby.org/java/forums/t84420.html * * @param file the file * @return a string */ private static String md5SumFile(File file) { MessageDigest digest; try { digest = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("Unable to find MD5 digest"); } InputStream is; try { is = new FileInputStream(file); } catch (FileNotFoundException e) { throw new RuntimeException("Unable to open file " + file); } byte[] buffer = new byte[8192]; int read; try { while ((read = is.read(buffer)) > 0) { digest.update(buffer, 0, read); } byte[] md5sum = digest.digest(); BigInteger bigInt = new BigInteger(1, md5sum); return bigInt.toString(16); } catch (IOException e) { throw new RuntimeException("Unable to process file for MD5", e); } finally { try { is.close(); } catch (IOException e) { throw new RuntimeException("Unable to close input stream for MD5 calculation", e); } } } private void checkMD5ofHeaderFile(VCFHeader header, String md5sum) { File myTempFile = null; PrintWriter pw = null; try { myTempFile = File.createTempFile("VCFHeader","vcf"); myTempFile.deleteOnExit(); pw = new PrintWriter(myTempFile); } catch (IOException e) { Assert.fail("Unable to make a temp file!"); } for (VCFHeaderLine line : header.getMetaDataInSortedOrder()) pw.println(line); pw.close(); Assert.assertEquals(md5SumFile(myTempFile), md5sum); } public static int VCF4headerStringCount = 16; public static String VCF4headerStrings = "##fileformat=VCFv4.0\n"+ "##filedate=2010-06-21\n"+ "##reference=NCBI36\n"+ "##INFO=\n"+ "##INFO=\n"+ "##INFO=\n"+ "##INFO=\n"+ "##INFO=\n"+ "##INFO=\n"+ "##INFO=\n"+ "##INFO=\n"+ "##INFO=\n"+ "##FILTER=\n"+ "##FORMAT=\n"+ "##FORMAT=\n"+ "##FORMAT=\n"+ "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\n"; public static String VCF4headerStrings_with_negativeOne = "##fileformat=VCFv4.0\n"+ "##filedate=2010-06-21\n"+ "##reference=NCBI36\n"+ "##INFO=\n"+ "##INFO=\n"+ "##INFO=\n"+ "##INFO=\n"+ "##INFO=\n"+ "##INFO=\n"+ "##INFO=\n"+ "##INFO=\n"+ "##INFO=\n"+ "##FILTER=\n"+ "##FORMAT=\n"+ "##FORMAT=\n"+ "##FORMAT=\n"+ "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\n"; }