Example usage for org.apache.commons.math3.stat.correlation SpearmansCorrelation correlation

List of usage examples for org.apache.commons.math3.stat.correlation SpearmansCorrelation correlation

Introduction

In this page you can find the example usage for org.apache.commons.math3.stat.correlation SpearmansCorrelation correlation.

Prototype

public double correlation(final double[] xArray, final double[] yArray) 

Source Link

Document

Computes the Spearman's rank correlation coefficient between the two arrays.

Usage

From source file:hms.hwestra.interactionrebuttal.InteractionRebuttal.java

public void iterativelyBuildProxiesAndCorrelate(String expressionfile, String gte, String outputdir,
        String probefile, int permutations, String proxy, Integer numProbesMax, boolean justcorrelate)
        throws IOException {

    Set<String> samplesToUse = null;
    if (gte != null) {
        TextFile tfq = new TextFile(gte, TextFile.R);
        samplesToUse = tfq.readAsSet(1, TextFile.tab);
        tfq.close();//from  ww w . j a va 2s .  c  o  m
    }
    DoubleMatrixDataset<String, String> rawExpressionDataset = new DoubleMatrixDataset<String, String>(
            expressionfile, null, samplesToUse);
    int numTotalIter = 0;

    ArrayList<String> probes = null;
    if (probefile != null) {
        TextFile tf = new TextFile(probefile, TextFile.R);
        probes = tf.readAsArrayList();
        tf.close();
        numProbesMax = probes.size();
    } else {
        System.out.println("Selecting random probes");
        List<String> rows = rawExpressionDataset.rowObjects;
        probes = new ArrayList<String>(rows);
    }

    outputdir = Gpio.formatAsDirectory(outputdir);
    Gpio.createDir(outputdir);
    int iter = 5;
    System.out.println(probes.size() + " probes availables");

    int remainder = numProbesMax % iter;
    int numProbesMaxIter = numProbesMax + (iter - remainder);

    if (!justcorrelate) {

        for (int num = 0; num < numProbesMaxIter + 1; num += iter) {
            int probesToSelect = num;
            if (num == 0) {
                probesToSelect = 1;
            }
            if (num > numProbesMax) {
                probesToSelect = numProbesMax;
            }
            System.out.println("Selecting: " + probesToSelect + " probes");
            for (int permutation = 0; permutation < permutations; permutation++) {
                Collections.shuffle(probes);
                List<String> subsample = probes.subList(0, probesToSelect);

                // create output dir
                String outputdirPerm = outputdir + probesToSelect + "-Probes/Permutation-" + permutation + "/";
                outputdirPerm = Gpio.formatAsDirectory(outputdirPerm);
                Gpio.createDir(outputdirPerm);
                String subset = outputdirPerm + "probes.txt";
                TextFile probeout = new TextFile(subset, TextFile.W);
                probeout.writeList(subsample);
                probeout.close();

                // run normalizer
                prepareDataForCelltypeSpecificEQTLMapping(rawExpressionDataset, expressionfile, outputdirPerm,
                        Double.NaN, subset, null, null, null, 4);
                // remove superfluous files
                // correlate with cell count
            }
            numTotalIter++;
        }
    }

    DoubleMatrixDataset<String, String> ds = new DoubleMatrixDataset<String, String>(proxy); // samples on the rows...
    ds.transposeDataset(); // samples on the columns
    for (int row = 0; row < ds.nrRows; row++) {
        String pheno = ds.rowObjects.get(row);

        double[] x = ds.rawData[row];
        System.out.println("x length: " + x.length);

        TextFile statsout = new TextFile(outputdir + pheno + ".txt", TextFile.W);
        statsout.writeln("Num\tMeanPearson\tsdPearson\tMeanSpearman\tsdSpearman");
        SpearmansCorrelation sp = new SpearmansCorrelation();
        for (int num = 0; num < numProbesMaxIter + 1; num += iter) {
            int probesToSelect = num;
            if (num == 0) {
                probesToSelect = 1;
            }
            if (num > numProbesMax) {
                probesToSelect = numProbesMax;
            }
            double[] allCorrelations = new double[permutations];
            double[] allCorrelationSpearman = new double[permutations];
            for (int permutation = 0; permutation < permutations; permutation++) {
                String inputdirPerm = outputdir + probesToSelect + "-Probes/Permutation-" + permutation
                        + "/CellTypeProxyFile.txt";
                DoubleMatrixDataset<String, String> ds2 = new DoubleMatrixDataset<String, String>(inputdirPerm);
                ds2.transposeDataset(); // samples on the column
                double[] y = new double[x.length];
                System.out.println("y: " + y.length);
                double[] ytmp = ds2.rawData[0];
                //                    if (ytmp.length != x.length) {
                //                        System.err.println("Error: " + y.length);
                //                        System.exit(-1);
                //                    } else {
                for (int col = 0; col < ds.nrCols; col++) {
                    int otherCol = ds2.hashCols.get(ds.colObjects.get(col));
                    y[col] = ytmp[otherCol];
                }
                double corr = JSci.maths.ArrayMath.correlation(x, y);
                System.out.println(num + "\t" + permutation + "\t" + corr);
                double spearman = sp.correlation(x, y);
                allCorrelations[permutation] = corr;
                allCorrelationSpearman[permutation] = spearman;
                //                    }
            }
            // doe
            double meanP = JSci.maths.ArrayMath.mean(allCorrelations);
            double sdP = JSci.maths.ArrayMath.standardDeviation(allCorrelations);
            double meanSP = JSci.maths.ArrayMath.mean(allCorrelationSpearman);
            double sdSP = JSci.maths.ArrayMath.standardDeviation(allCorrelationSpearman);
            statsout.writeln(num + "\t" + meanP + "\t" + sdP + "\t" + meanSP + "\t" + sdSP);
        }

        statsout.close();

    }

}

From source file:MSUmpire.MathPackage.PearsonCorr.java

public double CalcCorrV2(XYPointCollection CollectionA, XYPointCollection CollectionB, int NoPointPerInterval) {
    SpearmansCorrelation pearsonsCorrelation = new SpearmansCorrelation();

    int num = Math.max(CollectionA.PointCount(), CollectionB.PointCount()) / 2;
    float timeinterval = 2f / (float) NoPointPerInterval;
    if (num < 6) {
        return 0f;
    }/*ww  w  .  j  av  a 2 s.c o m*/

    double[] arrayA = new double[num];
    double[] arrayB = new double[num];

    float start = Math.max(CollectionA.Data.get(0).getX(), CollectionB.Data.get(0).getX());

    int i = 0;
    float low = start;
    float up = start + timeinterval;

    for (int j = 0; j < CollectionA.PointCount(); j++) {
        while (CollectionA.Data.get(j).getX() > up) {
            i++;
            low = up;
            up = low + timeinterval;
        }
        if (i >= num) {
            break;
        }
        if (CollectionA.Data.get(j).getX() >= low && CollectionA.Data.get(j).getX() < up) {
            if (CollectionA.Data.get(j).getY() > arrayA[i]) {
                arrayA[i] = CollectionA.Data.get(j).getY();
            }
        }
    }
    i = 0;
    low = start;
    up = start + timeinterval;
    for (int j = 0; j < CollectionB.PointCount(); j++) {
        while (CollectionB.Data.get(j).getX() > up) {
            i++;
            low = up;
            up = low + timeinterval;
        }
        if (i >= num) {
            break;
        }
        if (CollectionB.Data.get(j).getX() >= low && CollectionB.Data.get(j).getX() < up) {
            if (CollectionB.Data.get(j).getY() > arrayB[i]) {
                arrayB[i] = CollectionB.Data.get(j).getY();
            }
        }
    }

    if (arrayA[0] == 0f) {
        arrayA[0] = arrayA[1];
    }
    if (arrayB[0] == 0f) {
        arrayB[0] = arrayB[1];
    }
    for (int idx = 1; idx < num - 1; idx++) {
        if (arrayA[idx] == 0f) {
            arrayA[idx] = (arrayA[idx - 1] + arrayA[idx + 1]) / 2;
        }
        if (arrayB[idx] == 0f) {
            arrayB[idx] = (arrayB[idx - 1] + arrayB[idx + 1]) / 2;
        }
    }

    if (arrayA[num - 1] == 0f) {
        arrayA[num - 1] = arrayA[num - 2];
    }
    if (arrayB[num - 1] == 0f) {
        arrayB[num - 1] = arrayB[num - 2];
    }
    double R2 = pearsonsCorrelation.correlation(arrayA, arrayB);
    return R2;
}

From source file:hms.hwestra.interactionrebuttal.InteractionRebuttal.java

public void correlate(String file1, boolean transpose1, String file2, boolean transpose2, boolean addN,
        String probeFilter, String outfile) throws IOException {

    // exp has individuals on columns, check whether this is also the case for the cel
    // otherwise, transpose, make index
    int ct = 0;/*  w  w w.jav  a  2  s .  c  o m*/

    Set<String> probesToFilter = null;
    if (probeFilter != null) {
        TextFile tf1 = new TextFile(probeFilter, TextFile.R);
        probesToFilter = tf1.readAsSet(0, TextFile.tab);
        tf1.close();
    }

    DoubleMatrixDataset<String, String> exp = new DoubleMatrixDataset<String, String>(file1, probesToFilter);
    if (transpose1) {
        exp.transposeDataset();
    }
    DoubleMatrixDataset<String, String> cel = new DoubleMatrixDataset<String, String>(file2);
    if (transpose2) {
        cel.transposeDataset();
    }

    List<String> colsCel = cel.colObjects;

    for (String s : colsCel) {
        if (exp.hashCols.containsKey(s)) {

            ct++;
        }
    }
    if (ct == 0) {
        cel.transposeDataset();
        colsCel = cel.colObjects;
        for (String s : colsCel) {
            if (exp.hashCols.containsKey(s)) {
                ct++;
            }
        }
    }

    int[] index = new int[exp.nrCols];
    for (int i = 0; i < exp.nrCols; i++) {
        Integer id = cel.hashCols.get(exp.colObjects.get(i));
        if (id == null) {
            index[i] = -1;
        } else {
            index[i] = id;
        }
    }
    System.out.println("Found " + ct + " matching columns");
    SpearmansCorrelation corr = new SpearmansCorrelation();
    TextFile outputfile1 = new TextFile(outfile + "-Spearman.txt", TextFile.W);
    TextFile outputfile2 = new TextFile(outfile + "-Pearson.txt", TextFile.W);

    String header = "-";
    for (int row2 = 0; row2 < cel.nrRows; row2++) {
        if (addN) {
            header += "\t" + cel.rowObjects.get(row2) + "\t" + cel.rowObjects.get(row2) + "-N";
        } else {
            header += "\t" + cel.rowObjects.get(row2);
        }
    }
    outputfile1.writeln(header);
    outputfile2.writeln(header);

    for (int row = 0; row < exp.nrRows; row++) {

        String spearmanout = exp.rowObjects.get(row);
        String pearsonout = exp.rowObjects.get(row);

        for (int row2 = 0; row2 < cel.nrRows; row2++) {

            int ct2 = 0;
            for (int col = 0; col < exp.nrCols; col++) {
                int id = index[col];
                if (id != -1) {
                    // check whether value != null;
                    if (!Double.isNaN(exp.rawData[row][col]) && !Double.isNaN(cel.rawData[row2][id])) {
                        ct2++;
                    }
                }
            }

            double[] valsx = new double[ct2];
            double[] valsy = new double[ct2];
            int z = 0;
            for (int col = 0; col < exp.nrCols; col++) {
                int id = index[col];
                if (id != -1) {
                    // check whether value != null;
                    if (!Double.isNaN(exp.rawData[row][col]) && !Double.isNaN(cel.rawData[row2][id])) {
                        valsx[z] = exp.rawData[row][col];
                        valsy[z] = cel.rawData[row2][id];
                        z++;
                    }
                }
            }
            valsx = ztransform(valsx);
            valsy = ztransform(valsy);
            double spearman = corr.correlation(valsx, valsy);
            double pearson = JSci.maths.ArrayMath.correlation(valsx, valsy);
            if (addN) {
                pearsonout += "\t" + pearson + "\t" + ct2;
                spearmanout += "\t" + spearman + "\t" + ct2;
            } else {
                pearsonout += "\t" + pearson;
                spearmanout += "\t" + spearman;
            }
        }
        outputfile1.writeln(spearmanout);
        outputfile2.writeln(pearsonout);

    }
    outputfile1.close();
    outputfile2.close();
}

From source file:org.apache.solr.client.solrj.io.eval.CorrelationEvaluator.java

@Override
public Object doWork(Object... values) throws IOException {

    if (values.length == 2) {
        Object first = values[0];
        Object second = values[1];

        if (null == first) {
            throw new IOException(
                    String.format(Locale.ROOT, "Invalid expression %s - null found for the first value",
                            toExpression(constructingFactory)));
        }/*from  www .  j  a v  a 2s  .  c o m*/
        if (null == second) {
            throw new IOException(
                    String.format(Locale.ROOT, "Invalid expression %s - null found for the second value",
                            toExpression(constructingFactory)));
        }
        if (!(first instanceof List<?>)) {
            throw new IOException(String.format(Locale.ROOT,
                    "Invalid expression %s - found type %s for the first value, expecting a list of numbers",
                    toExpression(constructingFactory), first.getClass().getSimpleName()));
        }
        if (!(second instanceof List<?>)) {
            throw new IOException(String.format(Locale.ROOT,
                    "Invalid expression %s - found type %s for the second value, expecting a list of numbers",
                    toExpression(constructingFactory), first.getClass().getSimpleName()));
        }

        if (type.equals(CorrelationType.pearsons)) {
            PearsonsCorrelation pearsonsCorrelation = new PearsonsCorrelation();
            return pearsonsCorrelation.correlation(
                    ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray(),
                    ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue())
                            .toArray());
        } else if (type.equals(CorrelationType.kendalls)) {
            KendallsCorrelation kendallsCorrelation = new KendallsCorrelation();
            return kendallsCorrelation.correlation(
                    ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray(),
                    ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue())
                            .toArray());

        } else if (type.equals(CorrelationType.spearmans)) {
            SpearmansCorrelation spearmansCorrelation = new SpearmansCorrelation();
            return spearmansCorrelation.correlation(
                    ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray(),
                    ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue())
                            .toArray());
        } else {
            return null;
        }
    } else if (values.length == 1) {
        if (values[0] instanceof Matrix) {
            Matrix matrix = (Matrix) values[0];
            double[][] data = matrix.getData();
            if (type.equals(CorrelationType.pearsons)) {
                PearsonsCorrelation pearsonsCorrelation = new PearsonsCorrelation(data);
                RealMatrix corrMatrix = pearsonsCorrelation.getCorrelationMatrix();
                double[][] corrMatrixData = corrMatrix.getData();
                Matrix realMatrix = new Matrix(corrMatrixData);
                realMatrix.addToContext("corr", pearsonsCorrelation);
                return realMatrix;
            } else if (type.equals(CorrelationType.kendalls)) {
                KendallsCorrelation kendallsCorrelation = new KendallsCorrelation(data);
                RealMatrix corrMatrix = kendallsCorrelation.getCorrelationMatrix();
                double[][] corrMatrixData = corrMatrix.getData();
                Matrix realMatrix = new Matrix(corrMatrixData);
                realMatrix.addToContext("corr", kendallsCorrelation);
                return realMatrix;
            } else if (type.equals(CorrelationType.spearmans)) {
                SpearmansCorrelation spearmansCorrelation = new SpearmansCorrelation(
                        new Array2DRowRealMatrix(data));
                RealMatrix corrMatrix = spearmansCorrelation.getCorrelationMatrix();
                double[][] corrMatrixData = corrMatrix.getData();
                Matrix realMatrix = new Matrix(corrMatrixData);
                realMatrix.addToContext("corr", spearmansCorrelation.getRankCorrelation());
                return realMatrix;
            } else {
                return null;
            }
        } else {
            throw new IOException(
                    "corr function operates on either two numeric arrays or a single matrix as parameters.");
        }
    } else {
        throw new IOException(
                "corr function operates on either two numeric arrays or a single matrix as parameters.");
    }
}

From source file:org.apache.solr.client.solrj.io.eval.SpearmansCorrelationEvaluator.java

@Override
public Object doWork(Object first, Object second) throws IOException {
    if (null == first) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - null found for the first value", toExpression(constructingFactory)));
    }/*from  w w  w.  j a  va  2 s.c  om*/
    if (null == second) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - null found for the second value", toExpression(constructingFactory)));
    }
    if (!(first instanceof List<?>)) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - found type %s for the first value, expecting a list of numbers",
                toExpression(constructingFactory), first.getClass().getSimpleName()));
    }
    if (!(second instanceof List<?>)) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - found type %s for the second value, expecting a list of numbers",
                toExpression(constructingFactory), first.getClass().getSimpleName()));
    }

    SpearmansCorrelation spearmansCorrelation = new SpearmansCorrelation();

    return spearmansCorrelation.correlation(
            ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray(),
            ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray());
}

From source file:org.cbioportal.service.impl.GenesetCorrelationServiceImpl.java

/**
 * Calculates the Spearman correlation between the genesetValues list and the list built up from  
 * geneGeneticDataItems for the given sampleIds.
 * /* ww w .java2  s  . c om*/
 * Before calculating the correlation, this method prepares the gene values, filtering both them and genesetValues,
 * removing samples where the value is not present in either gene or gene set dimension.
 * 
 * @param sampleIds: samples over which to calculate correlation
 * @param geneGeneticDataItems: gene (expression) values for the set of samples
 * @param genesetValues: gene set scores for the set of samples
 * 
 * @return: Spearman's correlation value between values in geneGeneticDataItems and genesetValues.
 */
private double calculateCorrelation(List<String> sampleIds, List<GeneMolecularData> geneGeneticDataItems,
        double[] genesetValues) {

    //index geneData values
    Map<String, Double> sampleValues = new HashMap<String, Double>();
    for (GeneMolecularData geneGeneticDataItem : geneGeneticDataItems) {
        double value = Double.NaN;
        if (NumberUtils.isNumber(geneGeneticDataItem.getValue())) {
            value = Double.parseDouble(geneGeneticDataItem.getValue());
        }
        sampleValues.put(geneGeneticDataItem.getSampleId(), value);
    }
    //get values
    List<Double> geneValueList = new ArrayList<Double>();
    List<Double> genesetValueList = new ArrayList<Double>();
    for (int i = 0; i < sampleIds.size(); i++) {
        String sampleId = sampleIds.get(i);
        Double value = sampleValues.get(sampleId);
        if (value == null) {
            value = Double.NaN; //set to NaN when value is not available for this sample
        }
        // if both this value and genesetValues[i] are NaN, then skip this item (since it will not be allowed by the correlation method):
        if (!Double.isNaN(value) && !Double.isNaN(genesetValues[i])) {
            geneValueList.add(value);
            // build up genesetValues is same way:
            genesetValueList.add(genesetValues[i]);
        }
    }
    // final filtered lists:
    double[] geneValuesFiltered = geneValueList.stream().mapToDouble(d -> d).toArray();
    double[] genesetValuesFiltered = genesetValueList.stream().mapToDouble(d -> d).toArray();
    double correlationValue = 0;
    // arrays need to be at least 2 long to calculate correlation: 
    if (geneValuesFiltered.length >= 2) {
        // calculate spearman correlation
        SpearmansCorrelation spearmansCorrelation = new SpearmansCorrelation();
        correlationValue = spearmansCorrelation.correlation(geneValuesFiltered, genesetValuesFiltered);
    }
    return correlationValue;
}

From source file:org.mskcc.cbio.portal.servlet.CalcCoExp.java

/**
 * Handles the HTTP POST Request.//from   w  ww .  java2s .com
 *
 * @param httpServletRequest  HttpServletRequest
 * @param httpServletResponse HttpServletResponse
 * @throws ServletException
 */
protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
        throws ServletException, IOException {

    String geneXArr = httpServletRequest.getParameter("gene_x");
    String geneYArr = httpServletRequest.getParameter("gene_y");

    String[] _geneXstrArr = geneXArr.split("\\s+");
    String[] _geneYstrArr = geneYArr.split("\\s+");

    //Convert strings to doubles
    double[] _geneXvalArr = new double[_geneXstrArr.length];
    double[] _geneYvalArr = new double[_geneYstrArr.length];
    for (int i = 0; i < _geneXstrArr.length; i++) {
        double _valX = Double.parseDouble(_geneXstrArr[i]);
        double _valY = Double.parseDouble(_geneYstrArr[i]);
        _geneXvalArr[i] = _valX;
        _geneYvalArr[i] = _valY;
    }

    //Calculate Scores
    PearsonsCorrelation pearsonsCorrelation = new PearsonsCorrelation();
    SpearmansCorrelation spearmansCorrelation = new SpearmansCorrelation();
    double pearson = pearsonsCorrelation.correlation(_geneXvalArr, _geneYvalArr);
    double spearman = spearmansCorrelation.correlation(_geneXvalArr, _geneYvalArr);

    httpServletResponse.setContentType("text/html");
    PrintWriter out = httpServletResponse.getWriter();
    JSONValue.writeJSONString(pearson + " " + spearman, out);
}

From source file:org.mskcc.cbio.portal.servlet.GetCoExpressionJSON.java

/**
 * Handles the HTTP POST Request./* w  w w . j a v  a2 s  . c o  m*/
 *
 * @param httpServletRequest  HttpServletRequest
 * @param httpServletResponse HttpServletResponse
 * @throws ServletException
 */
protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
        throws ServletException, IOException {

    String cancerStudyIdentifier = httpServletRequest.getParameter("cancer_study_id");
    String geneSymbol = httpServletRequest.getParameter("gene");
    if (httpServletRequest instanceof XssRequestWrapper) {
        geneSymbol = ((XssRequestWrapper) httpServletRequest).getRawParameter("gene");
    }
    String profileId = httpServletRequest.getParameter("profile_id");
    String caseSetId = httpServletRequest.getParameter("case_set_id");
    String caseIdsKey = httpServletRequest.getParameter("case_ids_key");
    boolean isFullResult = Boolean.parseBoolean(httpServletRequest.getParameter("is_full_result"));

    PearsonsCorrelation pearsonsCorrelation = new PearsonsCorrelation();
    SpearmansCorrelation spearmansCorrelation = new SpearmansCorrelation();
    DaoGeneOptimized daoGeneOptimized = DaoGeneOptimized.getInstance();

    CanonicalGene geneObj = daoGeneOptimized.getGene(geneSymbol);
    Long queryGeneId = geneObj.getEntrezGeneId();

    if (!isFullResult) {
        ArrayList<JsonNode> fullResultJson = new ArrayList<JsonNode>();
        ObjectMapper mapper = new ObjectMapper();
        GeneticProfile final_gp = DaoGeneticProfile.getGeneticProfileByStableId(profileId);
        if (final_gp != null) {
            try {
                Map<Long, double[]> map = CoExpUtil.getExpressionMap(final_gp.getGeneticProfileId(), caseSetId,
                        caseIdsKey);
                int mapSize = map.size();
                List<Long> genes = new ArrayList<Long>(map.keySet());
                for (int i = 0; i < mapSize; i++) {
                    double[] query_gene_exp = map.get(queryGeneId);
                    long compared_gene_id = genes.get(i);
                    double[] compared_gene_exp = map.get(compared_gene_id);
                    if (compared_gene_exp != null && query_gene_exp != null) {
                        //Filter out cases with empty value on either side
                        int min_length = query_gene_exp.length < compared_gene_exp.length
                                ? query_gene_exp.length
                                : compared_gene_exp.length;
                        ArrayList<Double> new_query_gene_exp_arrlist = new ArrayList<Double>();
                        ArrayList<Double> new_compared_gene_exp_arrlist = new ArrayList<Double>();
                        for (int k = 0; k < min_length; k++) {
                            if (!Double.isNaN(query_gene_exp[k]) && !Double.isNaN(compared_gene_exp[k])) {
                                new_query_gene_exp_arrlist.add(query_gene_exp[k]);
                                new_compared_gene_exp_arrlist.add(compared_gene_exp[k]);
                            }
                        }
                        Double[] _new_query_gene_exp = new_query_gene_exp_arrlist.toArray(new Double[0]);
                        Double[] _new_compared_gene_exp = new_compared_gene_exp_arrlist.toArray(new Double[0]);
                        //convert double object to primitive data
                        double[] new_query_gene_exp = new double[_new_query_gene_exp.length];
                        double[] new_compared_gene_exp = new double[_new_compared_gene_exp.length];
                        for (int m = 0; m < _new_query_gene_exp.length; m++) {
                            new_query_gene_exp[m] = _new_query_gene_exp[m].doubleValue();
                            new_compared_gene_exp[m] = _new_compared_gene_exp[m].doubleValue();
                        }

                        if (new_query_gene_exp.length != 0 && new_compared_gene_exp.length != 0) {
                            double pearson = pearsonsCorrelation.correlation(new_query_gene_exp,
                                    new_compared_gene_exp);
                            if ((pearson >= coExpScoreThreshold || pearson <= (-1) * coExpScoreThreshold)
                                    && (compared_gene_id != queryGeneId)) {
                                //Only calculate spearman with high scored pearson gene pairs.
                                double spearman = spearmansCorrelation.correlation(new_query_gene_exp,
                                        new_compared_gene_exp);
                                if ((spearman >= coExpScoreThreshold || spearman <= (-1) * coExpScoreThreshold)
                                        && ((spearman > 0 && pearson > 0) || (spearman < 0 && pearson < 0))) {
                                    CanonicalGene comparedGene = daoGeneOptimized.getGene(compared_gene_id);
                                    ObjectNode _scores = mapper.createObjectNode();
                                    _scores.put("gene", comparedGene.getHugoGeneSymbolAllCaps());
                                    _scores.put("cytoband", comparedGene.getCytoband());
                                    _scores.put("pearson", pearson);
                                    _scores.put("spearman", spearman);
                                    fullResultJson.add(_scores);
                                }
                            }
                        }
                    }
                }
                httpServletResponse.setContentType("application/json");
                PrintWriter out = httpServletResponse.getWriter();
                mapper.writeValue(out, fullResultJson);
            } catch (DaoException e) {
                System.out.println(e.getMessage());
            }
        } else {
            JSONObject emptyResult = new JSONObject();
            httpServletResponse.setContentType("application/json");
            PrintWriter out = httpServletResponse.getWriter();
            mapper.writeValue(out, emptyResult);
        }
    } else {
        StringBuilder fullResutlStr = new StringBuilder();
        fullResutlStr.append("Gene Symbol\tCytoband\tPearson Score\tSpearman Score\n");
        GeneticProfile final_gp = DaoGeneticProfile.getGeneticProfileByStableId(profileId);
        if (final_gp != null) {
            try {
                Map<Long, double[]> map = CoExpUtil.getExpressionMap(final_gp.getGeneticProfileId(), caseSetId,
                        caseIdsKey);
                int mapSize = map.size();
                List<Long> genes = new ArrayList<Long>(map.keySet());
                for (int i = 0; i < mapSize; i++) {
                    double[] query_gene_exp = map.get(queryGeneId);
                    long compared_gene_id = genes.get(i);
                    double[] compared_gene_exp = map.get(compared_gene_id);
                    if (compared_gene_exp != null && query_gene_exp != null) {
                        //Filter out cases with empty value on either side
                        int min_length = (query_gene_exp.length < compared_gene_exp.length)
                                ? query_gene_exp.length
                                : compared_gene_exp.length;
                        ArrayList<Double> new_query_gene_exp_arrlist = new ArrayList<Double>();
                        ArrayList<Double> new_compared_gene_exp_arrlist = new ArrayList<Double>();
                        for (int k = 0; k < min_length; k++) {
                            if (!Double.isNaN(query_gene_exp[k]) && !Double.isNaN(compared_gene_exp[k])) {
                                new_query_gene_exp_arrlist.add(query_gene_exp[k]);
                                new_compared_gene_exp_arrlist.add(compared_gene_exp[k]);
                            }
                        }
                        Double[] _new_query_gene_exp = new_query_gene_exp_arrlist.toArray(new Double[0]);
                        Double[] _new_compared_gene_exp = new_compared_gene_exp_arrlist.toArray(new Double[0]);
                        //convert double object to primitive data
                        double[] new_query_gene_exp = new double[_new_query_gene_exp.length];
                        double[] new_compared_gene_exp = new double[_new_compared_gene_exp.length];
                        for (int m = 0; m < _new_query_gene_exp.length; m++) {
                            new_query_gene_exp[m] = _new_query_gene_exp[m].doubleValue();
                            new_compared_gene_exp[m] = _new_compared_gene_exp[m].doubleValue();
                        }
                        if (new_query_gene_exp.length != 0 && new_compared_gene_exp.length != 0
                                && compared_gene_id != queryGeneId) {
                            double pearson = pearsonsCorrelation.correlation(new_query_gene_exp,
                                    new_compared_gene_exp);
                            double spearman = spearmansCorrelation.correlation(new_query_gene_exp,
                                    new_compared_gene_exp);
                            CanonicalGene comparedGene = daoGeneOptimized.getGene(compared_gene_id);
                            fullResutlStr.append(
                                    comparedGene.getHugoGeneSymbolAllCaps() + "\t" + comparedGene.getCytoband()
                                            + "\t" + (double) Math.round(pearson * 100) / 100 + "\t"
                                            + (double) Math.round(spearman * 100) / 100 + "\n");
                        }
                    }
                }
                //construct file name
                String fileName = "coexpression_" + geneSymbol + "_"
                        + final_gp.getProfileName().replaceAll("\\s+", "_") + "_"
                        + cancerStudyIdentifier.replaceAll("\\s+", "_") + ".txt";

                httpServletResponse.setContentType("text/html");
                httpServletResponse.setContentType("application/force-download");
                httpServletResponse.setHeader("content-disposition", "inline; filename='" + fileName + "'");
                PrintWriter out = httpServletResponse.getWriter();
                JSONValue.writeJSONString(fullResutlStr, out);
            } catch (DaoException e) {
                System.out.println(e.getMessage());
            }
        } else {
            JSONObject emptyResult = new JSONObject();
            httpServletResponse.setContentType("application/json");
            PrintWriter out = httpServletResponse.getWriter();
            JSONValue.writeJSONString(emptyResult, out);
        }
    }

}

From source file:SocialRec.UserAgent.java

public static Double calculateRankCo(List<Double> X, List<Double> Y) {
    SpearmansCorrelation SC = new SpearmansCorrelation();
    double[] xArray = toDoubleArray(X);
    double[] yArray = toDoubleArray(Y);
    double corr = SC.correlation(xArray, yArray);

    return corr;// w ww  .j  ava 2 s  .c  o m
}