// Path: src/main/java/org/drpowell/tabix/TabixIndex.java
// public static class TabixConfig {
// 	public final int preset, seqCol, beginCol, endCol, commentChar, linesToSkip;
// 	public final String commentString;
// 	public TabixConfig (int p, int sc, int bc, int ec, int comment, int skipLines) {
// 		preset = p; seqCol = sc; beginCol = bc; endCol = ec; commentChar = comment; linesToSkip = skipLines; 
// 		commentString = Character.toString((char) commentChar);
// 	}
// 	public static final TabixConfig GFF = new TabixConfig(0, 1, 4, 5, '#', 0);
//     public static final TabixConfig BED = new TabixConfig(TBX_FLAG_UCSC, 1, 2, 3, '#', 0);
//     public static final TabixConfig PSLTBL = new TabixConfig(TBX_FLAG_UCSC, 15, 17, 18, '#', 0);
//     public static final TabixConfig SAM = new TabixConfig(TBX_PRESET_SAM, 3, 4, 0, '@', 0);
//     public static final TabixConfig VCF = new TabixConfig(TBX_PRESET_VCF, 1, 2, 0, '#', 0);
// }
// 
// Path: src/main/java/org/drpowell/util/LineIterator.java
// public class LineIterator extends AbstractPeekableIterator<String> {
// 	public final BufferedReader reader;
// 	
// 	public boolean handleException(Exception e) {
// 		Logger.getGlobal().log(Level.WARNING, e.toString());
// 		return true;
// 	}
// 	
// 	public LineIterator(BufferedReader reader) {
// 		this.reader = reader;
// 	}
// 
// 	@Override
// 	protected String computeNext() {
// 		String next = null;
// 		try {
// 			next = reader.readLine();
// 		} catch (IOException e) {
// 			if (!handleException(e)) {
// 				return computeNext();
// 			}
// 		}
// 		return next == null ? endOfData() : next;
// 	}
// 
// }


import java.util.List;
import net.sf.samtools.util.BlockCompressedFilePointerUtil;
import net.sf.samtools.util.BlockCompressedInputStream;
import net.sf.samtools.util.BlockCompressedOutputStream;
import org.drpowell.tabix.TabixIndex.TabixConfig;
import org.drpowell.util.LineIterator;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
				bin.add(chunk);
			}
		}
		
		// process linear index
		int startWindow = LinearIndex.convertToLinearIndexOffset(intv.getBegin());
		int endWindow = LinearIndex.convertToLinearIndexOffset(intv.getEnd());
		for (int win = startWindow; win <= endWindow; win++) {
			if (currLinearIndex.getPrimitive(win) == 0 || chunk.begin < currLinearIndex.getPrimitive(win)) {
				currLinearIndex.setPrimitive(win, chunk.begin);
			}
		}
	}
	
	private void finishPrevChromosome(int tidPrev) {
		if (currBinningIndex.isEmpty()) return; // saw nothing for this reference
		
		tabix.linearIndex.set(tidPrev, currLinearIndex.getCompacted());
		currLinearIndex.clear();
		
		// make things as compact as possible...
		tabix.binningIndex.set(tidPrev, new BinIndex(currBinningIndex));
		currBinningIndex = new BinIndex();
	}
	
	public void finish() throws IOException {
		finishPrevChromosome(tidCurr);
	}
	
	public static TabixIndex buildIndex(BufferedReader reader, String compressedFilename, TabixConfig config) throws IOException {
// Path: src/main/java/org/drpowell/tabix/TabixIndex.java
// public static class TabixConfig {
// 	public final int preset, seqCol, beginCol, endCol, commentChar, linesToSkip;
// 	public final String commentString;
// 	public TabixConfig (int p, int sc, int bc, int ec, int comment, int skipLines) {
// 		preset = p; seqCol = sc; beginCol = bc; endCol = ec; commentChar = comment; linesToSkip = skipLines; 
// 		commentString = Character.toString((char) commentChar);
// 	}
// 	public static final TabixConfig GFF = new TabixConfig(0, 1, 4, 5, '#', 0);
//     public static final TabixConfig BED = new TabixConfig(TBX_FLAG_UCSC, 1, 2, 3, '#', 0);
//     public static final TabixConfig PSLTBL = new TabixConfig(TBX_FLAG_UCSC, 15, 17, 18, '#', 0);
//     public static final TabixConfig SAM = new TabixConfig(TBX_PRESET_SAM, 3, 4, 0, '@', 0);
//     public static final TabixConfig VCF = new TabixConfig(TBX_PRESET_VCF, 1, 2, 0, '#', 0);
// }
// 
// Path: src/main/java/org/drpowell/util/LineIterator.java
// public class LineIterator extends AbstractPeekableIterator<String> {
// 	public final BufferedReader reader;
// 	
// 	public boolean handleException(Exception e) {
// 		Logger.getGlobal().log(Level.WARNING, e.toString());
// 		return true;
// 	}
// 	
// 	public LineIterator(BufferedReader reader) {
// 		this.reader = reader;
// 	}
// 
// 	@Override
// 	protected String computeNext() {
// 		String next = null;
// 		try {
// 			next = reader.readLine();
// 		} catch (IOException e) {
// 			if (!handleException(e)) {
// 				return computeNext();
// 			}
// 		}
// 		return next == null ? endOfData() : next;
// 	}
// 
// }


// Path: src/main/java/org/drpowell/tabix/TabixBuilder.java
import java.util.List;
import net.sf.samtools.util.BlockCompressedFilePointerUtil;
import net.sf.samtools.util.BlockCompressedInputStream;
import net.sf.samtools.util.BlockCompressedOutputStream;
import org.drpowell.tabix.TabixIndex.TabixConfig;
import org.drpowell.util.LineIterator;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;

				bin.add(chunk);
			}
		}
		
		// process linear index
		int startWindow = LinearIndex.convertToLinearIndexOffset(intv.getBegin());
		int endWindow = LinearIndex.convertToLinearIndexOffset(intv.getEnd());
		for (int win = startWindow; win <= endWindow; win++) {
			if (currLinearIndex.getPrimitive(win) == 0 || chunk.begin < currLinearIndex.getPrimitive(win)) {
				currLinearIndex.setPrimitive(win, chunk.begin);
			}
		}
	}
	
	private void finishPrevChromosome(int tidPrev) {
		if (currBinningIndex.isEmpty()) return; // saw nothing for this reference
		
		tabix.linearIndex.set(tidPrev, currLinearIndex.getCompacted());
		currLinearIndex.clear();
		
		// make things as compact as possible...
		tabix.binningIndex.set(tidPrev, new BinIndex(currBinningIndex));
		currBinningIndex = new BinIndex();
	}
	
	public void finish() throws IOException {
		finishPrevChromosome(tidCurr);
	}
	
	public static TabixIndex buildIndex(BufferedReader reader, String compressedFilename, TabixConfig config) throws IOException {
		return buildIndex(new LineIterator(reader), compressedFilename, config);