updated to jalview 2.1 and begun ArchiveClient/VamsasClient/VamsasStore updates.
[jalview.git] / src / jalview / bin / JalviewLite.java
1 /*
2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18 */
19 package jalview.bin;
20
21 import jalview.appletgui.AlignFrame;
22
23 import jalview.datamodel.*;
24
25 import jalview.io.*;
26
27 import java.applet.*;
28
29 import java.awt.*;
30 import java.awt.event.*;
31
32
33 /**
34  * Jalview Applet. Runs in Java 1.18 runtime
35  *
36  * @author $author$
37  * @version $Revision$
38  */
39 public class JalviewLite extends Applet
40 {
41     static int lastFrameX = 200;
42     static int lastFrameY = 200;
43     boolean fileFound = true;
44     String file = "No file";
45     Button launcher = new Button("Start Jalview");
46
47     //The currentAlignFrame is static, it will change
48     //if and when the user selects a new window
49     static AlignFrame currentAlignFrame;
50
51     //This is the first frame to be displayed, and does not change
52     AlignFrame initialAlignFrame;
53
54     boolean embedded = false;
55
56
57     /**
58      * init method for Jalview Applet
59      */
60     public void init()
61     {
62         int r = 255;
63         int g = 255;
64         int b = 255;
65         String param = getParameter("RGB");
66
67         if (param != null)
68         {
69             try
70             {
71                 r = Integer.parseInt(param.substring(0, 2), 16);
72                 g = Integer.parseInt(param.substring(2, 4), 16);
73                 b = Integer.parseInt(param.substring(4, 6), 16);
74             }
75             catch (Exception ex)
76             {
77                 r = 255;
78                 g = 255;
79                 b = 255;
80             }
81         }
82
83         param = getParameter("label");
84         if(param != null)
85           launcher.setLabel(param);
86
87         this.setBackground(new Color(r, g, b));
88
89         file = getParameter("file");
90
91         if(file==null)
92         {
93           //Maybe the sequences are added as parameters
94           StringBuffer data = new StringBuffer("PASTE");
95           int i=1;
96           while( (file=getParameter("sequence"+i))!=null)
97           {
98             data.append(file.toString()+"\n");
99             i++;
100           }
101           if(data.length()>5)
102             file = data.toString();
103         }
104
105         final JalviewLite applet = this;
106         if(getParameter("embedded")!=null
107            && getParameter("embedded").equalsIgnoreCase("true"))
108         {
109           embedded = true;
110           LoadingThread loader = new LoadingThread(file, applet);
111           loader.start();
112         }
113         else if (file != null)
114         {
115             add(launcher);
116
117             launcher.addActionListener(new java.awt.event.ActionListener()
118                 {
119                     public void actionPerformed(ActionEvent e)
120                     {
121                         LoadingThread loader = new LoadingThread(file,
122                             applet);
123                         loader.start();
124                       }
125                 });
126         }
127         else
128         {
129             file = "NO FILE";
130             fileFound = false;
131         }
132     }
133
134
135     public static void main(String [] args)
136     {
137       if(args.length!=1)
138       {
139         System.out.println("\nUsage: java -jar jalviewApplet.jar fileName\n");
140         System.exit(1);
141       }
142
143       String format = new jalview.io.IdentifyFile().Identify(args[0],AppletFormatAdapter.FILE);
144
145       SequenceI[] sequences = null;
146      try{
147        sequences = new AppletFormatAdapter().readFile(args[0], AppletFormatAdapter.FILE, format);
148      }catch(java.io.IOException ex)
149      {
150        ex.printStackTrace();
151      }
152       if ( (sequences != null) && (sequences.length > 0))
153       {
154         AlignFrame af = new AlignFrame(new Alignment(sequences), null, args[0], false);
155         af.statusBar.setText("Successfully loaded file " + args[0]);
156       }
157     }
158
159
160     /**
161      * Initialises and displays a new java.awt.Frame
162      *
163      * @param frame java.awt.Frame to be displayed
164      * @param title title of new frame
165      * @param width width if new frame
166      * @param height height of new frame
167      */
168     public static void addFrame(final Frame frame, String title, int width,
169         int height)
170     {
171         frame.setLocation(lastFrameX, lastFrameY);
172         lastFrameX += 40;
173         lastFrameY += 40;
174         frame.setSize(width, height);
175         frame.setTitle(title);
176         frame.addWindowListener(new WindowAdapter()
177             {
178                 public void windowClosing(WindowEvent e)
179                 {
180                     if(currentAlignFrame == frame)
181                     {
182                       currentAlignFrame = null;
183                     }
184                     lastFrameX -=40;
185                     lastFrameY -=40;
186                     frame.setMenuBar(null);
187                     frame.dispose();
188                 }
189                 public void windowActivated(WindowEvent e)
190                 {
191                   if(frame instanceof AlignFrame)
192                     currentAlignFrame = (AlignFrame)frame;
193                 }
194
195             });
196         frame.setVisible(true);
197     }
198
199     public String getSelectedSequences()
200     {
201       StringBuffer result = new StringBuffer("");
202
203       if(initialAlignFrame.viewport.getSelectionGroup()!=null)
204       {
205         SequenceI[] seqs = initialAlignFrame.viewport.getSelectionGroup().
206             getSequencesInOrder(
207                 initialAlignFrame.viewport.getAlignment());
208
209         for (int i = 0; i < seqs.length; i++)
210           result.append(seqs[i].getName() + "¬");
211       }
212
213       return result.toString();
214     }
215
216     public String getAlignment(String format)
217     {
218       return getAlignment(format, "true");
219     }
220
221     public String getAlignment(String format, String suffix)
222     {
223       try
224       {
225         boolean seqlimits = suffix.equalsIgnoreCase("true");
226
227         String reply = new AppletFormatAdapter().formatSequences(format,
228             currentAlignFrame.viewport.getAlignment().getSequences(), seqlimits);
229         return reply;
230       }
231       catch (Exception ex)
232       {
233         ex.printStackTrace();
234         return "Error retrieving alignment in " + format + " format. ";
235       }
236     }
237
238     /**
239      * This paints the background surrounding the "Launch Jalview button"
240      * <br>
241      * <br>If file given in parameter not found, displays error message
242      *
243      * @param g graphics context
244      */
245     public void paint(Graphics g)
246     {
247         if (!fileFound)
248         {
249             g.setColor(new Color(200, 200, 200));
250             g.setColor(Color.cyan);
251             g.fillRect(0, 0, getSize().width, getSize().height);
252             g.setColor(Color.red);
253             g.drawString("Jalview can't open file", 5, 15);
254             g.drawString("\"" + file + "\"", 5, 30);
255         }
256         else if(embedded)
257         {
258           g.setColor(Color.black);
259           g.setFont(new Font("Arial", Font.BOLD, 24));
260           g.drawString("Jalview Applet", 50, this.size().height/2 -30);
261           g.drawString("Loading Data...", 50, this.size().height/2);
262         }
263
264
265     }
266
267     class LoadingThread extends Thread
268     {
269         String file;
270         String protocol;
271         String format;
272         JalviewLite applet;
273
274         public LoadingThread(String _file,
275                              JalviewLite _applet)
276         {
277             file = _file;
278             if(file.startsWith("PASTE"))
279             {
280               file = file.substring(5);
281               protocol = AppletFormatAdapter.PASTE;
282             }
283             else if(inArchive(file))
284               protocol = AppletFormatAdapter.CLASSLOADER;
285             else
286             {
287               file = addProtocol(file);
288               protocol = AppletFormatAdapter.URL;
289             }
290             format = new jalview.io.IdentifyFile().Identify(file, protocol);
291             applet = _applet;
292         }
293
294         public void run()
295         {
296             SequenceI[] sequences = null;
297             try{
298               sequences = new AppletFormatAdapter().readFile(file, protocol,
299                   format);
300             }catch(java.io.IOException ex)
301             {
302               ex.printStackTrace();
303             }
304             if ((sequences != null) && (sequences.length > 0))
305             {
306               currentAlignFrame = new AlignFrame(new Alignment(sequences),
307                                                  applet,
308                                                  file,
309                                                  embedded);
310
311               if(protocol==jalview.io.AppletFormatAdapter.PASTE)
312                 currentAlignFrame.setTitle("Sequences from "+getDocumentBase());
313
314               initialAlignFrame = currentAlignFrame;
315
316               currentAlignFrame.statusBar.setText("Successfully loaded file " + file);
317
318
319                 String treeFile = applet.getParameter("tree");
320                 if(treeFile==null)
321                   treeFile = applet.getParameter("treeFile");
322
323                 if (treeFile != null)
324                 {
325                   try
326                   {
327                     if(inArchive(treeFile))
328                       protocol = AppletFormatAdapter.CLASSLOADER;
329                     else
330                     {
331                       protocol = AppletFormatAdapter.URL;
332                       treeFile = addProtocol(treeFile);
333                     }
334
335                     jalview.io.NewickFile fin = new jalview.io.NewickFile(treeFile, protocol);
336
337                     fin.parse();
338
339                     if (fin.getTree() != null)
340                     {
341                       currentAlignFrame.loadTree(fin, treeFile);
342                     }
343                   }
344                   catch (Exception ex)
345                   {
346                     ex.printStackTrace();
347                   }
348               }
349
350               String param = getParameter("features");
351               if (param != null)
352               {
353                 if( !inArchive(param) )
354                   param = addProtocol( param );
355
356                 currentAlignFrame.parseFeaturesFile(param, protocol);
357               }
358
359               param = getParameter("showFeatureSettings");
360               if(param !=null && param.equalsIgnoreCase("true"))
361               {
362                 currentAlignFrame.viewport.showSequenceFeatures(true);
363                 currentAlignFrame.featureSettings_actionPerformed();
364               }
365
366              param = getParameter("annotations");
367              if (param != null)
368              {
369                if( !inArchive(param) )
370                   param = addProtocol( param );
371
372                new AnnotationFile().readAnnotationFile(
373                    currentAlignFrame.viewport.getAlignment(),
374                    param);
375
376                currentAlignFrame.alignPanel.fontChanged();
377                currentAlignFrame.alignPanel.setScrollValues(0,0);
378
379              }
380
381              param = getParameter("jnetfile");
382              if (param != null)
383              {
384                try
385                {
386                  if (inArchive(param))
387                    protocol = AppletFormatAdapter.CLASSLOADER;
388                  else
389                  {
390                    protocol = AppletFormatAdapter.URL;
391                    param = addProtocol(param);
392                  }
393
394                  jalview.io.JPredFile predictions = new jalview.io.JPredFile(
395                      param, protocol);
396                  new JnetAnnotationMaker().add_annotation(predictions,
397                      currentAlignFrame.viewport.getAlignment(),
398                      0,false); // do not add sequence profile from concise output
399                  currentAlignFrame.alignPanel.fontChanged();
400                  currentAlignFrame.alignPanel.setScrollValues(0, 0);
401                } catch (Exception ex) {
402                  ex.printStackTrace();
403                }
404              }
405
406
407                 String pdbfile = applet.getParameter("PDBFILE");
408                 if(pdbfile!=null)
409                 {
410                   if( inArchive(pdbfile) )
411                     protocol = AppletFormatAdapter.CLASSLOADER;
412                   else
413                   {
414                     protocol = AppletFormatAdapter.URL;
415                     pdbfile = addProtocol(pdbfile);
416                   }
417
418                   String sequence = applet.getParameter("PDBSEQ");
419
420                   if(sequence!=null)
421                   {
422                     new MCview.AppletPDBViewer(pdbfile, protocol,
423                                                (Sequence)currentAlignFrame.getAlignViewport().getAlignment().findName(sequence),
424                                                currentAlignFrame.getSeqcanvas());
425                   }
426
427                 }
428             }
429             else
430             {
431                 fileFound = false;
432                 remove(launcher);
433                 repaint();
434             }
435         }
436
437         /**
438          * Discovers whether the given file is in the Applet Archive
439          * @param file String
440          * @return boolean
441          */
442         boolean inArchive(String file)
443         {
444           //This might throw a security exception in certain browsers
445           //Netscape Communicator for instance.
446           try{
447             return (getClass().getResourceAsStream("/" + file) != null);
448           }catch(Exception ex)
449           {
450             System.out.println("Exception checking resources: "+file+" "+ex);
451             return false;
452           }
453         }
454
455         String addProtocol(String file)
456         {
457           if (file.indexOf("://") == -1)
458              file = getCodeBase() + file;
459
460           return file;
461         }
462
463     }
464 }