Example usage for java.util Arrays parallelSort

List of usage examples for java.util Arrays parallelSort

Introduction

In this page you can find the example usage for java.util Arrays parallelSort.

Prototype

@SuppressWarnings("unchecked")
public static <T extends Comparable<? super T>> void parallelSort(T[] a, int fromIndex, int toIndex) 

Source Link

Document

Sorts the specified range of the specified array of objects into ascending order, according to the Comparable natural ordering of its elements.

Usage

From source file:nl.umcg.westrah.binarymetaanalyzer.BinaryMetaAnalysis.java

private void writeBuffer(String outdir, int permutation) throws IOException {

    // sort the finalbuffer for a last time
    if (locationToStoreResult != 0) {
        Arrays.parallelSort(finalEQTLs, 0, locationToStoreResult);
    }//from   w w  w  .j a v a  2s . co  m

    String outfilename = outdir + "eQTLs.txt.gz";
    FileFormat outformat = FileFormat.LARGE;
    if (permutation > 0) {
        outfilename = outdir + "PermutedEQTLsPermutationRound" + permutation + ".txt.gz";
        if (!fullpermutationoutput) {
            outformat = FileFormat.REDUCED;
        }
    }

    String tab = "\t";
    String newline = "\n";

    TextFile output = new TextFile(outfilename, TextFile.W, 10 * 1048576);

    String header = "PValue\t" + "SNPName\t" + "SNPChr\t" + "SNPChrPos\t" + "ProbeName\t" + "ProbeChr\t"
            + "ProbeCenterChrPos\t" + "CisTrans\t" + "SNPType\t" + "AlleleAssessed\t" + "OverallZScore\t"
            + "DatasetsWhereSNPProbePairIsAvailableAndPassesQC\t" + "DatasetsZScores\t" + "DatasetsNrSamples\t"
            + "IncludedDatasetsMeanProbeExpression\t" + "IncludedDatasetsProbeExpressionVariance\t"
            + "HGNCName\t" + "IncludedDatasetsCorrelationCoefficient\t" + "Meta-Beta (SE)\t" + "Beta (SE)\t"
            + "FoldChange";
    if (outformat.equals(FileFormat.REDUCED)) {
        header = "PValue\tSNP\tProbe\tGene\tAlleles\tAlleleAssessed\tZScore";
    }
    output.writeln(header);
    // PValue   SNPName   SNPChr   SNPChrPos   ProbeName   ProbeChr   ProbeCenterChrPos   CisTrans   SNPType   AlleleAssessed   OverallZScore   DatasetsWhereSNPProbePairIsAvailableAndPassesQC   DatasetsZScores   DatasetsNrSamples   IncludedDatasetsMeanProbeExpression   IncludedDatasetsProbeExpressionVariance   HGNCName   IncludedDatasetsCorrelationCoefficient   Meta-Beta (SE)   Beta (SE)   FoldChange   FDR

    DecimalFormat dformat = new DecimalFormat("###.####", new DecimalFormatSymbols(Locale.US));
    DecimalFormat pformat = new DecimalFormat("###.########", new DecimalFormatSymbols(Locale.US));
    DecimalFormat smallpFormat = new DecimalFormat("0.####E0", new DecimalFormatSymbols(Locale.US));

    int ctr = 0;
    for (int i = 0; i < settings.getFinalEQTLBufferMaxLength(); i++) {
        if (finalEQTLs[i] != null) {
            ctr++;
        }
    }
    int totalctr = 0;
    for (int i = 0; i < finalEQTLs.length; i++) {
        if (finalEQTLs[i] != null) {
            totalctr++;
        }
    }
    System.out.println("There are " + ctr + " results in the buffer to write. " + totalctr
            + " QTLs are in memory at the moment.");
    ProgressBar pb = new ProgressBar(ctr, "Writing: " + outfilename);

    String cistr = "Cis";
    String transtr = "Trans";
    String greyz = "Greyzone";

    for (int i = 0; i < settings.getFinalEQTLBufferMaxLength(); i++) {
        QTL q = finalEQTLs[i];
        if (q != null) {
            //            StringBuilder sb = new StringBuilder(4096);
            if (outformat.equals(FileFormat.LARGE)) {
                if (q.getPvalue() < 1E-4) {
                    output.append(smallpFormat.format(q.getPvalue()));
                } else {
                    output.append(pformat.format(q.getPvalue()));
                }
                output.append(tab);
                int snpId = q.getSNPId();
                output.append(snpList[snpId]);
                output.append(tab);
                output.append(snpChr[snpId]);
                output.append(tab);
                output.append("" + snpPositions[snpId]);
                output.append(tab);
                MetaQTL4MetaTrait t = q.getMetaTrait();
                output.append(t.getMetaTraitName());
                output.append(tab);
                output.append(t.getChr());
                output.append(tab);
                output.append("" + t.getChrMidpoint());
                output.append(tab);
                int dist = Math.abs(t.getChrMidpoint() - snpPositions[snpId]);
                boolean sameChr = t.getChr().equals(snpChr[snpId]);
                if (sameChr && dist < settings.getCisdistance()) {
                    output.append(cistr);
                } else if (sameChr && dist < settings.getTransdistance()) {
                    output.append(greyz);
                } else {
                    output.append(transtr);
                }

                output.append(tab);
                output.append(q.getAlleles());
                output.append(tab);
                output.append(q.getAlleleAssessed());
                output.append(tab);
                output.append(dformat.format(q.getZscore()));

                float[] datasetZScores = q.getDatasetZScores();
                String[] dsBuilder = new String[datasets.length];
                String[] dsNBuilder = new String[datasets.length];
                String[] dsZBuilder = new String[datasets.length];

                for (int d = 0; d < datasetZScores.length; d++) {

                    if (!Float.isNaN(datasetZScores[d])) {
                        String str = dformat.format(datasetZScores[d]);

                        dsBuilder[d] = settings.getDatasetnames().get(d);
                        dsNBuilder[d] = "" + q.getDatasetSampleSizes()[d];
                        dsZBuilder[d] = str;
                    } else {
                        dsBuilder[d] = "-";
                        dsNBuilder[d] = "-";
                        dsZBuilder[d] = "-";
                    }
                }

                output.append(tab);
                output.append(Strings.concat(dsBuilder, Strings.semicolon));

                output.append(tab);
                output.append(Strings.concat(dsZBuilder, Strings.semicolon));

                output.append(tab);
                output.append(Strings.concat(dsNBuilder, Strings.semicolon));
                output.append("\t-\t-\t");

                output.append(t.getAnnotation());
                output.append("\t-\t-\t-\t-");
                output.append(newline);

            } else {
                //               header = "PValue\tSNP\tProbe\tGene\tAlleles\tAlleleAssessed\tZScore";

                int snpId = q.getSNPId();
                MetaQTL4MetaTrait t = q.getMetaTrait();

                if (q.getPvalue() < 1E-4) {
                    output.append(smallpFormat.format(q.getPvalue()));
                } else {
                    output.append(pformat.format(q.getPvalue()));
                }
                output.append(tab);
                output.append(snpList[snpId]);
                output.append(tab);
                output.append(t.getMetaTraitName());
                output.append(tab);
                output.append(t.getAnnotation());
                output.append(tab);
                output.append(q.getAlleles());
                output.append(tab);
                output.append(q.getAlleleAssessed());
                output.append(tab);
                output.append(dformat.format(q.getZscore()));
                output.append(newline);
            }

            pb.iterate();
        }
        finalEQTLs[i] = null; // trash it immediately
    }

    pb.close();
    output.close();
    finalEQTLs = null; // ditch the whole resultset

    if (usetmp) {

        String filename = "eQTLs.txt.gz";
        if (permutation > 0) {
            filename = "PermutedEQTLsPermutationRound" + permutation + ".txt.gz";
        }
        File source = new File(tempDir + filename);
        File dest = new File(settings.getOutput() + filename);
        if (dest.exists()) {
            System.out.println("Destination file: " + dest.getAbsolutePath() + " exists already.. Deleting!");
            dest.delete();
        }
        System.out.println("Moving file: " + tempDir + filename + " --> " + settings.getOutput() + filename);
        FileUtils.moveFile(source, dest);

    }

    System.out.println("Done.");
}