5d2aa0488c2abce9ea468279ef0072157ede4f5d
[jalview.git] / src / jalview / bin / JalviewLite.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3  * Copyright (C) 2008 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 java.applet.*;
22
23 import java.awt.*;
24 import java.awt.event.*;
25 import java.util.*;
26
27 import jalview.appletgui.*;
28 import jalview.datamodel.*;
29 import jalview.io.*;
30
31 /**
32  * Jalview Applet. Runs in Java 1.18 runtime
33  * 
34  * @author $author$
35  * @version $Revision$
36  */
37 public class JalviewLite extends Applet
38 {
39
40   // /////////////////////////////////////////
41   // The following public methods maybe called
42   // externally, eg via javascript in HTML page
43   /**
44    * @return String list of selected sequence IDs, each terminated by "¬"
45    *         (¬)
46    */
47   public String getSelectedSequences()
48   {
49     return getSelectedSequencesFrom(getDefaultTargetFrame());
50   }
51
52   /**
53    * @param sep
54    *                separator string or null for default
55    * @return String list of selected sequence IDs, each terminated by sep or
56    *         ("¬" as default)
57    */
58   public String getSelectedSequences(String sep)
59   {
60     return getSelectedSequencesFrom(getDefaultTargetFrame(), sep);
61   }
62
63   /**
64    * @param alf
65    *                alignframe containing selection
66    * @return String list of selected sequence IDs, each terminated by "¬"
67    * 
68    */
69   public String getSelectedSequencesFrom(AlignFrame alf)
70   {
71     return getSelectedSequencesFrom(alf, "¬");
72   }
73
74   /**
75    * get list of selected sequence IDs separated by given separator
76    * 
77    * @param alf
78    *                window containing selection
79    * @param sep
80    *                separator string to use - default is "¬"
81    * @return String list of selected sequence IDs, each terminated by the given
82    *         separator
83    */
84   public String getSelectedSequencesFrom(AlignFrame alf, String sep)
85   {
86     StringBuffer result = new StringBuffer("");
87     if (sep == null || sep.length() == 0)
88     {
89       sep = "¬";
90     }
91     if (alf.viewport.getSelectionGroup() != null)
92     {
93       SequenceI[] seqs = alf.viewport.getSelectionGroup()
94               .getSequencesInOrder(alf.viewport.getAlignment());
95
96       for (int i = 0; i < seqs.length; i++)
97       {
98         result.append(seqs[i].getName());
99         result.append(sep);
100       }
101     }
102
103     return result.toString();
104   }
105
106   /**
107    * get sequences selected in current alignFrame and return their alignment in
108    * format 'format' either with or without suffix
109    * 
110    * @param alf -
111    *                where selection is
112    * @param format -
113    *                format of alignment file
114    * @param suffix -
115    *                "true" to append /start-end string to each sequence ID
116    * @return selected sequences as flat file or empty string if there was no
117    *         current selection
118    */
119   public String getSelectedSequencesAsAlignment(String format, String suffix)
120   {
121     return getSelectedSequencesAsAlignmentFrom(currentAlignFrame, format,
122             suffix);
123   }
124
125   /**
126    * get sequences selected in alf and return their alignment in format 'format'
127    * either with or without suffix
128    * 
129    * @param alf -
130    *                where selection is
131    * @param format -
132    *                format of alignment file
133    * @param suffix -
134    *                "true" to append /start-end string to each sequence ID
135    * @return selected sequences as flat file or empty string if there was no
136    *         current selection
137    */
138   public String getSelectedSequencesAsAlignmentFrom(AlignFrame alf,
139           String format, String suffix)
140   {
141     try
142     {
143       boolean seqlimits = suffix.equalsIgnoreCase("true");
144       if (alf.viewport.getSelectionGroup() != null)
145       {
146         String reply = new AppletFormatAdapter().formatSequences(format,
147                 new Alignment(alf.viewport.getSelectionAsNewSequence()),
148                 seqlimits);
149         return reply;
150       }
151     } catch (Exception ex)
152     {
153       ex.printStackTrace();
154       return "Error retrieving alignment in " + format + " format. ";
155     }
156     return "";
157   }
158
159   public String getAlignment(String format)
160   {
161     return getAlignmentFrom(getDefaultTargetFrame(), format, "true");
162   }
163
164   public String getAlignmentFrom(AlignFrame alf, String format)
165   {
166     return getAlignmentFrom(alf, format, "true");
167   }
168
169   public String getAlignment(String format, String suffix)
170   {
171     return getAlignmentFrom(getDefaultTargetFrame(), format, suffix);
172   }
173
174   public String getAlignmentFrom(AlignFrame alf, String format,
175           String suffix)
176   {
177     try
178     {
179       boolean seqlimits = suffix.equalsIgnoreCase("true");
180
181       String reply = new AppletFormatAdapter().formatSequences(format,
182               alf.viewport.getAlignment(), seqlimits);
183       return reply;
184     } catch (Exception ex)
185     {
186       ex.printStackTrace();
187       return "Error retrieving alignment in " + format + " format. ";
188     }
189   }
190
191   public void loadAnnotation(String annotation)
192   {
193     loadAnnotationFrom(getDefaultTargetFrame(), annotation);
194   }
195
196   public void loadAnnotationFrom(AlignFrame alf, String annotation)
197   {
198     if (new AnnotationFile().readAnnotationFile(alf.getAlignViewport()
199             .getAlignment(), annotation, AppletFormatAdapter.PASTE))
200     {
201       alf.alignPanel.fontChanged();
202       alf.alignPanel.setScrollValues(0, 0);
203     }
204     else
205     {
206       alf.parseFeaturesFile(annotation, AppletFormatAdapter.PASTE);
207     }
208   }
209
210   public String getFeatures(String format)
211   {
212     return getFeaturesFrom(getDefaultTargetFrame(), format);
213   }
214
215   public String getFeaturesFrom(AlignFrame alf, String format)
216   {
217     return alf.outputFeatures(false, format);
218   }
219
220   public String getAnnotation()
221   {
222     return getAnnotationFrom(getDefaultTargetFrame());
223   }
224
225   public String getAnnotationFrom(AlignFrame alf)
226   {
227     return alf.outputAnnotations(false);
228   }
229
230   public AlignFrame newView()
231   {
232     return newViewFrom(getDefaultTargetFrame());
233   }
234
235   public AlignFrame newView(String name)
236   {
237     return newViewFrom(getDefaultTargetFrame(), name);
238   }
239
240   public AlignFrame newViewFrom(AlignFrame alf)
241   {
242     return alf.newView(null);
243   }
244
245   public AlignFrame newViewFrom(AlignFrame alf, String name)
246   {
247     return alf.newView(name);
248   }
249
250   /**
251    * 
252    * @param text
253    *                alignment file as a string
254    * @param title
255    *                window title
256    * @return null or new alignment frame
257    */
258   public AlignFrame loadAlignment(String text, String title)
259   {
260     Alignment al = null;
261     
262     String format = new IdentifyFile().Identify(text,
263             AppletFormatAdapter.PASTE);
264     try
265     {
266       al = new AppletFormatAdapter().readFile(text,
267               AppletFormatAdapter.PASTE, format);
268       if (al.getHeight() > 0)
269       {
270         return new AlignFrame(al, this, title, false);
271       }
272     } catch (java.io.IOException ex)
273     {
274       ex.printStackTrace();
275     }
276     return null;
277   }
278
279   // //////////////////////////////////////////////
280   // //////////////////////////////////////////////
281
282   static int lastFrameX = 200;
283
284   static int lastFrameY = 200;
285
286   boolean fileFound = true;
287
288   String file = "No file";
289
290   Button launcher = new Button("Start Jalview");
291
292   /**
293    * The currentAlignFrame is static, it will change if and when the user
294    * selects a new window. Note that it will *never* point back to the embedded
295    * AlignFrame if the applet is started as embedded on the page and then
296    * afterwards a new view is created.
297    */
298   public static AlignFrame currentAlignFrame;
299
300   /**
301    * This is the first frame to be displayed, and does not change. API calls
302    * will default to this instance if currentAlignFrame is null.
303    */
304   AlignFrame initialAlignFrame;
305
306   boolean embedded = false;
307
308   private boolean checkForJmol = true;
309   private boolean checkedForJmol = false; // ensure we don't check for jmol every time the app is re-inited
310
311   public boolean jmolAvailable = false;
312
313   public static boolean debug;
314
315   /**
316    * init method for Jalview Applet
317    */
318   public void init()
319   {
320     /**
321      * turn on extra applet debugging
322      */
323     String dbg = getParameter("debug");
324     if (dbg != null)
325     {
326       debug = dbg.toLowerCase().equals("true");
327     }
328     /**
329      * if true disable the check for jmol
330      */
331     String chkforJmol = getParameter("nojmol");
332     if (chkforJmol != null)
333     {
334       checkForJmol = !chkforJmol.equals("true");
335     }
336     /**
337      * get the separator parameter if present
338      */
339     String sep = getParameter("separator");
340     if (sep != null)
341     {
342       if (sep.length() > 0)
343       {
344         separator = sep;
345         if (debug)
346         {
347           System.err.println("Separator set to '" + separator + "'");
348         }
349       }
350       else
351       {
352         throw new Error(
353                 "Invalid separator parameter - must be non-zero length");
354       }
355     }
356     int r = 255;
357     int g = 255;
358     int b = 255;
359     String param = getParameter("RGB");
360
361     if (param != null)
362     {
363       try
364       {
365         r = Integer.parseInt(param.substring(0, 2), 16);
366         g = Integer.parseInt(param.substring(2, 4), 16);
367         b = Integer.parseInt(param.substring(4, 6), 16);
368       } catch (Exception ex)
369       {
370         r = 255;
371         g = 255;
372         b = 255;
373       }
374     }
375
376     param = getParameter("label");
377     if (param != null)
378     {
379       launcher.setLabel(param);
380     }
381
382     this.setBackground(new Color(r, g, b));
383
384     file = getParameter("file");
385
386     if (file == null)
387     {
388       // Maybe the sequences are added as parameters
389       StringBuffer data = new StringBuffer("PASTE");
390       int i = 1;
391       while ((file = getParameter("sequence" + i)) != null)
392       {
393         data.append(file.toString() + "\n");
394         i++;
395       }
396       if (data.length() > 5)
397       {
398         file = data.toString();
399       }
400     }
401
402     final JalviewLite applet = this;
403     if (getParameter("embedded") != null
404             && getParameter("embedded").equalsIgnoreCase("true"))
405     {
406       // Launch as embedded applet in page
407       embedded = true;
408       LoadingThread loader = new LoadingThread(file, applet);
409       loader.start();
410     }
411     else if (file != null)
412     {
413       if (getParameter("showbutton") == null
414               || !getParameter("showbutton").equalsIgnoreCase("false"))
415       {
416         // Add the JalviewLite 'Button' to the page
417         add(launcher);
418         launcher.addActionListener(new java.awt.event.ActionListener()
419         {
420           public void actionPerformed(ActionEvent e)
421           {
422             LoadingThread loader = new LoadingThread(file, applet);
423             loader.start();
424           }
425         });
426       }
427       else
428       {
429         // Open jalviewLite immediately.
430         LoadingThread loader = new LoadingThread(file, applet);
431         loader.start();
432       }
433     }
434     else
435     {
436       // jalview initialisation with no alignment. loadAlignment() method can
437       // still be called to open new alignments.
438       file = "NO FILE";
439       fileFound = false;
440     }
441   }
442
443   /**
444    * Initialises and displays a new java.awt.Frame
445    * 
446    * @param frame
447    *                java.awt.Frame to be displayed
448    * @param title
449    *                title of new frame
450    * @param width
451    *                width if new frame
452    * @param height
453    *                height of new frame
454    */
455   public static void addFrame(final Frame frame, String title, int width,
456           int height)
457   {
458     frame.setLocation(lastFrameX, lastFrameY);
459     lastFrameX += 40;
460     lastFrameY += 40;
461     frame.setSize(width, height);
462     frame.setTitle(title);
463     frame.addWindowListener(new WindowAdapter()
464     {
465       public void windowClosing(WindowEvent e)
466       {
467         if (frame instanceof AlignFrame)
468         {
469           ((AlignFrame) frame).closeMenuItem_actionPerformed();
470         }
471         if (currentAlignFrame == frame)
472         {
473           currentAlignFrame = null;
474         }
475         lastFrameX -= 40;
476         lastFrameY -= 40;
477         if (frame instanceof EmbmenuFrame)
478         {
479           ((EmbmenuFrame) frame).destroyMenus();
480         }
481         frame.setMenuBar(null);
482         frame.dispose();
483       }
484
485       public void windowActivated(WindowEvent e)
486       {
487         if (frame instanceof AlignFrame)
488         {
489           currentAlignFrame = (AlignFrame) frame;
490           if (debug)
491           {
492             System.err.println("Activated window " + frame);
493           }
494         }
495         // be good.
496         super.windowActivated(e);
497       }
498       /*
499        * Probably not necessary to do this - see TODO above. (non-Javadoc)
500        * 
501        * @see java.awt.event.WindowAdapter#windowDeactivated(java.awt.event.WindowEvent)
502        * 
503        * public void windowDeactivated(WindowEvent e) { if (currentAlignFrame ==
504        * frame) { currentAlignFrame = null; if (debug) {
505        * System.err.println("Deactivated window "+frame); } }
506        * super.windowDeactivated(e); }
507        */
508     });
509     frame.setVisible(true);
510   }
511
512   /**
513    * This paints the background surrounding the "Launch Jalview button" <br>
514    * <br>
515    * If file given in parameter not found, displays error message
516    * 
517    * @param g
518    *                graphics context
519    */
520   public void paint(Graphics g)
521   {
522     if (!fileFound)
523     {
524       g.setColor(new Color(200, 200, 200));
525       g.setColor(Color.cyan);
526       g.fillRect(0, 0, getSize().width, getSize().height);
527       g.setColor(Color.red);
528       g.drawString("Jalview can't open file", 5, 15);
529       g.drawString("\"" + file + "\"", 5, 30);
530     }
531     else if (embedded)
532     {
533       g.setColor(Color.black);
534       g.setFont(new Font("Arial", Font.BOLD, 24));
535       g.drawString("Jalview Applet", 50, this.getSize().height / 2 - 30);
536       g.drawString("Loading Data...", 50, this.getSize().height / 2);
537     }
538   }
539
540   class LoadJmolThread extends Thread
541   {
542     private boolean running=false;
543
544     public void run()
545     {
546       if (running || checkedForJmol) {
547         return;
548       }
549       running=true;
550       if (checkForJmol)
551       {
552         try
553         {
554           if (!System.getProperty("java.version").startsWith("1.1"))
555           {
556             Class.forName("org.jmol.adapter.smarter.SmarterJmolAdapter");
557             jmolAvailable = true;
558           }
559           if (!jmolAvailable)
560           {
561             System.out
562                     .println("Jmol not available - Using MCview for structures");
563           }
564         } catch (java.lang.ClassNotFoundException ex)
565         {
566         }
567       }
568       else
569       {
570         jmolAvailable = false;
571         if (debug)
572         {
573           System.err
574                   .println("Skipping Jmol check. Will use MCView (probably)");
575         }
576       }
577       checkedForJmol=true;
578       running=false;
579     }
580
581     public boolean notFinished()
582     {
583       return running || !checkedForJmol;
584     }
585   }
586
587   class LoadingThread extends Thread
588   {
589     /**
590      * State variable: File source
591      */
592     String file;
593
594     /**
595      * State variable: protocol for access to file source
596      */
597     String protocol;
598
599     /**
600      * State variable: format of file source
601      */
602     String format;
603     String _file;
604     JalviewLite applet;
605     private void dbgMsg(String msg)
606     {
607       if (applet.debug)
608       {
609         System.err.println(msg);
610       }
611     }
612
613     /**
614      * update the protocol state variable for accessing the datasource located
615      * by file.
616      * 
617      * @param file
618      * @return possibly updated datasource string
619      */
620     public String setProtocolState(String file)
621     {
622       if (file.startsWith("PASTE"))
623       {
624         file = file.substring(5);
625         protocol = AppletFormatAdapter.PASTE;
626       }
627       else if (inArchive(file))
628       {
629         protocol = AppletFormatAdapter.CLASSLOADER;
630       }
631       else
632       {
633         file = addProtocol(file);
634         protocol = AppletFormatAdapter.URL;
635       }
636       dbgMsg("Protocol identified as '" + protocol + "'");
637       return file;
638     }
639     
640     public LoadingThread(String _file, JalviewLite _applet)
641     {
642       this._file=_file;
643       applet = _applet;
644     }
645
646     public void run()
647     {
648       LoadJmolThread jmolchecker = new LoadJmolThread();
649       jmolchecker.start();
650       while (jmolchecker.notFinished())
651       {
652         // wait around until the Jmol check is complete.
653         try { Thread.sleep(2); } catch (Exception e) {};
654       }
655       startLoading();
656     }
657
658     private void startLoading()
659     {
660       AlignFrame newAlignFrame;
661       dbgMsg("Loading thread started with:\n>>file\n" + _file + ">>endfile");
662       file = setProtocolState(_file);
663
664       format = new jalview.io.IdentifyFile().Identify(file, protocol);
665       dbgMsg("File identified as '" + format + "'");
666       dbgMsg("Loading started.");
667       Alignment al = null;
668       try
669       {
670         al = new AppletFormatAdapter().readFile(file, protocol, format);
671       } catch (java.io.IOException ex)
672       {
673         dbgMsg("File load exception.");
674         ex.printStackTrace();
675       }
676       if ((al != null) && (al.getHeight() > 0))
677       {
678         dbgMsg("Successfully loaded file.");
679         newAlignFrame = new AlignFrame(al, applet, file, embedded);
680         if (initialAlignFrame==null)
681         {
682           initialAlignFrame = newAlignFrame;
683         }
684         // update the focus.
685         currentAlignFrame = newAlignFrame;
686
687         if (protocol == jalview.io.AppletFormatAdapter.PASTE)
688         {
689           newAlignFrame.setTitle("Sequences from " + getDocumentBase());
690         }
691
692         newAlignFrame.statusBar.setText("Successfully loaded file "
693                 + file);
694
695         String treeFile = applet.getParameter("tree");
696         if (treeFile == null)
697         {
698           treeFile = applet.getParameter("treeFile");
699         }
700
701         if (treeFile != null)
702         {
703           try
704           {
705             treeFile = setProtocolState(treeFile);
706             /*
707              * if (inArchive(treeFile)) { protocol =
708              * AppletFormatAdapter.CLASSLOADER; } else { protocol =
709              * AppletFormatAdapter.URL; treeFile = addProtocol(treeFile); }
710              */
711             jalview.io.NewickFile fin = new jalview.io.NewickFile(treeFile,
712                     protocol);
713
714             fin.parse();
715
716             if (fin.getTree() != null)
717             {
718               newAlignFrame.loadTree(fin, treeFile);
719               dbgMsg("Successfuly imported tree.");
720             }
721             else
722             {
723               dbgMsg("Tree parameter did not resolve to a valid tree.");
724             }
725           } catch (Exception ex)
726           {
727             ex.printStackTrace();
728           }
729         }
730
731         String param = getParameter("features");
732         if (param != null)
733         {
734           param = setProtocolState(param);
735
736           newAlignFrame.parseFeaturesFile(param, protocol);
737         }
738
739         param = getParameter("showFeatureSettings");
740         if (param != null && param.equalsIgnoreCase("true"))
741         {
742           newAlignFrame.viewport.showSequenceFeatures(true);
743           new FeatureSettings(newAlignFrame.alignPanel);
744         }
745
746         param = getParameter("annotations");
747         if (param != null)
748         {
749           param = setProtocolState(param);
750
751           if (new AnnotationFile().readAnnotationFile(
752                   newAlignFrame.viewport.getAlignment(), param,
753                   protocol))
754           {
755             newAlignFrame.alignPanel.fontChanged();
756             newAlignFrame.alignPanel.setScrollValues(0, 0);
757           }
758           else
759           {
760             System.err
761                     .println("Annotations were not added from annotation file '"
762                             + param + "'");
763           }
764
765         }
766
767         param = getParameter("jnetfile");
768         if (param != null)
769         {
770           try
771           {
772             param = setProtocolState(param);
773             jalview.io.JPredFile predictions = new jalview.io.JPredFile(
774                     param, protocol);
775             JnetAnnotationMaker.add_annotation(predictions,
776                     newAlignFrame.viewport.getAlignment(), 0, false); // false==do
777             // not
778             // add
779             // sequence
780             // profile
781             // from
782             // concise
783             // output
784             newAlignFrame.alignPanel.fontChanged();
785             newAlignFrame.alignPanel.setScrollValues(0, 0);
786           } catch (Exception ex)
787           {
788             ex.printStackTrace();
789           }
790         }
791
792         /*
793          * <param name="PDBfile" value="1gaq.txt PDB|1GAQ|1GAQ|A PDB|1GAQ|1GAQ|B
794          * PDB|1GAQ|1GAQ|C">
795          * 
796          * <param name="PDBfile2" value="1gaq.txt A=SEQA B=SEQB C=SEQB">
797          * 
798          * <param name="PDBfile3" value="1q0o Q45135_9MICO">
799          */
800
801         int pdbFileCount = 0;
802         do
803         {
804           if (pdbFileCount > 0)
805             param = getParameter("PDBFILE" + pdbFileCount);
806           else
807             param = getParameter("PDBFILE");
808
809           if (param != null)
810           {
811             PDBEntry pdb = new PDBEntry();
812
813             String seqstring;
814             SequenceI[] seqs = null;
815             String[] chains = null;
816
817             StringTokenizer st = new StringTokenizer(param, " ");
818
819             if (st.countTokens() < 2)
820             {
821               String sequence = applet.getParameter("PDBSEQ");
822               if (sequence != null)
823                 seqs = new SequenceI[]
824                 { (Sequence) newAlignFrame.getAlignViewport()
825                         .getAlignment().findName(sequence) };
826
827             }
828             else
829             {
830               param = st.nextToken();
831               Vector tmp = new Vector();
832               Vector tmp2 = new Vector();
833
834               while (st.hasMoreTokens())
835               {
836                 seqstring = st.nextToken();
837                 StringTokenizer st2 = new StringTokenizer(seqstring, "=");
838                 if (st2.countTokens() > 1)
839                 {
840                   // This is the chain
841                   tmp2.addElement(st2.nextToken());
842                   seqstring = st2.nextToken();
843                 }
844                 tmp.addElement((Sequence) newAlignFrame
845                         .getAlignViewport().getAlignment().findName(
846                                 seqstring));
847               }
848
849               seqs = new SequenceI[tmp.size()];
850               tmp.copyInto(seqs);
851               if (tmp2.size() == tmp.size())
852               {
853                 chains = new String[tmp2.size()];
854                 tmp2.copyInto(chains);
855               }
856             }
857             param = setProtocolState(param);
858
859             if (// !jmolAvailable
860             // &&
861             protocol == AppletFormatAdapter.CLASSLOADER)
862             {
863               // TODO: verify this Re:
864               // https://mantis.lifesci.dundee.ac.uk/view.php?id=36605
865               // This exception preserves the current behaviour where, even if
866               // the local pdb file was identified in the class loader
867               protocol = AppletFormatAdapter.URL; // this is probably NOT
868               // CORRECT!
869               param = addProtocol(param); // 
870             }
871
872             pdb.setFile(param);
873
874             if (seqs != null)
875             {
876               for (int i = 0; i < seqs.length; i++)
877               {
878                 if (seqs[i] != null)
879                 {
880                   ((Sequence) seqs[i]).addPDBId(pdb);
881                 }
882                 else
883                 {
884                   if (JalviewLite.debug)
885                   {
886                     // this may not really be a problem but we give a warning
887                     // anyway
888                     System.err
889                             .println("Warning: Possible input parsing error: Null sequence for attachment of PDB (sequence "
890                                     + i + ")");
891                   }
892                 }
893               }
894
895               if (jmolAvailable)
896               {
897                 new jalview.appletgui.AppletJmol(pdb, seqs, chains,
898                         newAlignFrame.alignPanel, protocol);
899                 lastFrameX += 40;
900                 lastFrameY += 40;
901               }
902               else
903                 new MCview.AppletPDBViewer(pdb, seqs, chains,
904                         newAlignFrame.alignPanel, protocol);
905             }
906           }
907
908           pdbFileCount++;
909         } while (pdbFileCount < 10);
910
911         // ///////////////////////////
912         // modify display of features
913         //
914         // hide specific groups
915         param = getParameter("hidefeaturegroups");
916         if (param != null)
917         {
918           applet.setFeatureGroupStateOn(newAlignFrame,param, false);
919         }
920         // show specific groups
921         param = getParameter("showfeaturegroups");
922         if (param != null)
923         {
924           applet.setFeatureGroupStateOn(newAlignFrame,param, true);
925         }
926       }
927       else
928       {
929         fileFound = false;
930         remove(launcher);
931         repaint();
932       }
933     }
934
935     /**
936      * Discovers whether the given file is in the Applet Archive
937      * 
938      * @param file
939      *                String
940      * @return boolean
941      */
942     boolean inArchive(String file)
943     {
944       // This might throw a security exception in certain browsers
945       // Netscape Communicator for instance.
946       try
947       {
948         boolean rtn = (getClass().getResourceAsStream("/" + file) != null);
949         if (debug)
950         {
951           System.err.println("Resource '" + file + "' was "
952                   + (rtn ? "" : "not") + " located by classloader.");
953         }
954         return rtn;
955       } catch (Exception ex)
956       {
957         System.out.println("Exception checking resources: " + file + " "
958                 + ex);
959         return false;
960       }
961     }
962
963     String addProtocol(String file)
964     {
965       if (file.indexOf("://") == -1)
966       {
967         file = getCodeBase() + file;
968         if (debug)
969         {
970           System.err.println("Prepended codebase for resource: '" + file
971                   + "'");
972         }
973       }
974
975       return file;
976     }
977   }
978
979   /**
980    * @return the default alignFrame acted on by the public applet methods. May
981    *         return null with an error message on System.err indicating the
982    *         fact.
983    */
984   protected AlignFrame getDefaultTargetFrame()
985   {
986     if (currentAlignFrame != null)
987     {
988       return currentAlignFrame;
989     }
990     if (initialAlignFrame != null)
991     {
992       return initialAlignFrame;
993     }
994     System.err
995             .println("Implementation error: Jalview Applet API cannot work out which AlignFrame to use.");
996     return null;
997   }
998
999   /**
1000    * separator used for separatorList
1001    */
1002   protected String separator = "|"; // this is a safe(ish) separator - tabs
1003
1004   // don't work for firefox
1005
1006   /**
1007    * parse the string into a list
1008    * 
1009    * @param list
1010    * @return elements separated by separator
1011    */
1012   public String[] separatorListToArray(String list)
1013   {
1014     int seplen = separator.length();
1015     if (list == null || list.equals(""))
1016       return null;
1017     java.util.Vector jv = new Vector();
1018     int cp = 0, pos;
1019     while ((pos = list.indexOf(separator, cp)) > cp)
1020     {
1021       jv.addElement(list.substring(cp, pos));
1022       cp = pos + seplen;
1023     }
1024     if (cp < list.length())
1025     {
1026       jv.addElement(list.substring(cp));
1027     }
1028     if (jv.size() > 0)
1029     {
1030       String[] v = new String[jv.size()];
1031       for (int i = 0; i < v.length; i++)
1032       {
1033         v[i] = (String) jv.elementAt(i);
1034       }
1035       jv.removeAllElements();
1036       if (debug)
1037       {
1038         System.err.println("Array from '" + separator
1039                 + "' separated List:\n" + v.length);
1040         for (int i = 0; i < v.length; i++)
1041         {
1042           System.err.println("item " + i + " '" + v[i] + "'");
1043         }
1044       }
1045       return v;
1046     }
1047     if (debug)
1048     {
1049       System.err.println("Empty Array from '" + separator
1050               + "' separated List");
1051     }
1052     return null;
1053   }
1054
1055   /**
1056    * concatenate the list with separator
1057    * 
1058    * @param list
1059    * @return concatenated string
1060    */
1061   public String arrayToSeparatorList(String[] list)
1062   {
1063     StringBuffer v = new StringBuffer();
1064     if (list != null)
1065     {
1066       for (int i = 0, iSize = list.length - 1; i < iSize; i++)
1067       {
1068         if (list[i] != null)
1069         {
1070           v.append(list[i]);
1071         }
1072         v.append(separator);
1073       }
1074       if (list[list.length - 1] != null)
1075       {
1076         v.append(list[list.length - 1]);
1077       }
1078       if (debug)
1079       {
1080         System.err.println("Returning '" + separator
1081                 + "' separated List:\n");
1082         System.err.println(v);
1083       }
1084       return v.toString();
1085     }
1086     if (debug)
1087     {
1088       System.err.println("Returning empty '" + separator
1089               + "' separated List\n");
1090     }
1091     return "";
1092   }
1093
1094   /**
1095    * @return
1096    * @see jalview.appletgui.AlignFrame#getFeatureGroups()
1097    */
1098   public String getFeatureGroups()
1099   {
1100     String lst = arrayToSeparatorList(getDefaultTargetFrame()
1101             .getFeatureGroups());
1102     return lst;
1103   }
1104
1105   /**
1106    * @param alf
1107    *                alignframe to get feature groups on
1108    * @return
1109    * @see jalview.appletgui.AlignFrame#getFeatureGroups()
1110    */
1111   public String getFeatureGroupsOn(AlignFrame alf)
1112   {
1113     String lst = arrayToSeparatorList(alf.getFeatureGroups());
1114     return lst;
1115   }
1116
1117   /**
1118    * @param visible
1119    * @return
1120    * @see jalview.appletgui.AlignFrame#getFeatureGroupsOfState(boolean)
1121    */
1122   public String getFeatureGroupsOfState(boolean visible)
1123   {
1124     return arrayToSeparatorList(getDefaultTargetFrame()
1125             .getFeatureGroupsOfState(visible));
1126   }
1127
1128   /**
1129    * @param alf
1130    *                align frame to get groups of state visible
1131    * @param visible
1132    * @return
1133    * @see jalview.appletgui.AlignFrame#getFeatureGroupsOfState(boolean)
1134    */
1135   public String getFeatureGroupsOfStateOn(AlignFrame alf, boolean visible)
1136   {
1137     return arrayToSeparatorList(alf.getFeatureGroupsOfState(visible));
1138   }
1139
1140   /**
1141    * @param groups
1142    *                tab separated list of group names
1143    * @param state
1144    *                true or false
1145    * @see jalview.appletgui.AlignFrame#setFeatureGroupState(java.lang.String[],
1146    *      boolean)
1147    */
1148   public void setFeatureGroupStateOn(AlignFrame alf, String groups,
1149           boolean state)
1150   {
1151     boolean st = state;// !(state==null || state.equals("") ||
1152     // state.toLowerCase().equals("false"));
1153     alf.setFeatureGroupState(separatorListToArray(groups), st);
1154   }
1155
1156   public void setFeatureGroupState(String groups, boolean state)
1157   {
1158     setFeatureGroupStateOn(getDefaultTargetFrame(), groups, state);
1159   }
1160
1161   /**
1162    * List separator string
1163    * 
1164    * @return the separator
1165    */
1166   public String getSeparator()
1167   {
1168     return separator;
1169   }
1170
1171   /**
1172    * List separator string
1173    * 
1174    * @param separator
1175    *                the separator to set
1176    */
1177   public void setSeparator(String separator)
1178   {
1179     this.separator = separator;
1180   }
1181 }