繁体   English   中英

RCaller,线程处理和Java GUI

[英]RCaller, thread handling, and a Java GUI

我正在制作一个Java GUI,以与同事的定制R包IntramiRExploreR配合使用,该包包括使用以下参数通过igraph和IntramiRExploreR的Visualization函数创建交互式图形的功能:

    Visualisation(miR,mRNA_type=c('GeneSymbol'),method,thresh,platform=Platform,visualisation = 'igraph',layout = 'interactive')

其中miR是通过选定的JCheckbox创建的向量,方法,阈值和平台是从JRadioButtons填充的。 我毫不怀疑该函数本身以及如何填充变量是正确的,因为我已经在R中运行了该函数,并使用了文本输出格式来运行该函数,并且两者都可以正常运行。

代码首先使用以下结果正确地填充JTable

    Visualisation(miR,mRNA_type=c('GeneSymbol'),method,thresh,platform=Platform)

输出使用以下内容可访问的文本

    caller.getParser().getAsStringArray(//one of seven parameters)

然后提供一个JButton来使用相同的参数和对象来调用R中的上述igraph函数。但是,当单击JButton时,会创建igraph,但是一旦完全制作完图形,便会放置其框架。 第二次单击该按钮,再次调用该函数,提供的错误是:

    Exception in thread "AWT-EventQueue-0" com.github.rcaller.exception.ExecutionException: Can not run C:\Program Files\R\R-3.3.0\bin\x64\Rscript.exe. Reason: java.lang.IllegalThreadStateException

我应该创建新线程来处理igraph可视化,还是我缺少RCaller中的某些方法可以处理此问题? 在调用第二个RCaller和RCode块之后,Java是否清空了我的对象的内存?

在不违反我对保密性的同意的情况下,我可以显示以下代码:

    public void actionPerformed(ActionEvent e){//if goButton is clicked
                if(e.getSource() == goButton){
                    JFrame resultFrame = new JFrame("Results: For full readout, use export button below.");//creates entire resultFrame
                    resultFrame.setLayout(new BorderLayout());
                    resultFrame.setSize(new Dimension(950,750));
                    JPanel resultBack = new JPanel();
                    resultBack.setLayout(new BorderLayout());//creates the resultBack to be placed into JScrollPane

                //RESULTS (from user query; calls R commands to fill out JTable)
                    //create int checkCnt to keep track of how much info is needed
                        int checkCnt = 0;
                        for(int t = 0;t<155;t++){
                            if(selected[0][t]==true){//if targets for one miR with index t is queried
                                checkCnt++;
                            }}

                //create JTable 
                        //create column names
                        String[] colNames = {"miRNA", "tar_GS", "tar_FB", "tar_CG", "Score", "Function", "Experiment", "Method"};

                        //determine threshold
                            int threshold=0;
                            if(checkCnt==1){threshold=100;}
                            if(checkCnt==2){threshold=50;}
                            if(checkCnt==3){threshold=33;}
                            if(checkCnt==4){threshold=25;}
                            if(checkCnt>=5){threshold=20;}

                            /*create RCaller and wire query to buttons; 
                            code handles table filling,
                            ///code1 handles graphic display*/
                             RCaller caller = RCaller.create();
                             RCaller caller1 = RCaller.create();
                             RCode code = RCode.create();
                             RCode code1 = RCode.create();
                             code.R_require("IntramiRExploreR");
                             code.R_require("futile.logger");
                             code.R_require("devtools");
                             code.R_require("Runiversal");
                             code1.R_require("IntramiRExploreR");
                             code1.R_require("futile.logger");
                             code1.R_require("devtools");

                             //create array of selected miRs to input to R code
                             String[] chosen = new String[checkCnt];
                             for(int kk=0;kk<checkCnt;kk++){
                                 chosen[kk] = litmiR(selected)[kk];
                             }
                             code.addStringArray("miR", chosen);
                             code.addInt("thresh", threshold);
                             code1.addStringArray("miR", chosen);
                             code1.addInt("thresh", threshold);
                             String method =new String();

                             if(Pears.isSelected()){
                                 method="Pearson";
                                 code.addString("method", method);
                                 code1.addString("method", method);
                                }
                             else if(Dist.isSelected()){
                                 method="Distance";
                                 code.addString("method", method);
                                 code1.addString("method", method);
                                }
                             else{
                                 method="Both";
                                 code.addString("method", method);
                                 code1.addString("method", method);
                             }
                             if(Affy1.isSelected()){
                                 String Platform="Affy1";
                                 code.addString("Platform", Platform);
                                 code1.addString("Platform", Platform);
                             }
                             else{
                                 String Platform="Affy2";
                                 code.addString("Platform", Platform);
                                 code1.addString("Platform", Platform);
                             }


                             code.addRCode("yy <-Visualisation(miR,mRNA_type=c('GeneSymbol'),method,thresh,platform=Platform)"); 
                              String [] aa= caller.getParser().getAsStringArray("miRNA"); 
                              String [] aa1= caller.getParser().getAsStringArray("Target_GeneSymbol"); 
                              String [] aa2= caller.getParser().getAsStringArray("Targets_FBID"); 
                              String [] aa3= caller.getParser().getAsStringArray("Targets_CGID"); 
                              double [] aa4= caller.getParser().getAsDoubleArray("Score"); 
                              //convert double array to string array
                              String [] sa4= new String[aa4.length];
                              for(int ss=0;ss<aa4.length;ss++){
                                  sa4[ss]= Double.toString(aa4[ss]);
                              }
                              String [] aa5 = caller.getParser().getAsStringArray("GeneFunction");
                              String [] aa6 = caller.getParser().getAsStringArray("Experiments");


                        //create JTable objects
                            String[][] results = new String[checkCnt*threshold][8];
                            int w = 0;
                            int x = 0;
                            for(int n=0;n<checkCnt;n++){
                                for(int jj=0;jj<threshold;jj++){//first miR
                                    results[jj+w][0]=aa[jj+x*threshold];//the first miR, then the next one after n loops once
                                    results[jj+w][1]=aa1[jj+x*threshold];//tar_GS
                                    results[jj+w][2]=aa2[jj+x*threshold];//tar_FB
                                    results[jj+w][3]=aa3[jj+x*threshold];//tar_CG
                                    results[jj+w][4]= sa4[jj+x*threshold];//Score
                                    results[jj+w][5]=aa5[jj+x*threshold];//Function
                                    results[jj+w][6]=aa6[jj+x*threshold];//Experiment

                                }
                                w=w+threshold;
                                x++;
                            }
                            System.out.println(checkCnt);

                        //make JTable
                            JTable resultTable = new JTable(results, colNames); 

                    //create scroll pane to embed results JTable in; allow for vertical scrolling
                        JScrollPane scrollTable = new JScrollPane(resultTable);
                        resultTable.setFillsViewportHeight(true);
                        scrollTable.setPreferredSize(new Dimension(resultBack.getWidth(),(resultFrame.getHeight()-150)));
                        scrollTable.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
                        scrollTable.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
                        scrollTable.getVerticalScrollBar().setUnitIncrement(12);

                    //create bottom buttonPanel to allow for visualization, exportation, and ontological research
                        JPanel buttonPanel = new JPanel();
                        buttonPanel.setPreferredSize(new Dimension(200, 150));
                        buttonPanel.setBackground(Color.LIGHT_GRAY);
                        buttonPanel.setLayout(null);


                        //create buttons
                            JButton gOnt = new JButton("Gene Ontology");
                            gOnt.setFont(new Font("Arial", Font.PLAIN, 18));
                            gOnt.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                            gOnt.setBounds(50,50,250,100);
                            buttonPanel.add(gOnt);
                            JButton vis = new JButton("Visualization");
                            vis.setFont(new Font("Arial", Font.PLAIN, 18));
                            vis.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                            vis.setBounds(650,50,250,100);
                            buttonPanel.add(vis);
                            vis.addActionListener(new ActionListener(){
                                **public void actionPerformed(ActionEvent v){
                                    if(v.getSource() == vis){

                                        code1.addRCode("yy1<-Visualisation(miR,mRNA_type=c('GeneSymbol'),method,thresh,platform=Platform,visualisation = 'igraph',layout = 'interactive')");
                                        caller1.setRCode(code1);
                                        caller1.runAndReturnResult("yy1");
                                    }
                                }
                            });**
                            JButton exp = new JButton("Export as .txt file");
                            exp.setFont(new Font("Arial", Font.PLAIN, 18));
                            exp.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                            exp.setBounds(350, 50, 250, 100);
                            buttonPanel.add(exp);


                    resultFrame.setLocation(470,150);//add in the panels and display the resultFrame
                    resultFrame.add(buttonPanel, BorderLayout.PAGE_END);
                    resultFrame.add(scrollTable, BorderLayout.PAGE_START);
                    resultFrame.setVisible(true);
                    }}});

关注的领域是我的JButton vis的ActionListener。 我绝对可以确定其他一切都很好,但是igraph在填充后首先没有响应,然后第二次调用提供了IllegalThreadException错误。

这是我首先要检查的内容:

不能从NON gui线程修改GUI。

确保您有一个将信息传递到GUI的后台线程。 否则,GUI将无响应,直到完成处理为止(这是在没有后台线程的情况下)

您始终可以在actionPerformed代码周围放置可运行的gui。

就你而言

SwingUtilities.invokeLater(new Runnable(){...});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM