efficiency, and ensure mouseover messages originating from a source are not broadcast...
[jalview.git] / src / jalview / structure / StructureSelectionManager.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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.structure;
19
20 import java.io.*;
21 import java.util.*;
22
23 import MCview.*;
24 import jalview.analysis.*;
25 import jalview.api.AlignmentViewPanel;
26 import jalview.api.StructureSelectionManagerProvider;
27 import jalview.datamodel.*;
28
29 public class StructureSelectionManager
30 {
31   static IdentityHashMap<StructureSelectionManagerProvider,StructureSelectionManager> instances;
32
33   StructureMapping[] mappings;
34
35   /**
36    * debug function - write all mappings to stdout
37    */
38   public void reportMapping() {\r
39     if (mappings==null)\r
40     {\r
41       System.err.println("reportMapping: No PDB/Sequence mappings.");\r
42     }else{\r
43       System.err.println("reportMapping: There are "+mappings.length+" mappings.");\r
44       for (int m=0;m<mappings.length;m++)\r
45       {\r
46         System.err.println("mapping "+m+" : "+mappings[m].pdbfile);\r
47       }\r
48     }\r
49   }\r
50   Hashtable mappingData = new Hashtable();
51
52   public static StructureSelectionManager getStructureSelectionManager(StructureSelectionManagerProvider context)
53   {
54     if (instances == null)
55     {
56       instances = new java.util.IdentityHashMap<StructureSelectionManagerProvider,StructureSelectionManager>();
57     }
58     StructureSelectionManager instance=instances.get(context);
59     if (instance==null)
60     {
61       instances.put(context, instance=new StructureSelectionManager());
62     }
63     return instance;
64   }
65
66   /**
67    * flag controlling whether SeqMappings are relayed from received sequence
68    * mouse over events to other sequences
69    */
70   boolean relaySeqMappings = true;
71
72   /**
73    * Enable or disable relay of seqMapping events to other sequences. You might
74    * want to do this if there are many sequence mappings and the host computer
75    * is slow
76    * 
77    * @param relay
78    */
79   public void setRelaySeqMappings(boolean relay)
80   {
81     relaySeqMappings = relay;
82   }
83
84   /**
85    * get the state of the relay seqMappings flag.
86    * 
87    * @return true if sequence mouse overs are being relayed to other mapped
88    *         sequences
89    */
90   public boolean isRelaySeqMappingsEnabled()
91   {
92     return relaySeqMappings;
93   }
94
95   Vector listeners = new Vector();
96
97   /**
98    * register a listener for alignment sequence mouseover events
99    * @param svl
100    */
101   public void addStructureViewerListener(Object svl)
102   {
103     if (!listeners.contains(svl))
104     {
105       listeners.addElement(svl);
106     }
107   }
108
109   public String alreadyMappedToFile(String pdbid)
110   {
111     if (mappings != null)
112     {
113       for (int i = 0; i < mappings.length; i++)
114       {
115         if (mappings[i].getPdbId().equals(pdbid))
116         {
117           return mappings[i].pdbfile;
118         }
119       }
120     }
121     return null;
122   }
123
124   /**
125    * create sequence structure mappings between each sequence and the given
126    * pdbFile (retrieved via the given protocol).
127    * 
128    * @param sequence
129    *          - one or more sequences to be mapped to pdbFile
130    * @param targetChains
131    *          - optional chain specification for mapping each sequence to pdb
132    *          (may be nill, individual elements may be nill)
133    * @param pdbFile
134    *          - structure data resource
135    * @param protocol
136    *          - how to resolve data from resource
137    * @return null or the structure data parsed as a pdb file
138    */
139   synchronized public MCview.PDBfile setMapping(SequenceI[] sequence,
140           String[] targetChains, String pdbFile, String protocol)
141   {
142     /*
143      * There will be better ways of doing this in the future, for now we'll use
144      * the tried and tested MCview pdb mapping
145      */
146     MCview.PDBfile pdb = null;
147     try
148     {
149       pdb = new MCview.PDBfile(pdbFile, protocol);
150     } catch (Exception ex)
151     {
152       ex.printStackTrace();
153       return null;
154     }
155     
156     String targetChain;
157     for (int s = 0; s < sequence.length; s++)
158     {
159       boolean infChain = true;
160       if (targetChains != null && targetChains[s] != null)
161       {
162         infChain = false;
163         targetChain = targetChains[s];
164       }
165       else if (sequence[s].getName().indexOf("|") > -1)
166       {
167         targetChain = sequence[s].getName().substring(
168                 sequence[s].getName().lastIndexOf("|") + 1);
169         if (targetChain.length() > 1)
170         {
171           if (targetChain.trim().length() == 0)
172           {
173             targetChain = " ";
174           }
175           else
176           {
177             // not a valid chain identifier
178             targetChain = "";
179           }
180         }
181       }
182       else
183         targetChain = "";
184
185       int max = -10;
186       AlignSeq maxAlignseq = null;
187       String maxChainId = " ";
188       PDBChain maxChain = null;
189       boolean first = true;
190       for (int i = 0; i < pdb.chains.size(); i++)
191       {
192         PDBChain chain = ((PDBChain) pdb.chains.elementAt(i));
193         if (targetChain.length() > 0 && !targetChain.equals(chain.id) && !infChain)
194         {
195           continue; // don't try to map chains don't match.
196         }
197         // TODO: correctly determine sequence type for mixed na/peptide
198         // structures
199         AlignSeq as = new AlignSeq(sequence[s],
200                 ((PDBChain) pdb.chains.elementAt(i)).sequence,
201                 ((PDBChain) pdb.chains.elementAt(i)).isNa ? AlignSeq.DNA
202                         : AlignSeq.PEP);
203         as.calcScoreMatrix();
204         as.traceAlignment();
205
206         if (first || as.maxscore > max
207                 || (as.maxscore == max && chain.id.equals(targetChain)))
208         {
209           first = false;
210           maxChain = chain;
211           max = as.maxscore;
212           maxAlignseq = as;
213           maxChainId = chain.id;
214         }
215       }
216       if (maxChain == null)
217       {
218         continue;
219       }
220       final StringBuffer mappingDetails = new StringBuffer();
221       mappingDetails.append("\n\nPDB Sequence is :\nSequence = "
222               + maxChain.sequence.getSequenceAsString());
223       mappingDetails.append("\nNo of residues = "
224               + maxChain.residues.size() + "\n\n");
225       PrintStream ps = new PrintStream(System.out)
226       {
227         public void print(String x)
228         {
229           mappingDetails.append(x);
230         }
231
232         public void println()
233         {
234           mappingDetails.append("\n");
235         }
236       };
237
238       maxAlignseq.printAlignment(ps);
239
240       mappingDetails.append("\nPDB start/end " + maxAlignseq.seq2start
241               + " " + maxAlignseq.seq2end);
242       mappingDetails.append("\nSEQ start/end "
243               + (maxAlignseq.seq1start + sequence[s].getStart() - 1) + " "
244               + (maxAlignseq.seq1end + sequence[s].getEnd() - 1));
245
246       maxChain.makeExactMapping(maxAlignseq, sequence[s]);
247
248       maxChain.transferRESNUMFeatures(sequence[s], null);
249
250       // allocate enough slots to store the mapping from positions in
251       // sequence[s] to the associated chain
252       int[][] mapping = new int[sequence[s].findPosition(sequence[s].getLength()) + 2][2];
253       int resNum = -10000;
254       int index = 0;
255
256       do
257       {
258         Atom tmp = (Atom) maxChain.atoms.elementAt(index);
259         if (resNum != tmp.resNumber && tmp.alignmentMapping != -1)
260         {
261           resNum = tmp.resNumber;
262           mapping[tmp.alignmentMapping + 1][0] = tmp.resNumber;
263           mapping[tmp.alignmentMapping + 1][1] = tmp.atomIndex;
264         }
265
266         index++;
267       } while (index < maxChain.atoms.size());
268
269       if (mappings == null)
270       {
271         mappings = new StructureMapping[1];
272       }
273       else
274       {
275         StructureMapping[] tmp = new StructureMapping[mappings.length + 1];
276         System.arraycopy(mappings, 0, tmp, 0, mappings.length);
277         mappings = tmp;
278       }
279
280       if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
281         pdbFile = "INLINE" + pdb.id;
282
283       mappings[mappings.length - 1] = new StructureMapping(sequence[s],
284               pdbFile, pdb.id, maxChainId, mapping,
285               mappingDetails.toString());
286       maxChain.transferResidueAnnotation(mappings[mappings.length - 1]);
287     }
288     // ///////
289
290     return pdb;
291   }
292
293   public void removeStructureViewerListener(Object svl, String[] pdbfiles)
294   {
295     listeners.removeElement(svl);
296     if (svl instanceof SequenceListener)
297     {
298       for (int i=0;i<listeners.size();i++)
299       {
300         if (listeners.elementAt(i) instanceof StructureListener)
301         {
302           ((StructureListener)listeners.elementAt(i)).releaseReferences(svl);
303         }
304       }
305     }
306       
307     if (pdbfiles == null)
308     {
309       return;
310     }
311     boolean removeMapping = true;
312     String[] handlepdbs;
313     Vector pdbs = new Vector();
314     for (int i = 0; i < pdbfiles.length; pdbs.addElement(pdbfiles[i++]))
315       ;
316     StructureListener sl;
317     for (int i = 0; i < listeners.size(); i++)
318     {
319       if (listeners.elementAt(i) instanceof StructureListener)
320       {
321         sl = (StructureListener) listeners.elementAt(i);
322         handlepdbs = sl.getPdbFile();
323         for (int j = 0; j < handlepdbs.length; j++)
324         {
325           if (pdbs.contains(handlepdbs[j]))
326           {
327             pdbs.removeElement(handlepdbs[j]);
328           }
329         }
330
331       }
332     }
333
334     if (pdbs.size() > 0 && mappings != null)
335     {
336       Vector tmp = new Vector();
337       for (int i = 0; i < mappings.length; i++)
338       {
339         if (!pdbs.contains(mappings[i].pdbfile))
340         {
341           tmp.addElement(mappings[i]);
342         }
343       }
344
345       mappings = new StructureMapping[tmp.size()];
346       tmp.copyInto(mappings);
347     }
348   }
349
350   public void mouseOverStructure(int pdbResNum, String chain, String pdbfile)
351   {
352     if (listeners==null)
353     {
354       // old or prematurely sent event
355       return;
356     }
357     boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
358     SearchResults results = null;
359     SequenceI lastseq = null;
360     int lastipos = -1, indexpos;
361     for (int i = 0; i < listeners.size(); i++)
362     {
363       if (listeners.elementAt(i) instanceof SequenceListener)
364       {
365         if (results == null)
366         {
367           results = new SearchResults();
368         }
369         if (mappings != null)
370         {
371           for (int j = 0; j < mappings.length; j++)
372           {
373             if (mappings[j].pdbfile.equals(pdbfile)
374                     && mappings[j].pdbchain.equals(chain))
375             {
376               indexpos = mappings[j].getSeqPos(pdbResNum);
377               if (lastipos != indexpos && lastseq != mappings[j].sequence)
378               {
379                 results.addResult(mappings[j].sequence, indexpos, indexpos);
380                 lastipos = indexpos;
381                 lastseq = mappings[j].sequence;
382                 // construct highlighted sequence list
383                 if (seqmappings != null)
384                 {
385
386                   Enumeration e = seqmappings.elements();
387                   while (e.hasMoreElements())
388
389                   {
390                     ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
391                             mappings[j].sequence, indexpos, results);
392                   }
393                 }
394               }
395
396             }
397           }
398         }
399       }
400     }
401     if (results!=null)
402     {
403       for (int i = 0; i < listeners.size(); i++)
404       {
405         Object li = listeners.elementAt(i);
406         if (li instanceof SequenceListener)
407           ((SequenceListener) li).highlightSequence(results);
408       }
409     }
410   }
411
412   Vector seqmappings = null; // should be a simpler list of mapped seuqence
413
414   /**
415    * highlight regions associated with a position (indexpos) in seq
416    * 
417    * @param seq
418    *          the sequeence that the mouse over occured on
419    * @param indexpos
420    *          the absolute position being mouseovered in seq (0 to seq.length())
421    * @param index
422    *          the sequence position (if -1, seq.findPosition is called to
423    *          resolve the residue number)
424    */
425   public void mouseOverSequence(SequenceI seq, int indexpos, int index,
426           VamsasSource source)
427   {
428     boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
429     SearchResults results = null;
430     if (index == -1)
431       index = seq.findPosition(indexpos);
432     StructureListener sl;
433     int atomNo = 0;
434     for (int i = 0; i < listeners.size(); i++)
435     {
436       Object listener=listeners.elementAt(i);
437       if (listener==source)
438       {
439         continue;
440       }
441       if (listener instanceof StructureListener)
442       {
443         sl = (StructureListener) listener;
444         if (mappings == null)
445         {
446           continue;
447         }
448         for (int j = 0; j < mappings.length; j++)
449         {
450           if (mappings[j].sequence == seq
451                   || mappings[j].sequence == seq.getDatasetSequence())
452           {
453             atomNo = mappings[j].getAtomNum(index);
454
455             if (atomNo > 0)
456             {
457               sl.highlightAtom(atomNo, mappings[j].getPDBResNum(index),
458                       mappings[j].pdbchain, mappings[j].pdbfile);
459             }
460           }
461         }
462       }
463       else
464       {
465         if (relaySeqMappings && hasSequenceListeners
466                 && listener instanceof SequenceListener)
467         {
468           // DEBUG
469           // System.err.println("relay Seq " + seq.getDisplayId(false) + " " +
470           // index);
471
472           if (results == null)
473           {
474             results = new SearchResults();
475             if (index >= seq.getStart() && index <= seq.getEnd())
476             {
477               // construct highlighted sequence list
478
479               if (seqmappings != null)
480               {
481                 Enumeration e = seqmappings.elements();
482                 while (e.hasMoreElements())
483
484                 {
485                   ((AlignedCodonFrame) e.nextElement()).markMappedRegion(
486                           seq, index, results);
487                 }
488               }
489               // hasSequenceListeners = results.getSize() > 0;
490               if (handlingVamsasMo)
491               {
492                 // maybe have to resolve seq to a dataset seqeunce...
493                 // add in additional direct sequence and/or dataset sequence
494                 // highlighting
495                 results.addResult(seq, index, index);
496               }
497             }
498           }
499           if (hasSequenceListeners)
500           {
501             ((SequenceListener) listener)
502                     .highlightSequence(results);
503           }
504         }
505         else if (listener instanceof VamsasListener
506                 && !handlingVamsasMo)
507         {
508           // DEBUG
509           // System.err.println("Vamsas from Seq " + seq.getDisplayId(false) + "
510           // " +
511           // index);
512           // pass the mouse over and absolute position onto the
513           // VamsasListener(s)
514           ((VamsasListener) listener).mouseOver(seq,
515                   indexpos, source);
516         }
517         else if(listener instanceof SecondaryStructureListener){
518                 ((SecondaryStructureListener) listener).mouseOverSequence(seq,indexpos);
519         }
520       }
521     }
522   }
523
524   /**
525    * true if a mouse over event from an external (ie Vamsas) source is being
526    * handled
527    */
528   boolean handlingVamsasMo = false;
529
530   long lastmsg = 0;
531
532   /**
533    * as mouseOverSequence but only route event to SequenceListeners
534    * 
535    * @param sequenceI
536    * @param position
537    *          in an alignment sequence
538    */
539   public void mouseOverVamsasSequence(SequenceI sequenceI, int position,
540           VamsasSource source)
541   {
542     handlingVamsasMo = true;
543     long msg = sequenceI.hashCode() * (1 + position);
544     if (lastmsg != msg)
545     {
546       lastmsg = msg;
547       mouseOverSequence(sequenceI, position, -1, source);
548     }
549     handlingVamsasMo = false;
550   }
551
552   public Annotation[] colourSequenceFromStructure(SequenceI seq,
553           String pdbid)
554   {
555     return null;
556     // THIS WILL NOT BE AVAILABLE IN JALVIEW 2.3,
557     // UNTIL THE COLOUR BY ANNOTATION IS REWORKED
558     /*
559      * Annotation [] annotations = new Annotation[seq.getLength()];
560      * 
561      * StructureListener sl; int atomNo = 0; for (int i = 0; i <
562      * listeners.size(); i++) { if (listeners.elementAt(i) instanceof
563      * StructureListener) { sl = (StructureListener) listeners.elementAt(i);
564      * 
565      * for (int j = 0; j < mappings.length; j++) {
566      * 
567      * if (mappings[j].sequence == seq && mappings[j].getPdbId().equals(pdbid)
568      * && mappings[j].pdbfile.equals(sl.getPdbFile())) {
569      * System.out.println(pdbid+" "+mappings[j].getPdbId() +"
570      * "+mappings[j].pdbfile);
571      * 
572      * java.awt.Color col; for(int index=0; index<seq.getLength(); index++) {
573      * if(jalview.util.Comparison.isGap(seq.getCharAt(index))) continue;
574      * 
575      * atomNo = mappings[j].getAtomNum(seq.findPosition(index)); col =
576      * java.awt.Color.white; if (atomNo > 0) { col = sl.getColour(atomNo,
577      * mappings[j].getPDBResNum(index), mappings[j].pdbchain,
578      * mappings[j].pdbfile); }
579      * 
580      * annotations[index] = new Annotation("X",null,' ',0,col); } return
581      * annotations; } } } }
582      * 
583      * return annotations;
584      */
585   }
586
587   public void structureSelectionChanged()
588   {
589   }
590
591   public void sequenceSelectionChanged()
592   {
593   }
594
595   public void sequenceColoursChanged(Object source)
596   {
597     StructureListener sl;
598     for (int i = 0; i < listeners.size(); i++)
599     {
600       if (listeners.elementAt(i) instanceof StructureListener)
601       {
602         sl = (StructureListener) listeners.elementAt(i);
603         sl.updateColours(source);
604       }
605     }
606   }
607
608   public StructureMapping[] getMapping(String pdbfile)
609   {
610     Vector tmp = new Vector();
611     if (mappings != null)
612     {
613       for (int i = 0; i < mappings.length; i++)
614       {
615         if (mappings[i].pdbfile.equals(pdbfile))
616         {
617           tmp.addElement(mappings[i]);
618         }
619       }
620     }
621     StructureMapping[] ret = new StructureMapping[tmp.size()];
622     for (int i = 0; i < tmp.size(); i++)
623     {
624       ret[i] = (StructureMapping) tmp.elementAt(i);
625     }
626
627     return ret;
628   }
629
630   public String printMapping(String pdbfile)
631   {
632     StringBuffer sb = new StringBuffer();
633     for (int i = 0; i < mappings.length; i++)
634     {
635       if (mappings[i].pdbfile.equals(pdbfile))
636       {
637         sb.append(mappings[i].mappingDetails);
638       }
639     }
640
641     return sb.toString();
642   }
643
644   private int[] seqmappingrefs = null; // refcount for seqmappings elements
645
646   private synchronized void modifySeqMappingList(boolean add,
647           AlignedCodonFrame[] codonFrames)
648   {
649     if (!add && (seqmappings == null || seqmappings.size() == 0))
650       return;
651     if (seqmappings == null)
652       seqmappings = new Vector();
653     if (codonFrames != null && codonFrames.length > 0)
654     {
655       for (int cf = 0; cf < codonFrames.length; cf++)
656       {
657         if (seqmappings.contains(codonFrames[cf]))
658         {
659           if (add)
660           {
661             seqmappingrefs[seqmappings.indexOf(codonFrames[cf])]++;
662           }
663           else
664           {
665             if (--seqmappingrefs[seqmappings.indexOf(codonFrames[cf])] <= 0)
666             {
667               int pos = seqmappings.indexOf(codonFrames[cf]);
668               int[] nr = new int[seqmappingrefs.length - 1];
669               if (pos > 0)
670               {
671                 System.arraycopy(seqmappingrefs, 0, nr, 0, pos);
672               }
673               if (pos < seqmappingrefs.length - 1)
674               {
675                 System.arraycopy(seqmappingrefs, pos + 1, nr, 0,
676                         seqmappingrefs.length - pos - 2);
677               }
678             }
679           }
680         }
681         else
682         {
683           if (add)
684           {
685             seqmappings.addElement(codonFrames[cf]);
686
687             int[] nsr = new int[(seqmappingrefs == null) ? 1
688                     : seqmappingrefs.length + 1];
689             if (seqmappingrefs != null && seqmappingrefs.length > 0)
690               System.arraycopy(seqmappingrefs, 0, nsr, 0,
691                       seqmappingrefs.length);
692             nsr[(seqmappingrefs == null) ? 0 : seqmappingrefs.length] = 1;
693             seqmappingrefs = nsr;
694           }
695         }
696       }
697     }
698   }
699
700   public void removeMappings(AlignedCodonFrame[] codonFrames)
701   {
702     modifySeqMappingList(false, codonFrames);
703   }
704
705   public void addMappings(AlignedCodonFrame[] codonFrames)
706   {
707     modifySeqMappingList(true, codonFrames);
708   }
709
710   Vector<SelectionListener> sel_listeners = new Vector<SelectionListener>();
711
712   public void addSelectionListener(SelectionListener selecter)
713   {
714     if (!sel_listeners.contains(selecter))
715     {
716       sel_listeners.addElement(selecter);
717     }
718   }
719
720   public void removeSelectionListener(SelectionListener toremove)
721   {
722     if (sel_listeners.contains(toremove))
723     {
724       sel_listeners.removeElement(toremove);
725     }
726   }
727
728   public synchronized void sendSelection(
729           jalview.datamodel.SequenceGroup selection,
730           jalview.datamodel.ColumnSelection colsel, SelectionSource source)
731   {
732     if (sel_listeners != null && sel_listeners.size() > 0)
733     {
734       Enumeration listeners = sel_listeners.elements();
735       while (listeners.hasMoreElements())
736       {
737         SelectionListener slis = ((SelectionListener) listeners
738                 .nextElement());
739         if (slis != source)
740         {
741           slis.selection(selection, colsel, source);
742         }
743         ;
744       }
745     }
746   }
747   
748   Vector<AlignmentViewPanelListener> view_listeners=new Vector<AlignmentViewPanelListener>();
749   public synchronized void sendViewPosition(jalview.api.AlignmentViewPanel source, int startRes,
750           int endRes, int startSeq, int endSeq)
751   {
752
753     if (view_listeners != null && view_listeners.size() > 0)
754     {
755       Enumeration<AlignmentViewPanelListener> listeners = view_listeners.elements();
756       while (listeners.hasMoreElements())
757       {
758         AlignmentViewPanelListener slis = listeners
759                 .nextElement();
760         if (slis != source)
761         {
762           slis.viewPosition(startRes, endRes, startSeq, endSeq, source);
763         }
764         ;
765       }
766     }
767   }
768   
769
770   public void finalize() throws Throwable {
771     if (listeners!=null) {
772       listeners.clear();
773       listeners=null;
774     }
775     if (mappingData!=null)
776     {
777       mappingData.clear();
778       mappingData=null;
779     }
780     if (sel_listeners!=null)
781     {
782       sel_listeners.clear();
783       sel_listeners=null;
784     }
785     if (view_listeners!=null)
786     {
787       view_listeners.clear();
788       view_listeners=null;
789     }
790     mappings=null;
791     seqmappingrefs=null;
792   }
793
794   /**
795    * release all references associated with this manager provider
796    * @param jalviewLite
797    */
798   public static void release(StructureSelectionManagerProvider jalviewLite)
799   {
800 //    synchronized (instances)
801     {
802       if (instances == null)
803       {
804         return;
805       }
806       StructureSelectionManager mnger = (instances.get(jalviewLite));
807       if (mnger != null)
808       {
809         instances.remove(jalviewLite);
810         try
811         {
812           mnger.finalize();
813         } catch (Throwable x)
814         {
815         }
816         ;
817       }
818     }
819   }
820
821 }