/* AbstractReader * * created: July 2011 * * This file is part of Artemis * * Copyright(C) 2010 Genome Research Limited * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or(at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */ package uk.ac.sanger.artemis.components.variant; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.StringReader; import java.io.Writer; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.Hashtable; import java.util.List; import java.util.Map; import java.util.Vector; import uk.ac.sanger.artemis.FeatureVector; import uk.ac.sanger.artemis.components.variant.BCFReader.BCFReaderIterator; public abstract class AbstractVCFReader { private boolean vcf_v4 = false; protected String[] sampleNames; protected abstract String[] getSeqNames(); protected abstract String getFileName(); protected int nsamples = -1; private BCFReaderIterator bcfIterator = null; private TabixReader.Iterator tabixIterator = null; private String header; /** * Read and return the next record. * @param chr sequence name * @param sbeg start base * @param send end base * @return * @throws IOException */ public VCFRecord getNextRecord(String chr, int sbeg, int send) throws IOException { VCFRecord record; if(this instanceof BCFReader) { if(bcfIterator == null) bcfIterator = ((BCFReader)this).query(chr, sbeg, send); record = bcfIterator.next(); if(record == null) bcfIterator = null; } else { if(tabixIterator == null) { try { tabixIterator = ((TabixReader)this).query(chr+":"+sbeg+"-"+send); } catch(ArrayIndexOutOfBoundsException aob) { System.err.println(chr+":"+sbeg+"-"+send+" not found in "+((TabixReader)this).getFileName()); } } if(tabixIterator == null) return null; String s = tabixIterator.next(); if(s == null) { tabixIterator = null; return null; } record = VCFRecord.parse(s, getNumberOfSamples()); if(record == null) tabixIterator = null; } return record; } protected static int readInt(final InputStream is) throws IOException { byte[] buf = new byte[4]; is.read(buf); return ByteBuffer.wrap(buf).order(ByteOrder.LITTLE_ENDIAN).getInt(); } protected static float readFloat(final InputStream is) throws IOException { byte[] buf = new byte[4]; is.read(buf); return ByteBuffer.wrap(buf).order(ByteOrder.LITTLE_ENDIAN).getFloat(); } protected static long readLong(final InputStream is) throws IOException { byte[] buf = new byte[8]; is.read(buf); return ByteBuffer.wrap(buf).order(ByteOrder.LITTLE_ENDIAN).getLong(); } protected String getName() { if(getFileName() == null) return null; File f = new File(getFileName()); return f.getName(); } /** * Export VCF file * @param manualHash * @param vcfFileName * @param writer * @param vcfView * @param features * @throws IOException */ protected static void write(final Map manualHash, final String vcfFileName, final int vcfIndex, Writer writer, VCFview vcfView, FeatureVector features) throws IOException { // FILTER LINES if(IOUtils.isBCF(vcfFileName)) { BCFReader reader = new BCFReader(vcfFileName); // FIX for old style BAM files AbstractVCFReader readers[] = vcfView.getVcfReaders(); for(int i=0; i -1) { sampleNames = ln.substring(index+7).trim().split("[ \\t]"); nsamples = sampleNames.length; } } } } catch (IOException e) { System.err.println("Problem calculating the number of samples."); } } return nsamples; } protected String[] getSampleNames() { return sampleNames; } protected List getFORMAT() { return getListOfLines("FORMAT"); } protected List getFILTER() { return getListOfLines("FILTER"); } protected List getINFO() { return getListOfLines("INFO"); } /** * Return a list of the lines in the header. Each line is represented as a hash of * the key value pairs. * @param lineType * @return */ private List getListOfLines(String lineType) { List listOfType = new Vector(); try { BufferedReader reader = new BufferedReader(new StringReader(getHeader())); String str; while ((str = reader.readLine()) != null) { String origLine = new String(str); if (str.startsWith("##"+lineType)) { listOfType.add(new HeaderLine(origLine, lineType, getLineHash(lineType, str))); } } } catch (IOException ioe) { ioe.printStackTrace(); } return listOfType; } protected static Hashtable getLineHash(String lineType, String str) { Hashtable hash = new Hashtable(); hash.put("lineType", lineType); str = str.substring(lineType.length() + 4, str.length()-1); String parts[] = str.split(","); for(int i=0; i