// Path: src/main/java/genepi/imputationserver/steps/vcf/VcfLiftOverFast.java
// public class VcfLiftOverFast {
// 
// 	private static final int MAX_RECORDS_IN_RAM = 1000;
// 
// 	public static Vector<String> liftOver(String input, String output, String chainFile, String tempDir)
// 			throws IOException {
// 
// 		LineReader reader = new LineReader(input);
// 
// 		LiftOver liftOver = new LiftOver(new File(chainFile));
// 
// 		Vector<String> errors = new Vector<String>();
// 
// 		SortingCollection<VcfLine> sorter = VcfLineSortingCollection.newInstance(MAX_RECORDS_IN_RAM, tempDir);
// 
// 		BGzipLineWriter writer = new BGzipLineWriter(output);
// 		while (reader.next()) {
// 			String line = reader.get();
// 			if (line.startsWith("#")) {
// 				writer.write(line);
// 			} else {
// 
// 				VcfLine vcfLine = new VcfLine(line);
// 				String contig = "";
// 				String newContig = "";
// 
// 				if (vcfLine.getContig().equals("chr23")) {
// 					vcfLine.setContig("chrX");
// 				}
// 				if (vcfLine.getContig().equals("23")) {
// 					vcfLine.setContig("X");
// 				}
// 
// 				if (vcfLine.getContig().startsWith("chr")) {
// 					contig = vcfLine.getContig();
// 					newContig = vcfLine.getContig().replaceAll("chr", "");
// 
// 				} else {
// 					contig = "chr" + vcfLine.getContig();
// 					newContig = "chr" + vcfLine.getContig();
// 				}
// 				Interval source = new Interval(contig, vcfLine.getPosition(), vcfLine.getPosition() + 1, false,
// 						vcfLine.getContig() + ":" + vcfLine.getPosition());
// 				Interval target = liftOver.liftOver(source);
// 				if (target != null) {
// 					if (source.getContig().equals(target.getContig())) {
// 						vcfLine.setContig(newContig);
// 						vcfLine.setPosition(target.getStart());
// 						sorter.add(vcfLine);
// 					} else {
// 						errors.add(vcfLine.getContig() + ":" + vcfLine.getPosition() + "\t" + "LiftOver" + "\t"
// 								+ "On different chromosome after LiftOver. SNP removed.");
// 					}
// 				} else {
// 					errors.add(vcfLine.getContig() + ":" + vcfLine.getPosition() + "\t" + "LiftOver" + "\t"
// 							+ "LiftOver failed. SNP removed.");
// 				}
// 			}
// 
// 		}
// 		reader.close();
// 		sorter.doneAdding();
// 
// 		int pos = -1;
// 		for (VcfLine vcfLine : sorter) {
// 			if (vcfLine.getPosition() < pos) {
// 				throw new IOException("Sorting VCF file after Liftover failed.");
// 			}
// 			writer.write(vcfLine.getLine());
// 			pos = vcfLine.getPosition();
// 		}
// 		writer.close();
// 		sorter.cleanup();
// 
// 		return errors;
// 	}
// 
// }


import genepi.base.Tool;
import genepi.imputationserver.steps.vcf.VcfLiftOverFast;
package genepi.imputationserver.tools;


public class VcfLiftOverTool extends Tool {

	public VcfLiftOverTool(String[] args) {
		super(args);
	}

	@Override
	public void createParameters() {
		addParameter("input", "input vcf file");
		addParameter("output", "output vcf file");
		addParameter("chain", "chain file");
	}

	@Override
	public void init() {
		System.out.println("VcfLiftOver Tool");
		System.out.println("");
	}

	@Override
	public int run() {

		String input = getValue("input").toString();
		String output = getValue("output").toString();
		String chain = getValue("chain").toString();

		try {
// Path: src/main/java/genepi/imputationserver/steps/vcf/VcfLiftOverFast.java
// public class VcfLiftOverFast {
// 
// 	private static final int MAX_RECORDS_IN_RAM = 1000;
// 
// 	public static Vector<String> liftOver(String input, String output, String chainFile, String tempDir)
// 			throws IOException {
// 
// 		LineReader reader = new LineReader(input);
// 
// 		LiftOver liftOver = new LiftOver(new File(chainFile));
// 
// 		Vector<String> errors = new Vector<String>();
// 
// 		SortingCollection<VcfLine> sorter = VcfLineSortingCollection.newInstance(MAX_RECORDS_IN_RAM, tempDir);
// 
// 		BGzipLineWriter writer = new BGzipLineWriter(output);
// 		while (reader.next()) {
// 			String line = reader.get();
// 			if (line.startsWith("#")) {
// 				writer.write(line);
// 			} else {
// 
// 				VcfLine vcfLine = new VcfLine(line);
// 				String contig = "";
// 				String newContig = "";
// 
// 				if (vcfLine.getContig().equals("chr23")) {
// 					vcfLine.setContig("chrX");
// 				}
// 				if (vcfLine.getContig().equals("23")) {
// 					vcfLine.setContig("X");
// 				}
// 
// 				if (vcfLine.getContig().startsWith("chr")) {
// 					contig = vcfLine.getContig();
// 					newContig = vcfLine.getContig().replaceAll("chr", "");
// 
// 				} else {
// 					contig = "chr" + vcfLine.getContig();
// 					newContig = "chr" + vcfLine.getContig();
// 				}
// 				Interval source = new Interval(contig, vcfLine.getPosition(), vcfLine.getPosition() + 1, false,
// 						vcfLine.getContig() + ":" + vcfLine.getPosition());
// 				Interval target = liftOver.liftOver(source);
// 				if (target != null) {
// 					if (source.getContig().equals(target.getContig())) {
// 						vcfLine.setContig(newContig);
// 						vcfLine.setPosition(target.getStart());
// 						sorter.add(vcfLine);
// 					} else {
// 						errors.add(vcfLine.getContig() + ":" + vcfLine.getPosition() + "\t" + "LiftOver" + "\t"
// 								+ "On different chromosome after LiftOver. SNP removed.");
// 					}
// 				} else {
// 					errors.add(vcfLine.getContig() + ":" + vcfLine.getPosition() + "\t" + "LiftOver" + "\t"
// 							+ "LiftOver failed. SNP removed.");
// 				}
// 			}
// 
// 		}
// 		reader.close();
// 		sorter.doneAdding();
// 
// 		int pos = -1;
// 		for (VcfLine vcfLine : sorter) {
// 			if (vcfLine.getPosition() < pos) {
// 				throw new IOException("Sorting VCF file after Liftover failed.");
// 			}
// 			writer.write(vcfLine.getLine());
// 			pos = vcfLine.getPosition();
// 		}
// 		writer.close();
// 		sorter.cleanup();
// 
// 		return errors;
// 	}
// 
// }


// Path: src/main/java/genepi/imputationserver/tools/VcfLiftOverTool.java
import genepi.base.Tool;
import genepi.imputationserver.steps.vcf.VcfLiftOverFast;

package genepi.imputationserver.tools;


public class VcfLiftOverTool extends Tool {

	public VcfLiftOverTool(String[] args) {
		super(args);
	}

	@Override
	public void createParameters() {
		addParameter("input", "input vcf file");
		addParameter("output", "output vcf file");
		addParameter("chain", "chain file");
	}

	@Override
	public void init() {
		System.out.println("VcfLiftOver Tool");
		System.out.println("");
	}

	@Override
	public int run() {

		String input = getValue("input").toString();
		String output = getValue("output").toString();
		String chain = getValue("chain").toString();

		try {
			VcfLiftOverFast.liftOver(input, output, chain, "./");