簡體   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