Search in sources :

Example 1 with StringArrayVector

use of org.renjin.sexp.StringArrayVector 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 2 with StringArrayVector

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

the class RenjinLOD method run2.

public void run2() {
    // 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();
    }
    double[][] recomf = new double[scaffs.size()][scaffs.size()];
    double[][] lodscr = new double[scaffs.size()][scaffs.size()];
    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]);
                recomf[scf_i1][scf_i2] = f;
                recomf[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));
                }
                lodscr[scf_i1][scf_i2] = l;
                lodscr[scf_i2][scf_i1] = l;
            }
            br.close();
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    StringVector scf = new StringArrayVector(scaffs.values());
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) StringArrayVector(org.renjin.sexp.StringArrayVector) StringVector(org.renjin.sexp.StringVector) BufferedReader(java.io.BufferedReader) TreeBidiMap(org.apache.commons.collections4.bidimap.TreeBidiMap)

Example 3 with StringArrayVector

use of org.renjin.sexp.StringArrayVector 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)

Example 4 with StringArrayVector

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

the class RFUtils method makeRMatrix.

/**
 * make RData object from Renjin
 * renjin-script-engine-*-with dependencies.jar required
 * https://nexus.bedatadriven.com/content/groups/public/org/renjin/renjin-script-engine/
 */
protected static void makeRMatrix(String in_rf, String out_Rmat) {
    // TODO Auto-generated method stub
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("Renjin");
    if (engine == null) {
        throw new RuntimeException("Renjin not found!!!");
    }
    try {
        BufferedReader br = Utils.getBufferedReader(in_rf);
        final BidiMap<Integer, String> scaffs = new TreeBidiMap<Integer, String>();
        String line;
        String[] s;
        int w = 0;
        double d, l;
        while ((line = br.readLine()) != null && line.startsWith("##")) {
            scaffs.put(w++, line.replaceAll("^##", ""));
        }
        int n = scaffs.size();
        int A = w * (w - 1) / 2;
        DoubleMatrixBuilder dMat = new DoubleMatrixBuilder(n, n);
        DoubleMatrixBuilder lMat = new DoubleMatrixBuilder(n, n);
        DoubleMatrixBuilder iMat = new DoubleMatrixBuilder(n, n);
        DoubleMatrixBuilder dAllMat = new DoubleMatrixBuilder(A * 2, 4);
        DoubleMatrixBuilder lAllMat = new DoubleMatrixBuilder(A * 2, 4);
        w = 0;
        while (line != null) {
            s = line.split("\\s+");
            int i = scaffs.getKey(s[5]), j = scaffs.getKey(s[6]), hs = Integer.parseInt(s[7]);
            d = Math.min(RF_MAX, Double.parseDouble(s[0]));
            l = calcLODFromRf(d, hs);
            dMat.set(i, j, d);
            dMat.set(j, i, d);
            lMat.set(i, j, l);
            lMat.set(j, i, l);
            iMat.set(i, j, w + 1);
            iMat.set(j, i, w + 1 + A);
            for (int k = 0; k < 4; k++) {
                d = Math.min(RF_MAX, Double.parseDouble(s[k + 1]));
                l = calcLODFromRf(d, hs);
                dAllMat.set(w, k, d);
                dAllMat.set(w + A, (k == 0 || k == 3) ? k : (3 - k), d);
                lAllMat.set(w, k, l);
                lAllMat.set(w + A, (k == 0 || k == 3) ? k : (3 - k), l);
            }
            w++;
            line = br.readLine();
        }
        br.close();
        StringVector scf = new StringArrayVector(scaffs.values());
        dMat.setRowNames(scf);
        dMat.setColNames(scf);
        lMat.setRowNames(scf);
        lMat.setColNames(scf);
        iMat.setRowNames(scf);
        iMat.setColNames(scf);
        Context context = Context.newTopLevelContext();
        FileOutputStream fos = new FileOutputStream(out_Rmat);
        GZIPOutputStream zos = new GZIPOutputStream(fos);
        RDataWriter writer = new RDataWriter(context, zos);
        ListVector.NamedBuilder Rdat = new ListVector.NamedBuilder();
        Rdat.add("scaffs", scf);
        Rdat.add("n", n);
        Rdat.add("A", A);
        Rdat.add("distanceMat", dMat.build());
        Rdat.add("distanceAll", dAllMat.build());
        Rdat.add("lodMat", lMat.build());
        Rdat.add("lodAll", lAllMat.build());
        Rdat.add("indexMat", iMat.build());
        writer.save(Rdat.build());
        writer.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : Context(org.renjin.eval.Context) ScriptEngineManager(javax.script.ScriptEngineManager) StringArrayVector(org.renjin.sexp.StringArrayVector) IOException(java.io.IOException) RDataWriter(org.renjin.primitives.io.serialization.RDataWriter) ScriptEngine(javax.script.ScriptEngine) StringVector(org.renjin.sexp.StringVector) GZIPOutputStream(java.util.zip.GZIPOutputStream) ListVector(org.renjin.sexp.ListVector) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) TreeBidiMap(org.apache.commons.collections4.bidimap.TreeBidiMap) DoubleMatrixBuilder(org.renjin.primitives.matrix.DoubleMatrixBuilder)

Aggregations

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