Search in sources :

Example 1 with DoubleMatrixBuilder

use of org.renjin.primitives.matrix.DoubleMatrixBuilder 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 DoubleMatrixBuilder

use of org.renjin.primitives.matrix.DoubleMatrixBuilder 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;
        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 iMat = new DoubleMatrixBuilder(n, n);
        DoubleMatrixBuilder dAllMat = 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]);
            double d = Double.parseDouble(s[0]);
            dMat.set(i, j, d);
            dMat.set(j, i, d);
            iMat.set(i, j, w + 1);
            iMat.set(j, i, w + 1 + A);
            for (int k = 0; k < 4; k++) {
                d = Double.parseDouble(s[k + 1]);
                dAllMat.set(w, k, d);
                dAllMat.set(w + A, (k == 0 || k == 3) ? k : (3 - k), d);
            }
            w++;
            line = br.readLine();
        }
        br.close();
        StringVector scf = new StringArrayVector(scaffs.values());
        dMat.setRowNames(scf);
        dMat.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("distanceAll", dAllMat.build());
        Rdat.add("indexMat", iMat.build());
        Rdat.add("distanceMat", dMat.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)

Example 3 with DoubleMatrixBuilder

use of org.renjin.primitives.matrix.DoubleMatrixBuilder in project polyGembler by c-zhou.

the class TenXMoleculeStats method r.

private void r() {
    // TODO Auto-generated method stub
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("Renjin");
    if (engine == null) {
        throw new RuntimeException("Renjin not found!!!");
    }
    try {
        Process p = Runtime.getRuntime().exec("wc -l " + out_stats);
        p.waitFor();
        BufferedReader pbr = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        int mol_n = 0;
        while ((line = pbr.readLine()) != null) mol_n = Integer.parseInt(line.split("\\s+")[0]);
        pbr.close();
        DoubleMatrixBuilder mol_len = new DoubleMatrixBuilder(1, mol_n);
        final Map<String, Integer> bc_stats = new HashMap<String, Integer>();
        BufferedReader br = Utils.getBufferedReader(out_stats);
        String[] s;
        int i = 0;
        while ((line = br.readLine()) != null) {
            s = line.split("\\s+");
            if (bc_stats.containsKey(s[0]))
                bc_stats.put(s[0], bc_stats.get(s[0]) + 1);
            else
                bc_stats.put(s[0], 1);
            mol_len.set(0, i++, Integer.parseInt(s[2]));
        }
        br.close();
        int bc_n = bc_stats.size();
        DoubleMatrixBuilder bc_nstats = new DoubleMatrixBuilder(1, bc_n);
        i = 0;
        for (Map.Entry<String, Integer> entry : bc_stats.entrySet()) bc_nstats.set(0, i++, entry.getValue());
        Context context = Context.newTopLevelContext();
        FileOutputStream fos = new FileOutputStream(out_stats + ".RData");
        GZIPOutputStream zos = new GZIPOutputStream(fos);
        RDataWriter writer = new RDataWriter(context, zos);
        ListVector.NamedBuilder Rdat = new ListVector.NamedBuilder();
        Rdat.add("mol_stats", mol_len.build());
        Rdat.add("bc_stats", bc_nstats.build());
        writer.save(Rdat.build());
        writer.close();
    } catch (IOException | InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : HashMap(java.util.HashMap) ScriptEngineManager(javax.script.ScriptEngineManager) RDataWriter(org.renjin.primitives.io.serialization.RDataWriter) GZIPOutputStream(java.util.zip.GZIPOutputStream) DoubleMatrixBuilder(org.renjin.primitives.matrix.DoubleMatrixBuilder) Context(org.renjin.eval.Context) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) ScriptEngine(javax.script.ScriptEngine) ListVector(org.renjin.sexp.ListVector) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

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