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