@Override
	public void run()  {
		try {		
			BamWindow window = bamWindows.getWindow();
			ReferenceBAMEmitter emitter = new ReferenceBAMEmitter(referenceFile, counters, window, options);

			//Store intermediate results in temporary files
			String tmpDataPrefix =  "." + generateRandomString(12);

			File data = new File(tmpDataPrefix + ".data");
			File positionsFile = new File(tmpDataPrefix + ".pos");
			BufferedWriter posWriter = new BufferedWriter(new FileWriter(positionsFile));
			emitter.setPositionsWriter(posWriter);

			//Read BAM file, write results to temporary file
			PrintStream dataStream = new PrintStream(new FileOutputStream(data));		
						
			for(String contig : intervals.getContigs()) {
				for(Interval interval : intervals.getIntervalsInContig(contig)) {
					emitter.emitWindow(contig, interval.getFirstPos(), interval.getLastPos(), dataStream);
					basesComputed += interval.getSize();
				}
			}
			
			//CRITICAL: must return its bamWindow to the BAMWindowStore
			bamWindows.returnToStore(window);
			dataStream.close();
			posWriter.close();
	
			LIBSVMPredictor predictor = new LIBSVMPredictor();
			
			LIBSVMResult result = predictor.predictData(data, new LIBSVMModel(modelFile));
			result.setPositionsFile(positionsFile);

			ResultVariantConverter converter = new ResultVariantConverter();
			converter.setVariantQualityCutoff(options.getMinQuality());
			variants = converter.createVariantList(result);

			//Remove temporary files 
			if (options.isRemoveTempFiles()) {
				data.delete();
				positionsFile.delete();
			}
		}
		catch (IOException iox) {

			iox.printStackTrace();
		} catch (IndexNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		
	}