gt: 				if (pgsPanel != null) {
context: package genepi.imputationserver.steps;
  
  import java.io.File;
  import java.io.IOException;
  import java.util.HashMap;
  import java.util.Map;
  
  import cloudgene.sdk.internal.WorkflowContext;
  import genepi.hadoop.HadoopJob;
  import genepi.hadoop.HdfsUtil;
  import genepi.hadoop.io.HdfsLineWriter;
  import genepi.imputationserver.steps.imputation.ImputationJob;
  import genepi.imputationserver.steps.vcf.VcfChunk;
  import genepi.imputationserver.util.ContextLog;
  import genepi.imputationserver.util.DefaultPreferenceStore;
  import genepi.imputationserver.util.ParallelHadoopJobStep;
  import genepi.imputationserver.util.PgsPanel;
  import genepi.imputationserver.util.RefPanel;
  import genepi.imputationserver.util.RefPanelList;
  import genepi.io.FileUtil;
  import genepi.io.text.LineReader;
  
  public class Imputation extends ParallelHadoopJobStep {
  
  	Map<String, HadoopJob> jobs = null;
  
  	boolean error = false;
  
  	private WorkflowContext context;
  
  	private String errorChr = "";
  
  	private boolean running = true;
  
  	private String output;
  
  	private String outputScores;
  
  	private boolean ok = false;
  
  	public static int THREADS = 25;
  
  	public Imputation() {
  		super(THREADS);
  		jobs = new HashMap<String, HadoopJob>();
  	}
  
  	@Override
  	public void setup(WorkflowContext context) {
  
  		this.context = context;
  	}
  
  	@Override
  	public boolean run(WorkflowContext context) {
  
  		final String folder = getFolder(Imputation.class);
  
  		// inputs
  		String input = context.get("chunkFileDir");
  		String reference = context.get("refpanel");
  		String binariesHDFS = context.getConfig("binaries");
  		String mode = context.get("mode");
  		String phasing = context.get("phasing");
  		PgsPanel pgsPanel = PgsPanel.loadFromProperties(context.getData("pgsPanel"));
  
  		String r2Filter = context.get("r2Filter");
  		if (r2Filter == null) {
  			r2Filter = "0";
  		}
  
  		// outputs
  		output = context.get("outputimputation");
  		// output scores
  		outputScores = context.get("outputScores");
  
  		String log = context.get("logfile");
  
  		if (!(new File(input)).exists()) {
  			context.error("No chunks passed the QC step.");
  			return false;
  		}
  
  		// load reference panels
  
  		RefPanelList panels = RefPanelList.loadFromFile(FileUtil.path(folder, RefPanelList.FILENAME));
  
  		RefPanel panel = null;
  		try {
  			panel = panels.getById(reference, context.getData("refpanel"));
  			if (panel == null) {
  				context.error("reference panel '" + reference + "' not found.");
  				return false;
  			}
  		} catch (Exception e) {
  			context.error("Unable to parse reference panel '" + reference + "': " + e.getMessage());
  			return false;
  		}
  
  		context.println("Reference Panel: ");
  		context.println("  Name: " + reference);
  		context.println("  ID: " + panel.getId());
  		context.println("  Build: " + panel.getBuild());
  		context.println("  Location: " + panel.getHdfs());
  		context.println("  Legend: " + panel.getLegend());
  		context.println("  Version: " + panel.getVersion());
  		context.println("  Eagle Map: " + panel.getMapEagle());
  		context.println("  Eagle BCFs: " + panel.getRefEagle());
  		context.println("  Beagle Bref3: " + panel.getRefBeagle());
  		context.println("  Beagle Map: " + panel.getMapBeagle());
  		context.println("  Minimac Map: " + panel.getMapMinimac());
  		context.println("  Populations:");
  		for (Map.Entry<String, String> entry : panel.getPopulations().entrySet()) {
  			context.println("    " + entry.getKey() + "/" + entry.getValue());
  		}
  		context.println("  Samples:");
  		for (Map.Entry<String, String> entry : panel.getSamples().entrySet()) {
  			context.println("    " + entry.getKey() + "/" + entry.getValue());
  		}
  		if (panel.getQcFilter() != null) {
  			context.println("  QC Filters:");
  			for (Map.Entry<String, String> entry : panel.getQcFilter().entrySet()) {
  				context.println("    " + entry.getKey() + "/" + entry.getValue());
  			}
  		}
  		if (pgsPanel != null) {
  			context.println("  PGS: " + pgsPanel.getScores().size() + " scores");
  		} else {
  			context.println("  PGS: no scores selected");
  		}
  
  		// execute one job per chromosome
  		try {
  			String[] chunkFiles = FileUtil.getFiles(input, "*.*");
  
  			context.beginTask("Start Imputation...");
  
  			if (chunkFiles.length == 0) {
  				context.error("<br><b>Error:</b> No chunks found. Imputation cannot be started!");
  				return false;
  			}
  
  			for (String chunkFile : chunkFiles) {
  
  				String[] tiles = chunkFile.split("/");
  				String chr = tiles[tiles.length - 1];
  
  				ChunkFileConverterResult result = convertChunkfile(chunkFile, context.getHdfsTemp());
  
  				ImputationJob job = new ImputationJob(context.getJobId() + "-chr-" + chr, new ContextLog(context)) {
  					@Override
  					protected void readConfigFile() {
  						File file = new File(folder + "/" + CONFIG_FILE);
  						DefaultPreferenceStore preferenceStore = new DefaultPreferenceStore();
  						if (file.exists()) {
  							log.info("Loading distributed configuration file '" + file.getAbsolutePath() + "'...");
  							preferenceStore.load(file);
  
  						} else {
  							log.info("Configuration file '" + file.getAbsolutePath()
  									+ "' not available. Use default values");
  
  						}
  
  						preferenceStore.write(getConfiguration());
  						for (Object key : preferenceStore.getKeys()) {
  							log.info("  " + key + ": " + preferenceStore.getString(key.toString()));
  						}
  
  					}
  				};
  
  				job.setBinariesHDFS(binariesHDFS);
  
  				String hdfsFilenameChromosome = resolvePattern(panel.getHdfs(), chr);
  				job.setRefPanelHdfs(hdfsFilenameChromosome);
  
  				job.setR2Filter(r2Filter);
  				job.setBuild(panel.getBuild());
  				if (panel.getMapMinimac() != null) {
  					context.println("Setting up minimac map file...");
  					job.setMapMinimac(panel.getMapMinimac());
  				} else {
  					context.println("Reference panel has no minimac map file.");
  				}
  
  				if (result.needsPhasing) {
  					job.setPhasingRequired("true");
  					context.println("Input data is unphased.");
  
  					if (phasing.equals("beagle")) {
  						context.println("  Setting up beagle reference and map files...");
  						String refBeagleFilenameChromosome = resolvePattern(panel.getRefBeagle(), chr);
  						String mapBeagleFilenameChromosome = resolvePattern(panel.getMapBeagle(), chr);
  						job.setRefBeagleHdfs(refBeagleFilenameChromosome);
  						job.setMapBeagleHdfs(mapBeagleFilenameChromosome);
  					} else {
  
  						if (!panel.checkEagleMap()) {
  							context.error("Eagle map file not found.");
  							return false;
  						}
  
  						context.println("  Setting up eagle reference and map files...");
  						job.setMapEagleHdfs(panel.getMapEagle());
  						String refEagleFilenameChromosome = resolvePattern(panel.getRefEagle(), chr);
  						job.setRefEagleHdfs(refEagleFilenameChromosome);
  					}
  
  				} else {
  					context.println("Input data is phased.");
  					job.setPhasingRequired("false");
  				}
  
  				if (mode != null && mode.equals("phasing")) {
  					job.setPhasingOnly("true");
  				} else {
  					job.setPhasingOnly("false");
  				}
  
  				if (phasing != null) {
  					job.setPhasingEngine(phasing);
  				}
  				job.setInput(result.filename);
  				job.setOutput(HdfsUtil.path(output, chr));
  
  				if (outputScores != null) {
  					job.setOutputScores(outputScores);
  				}
  
  