// Path: halvade/src/be/ugent/intec/halvade/hadoop/mapreduce/HalvadeCounters.java
// public enum HalvadeCounters {
//     TIME_BWA_ALN,
//     TIME_BWA_MEM,
//     TIME_BOWTIE2,
//     TIME_CUSHAW2,
//     TIME_BWA_SAMPE,
//     TIME_STAR,
//     TIME_STAR_REF,
//     TIME_STAR_BUILD,
//     TIME_ELPREP,
//     TIME_BEDTOOLS,
//     TIME_SAMTOBAM,
//     TIME_SAMPLESAM,
//     TIME_HADOOP_SAMTOBAM,
//     TIME_PICARD_CLEANSAM,
//     TIME_PICARD_MARKDUP,
//     TIME_PICARD_ADDGRP,
//     TIME_FEATURECOUNTS,
//     TIME_PICARD_BAI,    
//     TIME_GATK_RECAL,  
//     TIME_GATK_TARGET_CREATOR, 
//     TIME_GATK_INDEL_REALN, 
//     TIME_GATK_PRINT_READS, 
//     TIME_GATK_COMBINE_VCF,
//     TIME_GATK_VARIANT_CALLER,
//     
//     IN_BWA_READS,
//     IN_PREP_READS,
//     
//     OUT_BWA_READS,
//     OUT_VCF_FILES,
//     OUT_UNMAPPED_READS,
//     OUT_DIFF_CHR_READS,
//     OUT_OVERLAPPING_READS,
//     
//     FOUT_BWA_TMP,
//     FOUT_STAR_TMP,
//     FOUT_GATK_TMP,
//     FOUT_GATK_VCF,
//     FOUT_TO_HDFS,
//     
//     FIN_FROM_HDFS,
//     
//     TOOLS_GATK,
//     
//     STILL_RUNNING_HEARTBEAT
// }


import be.ugent.intec.halvade.hadoop.mapreduce.HalvadeCounters;
import java.io.IOException;
import java.io.InputStream;
import org.apache.hadoop.mapreduce.Mapper;
import be.ugent.intec.halvade.utils.*;
import java.net.URISyntaxException;
import org.apache.hadoop.mapreduce.Mapper.Context;
        pbw.startProcess(null, System.err);
        // check if alive.
        if(!pbw.isAlive())
            throw new ProcessException("BWA mem", pbw.getExitState());
        pbw.getSTDINWriter();
        // make a SAMstream handler
        ssh = new SAMStreamHandler(instance, context, false);
        ssh.start();
    }
        
    /**
     * 
     * @return 1 is running, 0 is completed, -1 is error 
     */
    @Override
    public int getState() {
        return pbw.getState();
    }

    @Override
    public void closeAligner() throws InterruptedException {
        try {
            // close the input stream
            pbw.getSTDINWriter().flush();
            pbw.getSTDINWriter().close();
        } catch (IOException ex) {
            Logger.EXCEPTION(ex);
        }
        
        int error = pbw.waitForCompletion();
// Path: halvade/src/be/ugent/intec/halvade/hadoop/mapreduce/HalvadeCounters.java
// public enum HalvadeCounters {
//     TIME_BWA_ALN,
//     TIME_BWA_MEM,
//     TIME_BOWTIE2,
//     TIME_CUSHAW2,
//     TIME_BWA_SAMPE,
//     TIME_STAR,
//     TIME_STAR_REF,
//     TIME_STAR_BUILD,
//     TIME_ELPREP,
//     TIME_BEDTOOLS,
//     TIME_SAMTOBAM,
//     TIME_SAMPLESAM,
//     TIME_HADOOP_SAMTOBAM,
//     TIME_PICARD_CLEANSAM,
//     TIME_PICARD_MARKDUP,
//     TIME_PICARD_ADDGRP,
//     TIME_FEATURECOUNTS,
//     TIME_PICARD_BAI,    
//     TIME_GATK_RECAL,  
//     TIME_GATK_TARGET_CREATOR, 
//     TIME_GATK_INDEL_REALN, 
//     TIME_GATK_PRINT_READS, 
//     TIME_GATK_COMBINE_VCF,
//     TIME_GATK_VARIANT_CALLER,
//     
//     IN_BWA_READS,
//     IN_PREP_READS,
//     
//     OUT_BWA_READS,
//     OUT_VCF_FILES,
//     OUT_UNMAPPED_READS,
//     OUT_DIFF_CHR_READS,
//     OUT_OVERLAPPING_READS,
//     
//     FOUT_BWA_TMP,
//     FOUT_STAR_TMP,
//     FOUT_GATK_TMP,
//     FOUT_GATK_VCF,
//     FOUT_TO_HDFS,
//     
//     FIN_FROM_HDFS,
//     
//     TOOLS_GATK,
//     
//     STILL_RUNNING_HEARTBEAT
// }


// Path: halvade/src/be/ugent/intec/halvade/tools/BWAMemInstance.java
import be.ugent.intec.halvade.hadoop.mapreduce.HalvadeCounters;
import java.io.IOException;
import java.io.InputStream;
import org.apache.hadoop.mapreduce.Mapper;
import be.ugent.intec.halvade.utils.*;
import java.net.URISyntaxException;
import org.apache.hadoop.mapreduce.Mapper.Context;

        pbw.startProcess(null, System.err);
        // check if alive.
        if(!pbw.isAlive())
            throw new ProcessException("BWA mem", pbw.getExitState());
        pbw.getSTDINWriter();
        // make a SAMstream handler
        ssh = new SAMStreamHandler(instance, context, false);
        ssh.start();
    }
        
    /**
     * 
     * @return 1 is running, 0 is completed, -1 is error 
     */
    @Override
    public int getState() {
        return pbw.getState();
    }

    @Override
    public void closeAligner() throws InterruptedException {
        try {
            // close the input stream
            pbw.getSTDINWriter().flush();
            pbw.getSTDINWriter().close();
        } catch (IOException ex) {
            Logger.EXCEPTION(ex);
        }
        
        int error = pbw.waitForCompletion();
        context.getCounter(HalvadeCounters.TIME_BWA_MEM).increment(pbw.getExecutionTime());