1.1 compat
[jalview.git] / src / jalview / bin / JalviewLite.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3  * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.bin;
19
20 import jalview.appletgui.AlignFrame;
21 import jalview.appletgui.EmbmenuFrame;
22 import jalview.appletgui.FeatureSettings;
23 import jalview.datamodel.Alignment;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.ColumnSelection;
26 import jalview.datamodel.PDBEntry;
27 import jalview.datamodel.Sequence;
28 import jalview.datamodel.SequenceGroup;
29 import jalview.datamodel.SequenceI;
30 import jalview.io.AnnotationFile;
31 import jalview.io.AppletFormatAdapter;
32 import jalview.io.FileParse;
33 import jalview.io.IdentifyFile;
34 import jalview.io.JnetAnnotationMaker;
35 import jalview.javascript.JsCallBack;
36 import jalview.structure.SelectionListener;
37 import jalview.structure.StructureSelectionManager;
38
39 import java.applet.Applet;
40 import java.awt.Button;
41 import java.awt.Color;
42 import java.awt.Component;
43 import java.awt.Font;
44 import java.awt.Frame;
45 import java.awt.Graphics;
46 import java.awt.event.ActionEvent;
47 import java.awt.event.WindowAdapter;
48 import java.awt.event.WindowEvent;
49 import java.io.BufferedReader;
50 import java.io.InputStreamReader;
51 import java.util.StringTokenizer;
52 import java.util.Vector;
53
54 /**
55  * Jalview Applet. Runs in Java 1.18 runtime
56  * 
57  * @author $author$
58  * @version $Revision$
59  */
60 public class JalviewLite extends Applet
61 {
62
63   // /////////////////////////////////////////
64   // The following public methods maybe called
65   // externally, eg via javascript in HTML page
66   /**
67    * @return String list of selected sequence IDs, each terminated by "¬"
68    *         (&#172;)
69    */
70   public String getSelectedSequences()
71   {
72     return getSelectedSequencesFrom(getDefaultTargetFrame());
73   }
74
75   /**
76    * @param sep
77    *          separator string or null for default
78    * @return String list of selected sequence IDs, each terminated by sep or
79    *         ("¬" as default)
80    */
81   public String getSelectedSequences(String sep)
82   {
83     return getSelectedSequencesFrom(getDefaultTargetFrame(), sep);
84   }
85
86   /**
87    * @param alf
88    *          alignframe containing selection
89    * @return String list of selected sequence IDs, each terminated by "¬"
90    * 
91    */
92   public String getSelectedSequencesFrom(AlignFrame alf)
93   {
94     return getSelectedSequencesFrom(alf, "¬");
95   }
96
97   /**
98    * get list of selected sequence IDs separated by given separator
99    * 
100    * @param alf
101    *          window containing selection
102    * @param sep
103    *          separator string to use - default is "¬"
104    * @return String list of selected sequence IDs, each terminated by the given
105    *         separator
106    */
107   public String getSelectedSequencesFrom(AlignFrame alf, String sep)
108   {
109     StringBuffer result = new StringBuffer("");
110     if (sep == null || sep.length() == 0)
111     {
112       sep = "¬";
113     }
114     if (alf.viewport.getSelectionGroup() != null)
115     {
116       SequenceI[] seqs = alf.viewport.getSelectionGroup()
117               .getSequencesInOrder(alf.viewport.getAlignment());
118
119       for (int i = 0; i < seqs.length; i++)
120       {
121         result.append(seqs[i].getName());
122         result.append(sep);
123       }
124     }
125
126     return result.toString();
127   }
128
129   /**
130    * 
131    * @param sequenceId id of sequence to highlight
132    * @param position integer position [ tobe implemented or range ] on sequence
133    * @param alignedPosition true/false/empty string - indicate if position is an alignment column or unaligned sequence position
134    */
135   public void highlight(String sequenceId, String position, String alignedPosition)
136   {
137     highlight(currentAlignFrame, sequenceId, position, alignedPosition);
138   }
139   /**
140    * 
141    * @param sequenceId id of sequence to highlight
142    * @param position integer position [ tobe implemented or range ] on sequence
143    * @param alignedPosition false, blank or something else - indicate if position is an alignment column or unaligned sequence position
144    */
145   public void highlight(AlignFrame alf, String sequenceId, String position, String alignedPosition)
146   {
147     SequenceI sq = alf.getAlignViewport().getAlignment().findName(sequenceId);
148     if (sq!=null)
149     {
150       int pos, apos=-1;
151       try {
152         apos = new Integer(position).intValue();
153         apos--;
154       } catch (NumberFormatException ex)
155       {
156         return;
157       }
158       // use vamsas listener to broadcast to all listeners in scope
159       if (alignedPosition!=null && (alignedPosition.trim().length()==0 || alignedPosition.toLowerCase().indexOf("false")>-1))
160       {
161         StructureSelectionManager.getStructureSelectionManager().mouseOverVamsasSequence(sq,sq.findIndex(apos));
162       } else {
163         StructureSelectionManager.getStructureSelectionManager().mouseOverVamsasSequence(sq,apos); 
164       }
165               
166     }
167   }
168   /**
169    * select regions of the currrent alignment frame
170    * 
171    * @param sequenceIds String separated list of sequence ids or empty string
172    * @param columns 
173    *          String separated list { column range or column, ..} or empty string
174    */
175   public void select(String sequenceIds, String columns)
176   {
177     select(currentAlignFrame, sequenceIds, columns, "¬");
178   }
179
180   /**
181    * select regions of the currrent alignment frame
182    * 
183    * @param toselect
184    *          String separated list { column range, seq1...seqn sequence ids }
185    * @param sep
186    *          separator between toselect fields
187    */
188   public void select(String sequenceIds, String columns, String sep)
189   {
190     select(currentAlignFrame, sequenceIds, columns, sep);
191   }
192
193   /**
194    * select regions of the given alignment frame
195    * 
196    * @param alf
197    * @param toselect
198    *          String separated list { column range, seq1...seqn sequence ids }
199    * @param sep
200    *          separator between toselect fields
201    */
202   public void select(AlignFrame alf, String sequenceIds, String columns)
203   {
204     select(alf, sequenceIds, columns, separator);
205   }
206
207   /**
208    * select regions of the given alignment frame
209    * 
210    * @param alf
211    * @param toselect
212    *          String separated list { column range, seq1...seqn sequence ids }
213    * @param sep
214    *          separator between toselect fields
215    */
216   public void select(AlignFrame alf, String sequenceIds, String columns,
217           String sep)
218   {
219     if (sep == null || sep.length() == 0)
220     {
221       sep = separator;
222     }
223     // deparse fields
224     String[] ids = separatorListToArray(sequenceIds, sep);
225     String[] cols = separatorListToArray(columns, sep);
226     SequenceGroup sel = new SequenceGroup();
227     ColumnSelection csel = new ColumnSelection();
228     AlignmentI al = alf.viewport.getAlignment();
229     int start = 0, end = al.getWidth(), alw = al.getWidth();
230     if (ids != null && ids.length > 0)
231     {
232       for (int i = 0; i < ids.length; i++)
233       {
234         if (ids[i].trim().length() == 0)
235         {
236           continue;
237         }
238         SequenceI sq = al.findName(ids[i]);
239         if (sq != null)
240         {
241           sel.addSequence(sq, false);
242         }
243       }
244     }
245     if (cols != null && cols.length > 0)
246     {
247       boolean seset = false;
248       for (int i = 0; i < cols.length; i++)
249       {
250         String cl = cols[i].trim();
251         if (cl.length() == 0)
252         {
253           continue;
254         }
255         int p;
256         if ((p = cl.indexOf("-")) > -1)
257         {
258           int from = -1, to = -1;
259           try
260           {
261             from = new Integer(cl.substring(0, p)).intValue();
262             from--;
263           } catch (NumberFormatException ex)
264           {
265             System.err
266                     .println("ERROR: Couldn't parse first integer in range element column selection string '"
267                             + cl + "' - format is 'from-to'");
268             return;
269           }
270           try
271           {
272             to = new Integer(cl.substring(p + 1)).intValue();
273             to--;
274           } catch (NumberFormatException ex)
275           {
276             System.err
277                     .println("ERROR: Couldn't parse second integer in range element column selection string '"
278                             + cl + "' - format is 'from-to'");
279             return;
280           }
281           if (from >= 0 && to >= 0)
282           {
283             // valid range
284             if (from < to)
285             {
286               int t = to;
287               to = from;
288               to = t;
289             }
290             if (!seset)
291             {
292               start = from;
293               end = to;
294               seset = true;
295             }
296             else
297             {
298               // comment to prevent range extension
299               if (start > from)
300               {
301                 start = from;
302               }
303               if (end < to)
304               {
305                 end = to;
306               }
307             }
308             for (int r = from; r <= to; r++)
309             {
310               if (r >= 0 && r < alw)
311               {
312                 csel.addElement(r);
313               }
314             }
315             if (debug)
316             {
317               System.err.println("Range '" + cl + "' deparsed as [" + from
318                       + "," + to + "]");
319             }
320           }
321           else
322           {
323             System.err.println("ERROR: Invalid Range '" + cl
324                     + "' deparsed as [" + from + "," + to + "]");
325           }
326         }
327         else
328         {
329           int r = -1;
330           try
331           {
332             r = new Integer(cl).intValue();
333             r--;
334           } catch (NumberFormatException ex)
335           {
336             System.err
337                     .println("ERROR: Couldn't parse integer from point selection element of column selection string '"
338                             + cl + "'");
339             return;
340           }
341           if (r >= 0 && r <= alw)
342           {
343             if (!seset)
344             {
345               start = r;
346               end = r;
347               seset = true;
348             }
349             else
350             {
351               // comment to prevent range extension
352               if (start > r)
353               {
354                 start = r;
355               }
356               if (end < r)
357               {
358                 end = r;
359               }
360             }
361             csel.addElement(r);
362             if (debug)
363             {
364               System.err.println("Point selection '" + cl
365                       + "' deparsed as [" + r + "]");
366             }
367           }
368           else
369           {
370             System.err.println("ERROR: Invalid Point selection '" + cl
371                     + "' deparsed as [" + r + "]");
372           }
373         }
374       }
375     }
376     sel.setStartRes(start);
377     sel.setEndRes(end);
378     alf.select(sel, csel);
379
380   }
381
382   /**
383    * get sequences selected in current alignFrame and return their alignment in
384    * format 'format' either with or without suffix
385    * 
386    * @param alf
387    *          - where selection is
388    * @param format
389    *          - format of alignment file
390    * @param suffix
391    *          - "true" to append /start-end string to each sequence ID
392    * @return selected sequences as flat file or empty string if there was no
393    *         current selection
394    */
395   public String getSelectedSequencesAsAlignment(String format, String suffix)
396   {
397     return getSelectedSequencesAsAlignmentFrom(currentAlignFrame, format,
398             suffix);
399   }
400
401   /**
402    * get sequences selected in alf and return their alignment in format 'format'
403    * either with or without suffix
404    * 
405    * @param alf
406    *          - where selection is
407    * @param format
408    *          - format of alignment file
409    * @param suffix
410    *          - "true" to append /start-end string to each sequence ID
411    * @return selected sequences as flat file or empty string if there was no
412    *         current selection
413    */
414   public String getSelectedSequencesAsAlignmentFrom(AlignFrame alf,
415           String format, String suffix)
416   {
417     try
418     {
419       boolean seqlimits = suffix.equalsIgnoreCase("true");
420       if (alf.viewport.getSelectionGroup() != null)
421       {
422         String reply = new AppletFormatAdapter().formatSequences(format,
423                 new Alignment(alf.viewport.getSelectionAsNewSequence()),
424                 seqlimits);
425         return reply;
426       }
427     } catch (Exception ex)
428     {
429       ex.printStackTrace();
430       return "Error retrieving alignment in " + format + " format. ";
431     }
432     return "";
433   }
434
435   public String getAlignment(String format)
436   {
437     return getAlignmentFrom(getDefaultTargetFrame(), format, "true");
438   }
439
440   public String getAlignmentFrom(AlignFrame alf, String format)
441   {
442     return getAlignmentFrom(alf, format, "true");
443   }
444
445   public String getAlignment(String format, String suffix)
446   {
447     return getAlignmentFrom(getDefaultTargetFrame(), format, suffix);
448   }
449
450   public String getAlignmentFrom(AlignFrame alf, String format,
451           String suffix)
452   {
453     try
454     {
455       boolean seqlimits = suffix.equalsIgnoreCase("true");
456
457       String reply = new AppletFormatAdapter().formatSequences(format,
458               alf.viewport.getAlignment(), seqlimits);
459       return reply;
460     } catch (Exception ex)
461     {
462       ex.printStackTrace();
463       return "Error retrieving alignment in " + format + " format. ";
464     }
465   }
466
467   public void loadAnnotation(String annotation)
468   {
469     loadAnnotationFrom(getDefaultTargetFrame(), annotation);
470   }
471
472   public void loadAnnotationFrom(AlignFrame alf, String annotation)
473   {
474     if (new AnnotationFile().readAnnotationFile(alf.getAlignViewport()
475             .getAlignment(), annotation, AppletFormatAdapter.PASTE))
476     {
477       alf.alignPanel.fontChanged();
478       alf.alignPanel.setScrollValues(0, 0);
479     }
480     else
481     {
482       alf.parseFeaturesFile(annotation, AppletFormatAdapter.PASTE);
483     }
484   }
485
486   public String getFeatures(String format)
487   {
488     return getFeaturesFrom(getDefaultTargetFrame(), format);
489   }
490
491   public String getFeaturesFrom(AlignFrame alf, String format)
492   {
493     return alf.outputFeatures(false, format);
494   }
495
496   public String getAnnotation()
497   {
498     return getAnnotationFrom(getDefaultTargetFrame());
499   }
500
501   public String getAnnotationFrom(AlignFrame alf)
502   {
503     return alf.outputAnnotations(false);
504   }
505
506   public AlignFrame newView()
507   {
508     return newViewFrom(getDefaultTargetFrame());
509   }
510
511   public AlignFrame newView(String name)
512   {
513     return newViewFrom(getDefaultTargetFrame(), name);
514   }
515
516   public AlignFrame newViewFrom(AlignFrame alf)
517   {
518     return alf.newView(null);
519   }
520
521   public AlignFrame newViewFrom(AlignFrame alf, String name)
522   {
523     return alf.newView(name);
524   }
525
526   /**
527    * 
528    * @param text
529    *          alignment file as a string
530    * @param title
531    *          window title
532    * @return null or new alignment frame
533    */
534   public AlignFrame loadAlignment(String text, String title)
535   {
536     Alignment al = null;
537
538     String format = new IdentifyFile().Identify(text,
539             AppletFormatAdapter.PASTE);
540     try
541     {
542       al = new AppletFormatAdapter().readFile(text,
543               AppletFormatAdapter.PASTE, format);
544       if (al.getHeight() > 0)
545       {
546         return new AlignFrame(al, this, title, false);
547       }
548     } catch (java.io.IOException ex)
549     {
550       ex.printStackTrace();
551     }
552     return null;
553   }
554
555   public void setMouseoverListener(String listener)
556   {
557     setMouseoverListener(currentAlignFrame, listener);
558   }
559
560   private Vector mouseoverListeners = new Vector();
561
562   public void setMouseoverListener(AlignFrame af, String listener)
563   {
564     if (listener != null)
565     {
566       listener = listener.trim();
567       if (listener.length() == 0)
568       {
569         System.err
570                 .println("jalview Javascript error: Ignoring empty function for mouseover listener.");
571         return;
572       }
573     }
574     jalview.javascript.MouseOverListener mol = new jalview.javascript.MouseOverListener(
575             this, af, listener);
576     mouseoverListeners.addElement(mol);
577     StructureSelectionManager.getStructureSelectionManager()
578             .addStructureViewerListener(mol);
579     if (debug)
580     {
581       System.err.println("Added a mouseover listener for "
582               + ((af == null) ? "All frames" : "Just views for "
583                       + af.getAlignViewport().getSequenceSetId()));
584       System.err.println("There are now " + mouseoverListeners.size()
585               + " listeners in total.");
586     }
587   }
588
589   public void setSelectionListener(String listener)
590   {
591     setSelectionListener(currentAlignFrame, listener);
592   }
593
594   public void setSelectionListener(AlignFrame af, String listener)
595   {
596     if (listener != null)
597     {
598       listener = listener.trim();
599       if (listener.length() == 0)
600       {
601         System.err
602                 .println("jalview Javascript error: Ignoring empty function for selection listener.");
603         return;
604       }
605     }
606     jalview.javascript.JsSelectionSender mol = new jalview.javascript.JsSelectionSender(
607             this, af, listener);
608     mouseoverListeners.addElement(mol);
609     StructureSelectionManager.getStructureSelectionManager()
610             .addSelectionListener(mol);
611     if (debug)
612     {
613       System.err.println("Added a selection listener for "
614               + ((af == null) ? "All frames" : "Just views for "
615                       + af.getAlignViewport().getSequenceSetId()));
616       System.err.println("There are now " + mouseoverListeners.size()
617               + " listeners in total.");
618     }
619   }
620
621   /**
622    * remove any callback using the given listener function and associated with
623    * the given alignFrame (or null for all callbacks)
624    * 
625    * @param af
626    *          (may be null)
627    * @param listener
628    *          (may be null)
629    */
630   public void removeJavascriptListener(AlignFrame af, String listener)
631   {
632     if (listener != null)
633     {
634       listener = listener.trim();
635       if (listener.length() == 0)
636       {
637         listener = null;
638       }
639     }
640     boolean rprt = false;
641     for (int ms=0,msSize=mouseoverListeners.size();ms<msSize;)
642     {
643       Object lstn = mouseoverListeners.elementAt(ms);
644       JsCallBack lstner = (JsCallBack) lstn;
645       if ((af == null || lstner.getAlignFrame() == af)
646               && (listener == null || lstner.getListenerFunction().equals(
647                       listener)))
648       {
649         mouseoverListeners.removeElement(lstner);
650         msSize--;
651         if (lstner instanceof SelectionListener)
652         {
653           StructureSelectionManager.getStructureSelectionManager()
654                   .removeSelectionListener((SelectionListener) lstner);
655         }
656         else
657         {
658           StructureSelectionManager.getStructureSelectionManager()
659                   .removeStructureViewerListener(lstner, null);
660         }
661         rprt = debug;
662         if (debug)
663         {
664           System.err.println("Removed listener '" + listener + "'");
665         }
666       } else {
667         ms++;
668       }
669     }
670     if (rprt)
671     {
672       System.err.println("There are now " + mouseoverListeners.size()
673               + " listeners in total.");
674     }
675   }
676
677   public void stop()
678   {
679     if (mouseoverListeners!=null) 
680     {
681       while (mouseoverListeners.size()>0)
682       {
683         Object mol =         mouseoverListeners.elementAt(0);
684         mouseoverListeners.removeElement(mol);
685         if (mol instanceof SelectionListener)
686         {
687           StructureSelectionManager.getStructureSelectionManager().removeSelectionListener((SelectionListener)mol);          
688         } else {
689           StructureSelectionManager.getStructureSelectionManager().removeStructureViewerListener(mol, null);
690         }
691       }
692     }
693   }
694   /**
695    * send a mouseover message to all the alignment windows associated with the
696    * given residue in the pdbfile
697    * 
698    * @param pdbResNum
699    * @param chain
700    * @param pdbfile
701    */
702   public void mouseOverStructure(int pdbResNum, String chain, String pdbfile)
703   {
704     StructureSelectionManager.getStructureSelectionManager()
705             .mouseOverStructure(pdbResNum, chain, pdbfile);
706   }
707
708   // //////////////////////////////////////////////
709   // //////////////////////////////////////////////
710
711   public static int lastFrameX = 200;
712
713   public static int lastFrameY = 200;
714
715   boolean fileFound = true;
716
717   String file = "No file";
718
719   Button launcher = new Button("Start Jalview");
720
721   /**
722    * The currentAlignFrame is static, it will change if and when the user
723    * selects a new window. Note that it will *never* point back to the embedded
724    * AlignFrame if the applet is started as embedded on the page and then
725    * afterwards a new view is created.
726    */
727   public static AlignFrame currentAlignFrame = null;
728
729   /**
730    * This is the first frame to be displayed, and does not change. API calls
731    * will default to this instance if currentAlignFrame is null.
732    */
733   AlignFrame initialAlignFrame = null;
734
735   boolean embedded = false;
736
737   private boolean checkForJmol = true;
738
739   private boolean checkedForJmol = false; // ensure we don't check for jmol
740
741   // every time the app is re-inited
742
743   public boolean jmolAvailable = false;
744
745   private boolean alignPdbStructures = false;
746
747   public static boolean debug = false;
748
749   static String builddate = null, version = null;
750
751   private static void initBuildDetails()
752   {
753     if (builddate == null)
754     {
755       builddate = "unknown";
756       version = "test";
757       java.net.URL url = JalviewLite.class
758               .getResource("/.build_properties");
759       if (url != null)
760       {
761         try
762         {
763           BufferedReader reader = new BufferedReader(new InputStreamReader(
764                   url.openStream()));
765           String line;
766           while ((line = reader.readLine()) != null)
767           {
768             if (line.indexOf("VERSION") > -1)
769             {
770               version = line.substring(line.indexOf("=") + 1);
771             }
772             if (line.indexOf("BUILD_DATE") > -1)
773             {
774               builddate = line.substring(line.indexOf("=") + 1);
775             }
776           }
777         } catch (Exception ex)
778         {
779           ex.printStackTrace();
780         }
781       }
782     }
783   }
784
785   public static String getBuildDate()
786   {
787     initBuildDetails();
788     return builddate;
789   }
790
791   public static String getVersion()
792   {
793     initBuildDetails();
794     return version;
795   }
796
797   /**
798    * init method for Jalview Applet
799    */
800   public void init()
801   {
802     // remove any handlers that might be hanging around from an earlier instance
803     
804     /**
805      * turn on extra applet debugging
806      */
807     String dbg = getParameter("debug");
808     if (dbg != null)
809     {
810       debug = dbg.toLowerCase().equals("true");
811     }
812     if (debug)
813     {
814
815       System.err.println("JalviewLite Version " + getVersion());
816       System.err.println("Build Date : " + getBuildDate());
817
818     }
819     /**
820      * if true disable the check for jmol
821      */
822     String chkforJmol = getParameter("nojmol");
823     if (chkforJmol != null)
824     {
825       checkForJmol = !chkforJmol.equals("true");
826     }
827     /**
828      * get the separator parameter if present
829      */
830     String sep = getParameter("separator");
831     if (sep != null)
832     {
833       if (sep.length() > 0)
834       {
835         separator = sep;
836         if (debug)
837         {
838           System.err.println("Separator set to '" + separator + "'");
839         }
840       }
841       else
842       {
843         throw new Error(
844                 "Invalid separator parameter - must be non-zero length");
845       }
846     }
847     int r = 255;
848     int g = 255;
849     int b = 255;
850     String param = getParameter("RGB");
851
852     if (param != null)
853     {
854       try
855       {
856         r = Integer.parseInt(param.substring(0, 2), 16);
857         g = Integer.parseInt(param.substring(2, 4), 16);
858         b = Integer.parseInt(param.substring(4, 6), 16);
859       } catch (Exception ex)
860       {
861         r = 255;
862         g = 255;
863         b = 255;
864       }
865     }
866
867     param = getParameter("label");
868     if (param != null)
869     {
870       launcher.setLabel(param);
871     }
872
873     this.setBackground(new Color(r, g, b));
874
875     file = getParameter("file");
876
877     if (file == null)
878     {
879       // Maybe the sequences are added as parameters
880       StringBuffer data = new StringBuffer("PASTE");
881       int i = 1;
882       while ((file = getParameter("sequence" + i)) != null)
883       {
884         data.append(file.toString() + "\n");
885         i++;
886       }
887       if (data.length() > 5)
888       {
889         file = data.toString();
890       }
891     }
892
893     final JalviewLite applet = this;
894     if (getParameter("embedded") != null
895             && getParameter("embedded").equalsIgnoreCase("true"))
896     {
897       // Launch as embedded applet in page
898       embedded = true;
899       LoadingThread loader = new LoadingThread(file, applet);
900       loader.start();
901     }
902     else if (file != null)
903     {
904       if (getParameter("showbutton") == null
905               || !getParameter("showbutton").equalsIgnoreCase("false"))
906       {
907         // Add the JalviewLite 'Button' to the page
908         add(launcher);
909         launcher.addActionListener(new java.awt.event.ActionListener()
910         {
911           public void actionPerformed(ActionEvent e)
912           {
913             LoadingThread loader = new LoadingThread(file, applet);
914             loader.start();
915           }
916         });
917       }
918       else
919       {
920         // Open jalviewLite immediately.
921         LoadingThread loader = new LoadingThread(file, applet);
922         loader.start();
923       }
924     }
925     else
926     {
927       // jalview initialisation with no alignment. loadAlignment() method can
928       // still be called to open new alignments.
929       file = "NO FILE";
930       fileFound = false;
931     }
932   }
933
934   /**
935    * Initialises and displays a new java.awt.Frame
936    * 
937    * @param frame
938    *          java.awt.Frame to be displayed
939    * @param title
940    *          title of new frame
941    * @param width
942    *          width if new frame
943    * @param height
944    *          height of new frame
945    */
946   public static void addFrame(final Frame frame, String title, int width,
947           int height)
948   {
949     frame.setLocation(lastFrameX, lastFrameY);
950     lastFrameX += 40;
951     lastFrameY += 40;
952     frame.setSize(width, height);
953     frame.setTitle(title);
954     frame.addWindowListener(new WindowAdapter()
955     {
956       public void windowClosing(WindowEvent e)
957       {
958         if (frame instanceof AlignFrame)
959         {
960           ((AlignFrame) frame).closeMenuItem_actionPerformed();
961         }
962         if (currentAlignFrame == frame)
963         {
964           currentAlignFrame = null;
965         }
966         lastFrameX -= 40;
967         lastFrameY -= 40;
968         if (frame instanceof EmbmenuFrame)
969         {
970           ((EmbmenuFrame) frame).destroyMenus();
971         }
972         frame.setMenuBar(null);
973         frame.dispose();
974       }
975
976       public void windowActivated(WindowEvent e)
977       {
978         if (frame instanceof AlignFrame)
979         {
980           currentAlignFrame = (AlignFrame) frame;
981           if (debug)
982           {
983             System.err.println("Activated window " + frame);
984           }
985         }
986         // be good.
987         super.windowActivated(e);
988       }
989       /*
990        * Probably not necessary to do this - see TODO above. (non-Javadoc)
991        * 
992        * @see
993        * java.awt.event.WindowAdapter#windowDeactivated(java.awt.event.WindowEvent
994        * )
995        * 
996        * public void windowDeactivated(WindowEvent e) { if (currentAlignFrame ==
997        * frame) { currentAlignFrame = null; if (debug) {
998        * System.err.println("Deactivated window "+frame); } }
999        * super.windowDeactivated(e); }
1000        */
1001     });
1002     frame.setVisible(true);
1003   }
1004
1005   /**
1006    * This paints the background surrounding the "Launch Jalview button" <br>
1007    * <br>
1008    * If file given in parameter not found, displays error message
1009    * 
1010    * @param g
1011    *          graphics context
1012    */
1013   public void paint(Graphics g)
1014   {
1015     if (!fileFound)
1016     {
1017       g.setColor(new Color(200, 200, 200));
1018       g.setColor(Color.cyan);
1019       g.fillRect(0, 0, getSize().width, getSize().height);
1020       g.setColor(Color.red);
1021       g.drawString("Jalview can't open file", 5, 15);
1022       g.drawString("\"" + file + "\"", 5, 30);
1023     }
1024     else if (embedded)
1025     {
1026       g.setColor(Color.black);
1027       g.setFont(new Font("Arial", Font.BOLD, 24));
1028       g.drawString("Jalview Applet", 50, this.getSize().height / 2 - 30);
1029       g.drawString("Loading Data...", 50, this.getSize().height / 2);
1030     }
1031   }
1032
1033   class LoadJmolThread extends Thread
1034   {
1035     private boolean running = false;
1036
1037     public void run()
1038     {
1039       if (running || checkedForJmol)
1040       {
1041         return;
1042       }
1043       running = true;
1044       if (checkForJmol)
1045       {
1046         try
1047         {
1048           if (!System.getProperty("java.version").startsWith("1.1"))
1049           {
1050             Class.forName("org.jmol.adapter.smarter.SmarterJmolAdapter");
1051             jmolAvailable = true;
1052           }
1053           if (!jmolAvailable)
1054           {
1055             System.out
1056                     .println("Jmol not available - Using MCview for structures");
1057           }
1058         } catch (java.lang.ClassNotFoundException ex)
1059         {
1060         }
1061       }
1062       else
1063       {
1064         jmolAvailable = false;
1065         if (debug)
1066         {
1067           System.err
1068                   .println("Skipping Jmol check. Will use MCView (probably)");
1069         }
1070       }
1071       checkedForJmol = true;
1072       running = false;
1073     }
1074
1075     public boolean notFinished()
1076     {
1077       return running || !checkedForJmol;
1078     }
1079   }
1080
1081   class LoadingThread extends Thread
1082   {
1083     /**
1084      * State variable: File source
1085      */
1086     String file;
1087
1088     /**
1089      * State variable: protocol for access to file source
1090      */
1091     String protocol;
1092
1093     /**
1094      * State variable: format of file source
1095      */
1096     String format;
1097
1098     String _file;
1099
1100     JalviewLite applet;
1101
1102     private void dbgMsg(String msg)
1103     {
1104       if (applet.debug)
1105       {
1106         System.err.println(msg);
1107       }
1108     }
1109
1110     /**
1111      * update the protocol state variable for accessing the datasource located
1112      * by file.
1113      * 
1114      * @param file
1115      * @return possibly updated datasource string
1116      */
1117     public String setProtocolState(String file)
1118     {
1119       if (file.startsWith("PASTE"))
1120       {
1121         file = file.substring(5);
1122         protocol = AppletFormatAdapter.PASTE;
1123       }
1124       else if (inArchive(file))
1125       {
1126         protocol = AppletFormatAdapter.CLASSLOADER;
1127       }
1128       else
1129       {
1130         file = addProtocol(file);
1131         protocol = AppletFormatAdapter.URL;
1132       }
1133       dbgMsg("Protocol identified as '" + protocol + "'");
1134       return file;
1135     }
1136
1137     public LoadingThread(String _file, JalviewLite _applet)
1138     {
1139       this._file = _file;
1140       applet = _applet;
1141     }
1142
1143     public void run()
1144     {
1145       LoadJmolThread jmolchecker = new LoadJmolThread();
1146       jmolchecker.start();
1147       while (jmolchecker.notFinished())
1148       {
1149         // wait around until the Jmol check is complete.
1150         try
1151         {
1152           Thread.sleep(2);
1153         } catch (Exception e)
1154         {
1155         }
1156         ;
1157       }
1158       startLoading();
1159     }
1160
1161     private void startLoading()
1162     {
1163       AlignFrame newAlignFrame;
1164       dbgMsg("Loading thread started with:\n>>file\n" + _file + ">>endfile");
1165       file = setProtocolState(_file);
1166
1167       format = new jalview.io.IdentifyFile().Identify(file, protocol);
1168       dbgMsg("File identified as '" + format + "'");
1169       dbgMsg("Loading started.");
1170       Alignment al = null;
1171       try
1172       {
1173         al = new AppletFormatAdapter().readFile(file, protocol, format);
1174       } catch (java.io.IOException ex)
1175       {
1176         dbgMsg("File load exception.");
1177         ex.printStackTrace();
1178         if (debug)
1179         {
1180           try
1181           {
1182             FileParse fp = new FileParse(file, protocol);
1183             String ln = null;
1184             dbgMsg(">>>Dumping contents of '" + file + "' " + "("
1185                     + protocol + ")");
1186             while ((ln = fp.nextLine()) != null)
1187             {
1188               dbgMsg(ln);
1189             }
1190             dbgMsg(">>>Dump finished.");
1191           } catch (Exception e)
1192           {
1193             System.err
1194                     .println("Exception when trying to dump the content of the file parameter.");
1195             e.printStackTrace();
1196           }
1197         }
1198       }
1199       if ((al != null) && (al.getHeight() > 0))
1200       {
1201         dbgMsg("Successfully loaded file.");
1202         newAlignFrame = new AlignFrame(al, applet, file, embedded);
1203         if (initialAlignFrame == null)
1204         {
1205           initialAlignFrame = newAlignFrame;
1206         }
1207         // update the focus.
1208         currentAlignFrame = newAlignFrame;
1209
1210         if (protocol == jalview.io.AppletFormatAdapter.PASTE)
1211         {
1212           newAlignFrame.setTitle("Sequences from " + getDocumentBase());
1213         }
1214
1215         newAlignFrame.statusBar.setText("Successfully loaded file " + file);
1216
1217         String treeFile = applet.getParameter("tree");
1218         if (treeFile == null)
1219         {
1220           treeFile = applet.getParameter("treeFile");
1221         }
1222
1223         if (treeFile != null)
1224         {
1225           try
1226           {
1227             treeFile = setProtocolState(treeFile);
1228             /*
1229              * if (inArchive(treeFile)) { protocol =
1230              * AppletFormatAdapter.CLASSLOADER; } else { protocol =
1231              * AppletFormatAdapter.URL; treeFile = addProtocol(treeFile); }
1232              */
1233             jalview.io.NewickFile fin = new jalview.io.NewickFile(treeFile,
1234                     protocol);
1235
1236             fin.parse();
1237
1238             if (fin.getTree() != null)
1239             {
1240               newAlignFrame.loadTree(fin, treeFile);
1241               dbgMsg("Successfuly imported tree.");
1242             }
1243             else
1244             {
1245               dbgMsg("Tree parameter did not resolve to a valid tree.");
1246             }
1247           } catch (Exception ex)
1248           {
1249             ex.printStackTrace();
1250           }
1251         }
1252
1253         String param = getParameter("features");
1254         if (param != null)
1255         {
1256           param = setProtocolState(param);
1257
1258           newAlignFrame.parseFeaturesFile(param, protocol);
1259         }
1260
1261         param = getParameter("showFeatureSettings");
1262         if (param != null && param.equalsIgnoreCase("true"))
1263         {
1264           newAlignFrame.viewport.showSequenceFeatures(true);
1265           new FeatureSettings(newAlignFrame.alignPanel);
1266         }
1267
1268         param = getParameter("annotations");
1269         if (param != null)
1270         {
1271           param = setProtocolState(param);
1272
1273           if (new AnnotationFile().readAnnotationFile(
1274                   newAlignFrame.viewport.getAlignment(), param, protocol))
1275           {
1276             newAlignFrame.alignPanel.fontChanged();
1277             newAlignFrame.alignPanel.setScrollValues(0, 0);
1278           }
1279           else
1280           {
1281             System.err
1282                     .println("Annotations were not added from annotation file '"
1283                             + param + "'");
1284           }
1285
1286         }
1287
1288         param = getParameter("jnetfile");
1289         if (param != null)
1290         {
1291           try
1292           {
1293             param = setProtocolState(param);
1294             jalview.io.JPredFile predictions = new jalview.io.JPredFile(
1295                     param, protocol);
1296             JnetAnnotationMaker.add_annotation(predictions,
1297                     newAlignFrame.viewport.getAlignment(), 0, false); // false==do
1298             // not
1299             // add
1300             // sequence
1301             // profile
1302             // from
1303             // concise
1304             // output
1305             newAlignFrame.alignPanel.fontChanged();
1306             newAlignFrame.alignPanel.setScrollValues(0, 0);
1307           } catch (Exception ex)
1308           {
1309             ex.printStackTrace();
1310           }
1311         }
1312         /*
1313          * <param name="alignpdbfiles" value="false/true"/> Undocumented for 2.6
1314          * - related to JAL-434
1315          */
1316         applet.setAlignPdbStructures(getDefaultParameter("alignpdbfiles",
1317                 false));
1318         /*
1319          * <param name="PDBfile" value="1gaq.txt PDB|1GAQ|1GAQ|A PDB|1GAQ|1GAQ|B
1320          * PDB|1GAQ|1GAQ|C">
1321          * 
1322          * <param name="PDBfile2" value="1gaq.txt A=SEQA B=SEQB C=SEQB">
1323          * 
1324          * <param name="PDBfile3" value="1q0o Q45135_9MICO">
1325          */
1326
1327         int pdbFileCount = 0;
1328         // Accumulate pdbs here if they are heading for the same view (if
1329         // alignPdbStructures is true)
1330         Vector pdbs = new Vector();
1331         do
1332         {
1333           if (pdbFileCount > 0)
1334             param = getParameter("PDBFILE" + pdbFileCount);
1335           else
1336             param = getParameter("PDBFILE");
1337
1338           if (param != null)
1339           {
1340             PDBEntry pdb = new PDBEntry();
1341
1342             String seqstring;
1343             SequenceI[] seqs = null;
1344             String[] chains = null;
1345
1346             StringTokenizer st = new StringTokenizer(param, " ");
1347
1348             if (st.countTokens() < 2)
1349             {
1350               String sequence = applet.getParameter("PDBSEQ");
1351               if (sequence != null)
1352                 seqs = new SequenceI[]
1353                 { (Sequence) newAlignFrame.getAlignViewport()
1354                         .getAlignment().findName(sequence) };
1355
1356             }
1357             else
1358             {
1359               param = st.nextToken();
1360               Vector tmp = new Vector();
1361               Vector tmp2 = new Vector();
1362
1363               while (st.hasMoreTokens())
1364               {
1365                 seqstring = st.nextToken();
1366                 StringTokenizer st2 = new StringTokenizer(seqstring, "=");
1367                 if (st2.countTokens() > 1)
1368                 {
1369                   // This is the chain
1370                   tmp2.addElement(st2.nextToken());
1371                   seqstring = st2.nextToken();
1372                 }
1373                 tmp.addElement((Sequence) newAlignFrame.getAlignViewport()
1374                         .getAlignment().findName(seqstring));
1375               }
1376
1377               seqs = new SequenceI[tmp.size()];
1378               tmp.copyInto(seqs);
1379               if (tmp2.size() == tmp.size())
1380               {
1381                 chains = new String[tmp2.size()];
1382                 tmp2.copyInto(chains);
1383               }
1384             }
1385             param = setProtocolState(param);
1386
1387             if (// !jmolAvailable
1388             // &&
1389             protocol == AppletFormatAdapter.CLASSLOADER)
1390             {
1391               // TODO: verify this Re:
1392               // https://mantis.lifesci.dundee.ac.uk/view.php?id=36605
1393               // This exception preserves the current behaviour where, even if
1394               // the local pdb file was identified in the class loader
1395               protocol = AppletFormatAdapter.URL; // this is probably NOT
1396               // CORRECT!
1397               param = addProtocol(param); //
1398             }
1399
1400             pdb.setFile(param);
1401
1402             if (seqs != null)
1403             {
1404               for (int i = 0; i < seqs.length; i++)
1405               {
1406                 if (seqs[i] != null)
1407                 {
1408                   ((Sequence) seqs[i]).addPDBId(pdb);
1409                 }
1410                 else
1411                 {
1412                   if (JalviewLite.debug)
1413                   {
1414                     // this may not really be a problem but we give a warning
1415                     // anyway
1416                     System.err
1417                             .println("Warning: Possible input parsing error: Null sequence for attachment of PDB (sequence "
1418                                     + i + ")");
1419                   }
1420                 }
1421               }
1422
1423               if (!alignPdbStructures)
1424               {
1425                 newAlignFrame.newStructureView(applet, pdb, seqs, chains,
1426                         protocol);
1427               }
1428               else
1429               {
1430                 pdbs.addElement(new Object[]
1431                 { pdb, seqs, chains, new String(protocol) });
1432               }
1433             }
1434           }
1435
1436           pdbFileCount++;
1437         } while (pdbFileCount < 10);
1438         if (pdbs.size() > 0)
1439         {
1440           SequenceI[][] seqs = new SequenceI[pdbs.size()][];
1441           PDBEntry[] pdb = new PDBEntry[pdbs.size()];
1442           String[][] chains = new String[pdbs.size()][];
1443           String[] protocols = new String[pdbs.size()];
1444           for (int pdbsi = 0, pdbsiSize = pdbs.size(); pdbsi < pdbsiSize; pdbsi++)
1445           {
1446             Object[] o = (Object[]) pdbs.elementAt(pdbsi);
1447             pdb[pdbsi] = (PDBEntry) o[0];
1448             seqs[pdbsi] = (SequenceI[]) o[1];
1449             chains[pdbsi] = (String[]) o[2];
1450             protocols[pdbsi] = (String) o[3];
1451           }
1452           newAlignFrame.alignedStructureView(applet, pdb, seqs, chains,
1453                   protocols);
1454
1455         }
1456         // ///////////////////////////
1457         // modify display of features
1458         //
1459         // hide specific groups
1460         param = getParameter("hidefeaturegroups");
1461         if (param != null)
1462         {
1463           applet.setFeatureGroupStateOn(newAlignFrame, param, false);
1464         }
1465         // show specific groups
1466         param = getParameter("showfeaturegroups");
1467         if (param != null)
1468         {
1469           applet.setFeatureGroupStateOn(newAlignFrame, param, true);
1470         }
1471       }
1472       else
1473       {
1474         fileFound = false;
1475         remove(launcher);
1476         repaint();
1477       }
1478     }
1479
1480     /**
1481      * Discovers whether the given file is in the Applet Archive
1482      * 
1483      * @param file
1484      *          String
1485      * @return boolean
1486      */
1487     boolean inArchive(String file)
1488     {
1489       // This might throw a security exception in certain browsers
1490       // Netscape Communicator for instance.
1491       try
1492       {
1493         boolean rtn = (getClass().getResourceAsStream("/" + file) != null);
1494         if (debug)
1495         {
1496           System.err.println("Resource '" + file + "' was "
1497                   + (rtn ? "" : "not") + " located by classloader.");
1498         }
1499         return rtn;
1500       } catch (Exception ex)
1501       {
1502         System.out.println("Exception checking resources: " + file + " "
1503                 + ex);
1504         return false;
1505       }
1506     }
1507
1508     String addProtocol(String file)
1509     {
1510       if (file.indexOf("://") == -1)
1511       {
1512         file = getCodeBase() + file;
1513         if (debug)
1514         {
1515           System.err.println("Prepended codebase for resource: '" + file
1516                   + "'");
1517         }
1518       }
1519
1520       return file;
1521     }
1522   }
1523
1524   /**
1525    * @return the default alignFrame acted on by the public applet methods. May
1526    *         return null with an error message on System.err indicating the
1527    *         fact.
1528    */
1529   protected AlignFrame getDefaultTargetFrame()
1530   {
1531     if (currentAlignFrame != null)
1532     {
1533       return currentAlignFrame;
1534     }
1535     if (initialAlignFrame != null)
1536     {
1537       return initialAlignFrame;
1538     }
1539     System.err
1540             .println("Implementation error: Jalview Applet API cannot work out which AlignFrame to use.");
1541     return null;
1542   }
1543
1544   /**
1545    * separator used for separatorList
1546    */
1547   protected String separator = "|"; // this is a safe(ish) separator - tabs
1548
1549   // don't work for firefox
1550
1551   /**
1552    * parse the string into a list
1553    * 
1554    * @param list
1555    * @return elements separated by separator
1556    */
1557   public String[] separatorListToArray(String list)
1558   {
1559     return separatorListToArray(list, separator);
1560   }
1561
1562   /**
1563    * parse the string into a list
1564    * 
1565    * @param list
1566    * @param separator
1567    * @return elements separated by separator
1568    */
1569   public String[] separatorListToArray(String list, String separator)
1570   {
1571     // note separator local variable intentionally masks object field
1572     int seplen = separator.length();
1573     if (list == null || list.equals("") || list.equals(separator))
1574       return null;
1575     java.util.Vector jv = new Vector();
1576     int cp = 0, pos;
1577     while ((pos = list.indexOf(separator, cp)) > cp)
1578     {
1579       jv.addElement(list.substring(cp, pos));
1580       cp = pos + seplen;
1581     }
1582     if (cp < list.length())
1583     {
1584       jv.addElement(list.substring(cp));
1585     }
1586     if (jv.size() > 0)
1587     {
1588       String[] v = new String[jv.size()];
1589       for (int i = 0; i < v.length; i++)
1590       {
1591         v[i] = (String) jv.elementAt(i);
1592       }
1593       jv.removeAllElements();
1594       if (debug)
1595       {
1596         System.err.println("Array from '" + separator
1597                 + "' separated List:\n" + v.length);
1598         for (int i = 0; i < v.length; i++)
1599         {
1600           System.err.println("item " + i + " '" + v[i] + "'");
1601         }
1602       }
1603       return v;
1604     }
1605     if (debug)
1606     {
1607       System.err.println("Empty Array from '" + separator
1608               + "' separated List");
1609     }
1610     return null;
1611   }
1612
1613   /**
1614    * concatenate the list with separator
1615    * 
1616    * @param list
1617    * @return concatenated string
1618    */
1619   public String arrayToSeparatorList(String[] list)
1620   {
1621     return arrayToSeparatorList(list, separator);
1622   }
1623
1624   /**
1625    * concatenate the list with separator
1626    * 
1627    * @param list
1628    * @param separator
1629    * @return concatenated string
1630    */
1631   public String arrayToSeparatorList(String[] list, String separator)
1632   {
1633     StringBuffer v = new StringBuffer();
1634     if (list != null && list.length > 0)
1635     {
1636       for (int i = 0, iSize = list.length - 1; i < iSize; i++)
1637       {
1638         if (list[i] != null)
1639         {
1640           v.append(list[i]);
1641         }
1642         v.append(separator);
1643       }
1644       if (list[list.length - 1] != null)
1645       {
1646         v.append(list[list.length - 1]);
1647       }
1648       if (debug)
1649       {
1650         System.err.println("Returning '" + separator
1651                 + "' separated List:\n");
1652         System.err.println(v);
1653       }
1654       return v.toString();
1655     }
1656     if (debug)
1657     {
1658       System.err.println("Returning empty '" + separator
1659               + "' separated List\n");
1660     }
1661     return "" + separator;
1662   }
1663
1664   /**
1665    * @return
1666    * @see jalview.appletgui.AlignFrame#getFeatureGroups()
1667    */
1668   public String getFeatureGroups()
1669   {
1670     String lst = arrayToSeparatorList(getDefaultTargetFrame()
1671             .getFeatureGroups());
1672     return lst;
1673   }
1674
1675   /**
1676    * @param alf
1677    *          alignframe to get feature groups on
1678    * @return
1679    * @see jalview.appletgui.AlignFrame#getFeatureGroups()
1680    */
1681   public String getFeatureGroupsOn(AlignFrame alf)
1682   {
1683     String lst = arrayToSeparatorList(alf.getFeatureGroups());
1684     return lst;
1685   }
1686
1687   /**
1688    * @param visible
1689    * @return
1690    * @see jalview.appletgui.AlignFrame#getFeatureGroupsOfState(boolean)
1691    */
1692   public String getFeatureGroupsOfState(boolean visible)
1693   {
1694     return arrayToSeparatorList(getDefaultTargetFrame()
1695             .getFeatureGroupsOfState(visible));
1696   }
1697
1698   /**
1699    * @param alf
1700    *          align frame to get groups of state visible
1701    * @param visible
1702    * @return
1703    * @see jalview.appletgui.AlignFrame#getFeatureGroupsOfState(boolean)
1704    */
1705   public String getFeatureGroupsOfStateOn(AlignFrame alf, boolean visible)
1706   {
1707     return arrayToSeparatorList(alf.getFeatureGroupsOfState(visible));
1708   }
1709
1710   /**
1711    * @param groups
1712    *          tab separated list of group names
1713    * @param state
1714    *          true or false
1715    * @see jalview.appletgui.AlignFrame#setFeatureGroupState(java.lang.String[],
1716    *      boolean)
1717    */
1718   public void setFeatureGroupStateOn(AlignFrame alf, String groups,
1719           boolean state)
1720   {
1721     boolean st = state;// !(state==null || state.equals("") ||
1722     // state.toLowerCase().equals("false"));
1723     alf.setFeatureGroupState(separatorListToArray(groups), st);
1724   }
1725
1726   public void setFeatureGroupState(String groups, boolean state)
1727   {
1728     setFeatureGroupStateOn(getDefaultTargetFrame(), groups, state);
1729   }
1730
1731   /**
1732    * List separator string
1733    * 
1734    * @return the separator
1735    */
1736   public String getSeparator()
1737   {
1738     return separator;
1739   }
1740
1741   /**
1742    * List separator string
1743    * 
1744    * @param separator
1745    *          the separator to set
1746    */
1747   public void setSeparator(String separator)
1748   {
1749     this.separator = separator;
1750   }
1751
1752   /**
1753    * get boolean value of applet parameter 'name' and return default if
1754    * parameter is not set
1755    * 
1756    * @param name
1757    *          name of paremeter
1758    * @param def
1759    *          the value to return otherwise
1760    * @return true or false
1761    */
1762   public boolean getDefaultParameter(String name, boolean def)
1763   {
1764     String stn;
1765     if ((stn = getParameter(name)) == null)
1766     {
1767       return def;
1768     }
1769     if (stn.toLowerCase().equals("true"))
1770     {
1771       return true;
1772     }
1773     return false;
1774   }
1775
1776   /**
1777    * bind a pdb file to a sequence in the given alignFrame.
1778    * 
1779    * @param alFrame
1780    *          - null or specific alignFrame. This specifies the dataset that
1781    *          will be searched for a seuqence called sequenceId
1782    * @param sequenceId
1783    *          - sequenceId within the dataset.
1784    * @param pdbEntryString
1785    *          - the short name for the PDB file
1786    * @param pdbFile
1787    *          - pdb file - either a URL or a valid PDB file.
1788    * @return true if binding was as success TODO: consider making an exception
1789    *         structure for indicating when PDB parsing or seqeunceId location
1790    *         fails.
1791    */
1792   public boolean addPdbFile(AlignFrame alFrame, String sequenceId,
1793           String pdbEntryString, String pdbFile)
1794   {
1795     return alFrame.addPdbFile(sequenceId, pdbEntryString, pdbFile);
1796   }
1797
1798   protected void setAlignPdbStructures(boolean alignPdbStructures)
1799   {
1800     this.alignPdbStructures = alignPdbStructures;
1801   }
1802
1803   public boolean isAlignPdbStructures()
1804   {
1805     return alignPdbStructures;
1806   }
1807
1808   /**
1809    * get all components associated with the applet of the given type
1810    * 
1811    * @param class1
1812    * @return
1813    */
1814   public Vector getAppletWindow(Class class1)
1815   {
1816     Vector wnds = new Vector();
1817     Component[] cmp = getComponents();
1818     if (cmp != null)
1819     {
1820       for (int i = 0; i < cmp.length; i++)
1821       {
1822         if (class1.isAssignableFrom(cmp[i].getClass()))
1823         {
1824           wnds.addElement(cmp);
1825         }
1826       }
1827     }
1828     return wnds;
1829   }
1830
1831   /**
1832    * bind structures in a viewer to any matching sequences in an alignFrame (use
1833    * sequenceIds to limit scope of search to specific sequences)
1834    * 
1835    * @param alFrame
1836    * @param viewer
1837    * @param sequenceIds
1838    * @return TODO: consider making an exception structure for indicating when
1839    *         binding fails public SequenceStructureBinding
1840    *         addStructureViewInstance( AlignFrame alFrame, Object viewer, String
1841    *         sequenceIds) {
1842    * 
1843    *         if (sequenceIds != null && sequenceIds.length() > 0) { return
1844    *         alFrame.addStructureViewInstance(viewer,
1845    *         separatorListToArray(sequenceIds)); } else { return
1846    *         alFrame.addStructureViewInstance(viewer, null); } // return null; }
1847    */
1848 }