# Buggy code
def _sge_info(queue):
    """Returns machine information for an sge job scheduler."""
    qhost_out = subprocess.check_output(["qhost", "-q", "-xml"]).decode()
    qstat_queue = ["-q", queue] if queue and "," not in queue else []
    qstat_out = subprocess.check_output(["qstat", "-f", "-xml"] + qstat_queue).decode()
    slot_info = _sge_get_slots(qstat_out)
    mem_info = _sge_get_mem(qhost_out, queue)
    machine_keys = slot_info.keys()
    # num_cpus_vec = [slot_info[x]["slots_total"] for x in machine_keys]
    # mem_vec = [mem_info[x]["mem_total"] for x in machine_keys]
    mem_per_slot = [
        mem_info[x]["mem_total"] / float(slot_info[x]["slots_total"])
        for x in machine_keys
    ]
    min_ratio_index = mem_per_slot.index(median_left(mem_per_slot))
    mem_info[machine_keys[min_ratio_index]]["mem_total"]
    return [
        {
            "cores": slot_info[machine_keys[min_ratio_index]]["slots_total"],
            "memory": mem_info[machine_keys[min_ratio_index]]["mem_total"],
            "name": "sge_machine",
        }
    ]


# Traceback (subprocess.CalledProcessError)
Using GATK jar /export/home/ncit/external/a.mizeranschi/bcbio_nextgen/anaconda/share/gatk4-4.1.4.1-1/gatk-package-4.1.4.1-local.jar
Running:
java -Dsamjdk.use_async_io_read_samtools=false -Dsamjdk.use_async_io_write_samtools=true -Dsamjdk.use_async_io_write_tribble=false -Dsamjdk.compression_level=2 -Xms681m -Xmx1g -XX:+UseSerialGC -Djava.io.tmpdir=/export/home/ncit/external/a.mizeranschi/automated-VC-test/testingVC/work/bcbiotx/tmp6esj9s4r -jar /export/home/ncit/external/a.mizeranschi/bcbio_nextgen/anaconda/share/gatk4-4.1.4.1-1/gatk-package-4.1.4.1-local.jar HaplotypeCaller -R /export/home/ncit/external/a.mizeranschi/bcbio_nextgen/genomes/Hsapiens/hg38/seq/hg38.fa --annotation MappingQualityRankSumTest --annotation MappingQualityZero --annotation QualByDepth --annotation ReadPosRankSumTest --annotation RMSMappingQuality --annotation BaseQualityRankSumTest --annotation FisherStrand --annotation MappingQuality --annotation DepthPerAlleleBySample --annotation Coverage -I /export/home/ncit/external/a.mizeranschi/automated-VC-test/testingVC/work/align/HG01518/HG01518-sort.bam -L /export/home/ncit/external/a.mizeranschi/automated-VC-test/testingVC/work/gatk-haplotype/chr1/HG01518-chr1_0_50142832-regions.bed --interval-set-rule INTERSECTION --annotation ClippingRankSumTest --annotation DepthPerSampleHC --native-pair-hmm-threads 1 --verbosity ERROR --emit-ref-confidence GVCF -GQB 10 -GQB 20 -GQB 30 -GQB 40 -GQB 60 -GQB 80 -ploidy 2 --output /export/home/ncit/external/a.mizeranschi/automated-VC-test/testingVC/work/bcbiotx/tmp6esj9s4r/HG01518-chr1_0_50142832.vcf.gz
' returned non-zero exit status 1.
[2020-01-20T08:09Z] haswell-wn35.grid.pub.ro: Unexpected error
Traceback (most recent call last):
File "/export/home/ncit/external/a.mizeranschi/bcbio_nextgen/anaconda/lib/python3.6/site-packages/bcbio/distributed/ipythontasks.py", line 54, in _setup_logging
yield config
File "/export/home/ncit/external/a.mizeranschi/bcbio_nextgen/anaconda/lib/python3.6/site-packages/bcbio/distributed/ipythontasks.py", line 439, in variantcall_sample
return ipython.zip_args(apply(genotype.variantcall_sample, *args))
File "/export/home/ncit/external/a.mizeranschi/bcbio_nextgen/anaconda/lib/python3.6/site-packages/bcbio/distributed/ipythontasks.py", line 82, in apply
return object(*args, **kwargs)
File "/export/home/ncit/external/a.mizeranschi/bcbio_nextgen/anaconda/lib/python3.6/site-packages/bcbio/variation/genotype.py", line 377, in variantcall_sample
out_file = caller_fn(align_bams, items, ref_file, assoc_files, region, out_file)
File "/export/home/ncit/external/a.mizeranschi/bcbio_nextgen/anaconda/lib/python3.6/site-packages/bcbio/variation/gatk.py", line 180, in haplotype_caller
parallel_gc=_use_spark(num_cores, gatk_type, items, spark_opts))
File "/export/home/ncit/external/a.mizeranschi/bcbio_nextgen/anaconda/lib/python3.6/site-packages/bcbio/broad/__init__.py", line 365, in run_gatk
log_error=log_error)
File "/export/home/ncit/external/a.mizeranschi/bcbio_nextgen/anaconda/lib/python3.6/site-packages/bcbio/provenance/do.py", line 26, in run
_do_run(cmd, checks, log_stdout, env=env)
File "/export/home/ncit/external/a.mizeranschi/bcbio_nextgen/anaconda/lib/python3.6/site-packages/bcbio/provenance/do.py", line 106, in _do_run
raise subprocess.CalledProcessError(exitcode, error_msg)
subprocess.CalledProcessError: Command 'set -o pipefail; unset JAVA_HOME &amp;&amp; export PATH=/export/home/ncit/external/a.mizeranschi/bcbio_nextgen/anaconda/bin:"$PATH" &amp;&amp; gatk --java-options '-Xms681m -Xmx1g -XX:+UseSerialGC -Djava.io.tmpdir=/export/home/ncit/external/a.mizeranschi/automated-VC-test/testingVC/work/bcbiotx/tmp6esj9s4r' HaplotypeCaller -R /export/home/ncit/external/a.mizeranschi/bcbio_nextgen/genomes/Hsapiens/hg38/seq/hg38.fa --annotation MappingQualityRankSumTest --annotation MappingQualityZero --annotation QualByDepth --annotation ReadPosRankSumTest --annotation RMSMappingQuality --annotation BaseQualityRankSumTest --annotation FisherStrand --annotation MappingQuality --annotation DepthPerAlleleBySample --annotation Coverage -I /export/home/ncit/external/a.mizeranschi/automated-VC-test/testingVC/work/align/HG01518/HG01518-sort.bam -L /export/home/ncit/external/a.mizeranschi/automated-VC-test/testingVC/work/gatk-haplotype/chr1/HG01518-chr1_0_50142832-regions.bed --interval-set-rule INTERSECTION --annotation ClippingRankSumTest --annotation DepthPerSampleHC --native-pair-hmm-threads 1 --verbosity ERROR --emit-ref-confidence GVCF -GQB 10 -GQB 20 -GQB 30 -GQB 40 -GQB 60 -GQB 80 -ploidy 2 --output /export/home/ncit/external/a.mizeranschi/automated-VC-test/testingVC/work/bcbiotx/tmp6esj9s4r/HG01518-chr1_0_50142832.vcf.gz
[January 20, 2020 10:09:19 AM EET] org.broadinstitute.hellbender.tools.walkers.haplotypecaller.HaplotypeCaller done. Elapsed time: 1.44 minutes.
Runtime.totalMemory()=1037959168
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at org.apache.commons.math3.util.MathArrays.copyOfRange(MathArrays.java:827)
at org.apache.commons.math3.stat.descriptive.rank.Percentile.copyOf(Percentile.java:479)
at org.apache.commons.math3.stat.descriptive.rank.Percentile.removeAndSlice(Percentile.java:527)
at org.apache.commons.math3.stat.descriptive.rank.Percentile.getWorkArray(Percentile.java:455)
at org.apache.commons.math3.stat.descriptive.rank.Percentile.evaluate(Percentile.java:351)
at org.apache.commons.math3.stat.descriptive.rank.Percentile.evaluate(Percentile.java:302)
at org.apache.commons.math3.stat.descriptive.AbstractUnivariateStatistic.evaluate(AbstractUnivariateStatistic.java:121)
at org.broadinstitute.hellbender.utils.MathUtils.median(MathUtils.java:839)
at org.broadinstitute.hellbender.utils.variant.writers.GVCFBlock.getMedianDP(GVCFBlock.java:75)
at org.broadinstitute.hellbender.utils.variant.writers.HomRefBlock.createHomRefGenotype(HomRefBlock.java:73)
at org.broadinstitute.hellbender.utils.variant.writers.GVCFBlock.toVariantContext(GVCFBlock.java:49)
at org.broadinstitute.hellbender.utils.variant.writers.GVCFBlockCombiner.emitCurrentBlock(GVCFBlockCombiner.java:177)
at org.broadinstitute.hellbender.utils.variant.writers.GVCFBlockCombiner.signalEndOfInput(GVCFBlockCombiner.java:227)
at org.broadinstitute.hellbender.utils.variant.writers.GVCFWriter.close(GVCFWriter.java:70)
at org.broadinstitute.hellbender.tools.walkers.haplotypecaller.HaplotypeCaller.closeTool(HaplotypeCaller.java:246)
at org.broadinstitute.hellbender.engine.GATKTool.doWork(GATKTool.java:1052)
at org.broadinstitute.hellbender.cmdline.CommandLineProgram.runTool(CommandLineProgram.java:139)
at org.broadinstitute.hellbender.cmdline.CommandLineProgram.instanceMainPostParseArgs(CommandLineProgram.java:191)
at org.broadinstitute.hellbender.cmdline.CommandLineProgram.instanceMain(CommandLineProgram.java:210)
at org.broadinstitute.hellbender.Main.runCommandLineProgram(Main.java:163)
at org.broadinstitute.hellbender.Main.mainEntry(Main.java:206)
at org.broadinstitute.hellbender.Main.main(Main.java:292)

# Fixed code
def _sge_info(queue):
    """Returns machine information for an sge job scheduler."""
    qhost_out = subprocess.check_output(["qhost", "-q", "-xml"]).decode()
    qstat_queue = ["-q", queue] if queue and "," not in queue else []
    qstat_out = subprocess.check_output(["qstat", "-f", "-xml"] + qstat_queue).decode()
    slot_info = _sge_get_slots(qstat_out)
    mem_info = _sge_get_mem(qhost_out, queue)
    machine_keys = list(slot_info.keys())
    # num_cpus_vec = [slot_info[x]["slots_total"] for x in machine_keys]
    # mem_vec = [mem_info[x]["mem_total"] for x in machine_keys]
    mem_per_slot = [
        mem_info[x]["mem_total"] / float(slot_info[x]["slots_total"])
        for x in machine_keys
    ]
    min_ratio_index = mem_per_slot.index(median_left(mem_per_slot))
    mem_info[machine_keys[min_ratio_index]]["mem_total"]
    return [
        {
            "cores": slot_info[machine_keys[min_ratio_index]]["slots_total"],
            "memory": mem_info[machine_keys[min_ratio_index]]["mem_total"],
            "name": "sge_machine",
        }
    ]