Search in sources :

Example 1 with DoubleArrayVector

use of org.renjin.sexp.DoubleArrayVector in project atav by igm-team.

the class LogisticOutput method getPValue.

private double getPValue(String model) {
    if (model.equals("recessive") && !isRecessive()) {
        return Data.NA;
    }
    int[] genoList = modelGenoMap.get(model);
    if (null == genoList || genoList.length <= 1) {
        return Data.NA;
    }
    try {
        // Put indices
        MathManager.getRenjinEngine().put("ind", sampleIndexList);
        // Getting genotype info
        MathManager.getRenjinEngine().put("xt1" + model, genoList);
        // The additive model creates a 3rd level for genotype so need to factorize
        if (model.equals("additive")) {
            MathManager.getRenjinEngine().eval("xt1" + model + " <- factor(" + "xt1" + model + ")");
        }
        // Getting covariate subset
        for (int i = 1; i <= SampleManager.getCovariateNum(); i++) {
            MathManager.getRenjinEngine().eval("xt" + (i + 1) + "<- x" + i + "[ind+1]");
        }
        // Getting response subset
        MathManager.getRenjinEngine().eval("yt <- y[ind+1]");
        // Formulating regression with geno
        MathManager.getRenjinEngine().eval("withgeno <-glm(" + expression.toString().replaceAll("xt1\\+", "xt1" + model + "\\+") + ", family=\"binomial\" )");
        if (model.equals("additive")) {
            // Formulating regression without geno for additive
            MathManager.getRenjinEngine().eval("withoutgeno <-glm(" + expression.toString().replaceAll("xt1\\+", "") + ", family=\"binomial\" )");
            // Getting P value for genotype
            DoubleArrayVector res = (DoubleArrayVector) MathManager.getRenjinEngine().eval("anova(withgeno, withoutgeno, test=\"LRT\")$\"Pr(>Chi)\"[2]");
            return (null != res) ? res.getElementAsDouble(0) : Data.NA;
        } else {
            // Getting P value for genotype
            DoubleArrayVector res = (DoubleArrayVector) MathManager.getRenjinEngine().eval("coef(summary(withgeno))[2,4]");
            return (null != res) ? res.getElementAsDouble(0) : Data.NA;
        }
    } catch (Exception e) {
        ErrorManager.send(e);
        return Data.NA;
    }
}
Also used : DoubleArrayVector(org.renjin.sexp.DoubleArrayVector)

Example 2 with DoubleArrayVector

use of org.renjin.sexp.DoubleArrayVector in project polyGembler by c-zhou.

the class RenjinLOD method run.

@Override
public void run() {
    // TODO Auto-generated method stub
    final BidiMap<Integer, String> scaffs = new TreeBidiMap<Integer, String>();
    final List<Double> boundary_lg = new ArrayList<Double>();
    try {
        BufferedReader br = Utils.getBufferedReader(txt_in);
        String line = br.readLine();
        String[] s;
        while (line != null) {
            if (line.startsWith("$order")) {
                int i = 0;
                while ((line = br.readLine()) != null && line.length() > 0) {
                    s = line.split("\\s+")[1].split("-");
                    for (String scf : s) scaffs.put(i++, scf.replaceAll("^scaffold", ""));
                    boundary_lg.add((double) i);
                }
                break;
            }
            line = br.readLine();
        }
        br.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    int n = scaffs.size();
    double[][] recomfJ = new double[n][n];
    double[][] lodscrJ = new double[n][n];
    final double constr = Math.log10(.5);
    try {
        String line;
        String[] s;
        Integer scf_i1, scf_i2;
        double f, l, NR, R;
        for (String in : ds_in) {
            BufferedReader br = Utils.getBufferedReader(in);
            while ((line = br.readLine()) != null) {
                s = line.split("\\s+");
                scf_i1 = scaffs.getKey(s[6]);
                scf_i2 = scaffs.getKey(s[7]);
                if (scf_i1 == null || scf_i2 == null)
                    continue;
                f = Double.parseDouble(s[1]);
                recomfJ[scf_i1][scf_i2] = f;
                recomfJ[scf_i2][scf_i1] = f;
                if (f == 0) {
                    l = 100.0;
                } else {
                    NR = (1 - f);
                    R = f;
                    l = Math.min(100.0, n_hap * (Math.log10(NR) * NR + Math.log10(R) * R - constr));
                }
                lodscrJ[scf_i1][scf_i2] = l;
                lodscrJ[scf_i2][scf_i1] = l;
            }
            br.close();
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    DoubleMatrixBuilder reccoo = new DoubleMatrixBuilder(n * (n - 1), 5);
    int w = 0;
    for (int i = 1; i < n; i++) {
        for (int j = 0; j < i; j++) {
            reccoo.set(w, 0, j);
            reccoo.set(w, 1, i);
            reccoo.set(w, 2, j + 1);
            reccoo.set(w, 3, i + 1);
            reccoo.set(w, 4, lodscrJ[i][j]);
            w++;
            reccoo.set(w, 0, i);
            reccoo.set(w, 1, j);
            reccoo.set(w, 2, i + 1);
            reccoo.set(w, 3, j + 1);
            reccoo.set(w, 4, recomfJ[i][j]);
            w++;
        }
    }
    DoubleMatrixBuilder recomf = new DoubleMatrixBuilder(n, n);
    DoubleMatrixBuilder lodscr = new DoubleMatrixBuilder(n, n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            recomf.set(i, j, recomfJ[i][j]);
            lodscr.set(i, j, lodscrJ[i][j]);
        }
    }
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("Renjin");
    if (engine == null) {
        throw new RuntimeException("Renjin not found!!!");
    }
    StringVector scf = new StringArrayVector(scaffs.values());
    recomf.setRowNames(scf);
    recomf.setColNames(scf);
    lodscr.setRowNames(scf);
    lodscr.setColNames(scf);
    try {
        Context context = Context.newTopLevelContext();
        FileOutputStream fos = new FileOutputStream(r_out);
        GZIPOutputStream zos = new GZIPOutputStream(fos);
        RDataWriter writer = new RDataWriter(context, zos);
        ListVector.NamedBuilder dat = new ListVector.NamedBuilder();
        dat.add("scaffs", scf);
        dat.add("boundary_lg", new DoubleArrayVector(boundary_lg));
        dat.add("recomf", recomf.build());
        dat.add("lodscr", lodscr.build());
        dat.add("reccoo", reccoo.build());
        writer.save(dat.build());
        writer.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : DoubleArrayVector(org.renjin.sexp.DoubleArrayVector) ArrayList(java.util.ArrayList) ScriptEngineManager(javax.script.ScriptEngineManager) StringArrayVector(org.renjin.sexp.StringArrayVector) RDataWriter(org.renjin.primitives.io.serialization.RDataWriter) GZIPOutputStream(java.util.zip.GZIPOutputStream) TreeBidiMap(org.apache.commons.collections4.bidimap.TreeBidiMap) DoubleMatrixBuilder(org.renjin.primitives.matrix.DoubleMatrixBuilder) Context(org.renjin.eval.Context) IOException(java.io.IOException) ScriptEngine(javax.script.ScriptEngine) StringVector(org.renjin.sexp.StringVector) ListVector(org.renjin.sexp.ListVector) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader)

Example 3 with DoubleArrayVector

use of org.renjin.sexp.DoubleArrayVector in project rsession by yannrichet.

the class RenjinSession method set.

@Override
public boolean set(String varname, double[][] data, String... names) {
    if (data == null) {
        if (names == null) {
            return false;
        }
        List<SEXP> nulls = new LinkedList<>();
        for (int i = 0; i < names.length; i++) {
            nulls.add(Null.INSTANCE.clone());
        }
        ListVector l = new ListVector(nulls);
        synchronized (R) {
            R.put(varname, l);
            R.put(varname + ".names", new StringArrayVector(names));
            try {
                R.eval("names(" + varname + ") <- " + varname + ".names");
            // R.eval(varname + " <- data.frame(" + varname + ")");
            } catch (ScriptException ex) {
                ex.printStackTrace();
                return false;
            }
        }
        return true;
    } else {
        DoubleVector[] d = new DoubleVector[data[0].length];
        for (int i = 0; i < d.length; i++) {
            d[i] = new DoubleArrayVector(DoubleArray.getColumnCopy(data, i));
        }
        ListVector l = new ListVector(d);
        // l.setAttribute(Symbols.NAMES, new StringArrayVector(names));
        synchronized (R) {
            R.put(varname, l);
            // R.put("names("+varname+")",new StringArrayVector(names));
            R.put(varname + ".names", new StringArrayVector(names));
            try {
                R.eval("names(" + varname + ") <- " + varname + ".names");
                R.eval(varname + " <- data.frame(" + varname + ")");
            } catch (ScriptException ex) {
                ex.printStackTrace();
                return false;
            }
        }
        return true;
    }
}
Also used : DoubleArrayVector(org.renjin.sexp.DoubleArrayVector) ScriptException(javax.script.ScriptException) ListVector(org.renjin.sexp.ListVector) StringArrayVector(org.renjin.sexp.StringArrayVector) DoubleVector(org.renjin.sexp.DoubleVector) SEXP(org.renjin.sexp.SEXP) LinkedList(java.util.LinkedList)

Aggregations

DoubleArrayVector (org.renjin.sexp.DoubleArrayVector)3 ListVector (org.renjin.sexp.ListVector)2 StringArrayVector (org.renjin.sexp.StringArrayVector)2 BufferedReader (java.io.BufferedReader)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 ScriptEngine (javax.script.ScriptEngine)1 ScriptEngineManager (javax.script.ScriptEngineManager)1 ScriptException (javax.script.ScriptException)1 TreeBidiMap (org.apache.commons.collections4.bidimap.TreeBidiMap)1 Context (org.renjin.eval.Context)1 RDataWriter (org.renjin.primitives.io.serialization.RDataWriter)1 DoubleMatrixBuilder (org.renjin.primitives.matrix.DoubleMatrixBuilder)1 DoubleVector (org.renjin.sexp.DoubleVector)1 SEXP (org.renjin.sexp.SEXP)1 StringVector (org.renjin.sexp.StringVector)1