首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RCaller、线程处理和Java GUI

RCaller、线程处理和Java GUI
EN

Stack Overflow用户
提问于 2017-01-21 23:30:10
回答 1查看 146关注 0票数 0

我正在制作一个Java GUI来配合我同事的定制R包IntramiRExploreR,其中包括一个通过igraph创建交互式图形的函数和IntramiRExploreR的可视化函数,使用以下参数:

代码语言:javascript
复制
    Visualisation(miR,mRNA_type=c('GeneSymbol'),method,thresh,platform=Platform,visualisation = 'igraph',layout = 'interactive')

其中miR是通过选择的JCheckboxes生成的向量,方法、阈值和平台是从JRadioButtons填充的。我毫不怀疑函数本身和变量的填充方式是正确的,因为我在R中运行了函数,并使用文本输出格式运行了函数,两者都正确运行。

代码首先使用来自的结果正确地填充JTable

代码语言:javascript
复制
    Visualisation(miR,mRNA_type=c('GeneSymbol'),method,thresh,platform=Platform)

输出可访问的文本的

代码语言:javascript
复制
    caller.getParser().getAsStringArray(//one of seven parameters)

然后提供一个JButton来使用相同的参数和对象来调用R中的前述图形函数。然而,当单击JButton时,图形会被创建,但一旦图形完成,它的帧就会被释放。第二次单击该按钮,再次调用该函数时,提供的错误为:

代码语言:javascript
复制
    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

我应该创建新的线程来处理图形可视化,还是在RCaller中有一些我遗漏的方法可以处理这个问题?在我调用第二个RCaller和RCode块之后,Java是否会清空内存中的对象?

以下是我可以在不违反保密协议的情况下展示的代码:

代码语言:javascript
复制
    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错误。

EN

回答 1

Stack Overflow用户

发布于 2017-01-21 23:37:34

这是我首先要检查的:

不能从非GUI线程修改GUI。

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

您总是可以在actionPerformed代码周围放置一个可运行的gui。

在你的情况下

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41781109

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档