001/*
002 *                    PDB web development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 *
015 * Created on Jun 13, 2009
016 * Created by Andreas Prlic
017 *
018 */
019
020package org.biojava.nbio.structure.align.webstart;
021
022import org.biojava.nbio.structure.Atom;
023import org.biojava.nbio.structure.StructureException;
024import org.biojava.nbio.structure.align.StructureAlignment;
025import org.biojava.nbio.structure.align.StructureAlignmentFactory;
026import org.biojava.nbio.structure.align.ce.CeCPMain;
027import org.biojava.nbio.structure.align.ce.CeMain;
028import org.biojava.nbio.structure.align.client.FarmJobParameters;
029import org.biojava.nbio.structure.align.client.JFatCatClient;
030import org.biojava.nbio.structure.align.client.PdbPair;
031import org.biojava.nbio.structure.align.fatcat.FatCatFlexible;
032import org.biojava.nbio.structure.align.fatcat.FatCatRigid;
033import org.biojava.nbio.structure.align.gui.AlignmentGui;
034import org.biojava.nbio.structure.align.gui.ChooseDirAction;
035import org.biojava.nbio.structure.align.gui.DisplayAFP;
036import org.biojava.nbio.structure.align.gui.StructureAlignmentDisplay;
037import org.biojava.nbio.structure.align.gui.jmol.StructureAlignmentJmol;
038import org.biojava.nbio.structure.align.model.AFPChain;
039import org.biojava.nbio.structure.align.seq.SmithWaterman3Daligner;
040import org.biojava.nbio.structure.align.util.AtomCache;
041import org.biojava.nbio.structure.align.util.UserConfiguration;
042
043import javax.swing.*;
044
045import java.io.File;
046
047public class WebStartMain
048{
049
050        static UserConfiguration userConfig;
051
052        /**
053         *  If no arguments, shows AlignmentGui for pairwise alignments.
054         *  if 1 argument: dbmenu shows the DBSearchGUI, otherwise the AlignentGUI is shown.
055         *
056         * if more than 3 arguments takes the following arguments
057         * arg0 : fatcat or biojava .
058         * arg1 : pdb1.X
059         * arg2 ; pdb2.X
060         *
061         * The 4th argument is optional and it could be the serverLocation which the client should talk to.
062         *
063         * @param args
064         */
065        public static void main(String[] args){
066
067                AligUIManager.setLookAndFeel();
068
069                if ( args.length == 0){
070
071                        //JOptionPane.showMessageDialog(null,
072                        //              "Not enough arguments. Need at least 3, but only got " + args.length);
073
074                        // we did not get enough arguments, show the general user interface...
075
076                        javax.swing.SwingUtilities.invokeLater(new Runnable() {
077                                @Override
078                                public void run() {
079
080                                        AlignmentGui.getInstance();
081                                }
082                        });
083
084                        return;
085
086                }
087
088                else if ( args.length < 3){
089                        //String arg0 = args[0];
090
091                        javax.swing.SwingUtilities.invokeLater(new Runnable() {
092                                @Override
093                                public void run() {
094                                        AlignmentGui.getInstance();
095                                }
096                        });
097
098                        return;
099
100                }
101
102                String arg0 = args[0];
103
104                if (! (arg0.equals("fatcat") ||
105                                arg0.equals("biojava") ||
106                                arg0.equals("fatcat_flexible") ||
107                                arg0.equals("ce") ||
108                                arg0.equals("ce_cp") ||
109                                arg0.equals("sw")
110                )){
111                        JOptionPane.showMessageDialog(null,
112                                        "Wrong arguments. First argument has to be \"fatcat\", \"ce\", \"ce_cp\", \"sw\", \"fatcat_flexible\", or \"biojava\", but got " + arg0);
113                        return;
114                }
115
116                String serverLocation = FarmJobParameters.DEFAULT_SERVER_URL;
117
118                if ( args.length  > 3 ) {
119                        // we have 4 arguments.
120                        // in this case the 4th has to be the server URL
121                        serverLocation = args[3];
122                }
123
124                try {
125
126                        String name1 = args[1];
127                        String name2 = args[2];
128
129                        PdbPair pair = new PdbPair(name1, name2);
130                        System.out.println("### user provided: " + pair);
131
132                        UserConfiguration config = getWebStartConfig();
133
134                        System.setProperty(UserConfiguration.PDB_DIR,config.getPdbFilePath());
135                        System.out.println("using PDB file path: " + config.getPdbFilePath());
136
137                        AtomCache cache = new AtomCache(config);
138
139                        JFrame frame = new JFrame();
140                        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
141
142                        showProgressBar(frame,"Loading PDB files...", "Loading files from local directory or via FTP.");
143
144                        Atom[] ca1 = cache.getAtoms(pair.getName1());
145                        Atom[] ca2 = cache.getAtoms(pair.getName2());
146
147                        frame.dispose();
148
149                        System.out.println("done reading structures");
150
151
152                        if (arg0.equalsIgnoreCase("ce") ||
153                                        arg0.equalsIgnoreCase("ce_cp") ||
154                                        arg0.equalsIgnoreCase("sw") ||
155                                        arg0.equalsIgnoreCase("fatcat") ||
156                                        arg0.equalsIgnoreCase("fatcat_flexible")){
157                                try {
158
159                                        StructureAlignment algorithm ;
160                                        if ( arg0.equalsIgnoreCase("ce"))
161                                                algorithm = StructureAlignmentFactory.getAlgorithm(CeMain.algorithmName);
162                                        else if ( arg0.equalsIgnoreCase("ce_cp"))
163                                                algorithm = StructureAlignmentFactory.getAlgorithm(CeCPMain.algorithmName);
164                                        else if ( arg0.equalsIgnoreCase("fatcat"))
165                                                algorithm = StructureAlignmentFactory.getAlgorithm(FatCatRigid.algorithmName);
166                                        else if ( arg0.equalsIgnoreCase("fatcat_flexible"))
167                                                algorithm = StructureAlignmentFactory.getAlgorithm(FatCatFlexible.algorithmName);
168                                        else
169                                                algorithm = new SmithWaterman3Daligner();
170
171                                        showStructureAlignment(serverLocation,algorithm ,ca1,ca2,pair.getName1(),pair.getName2());
172
173                                } catch (Exception e){
174                                        e.printStackTrace();
175                                        JOptionPane.showMessageDialog(null,
176                                                        "Something went wrong! : " + e.getMessage());
177                                }
178
179
180                        } else if ( arg0.equalsIgnoreCase("biojava")){
181                                try {
182                                        //showBiojava(ca1,ca2);
183                                } catch (Exception e){
184                                        e.printStackTrace();
185                                        JOptionPane.showMessageDialog(null,
186                                                        "Something went wrong! : " + e.getMessage());
187                                        System.exit(0);
188                                }
189                        }
190
191
192                } catch (Exception e) {
193                        e.printStackTrace();
194                        JOptionPane.showMessageDialog(null,
195                                        "Error: " + e.getMessage());
196                        System.exit(0);
197                        return;
198                }
199        }
200
201        private static JProgressBar showProgressBar(JFrame frame, String title, String displayTxt){
202
203                frame.setTitle(title);
204                JProgressBar progressBar;
205
206                JPanel content = new JPanel();
207                content.setOpaque(true);
208
209                //Where the GUI is constructed:
210                progressBar = new JProgressBar();
211                progressBar.setToolTipText(title);
212                progressBar.setValue(0);
213                progressBar.setStringPainted(true);
214                progressBar.setIndeterminate(true);
215
216                JTextField txt = new JTextField(displayTxt);
217                txt.setEditable(false);
218                content.add(txt);
219                content.add(progressBar);
220
221                frame.getContentPane().add(content);
222                frame.pack();
223                frame.setVisible(true);
224
225                return progressBar;
226        }
227
228
229        public static UserConfiguration getWebStartConfig(){
230
231                // check if we could load it (i.e. we are running in web start mode)
232                if ( userConfig == null ) {
233                        userConfig = WebStartMain.getDefaultConfig();
234
235                }
236
237                return userConfig;
238        }
239
240
241        public static UserConfiguration getDefaultConfig(){
242                userConfig = new UserConfiguration();
243
244                String pdbDir = System.getProperty(UserConfiguration.PDB_DIR);
245                if ( pdbDir != null) {
246                        userConfig.setPdbFilePath(pdbDir);
247
248                }
249
250                return userConfig;
251        }
252
253
254        public static UserConfiguration requestUserConfig(){
255
256                if ( userConfig == null) {
257
258                        //UserConfiguration config = new UserConfiguration();
259                        userConfig = new UserConfiguration();
260
261                        String pdbDir = System.getProperty(UserConfiguration.PDB_DIR);
262                        if ( pdbDir != null) {
263                                userConfig.setPdbFilePath(pdbDir);
264                                return userConfig;
265                        }
266
267                }
268                JTextField textField = new JTextField();
269                ChooseDirAction action = new ChooseDirAction(textField, userConfig);
270                action.actionPerformed(null);
271                if ( textField.getText() == null) {
272
273                        // accessing temp. OS directory:
274                        String property = "java.io.tmpdir";
275                        String lineSplit = System.getProperty("file.separator");
276
277                        String tempdir = System.getProperty(property);
278
279                        if ( !(tempdir.endsWith(lineSplit ) ) )
280                                tempdir = tempdir + lineSplit;
281
282                        userConfig.setPdbFilePath(tempdir);
283                        return userConfig;
284                }
285
286                File file = new File(textField.getText());
287                if ( ! file.isDirectory() ){
288                        // should not happen
289                        System.err.println("did not provide directory, going on level higher! " + file.getAbsolutePath());
290                        file = file.getParentFile();
291                }
292                System.setProperty(UserConfiguration.PDB_DIR, file.getAbsolutePath());
293                userConfig.setPdbFilePath(file.getAbsolutePath());
294
295                return userConfig;
296        }
297
298
299
300        private static void showStructureAlignment(String serverLocation, StructureAlignment algorithm, Atom[] ca1, Atom[] ca2, String name1, String name2) throws StructureException{
301                JFrame tmpFrame = new JFrame();
302                tmpFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
303
304                String title = "Calculating " + algorithm.getAlgorithmName() + " V." + algorithm.getVersion()+" alignment... ";
305
306
307                showProgressBar(tmpFrame,title, "Calculating the structure alignment.");
308
309
310                //do the actual alignment
311                AFPChain afpChain = null;
312
313                try {
314                        // using 10 sec as timeout on server now, since we expect the server to be able to complete the calculation within that time...
315                        afpChain =  JFatCatClient.getAFPChainFromServer(serverLocation,algorithm.getAlgorithmName(), name1, name2, ca1, ca2, 10000);
316                } catch (Exception e){
317                        e.printStackTrace();
318                }
319
320                if ( afpChain == null )  {
321                        afpChain = algorithm.align(ca1, ca2);
322                }
323
324                afpChain.setName1(name1);
325                afpChain.setName2(name2);
326
327                tmpFrame.dispose();
328
329
330                // show results
331                StructureAlignmentJmol jmol =  StructureAlignmentDisplay.display(afpChain,ca1,ca2);
332
333                System.out.println(afpChain.toCE(ca1, ca2));
334
335                DisplayAFP.showAlignmentPanel(afpChain,ca1,ca2,jmol);
336
337        }
338
339
340//      private static void showBiojava(Atom[] ca1, Atom[] ca2) throws StructureException{
341//
342//              StructurePairAligner aligner = new StructurePairAligner();
343//
344//              StrucAligParameters params = StrucAligParameters.getDefaultParameters();
345//              //params.setJoinFast(true);
346//              //params.setMaxIter(1);
347//              try {
348//                      // align the full 2 structures with default parameters.
349//                      // see StructurePairAligner for more options and how to align
350//                      // any set of Atoms
351//                      long start = System.currentTimeMillis();
352//
353//
354//                      aligner.align(ca1,ca2,params);
355//                      long end = System.currentTimeMillis();
356//                      System.out.println("calculation time:" + (end-start));
357//
358//                      AlternativeAlignment[] aligs = aligner.getAlignments();
359//                      AlternativeAlignment a = aligs[0];
360//                      System.out.println(a);
361//
362//                      if (! BiojavaJmol.jmolInClassPath()){
363//                              System.err.println("Could not find Jmol in classpath, please install first!");
364//                              return;
365//                      }
366//                      // display the alignment in Jmol
367//
368//
369//
370//                      // first get an artificial structure for the alignment
371//                      Structure artificial = a.getAlignedStructure(structure1, structure2);
372//
373//                      // and then send it to Jmol (only will work if Jmol is in the Classpath)
374//                      BiojavaJmol jmol = new BiojavaJmol();
375//                      jmol.setTitle(artificial.getName());
376//                      jmol.setStructure(artificial);
377//
378//                      // color the two structures
379//
380//
381//                      jmol.evalString("select *; backbone 0.4; wireframe off; spacefill off; " +
382//                      "select not protein and not solvent; spacefill on;");
383//                      jmol.evalString("select */1 ; color red; model 1; ");
384//
385//
386//                      // now color the equivalent residues ...
387//
388//                      String[] pdb1 = a.getPDBresnum1();
389//                      for (String res : pdb1 ){
390//                              jmol.evalString("select " + res + "/1 ; backbone 0.6; color orange;");
391//                      }
392//
393//                      jmol.evalString("select */2; color blue; model 2;");
394//                      String[] pdb2 = a.getPDBresnum2();
395//                      for (String res :pdb2 ){
396//                              jmol.evalString("select " + res + "/2 ; backbone 0.6; color cyan;");
397//                      }
398//
399//
400//                      // now show both models again.
401//                      jmol.evalString("model 0;");
402//
403//              } catch (StructureException e){
404//                      e.printStackTrace();
405//              }
406//
407//      }
408}