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
5 * This file is part of Jalview.
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.
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.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
20 import java.awt.Rectangle;
22 import java.lang.reflect.InvocationTargetException;
25 import java.util.Map.Entry;
26 import java.util.jar.*;
30 import org.exolab.castor.xml.*;
32 import uk.ac.vamsas.objects.utils.MapList;
33 import jalview.bin.Cache;
34 import jalview.datamodel.Alignment;
35 import jalview.datamodel.AlignmentAnnotation;
36 import jalview.datamodel.AlignmentI;
37 import jalview.datamodel.SequenceI;
38 import jalview.schemabinding.version2.*;
39 import jalview.schemes.*;
40 import jalview.structure.StructureSelectionManager;
41 import jalview.util.jarInputStreamProvider;
44 * Write out the current jalview desktop state as a Jalview XML stream.
46 * Note: the vamsas objects referred to here are primitive versions of the
47 * VAMSAS project schema elements - they are not the same and most likely never
51 * @version $Revision: 1.134 $
53 public class Jalview2XML
56 * create/return unique hash string for sq
59 * @return new or existing unique string for sq
61 String seqHash(SequenceI sq)
63 if (seqsToIds == null)
67 if (seqsToIds.containsKey(sq))
69 return (String) seqsToIds.get(sq);
73 // create sequential key
74 String key = "sq" + (seqsToIds.size() + 1);
75 key = makeHashCode(sq, key); // check we don't have an external reference
77 seqsToIds.put(sq, key);
86 if (seqRefIds != null)
90 if (seqsToIds != null)
100 warn("clearSeqRefs called when _cleartables was not set. Doing nothing.");
101 // seqRefIds = new Hashtable();
102 // seqsToIds = new IdentityHashMap();
108 if (seqsToIds == null)
110 seqsToIds = new IdentityHashMap();
112 if (seqRefIds == null)
114 seqRefIds = new Hashtable();
119 * SequenceI reference -> XML ID string in jalview XML. Populated as XML reps
120 * of sequence objects are created.
122 java.util.IdentityHashMap seqsToIds = null;
125 * jalview XML Sequence ID to jalview sequence object reference (both dataset
126 * and alignment sequences. Populated as XML reps of sequence objects are
129 java.util.Hashtable seqRefIds = null; // key->SequenceI resolution
131 Vector frefedSequence = null;
133 boolean raiseGUI = true; // whether errors are raised in dialog boxes or not
139 public Jalview2XML(boolean raiseGUI)
141 this.raiseGUI = raiseGUI;
144 public void resolveFrefedSequences()
146 if (frefedSequence.size() > 0)
148 int r = 0, rSize = frefedSequence.size();
151 Object[] ref = (Object[]) frefedSequence.elementAt(r);
154 String sref = (String) ref[0];
155 if (seqRefIds.containsKey(sref))
157 if (ref[1] instanceof jalview.datamodel.Mapping)
159 SequenceI seq = (SequenceI) seqRefIds.get(sref);
160 while (seq.getDatasetSequence() != null)
162 seq = seq.getDatasetSequence();
164 ((jalview.datamodel.Mapping) ref[1]).setTo(seq);
168 if (ref[1] instanceof jalview.datamodel.AlignedCodonFrame)
170 SequenceI seq = (SequenceI) seqRefIds.get(sref);
171 while (seq.getDatasetSequence() != null)
173 seq = seq.getDatasetSequence();
176 && ref[2] instanceof jalview.datamodel.Mapping)
178 jalview.datamodel.Mapping mp = (jalview.datamodel.Mapping) ref[2];
179 ((jalview.datamodel.AlignedCodonFrame) ref[1]).addMap(
180 seq, mp.getTo(), mp.getMap());
185 .println("IMPLEMENTATION ERROR: Unimplemented forward sequence references for AlcodonFrames involving "
186 + ref[2].getClass() + " type objects.");
192 .println("IMPLEMENTATION ERROR: Unimplemented forward sequence references for "
193 + ref[1].getClass() + " type objects.");
196 frefedSequence.remove(r);
202 .println("IMPLEMENTATION WARNING: Unresolved forward reference for hash string "
204 + " with objecttype "
205 + ref[1].getClass());
212 frefedSequence.remove(r);
220 * This maintains a list of viewports, the key being the seqSetId. Important
221 * to set historyItem and redoList for multiple views
223 Hashtable viewportsAdded;
225 Hashtable annotationIds = new Hashtable();
227 String uniqueSetSuffix = "";
230 * List of pdbfiles added to Jar
232 Vector pdbfiles = null;
234 // SAVES SEVERAL ALIGNMENT WINDOWS TO SAME JARFILE
235 public void SaveState(File statefile)
239 FileOutputStream fos = new FileOutputStream(statefile);
240 JarOutputStream jout = new JarOutputStream(fos);
243 } catch (Exception e)
245 // TODO: inform user of the problem - they need to know if their data was
247 if (errorMessage == null)
249 errorMessage = "Couldn't write Jalview Archive to output file '"
250 + statefile + "' - See console error log for details";
254 errorMessage += "(output file was '" + statefile + "')";
262 * Writes a jalview project archive to the given Jar output stream.
266 public void SaveState(JarOutputStream jout)
268 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
278 // NOTE UTF-8 MUST BE USED FOR WRITING UNICODE CHARS
279 // //////////////////////////////////////////////////
280 // NOTE ALSO new PrintWriter must be used for each new JarEntry
281 PrintWriter out = null;
283 Vector shortNames = new Vector();
286 for (int i = frames.length - 1; i > -1; i--)
288 if (frames[i] instanceof AlignFrame)
290 AlignFrame af = (AlignFrame) frames[i];
293 && skipList.containsKey(af.getViewport()
294 .getSequenceSetId()))
299 String shortName = af.getTitle();
301 if (shortName.indexOf(File.separatorChar) > -1)
303 shortName = shortName.substring(shortName
304 .lastIndexOf(File.separatorChar) + 1);
309 while (shortNames.contains(shortName))
311 if (shortName.endsWith("_" + (count - 1)))
313 shortName = shortName
314 .substring(0, shortName.lastIndexOf("_"));
317 shortName = shortName.concat("_" + count);
321 shortNames.addElement(shortName);
323 if (!shortName.endsWith(".xml"))
325 shortName = shortName + ".xml";
328 int ap, apSize = af.alignPanels.size();
329 for (ap = 0; ap < apSize; ap++)
331 AlignmentPanel apanel = (AlignmentPanel) af.alignPanels
333 String fileName = apSize == 1 ? shortName : ap + shortName;
334 if (!fileName.endsWith(".xml"))
336 fileName = fileName + ".xml";
339 SaveState(apanel, fileName, jout);
346 } catch (Exception foo)
351 } catch (Exception ex)
353 // TODO: inform user of the problem - they need to know if their data was
355 if (errorMessage == null)
357 errorMessage = "Couldn't write Jalview Archive - see error output for details";
359 ex.printStackTrace();
363 // USE THIS METHOD TO SAVE A SINGLE ALIGNMENT WINDOW
364 public boolean SaveAlignment(AlignFrame af, String jarFile,
369 int ap, apSize = af.alignPanels.size();
370 FileOutputStream fos = new FileOutputStream(jarFile);
371 JarOutputStream jout = new JarOutputStream(fos);
372 for (ap = 0; ap < apSize; ap++)
374 AlignmentPanel apanel = (AlignmentPanel) af.alignPanels
376 String jfileName = apSize == 1 ? fileName : fileName + ap;
377 if (!jfileName.endsWith(".xml"))
379 jfileName = jfileName + ".xml";
381 SaveState(apanel, jfileName, jout);
387 } catch (Exception foo)
393 } catch (Exception ex)
395 errorMessage = "Couldn't Write alignment view to Jalview Archive - see error output for details";
396 ex.printStackTrace();
402 * create a JalviewModel from an algnment view and marshall it to a
406 * panel to create jalview model for
408 * name of alignment panel written to output stream
414 public JalviewModel SaveState(AlignmentPanel ap, String fileName,
415 JarOutputStream jout)
418 Vector jmolViewIds = new Vector(); //
419 Vector userColours = new Vector();
421 AlignViewport av = ap.av;
423 JalviewModel object = new JalviewModel();
424 object.setVamsasModel(new jalview.schemabinding.version2.VamsasModel());
426 object.setCreationDate(new java.util.Date(System.currentTimeMillis()));
427 object.setVersion(jalview.bin.Cache.getProperty("VERSION"));
429 jalview.datamodel.AlignmentI jal = av.getAlignment();
431 if (av.hasHiddenRows())
433 jal = jal.getHiddenSequences().getFullAlignment();
436 SequenceSet vamsasSet = new SequenceSet();
438 JalviewModelSequence jms = new JalviewModelSequence();
440 vamsasSet.setGapChar(jal.getGapCharacter() + "");
442 if (jal.getDataset() != null)
444 // dataset id is the dataset's hashcode
445 vamsasSet.setDatasetId(getDatasetIdRef(jal.getDataset()));
447 if (jal.getProperties() != null)
449 Enumeration en = jal.getProperties().keys();
450 while (en.hasMoreElements())
452 String key = en.nextElement().toString();
453 SequenceSetProperties ssp = new SequenceSetProperties();
455 ssp.setValue(jal.getProperties().get(key).toString());
456 vamsasSet.addSequenceSetProperties(ssp);
464 jalview.datamodel.SequenceI jds;
465 for (int i = 0; i < jal.getHeight(); i++)
467 jds = jal.getSequenceAt(i);
470 if (seqRefIds.get(id) != null)
472 // This happens for two reasons: 1. multiple views are being serialised.
473 // 2. the hashCode has collided with another sequence's code. This DOES
474 // HAPPEN! (PF00072.15.stk does this)
475 // JBPNote: Uncomment to debug writing out of files that do not read
476 // back in due to ArrayOutOfBoundExceptions.
477 // System.err.println("vamsasSeq backref: "+id+"");
478 // System.err.println(jds.getName()+"
479 // "+jds.getStart()+"-"+jds.getEnd()+" "+jds.getSequenceAsString());
480 // System.err.println("Hashcode: "+seqHash(jds));
481 // SequenceI rsq = (SequenceI) seqRefIds.get(id + "");
482 // System.err.println(rsq.getName()+"
483 // "+rsq.getStart()+"-"+rsq.getEnd()+" "+rsq.getSequenceAsString());
484 // System.err.println("Hashcode: "+seqHash(rsq));
488 vamsasSeq = createVamsasSequence(id, jds);
489 vamsasSet.addSequence(vamsasSeq);
490 seqRefIds.put(id, jds);
494 jseq.setStart(jds.getStart());
495 jseq.setEnd(jds.getEnd());
496 jseq.setColour(av.getSequenceColour(jds).getRGB());
498 jseq.setId(id); // jseq id should be a string not a number
500 if (av.hasHiddenRows())
502 jseq.setHidden(av.getAlignment().getHiddenSequences().isHidden(jds));
504 if (av.isHiddenRepSequence(jal.getSequenceAt(i)))
506 jalview.datamodel.SequenceI[] reps = av.getRepresentedSequences(jal.getSequenceAt(i)).getSequencesInOrder(jal);
508 for (int h = 0; h < reps.length; h++)
510 if (reps[h] != jal.getSequenceAt(i))
512 jseq.addHiddenSequences(jal.findIndex(reps[h]));
518 if (jds.getDatasetSequence().getSequenceFeatures() != null)
520 jalview.datamodel.SequenceFeature[] sf = jds.getDatasetSequence()
521 .getSequenceFeatures();
523 while (index < sf.length)
525 Features features = new Features();
527 features.setBegin(sf[index].getBegin());
528 features.setEnd(sf[index].getEnd());
529 features.setDescription(sf[index].getDescription());
530 features.setType(sf[index].getType());
531 features.setFeatureGroup(sf[index].getFeatureGroup());
532 features.setScore(sf[index].getScore());
533 if (sf[index].links != null)
535 for (int l = 0; l < sf[index].links.size(); l++)
537 OtherData keyValue = new OtherData();
538 keyValue.setKey("LINK_" + l);
539 keyValue.setValue(sf[index].links.elementAt(l).toString());
540 features.addOtherData(keyValue);
543 if (sf[index].otherDetails != null)
546 Enumeration keys = sf[index].otherDetails.keys();
547 while (keys.hasMoreElements())
549 key = keys.nextElement().toString();
550 OtherData keyValue = new OtherData();
551 keyValue.setKey(key);
552 keyValue.setValue(sf[index].otherDetails.get(key).toString());
553 features.addOtherData(keyValue);
557 jseq.addFeatures(features);
562 if (jds.getDatasetSequence().getPDBId() != null)
564 Enumeration en = jds.getDatasetSequence().getPDBId().elements();
565 while (en.hasMoreElements())
567 Pdbids pdb = new Pdbids();
568 jalview.datamodel.PDBEntry entry = (jalview.datamodel.PDBEntry) en
571 pdb.setId(entry.getId());
572 pdb.setType(entry.getType());
575 // This must have been loaded, is it still visible?
576 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
577 String matchedFile = null;
578 for (int f = frames.length - 1; f > -1; f--)
580 if (frames[f] instanceof AppJmol)
582 jmol = (AppJmol) frames[f];
583 for (int peid = 0; peid < jmol.jmb.pdbentry.length; peid++)
585 if (!jmol.jmb.pdbentry[peid].getId().equals(entry.getId())
586 && !(entry.getId().length() > 4 && entry
590 jmol.jmb.pdbentry[peid].getId()
593 if (matchedFile == null)
595 matchedFile = jmol.jmb.pdbentry[peid].getFile();
597 else if (!matchedFile.equals(jmol.jmb.pdbentry[peid]
601 .warn("Probably lost some PDB-Sequence mappings for this structure file (which apparently has same PDB Entry code): "
602 + jmol.jmb.pdbentry[peid].getFile());
606 // can get at it if the ID
607 // match is ambiguous (e.g.
609 String statestring = jmol.jmb.viewer.getStateInfo();
611 for (int smap = 0; smap < jmol.jmb.sequence[peid].length; smap++)
613 if (jal.findIndex(jmol.jmb.sequence[peid][smap]) > -1)
615 StructureState state = new StructureState();
616 state.setVisible(true);
617 state.setXpos(jmol.getX());
618 state.setYpos(jmol.getY());
619 state.setWidth(jmol.getWidth());
620 state.setHeight(jmol.getHeight());
621 state.setViewId(jmol.getViewId());
622 state.setAlignwithAlignPanel(jmol.isUsedforaligment(ap));
623 state.setColourwithAlignPanel(jmol
624 .isUsedforcolourby(ap));
625 state.setColourByJmol(jmol.isColouredByJmol());
626 if (!jmolViewIds.contains(state.getViewId()))
628 // Make sure we only store a Jmol state once in each XML
630 jmolViewIds.addElement(state.getViewId());
631 state.setContent(statestring.replaceAll("\n", ""));
635 state.setContent("# duplicate state");
637 pdb.addStructureState(state);
644 if (matchedFile != null || entry.getFile() != null)
646 if (entry.getFile() != null)
649 matchedFile = entry.getFile();
651 pdb.setFile(matchedFile); // entry.getFile());
652 if (pdbfiles == null)
654 pdbfiles = new Vector();
657 if (!pdbfiles.contains(entry.getId()))
659 pdbfiles.addElement(entry.getId());
662 File file = new File(matchedFile);
663 if (file.exists() && jout != null)
665 byte[] data = new byte[(int) file.length()];
666 jout.putNextEntry(new JarEntry(entry.getId()));
667 DataInputStream dis = new DataInputStream(
668 new FileInputStream(file));
671 DataOutputStream dout = new DataOutputStream(jout);
672 dout.write(data, 0, data.length);
676 } catch (Exception ex)
678 ex.printStackTrace();
684 if (entry.getProperty() != null)
686 PdbentryItem item = new PdbentryItem();
687 Hashtable properties = entry.getProperty();
688 Enumeration en2 = properties.keys();
689 while (en2.hasMoreElements())
691 Property prop = new Property();
692 String key = en2.nextElement().toString();
694 prop.setValue(properties.get(key).toString());
695 item.addProperty(prop);
697 pdb.addPdbentryItem(item);
707 if (av.hasHiddenRows())
709 jal = av.getAlignment();
712 if (jal.getCodonFrames() != null && jal.getCodonFrames().length > 0)
714 jalview.datamodel.AlignedCodonFrame[] jac = jal.getCodonFrames();
715 for (int i = 0; i < jac.length; i++)
717 AlcodonFrame alc = new AlcodonFrame();
718 vamsasSet.addAlcodonFrame(alc);
719 for (int p = 0; p < jac[i].aaWidth; p++)
721 Alcodon cmap = new Alcodon();
722 if (jac[i].codons[p] != null)
724 // Null codons indicate a gapped column in the translated peptide
726 cmap.setPos1(jac[i].codons[p][0]);
727 cmap.setPos2(jac[i].codons[p][1]);
728 cmap.setPos3(jac[i].codons[p][2]);
730 alc.addAlcodon(cmap);
732 if (jac[i].getProtMappings() != null
733 && jac[i].getProtMappings().length > 0)
735 SequenceI[] dnas = jac[i].getdnaSeqs();
736 jalview.datamodel.Mapping[] pmaps = jac[i].getProtMappings();
737 for (int m = 0; m < pmaps.length; m++)
739 AlcodMap alcmap = new AlcodMap();
740 alcmap.setDnasq(seqHash(dnas[m]));
741 alcmap.setMapping(createVamsasMapping(pmaps[m], dnas[m], null,
743 alc.addAlcodMap(alcmap);
750 // /////////////////////////////////
751 if (av.currentTree != null)
753 // FIND ANY ASSOCIATED TREES
754 // NOT IMPLEMENTED FOR HEADLESS STATE AT PRESENT
755 if (Desktop.desktop != null)
757 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
759 for (int t = 0; t < frames.length; t++)
761 if (frames[t] instanceof TreePanel)
763 TreePanel tp = (TreePanel) frames[t];
765 if (tp.treeCanvas.av.getAlignment() == jal)
767 Tree tree = new Tree();
768 tree.setTitle(tp.getTitle());
769 tree.setCurrentTree((av.currentTree == tp.getTree()));
770 tree.setNewick(tp.getTree().toString());
771 tree.setThreshold(tp.treeCanvas.threshold);
773 tree.setFitToWindow(tp.fitToWindow.getState());
774 tree.setFontName(tp.getTreeFont().getName());
775 tree.setFontSize(tp.getTreeFont().getSize());
776 tree.setFontStyle(tp.getTreeFont().getStyle());
777 tree.setMarkUnlinked(tp.placeholdersMenu.getState());
779 tree.setShowBootstrap(tp.bootstrapMenu.getState());
780 tree.setShowDistances(tp.distanceMenu.getState());
782 tree.setHeight(tp.getHeight());
783 tree.setWidth(tp.getWidth());
784 tree.setXpos(tp.getX());
785 tree.setYpos(tp.getY());
786 tree.setId(makeHashCode(tp, null));
796 * store forward refs from an annotationRow to any groups
798 IdentityHashMap groupRefs = new IdentityHashMap();
799 if (jal.getAlignmentAnnotation() != null)
801 jalview.datamodel.AlignmentAnnotation[] aa = jal
802 .getAlignmentAnnotation();
804 for (int i = 0; i < aa.length; i++)
806 Annotation an = new Annotation();
808 if (aa[i].annotationId != null)
810 annotationIds.put(aa[i].annotationId, aa[i]);
813 an.setId(aa[i].annotationId);
815 an.setVisible(aa[i].visible);
817 an.setDescription(aa[i].description);
819 if (aa[i].sequenceRef != null)
821 // TODO later annotation sequenceRef should be the XML ID of the
822 // sequence rather than its display name
823 an.setSequenceRef(aa[i].sequenceRef.getName());
825 if (aa[i].groupRef != null)
827 Object groupIdr = groupRefs.get(aa[i].groupRef);
828 if (groupIdr == null)
830 // make a locally unique String
831 groupRefs.put(aa[i].groupRef,
832 groupIdr = ("" + System.currentTimeMillis()
833 + aa[i].groupRef.getName() + groupRefs.size()));
835 an.setGroupRef(groupIdr.toString());
838 // store all visualization attributes for annotation
839 an.setGraphHeight(aa[i].graphHeight);
840 an.setCentreColLabels(aa[i].centreColLabels);
841 an.setScaleColLabels(aa[i].scaleColLabel);
842 an.setShowAllColLabels(aa[i].showAllColLabels);
847 an.setGraphType(aa[i].graph);
848 an.setGraphGroup(aa[i].graphGroup);
849 if (aa[i].getThreshold() != null)
851 ThresholdLine line = new ThresholdLine();
852 line.setLabel(aa[i].getThreshold().label);
853 line.setValue(aa[i].getThreshold().value);
854 line.setColour(aa[i].getThreshold().colour.getRGB());
855 an.setThresholdLine(line);
863 an.setLabel(aa[i].label);
865 if (aa[i] == av.getAlignmentQualityAnnot() || aa[i] == av.getAlignmentConservationAnnotation()
866 || aa[i] == av.getAlignmentConsensusAnnotation() || aa[i].autoCalculated)
868 // new way of indicating autocalculated annotation -
869 an.setAutoCalculated(aa[i].autoCalculated);
871 if (aa[i].hasScore())
873 an.setScore(aa[i].getScore());
875 AnnotationElement ae;
876 if (aa[i].annotations != null)
878 an.setScoreOnly(false);
879 for (int a = 0; a < aa[i].annotations.length; a++)
881 if ((aa[i] == null) || (aa[i].annotations[a] == null))
886 ae = new AnnotationElement();
887 if (aa[i].annotations[a].description != null)
888 ae.setDescription(aa[i].annotations[a].description);
889 if (aa[i].annotations[a].displayCharacter != null)
890 ae.setDisplayCharacter(aa[i].annotations[a].displayCharacter);
892 if (!Float.isNaN(aa[i].annotations[a].value))
893 ae.setValue(aa[i].annotations[a].value);
896 if (aa[i].annotations[a].secondaryStructure != ' '
897 && aa[i].annotations[a].secondaryStructure != '\0')
898 ae.setSecondaryStructure(aa[i].annotations[a].secondaryStructure
901 if (aa[i].annotations[a].colour != null
902 && aa[i].annotations[a].colour != java.awt.Color.black)
904 ae.setColour(aa[i].annotations[a].colour.getRGB());
907 an.addAnnotationElement(ae);
908 if (aa[i].autoCalculated)
910 // only write one non-null entry into the annotation row -
911 // sufficient to get the visualization attributes necessary to
919 an.setScoreOnly(true);
921 vamsasSet.addAnnotation(an);
925 if (jal.getGroups() != null)
927 JGroup[] groups = new JGroup[jal.getGroups().size()];
929 for (int i = 0; i < groups.length; i++)
931 groups[i] = new JGroup();
933 jalview.datamodel.SequenceGroup sg = (jalview.datamodel.SequenceGroup) jal
934 .getGroups().elementAt(i);
935 groups[i].setStart(sg.getStartRes());
936 groups[i].setEnd(sg.getEndRes());
937 groups[i].setName(sg.getName());
938 if (groupRefs.containsKey(sg))
940 // group has references so set it's ID field
941 groups[i].setId(groupRefs.get(sg).toString());
945 if (sg.cs.conservationApplied())
947 groups[i].setConsThreshold(sg.cs.getConservationInc());
949 if (sg.cs instanceof jalview.schemes.UserColourScheme)
951 groups[i].setColour(SetUserColourScheme(sg.cs, userColours,
957 .setColour(ColourSchemeProperty.getColourName(sg.cs));
960 else if (sg.cs instanceof jalview.schemes.AnnotationColourGradient)
963 .setColour(ColourSchemeProperty
964 .getColourName(((jalview.schemes.AnnotationColourGradient) sg.cs)
967 else if (sg.cs instanceof jalview.schemes.UserColourScheme)
970 .setColour(SetUserColourScheme(sg.cs, userColours, jms));
974 groups[i].setColour(ColourSchemeProperty.getColourName(sg.cs));
977 groups[i].setPidThreshold(sg.cs.getThreshold());
980 groups[i].setOutlineColour(sg.getOutlineColour().getRGB());
981 groups[i].setDisplayBoxes(sg.getDisplayBoxes());
982 groups[i].setDisplayText(sg.getDisplayText());
983 groups[i].setColourText(sg.getColourText());
984 groups[i].setTextCol1(sg.textColour.getRGB());
985 groups[i].setTextCol2(sg.textColour2.getRGB());
986 groups[i].setTextColThreshold(sg.thresholdTextColour);
987 groups[i].setShowUnconserved(sg.getShowNonconserved());
988 groups[i].setIgnoreGapsinConsensus(sg.getIgnoreGapsConsensus());
989 groups[i].setShowConsensusHistogram(sg.isShowConsensusHistogram());
990 groups[i].setShowSequenceLogo(sg.isShowSequenceLogo());
991 for (int s = 0; s < sg.getSize(); s++)
993 jalview.datamodel.Sequence seq = (jalview.datamodel.Sequence) sg
995 groups[i].addSeq(seqHash(seq));
999 jms.setJGroup(groups);
1002 // /////////SAVE VIEWPORT
1003 Viewport view = new Viewport();
1004 view.setTitle(ap.alignFrame.getTitle());
1005 view.setSequenceSetId(makeHashCode(av.getSequenceSetId(),
1006 av.getSequenceSetId()));
1007 view.setId(av.getViewId());
1008 view.setViewName(av.viewName);
1009 view.setGatheredViews(av.gatherViewsHere);
1011 if (ap.av.explodedPosition != null)
1013 view.setXpos(av.explodedPosition.x);
1014 view.setYpos(av.explodedPosition.y);
1015 view.setWidth(av.explodedPosition.width);
1016 view.setHeight(av.explodedPosition.height);
1020 view.setXpos(ap.alignFrame.getBounds().x);
1021 view.setYpos(ap.alignFrame.getBounds().y);
1022 view.setWidth(ap.alignFrame.getBounds().width);
1023 view.setHeight(ap.alignFrame.getBounds().height);
1026 view.setStartRes(av.startRes);
1027 view.setStartSeq(av.startSeq);
1029 if (av.getGlobalColourScheme() instanceof jalview.schemes.UserColourScheme)
1031 view.setBgColour(SetUserColourScheme(av.getGlobalColourScheme(),
1034 else if (av.getGlobalColourScheme() instanceof jalview.schemes.AnnotationColourGradient)
1036 jalview.schemes.AnnotationColourGradient acg = (jalview.schemes.AnnotationColourGradient) av
1037 .getGlobalColourScheme();
1039 AnnotationColours ac = new AnnotationColours();
1040 ac.setAboveThreshold(acg.getAboveThreshold());
1041 ac.setThreshold(acg.getAnnotationThreshold());
1042 ac.setAnnotation(acg.getAnnotation());
1043 if (acg.getBaseColour() instanceof jalview.schemes.UserColourScheme)
1045 ac.setColourScheme(SetUserColourScheme(acg.getBaseColour(),
1050 ac.setColourScheme(ColourSchemeProperty.getColourName(acg
1054 ac.setMaxColour(acg.getMaxColour().getRGB());
1055 ac.setMinColour(acg.getMinColour().getRGB());
1056 view.setAnnotationColours(ac);
1057 view.setBgColour("AnnotationColourGradient");
1061 view.setBgColour(ColourSchemeProperty.getColourName(av
1062 .getGlobalColourScheme()));
1065 ColourSchemeI cs = av.getGlobalColourScheme();
1069 if (cs.conservationApplied())
1071 view.setConsThreshold(cs.getConservationInc());
1072 if (cs instanceof jalview.schemes.UserColourScheme)
1074 view.setBgColour(SetUserColourScheme(cs, userColours, jms));
1078 if (cs instanceof ResidueColourScheme)
1080 view.setPidThreshold(cs.getThreshold());
1084 view.setConservationSelected(av.getConservationSelected());
1085 view.setPidSelected(av.getAbovePIDThreshold());
1086 view.setFontName(av.font.getName());
1087 view.setFontSize(av.font.getSize());
1088 view.setFontStyle(av.font.getStyle());
1089 view.setRenderGaps(av.renderGaps);
1090 view.setShowAnnotation(av.getShowAnnotation());
1091 view.setShowBoxes(av.getShowBoxes());
1092 view.setShowColourText(av.getColourText());
1093 view.setShowFullId(av.getShowJVSuffix());
1094 view.setRightAlignIds(av.rightAlignIds);
1095 view.setShowSequenceFeatures(av.showSequenceFeatures);
1096 view.setShowText(av.getShowText());
1097 view.setShowUnconserved(av.getShowUnconserved());
1098 view.setWrapAlignment(av.getWrapAlignment());
1099 view.setTextCol1(av.textColour.getRGB());
1100 view.setTextCol2(av.textColour2.getRGB());
1101 view.setTextColThreshold(av.thresholdTextColour);
1102 view.setShowConsensusHistogram(av.isShowConsensusHistogram());
1103 view.setShowSequenceLogo(av.isShowSequenceLogo());
1104 view.setShowGroupConsensus(av.isShowGroupConsensus());
1105 view.setShowGroupConservation(av.isShowGroupConservation());
1106 view.setShowNPfeatureTooltip(av.isShowNpFeats());
1107 view.setShowDbRefTooltip(av.isShowDbRefs());
1108 view.setFollowHighlight(av.followHighlight);
1109 view.setFollowSelection(av.followSelection);
1110 view.setIgnoreGapsinConsensus(av.getIgnoreGapsConsensus());
1111 if (av.featuresDisplayed != null)
1113 jalview.schemabinding.version2.FeatureSettings fs = new jalview.schemabinding.version2.FeatureSettings();
1115 String[] renderOrder = ap.seqPanel.seqCanvas.getFeatureRenderer().renderOrder;
1117 Vector settingsAdded = new Vector();
1118 Object gstyle = null;
1119 GraduatedColor gcol = null;
1120 if (renderOrder != null)
1122 for (int ro = 0; ro < renderOrder.length; ro++)
1124 gstyle = ap.seqPanel.seqCanvas.getFeatureRenderer()
1125 .getFeatureStyle(renderOrder[ro]);
1126 Setting setting = new Setting();
1127 setting.setType(renderOrder[ro]);
1128 if (gstyle instanceof GraduatedColor)
1130 gcol = (GraduatedColor) gstyle;
1131 setting.setColour(gcol.getMaxColor().getRGB());
1132 setting.setMincolour(gcol.getMinColor().getRGB());
1133 setting.setMin(gcol.getMin());
1134 setting.setMax(gcol.getMax());
1135 setting.setColourByLabel(gcol.isColourByLabel());
1136 setting.setAutoScale(gcol.isAutoScale());
1137 setting.setThreshold(gcol.getThresh());
1138 setting.setThreshstate(gcol.getThreshType());
1142 setting.setColour(ap.seqPanel.seqCanvas.getFeatureRenderer()
1143 .getColour(renderOrder[ro]).getRGB());
1146 setting.setDisplay(av.featuresDisplayed
1147 .containsKey(renderOrder[ro]));
1148 float rorder = ap.seqPanel.seqCanvas.getFeatureRenderer()
1149 .getOrder(renderOrder[ro]);
1152 setting.setOrder(rorder);
1154 fs.addSetting(setting);
1155 settingsAdded.addElement(renderOrder[ro]);
1159 // Make sure we save none displayed feature settings
1160 Enumeration en = ap.seqPanel.seqCanvas.getFeatureRenderer().featureColours
1162 while (en.hasMoreElements())
1164 String key = en.nextElement().toString();
1165 if (settingsAdded.contains(key))
1170 Setting setting = new Setting();
1171 setting.setType(key);
1172 setting.setColour(ap.seqPanel.seqCanvas.getFeatureRenderer()
1173 .getColour(key).getRGB());
1175 setting.setDisplay(false);
1176 float rorder = ap.seqPanel.seqCanvas.getFeatureRenderer().getOrder(
1180 setting.setOrder(rorder);
1182 fs.addSetting(setting);
1183 settingsAdded.addElement(key);
1185 en = ap.seqPanel.seqCanvas.getFeatureRenderer().featureGroups.keys();
1186 Vector groupsAdded = new Vector();
1187 while (en.hasMoreElements())
1189 String grp = en.nextElement().toString();
1190 if (groupsAdded.contains(grp))
1194 Group g = new Group();
1196 g.setDisplay(((Boolean) ap.seqPanel.seqCanvas.getFeatureRenderer().featureGroups
1197 .get(grp)).booleanValue());
1199 groupsAdded.addElement(grp);
1201 jms.setFeatureSettings(fs);
1205 if (av.hasHiddenColumns())
1207 if (av.getColumnSelection() == null
1208 || av.getColumnSelection().getHiddenColumns() == null)
1210 warn("REPORT BUG: avoided null columnselection bug (DMAM reported). Please contact Jim about this.");
1214 for (int c = 0; c < av.getColumnSelection().getHiddenColumns()
1217 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
1219 HiddenColumns hc = new HiddenColumns();
1220 hc.setStart(region[0]);
1221 hc.setEnd(region[1]);
1222 view.addHiddenColumns(hc);
1227 jms.addViewport(view);
1229 object.setJalviewModelSequence(jms);
1230 object.getVamsasModel().addSequenceSet(vamsasSet);
1232 if (jout != null && fileName != null)
1234 // We may not want to write the object to disk,
1235 // eg we can copy the alignViewport to a new view object
1236 // using save and then load
1239 JarEntry entry = new JarEntry(fileName);
1240 jout.putNextEntry(entry);
1241 PrintWriter pout = new PrintWriter(new OutputStreamWriter(jout,
1243 org.exolab.castor.xml.Marshaller marshaller = new org.exolab.castor.xml.Marshaller(
1245 marshaller.marshal(object);
1248 } catch (Exception ex)
1250 // TODO: raise error in GUI if marshalling failed.
1251 ex.printStackTrace();
1258 * External mapping between jalview objects and objects yielding a valid and
1259 * unique object ID string. This is null for normal Jalview project IO, but
1260 * non-null when a jalview project is being read or written as part of a
1263 IdentityHashMap jv2vobj = null;
1266 * Construct a unique ID for jvobj using either existing bindings or if none
1267 * exist, the result of the hashcode call for the object.
1270 * jalview data object
1271 * @return unique ID for referring to jvobj
1273 private String makeHashCode(Object jvobj, String altCode)
1275 if (jv2vobj != null)
1277 Object id = jv2vobj.get(jvobj);
1280 return id.toString();
1282 // check string ID mappings
1283 if (jvids2vobj != null && jvobj instanceof String)
1285 id = jvids2vobj.get(jvobj);
1289 return id.toString();
1291 // give up and warn that something has gone wrong
1292 warn("Cannot find ID for object in external mapping : " + jvobj);
1298 * return local jalview object mapped to ID, if it exists
1302 * @return null or object bound to idcode
1304 private Object retrieveExistingObj(String idcode)
1306 if (idcode != null && vobj2jv != null)
1308 return vobj2jv.get(idcode);
1314 * binding from ID strings from external mapping table to jalview data model
1317 private Hashtable vobj2jv;
1319 private Sequence createVamsasSequence(String id, SequenceI jds)
1321 return createVamsasSequence(true, id, jds, null);
1324 private Sequence createVamsasSequence(boolean recurse, String id,
1325 SequenceI jds, SequenceI parentseq)
1327 Sequence vamsasSeq = new Sequence();
1328 vamsasSeq.setId(id);
1329 vamsasSeq.setName(jds.getName());
1330 vamsasSeq.setSequence(jds.getSequenceAsString());
1331 vamsasSeq.setDescription(jds.getDescription());
1332 jalview.datamodel.DBRefEntry[] dbrefs = null;
1333 if (jds.getDatasetSequence() != null)
1335 vamsasSeq.setDsseqid(seqHash(jds.getDatasetSequence()));
1336 if (jds.getDatasetSequence().getDBRef() != null)
1338 dbrefs = jds.getDatasetSequence().getDBRef();
1343 vamsasSeq.setDsseqid(id); // so we can tell which sequences really are
1344 // dataset sequences only
1345 dbrefs = jds.getDBRef();
1349 for (int d = 0; d < dbrefs.length; d++)
1351 DBRef dbref = new DBRef();
1352 dbref.setSource(dbrefs[d].getSource());
1353 dbref.setVersion(dbrefs[d].getVersion());
1354 dbref.setAccessionId(dbrefs[d].getAccessionId());
1355 if (dbrefs[d].hasMap())
1357 Mapping mp = createVamsasMapping(dbrefs[d].getMap(), parentseq,
1359 dbref.setMapping(mp);
1361 vamsasSeq.addDBRef(dbref);
1367 private Mapping createVamsasMapping(jalview.datamodel.Mapping jmp,
1368 SequenceI parentseq, SequenceI jds, boolean recurse)
1371 if (jmp.getMap() != null)
1375 jalview.util.MapList mlst = jmp.getMap();
1376 int r[] = mlst.getFromRanges();
1377 for (int s = 0; s < r.length; s += 2)
1379 MapListFrom mfrom = new MapListFrom();
1380 mfrom.setStart(r[s]);
1381 mfrom.setEnd(r[s + 1]);
1382 mp.addMapListFrom(mfrom);
1384 r = mlst.getToRanges();
1385 for (int s = 0; s < r.length; s += 2)
1387 MapListTo mto = new MapListTo();
1389 mto.setEnd(r[s + 1]);
1390 mp.addMapListTo(mto);
1392 mp.setMapFromUnit(mlst.getFromRatio());
1393 mp.setMapToUnit(mlst.getToRatio());
1394 if (jmp.getTo() != null)
1396 MappingChoice mpc = new MappingChoice();
1398 && (parentseq != jmp.getTo() || parentseq
1399 .getDatasetSequence() != jmp.getTo()))
1401 mpc.setSequence(createVamsasSequence(false, seqHash(jmp.getTo()),
1407 SequenceI ps = null;
1408 if (parentseq != jmp.getTo()
1409 && parentseq.getDatasetSequence() != jmp.getTo())
1411 // chaining dbref rather than a handshaking one
1412 jmpid = seqHash(ps = jmp.getTo());
1416 jmpid = seqHash(ps = parentseq);
1418 mpc.setDseqFor(jmpid);
1419 if (!seqRefIds.containsKey(mpc.getDseqFor()))
1421 jalview.bin.Cache.log.debug("creatign new DseqFor ID");
1422 seqRefIds.put(mpc.getDseqFor(), ps);
1426 jalview.bin.Cache.log.debug("reusing DseqFor ID");
1429 mp.setMappingChoice(mpc);
1435 String SetUserColourScheme(jalview.schemes.ColourSchemeI cs,
1436 Vector userColours, JalviewModelSequence jms)
1439 jalview.schemes.UserColourScheme ucs = (jalview.schemes.UserColourScheme) cs;
1440 boolean newucs = false;
1441 if (!userColours.contains(ucs))
1443 userColours.add(ucs);
1446 id = "ucs" + userColours.indexOf(ucs);
1449 // actually create the scheme's entry in the XML model
1450 java.awt.Color[] colours = ucs.getColours();
1451 jalview.schemabinding.version2.UserColours uc = new jalview.schemabinding.version2.UserColours();
1452 jalview.schemabinding.version2.UserColourScheme jbucs = new jalview.schemabinding.version2.UserColourScheme();
1454 for (int i = 0; i < colours.length; i++)
1456 jalview.schemabinding.version2.Colour col = new jalview.schemabinding.version2.Colour();
1457 col.setName(ResidueProperties.aa[i]);
1458 col.setRGB(jalview.util.Format.getHexString(colours[i]));
1459 jbucs.addColour(col);
1461 if (ucs.getLowerCaseColours() != null)
1463 colours = ucs.getLowerCaseColours();
1464 for (int i = 0; i < colours.length; i++)
1466 jalview.schemabinding.version2.Colour col = new jalview.schemabinding.version2.Colour();
1467 col.setName(ResidueProperties.aa[i].toLowerCase());
1468 col.setRGB(jalview.util.Format.getHexString(colours[i]));
1469 jbucs.addColour(col);
1474 uc.setUserColourScheme(jbucs);
1475 jms.addUserColours(uc);
1481 jalview.schemes.UserColourScheme GetUserColourScheme(
1482 JalviewModelSequence jms, String id)
1484 UserColours[] uc = jms.getUserColours();
1485 UserColours colours = null;
1487 for (int i = 0; i < uc.length; i++)
1489 if (uc[i].getId().equals(id))
1497 java.awt.Color[] newColours = new java.awt.Color[24];
1499 for (int i = 0; i < 24; i++)
1501 newColours[i] = new java.awt.Color(Integer.parseInt(colours
1502 .getUserColourScheme().getColour(i).getRGB(), 16));
1505 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(
1508 if (colours.getUserColourScheme().getColourCount() > 24)
1510 newColours = new java.awt.Color[23];
1511 for (int i = 0; i < 23; i++)
1513 newColours[i] = new java.awt.Color(Integer.parseInt(colours
1514 .getUserColourScheme().getColour(i + 24).getRGB(), 16));
1516 ucs.setLowerCaseColours(newColours);
1523 * contains last error message (if any) encountered by XML loader.
1525 String errorMessage = null;
1528 * flag to control whether the Jalview2XML_V1 parser should be deferred to if
1529 * exceptions are raised during project XML parsing
1531 public boolean attemptversion1parse = true;
1534 * Load a jalview project archive from a jar file
1537 * - HTTP URL or filename
1539 public AlignFrame LoadJalviewAlign(final String file)
1542 jalview.gui.AlignFrame af = null;
1546 // UNMARSHALLER SEEMS TO CLOSE JARINPUTSTREAM, MOST ANNOYING
1547 // Workaround is to make sure caller implements the JarInputStreamProvider
1549 // so we can re-open the jar input stream for each entry.
1551 jarInputStreamProvider jprovider = createjarInputStreamProvider(file);
1552 af = LoadJalviewAlign(jprovider);
1553 } catch (MalformedURLException e)
1555 errorMessage = "Invalid URL format for '" + file + "'";
1561 private jarInputStreamProvider createjarInputStreamProvider(
1562 final String file) throws MalformedURLException
1565 errorMessage = null;
1566 uniqueSetSuffix = null;
1568 viewportsAdded = null;
1569 frefedSequence = null;
1571 if (file.startsWith("http://"))
1573 url = new URL(file);
1575 final URL _url = url;
1576 return new jarInputStreamProvider()
1579 public JarInputStream getJarInputStream() throws IOException
1583 return new JarInputStream(_url.openStream());
1587 return new JarInputStream(new FileInputStream(file));
1591 public String getFilename()
1599 * Recover jalview session from a jalview project archive. Caller may
1600 * initialise uniqueSetSuffix, seqRefIds, viewportsAdded and frefedSequence
1601 * themselves. Any null fields will be initialised with default values,
1602 * non-null fields are left alone.
1607 public AlignFrame LoadJalviewAlign(final jarInputStreamProvider jprovider)
1609 errorMessage = null;
1610 if (uniqueSetSuffix == null)
1612 uniqueSetSuffix = System.currentTimeMillis() % 100000 + "";
1614 if (seqRefIds == null)
1616 seqRefIds = new Hashtable();
1618 if (viewportsAdded == null)
1620 viewportsAdded = new Hashtable();
1622 if (frefedSequence == null)
1624 frefedSequence = new Vector();
1627 jalview.gui.AlignFrame af = null;
1628 Hashtable gatherToThisFrame = new Hashtable();
1629 final String file = jprovider.getFilename();
1632 JarInputStream jin = null;
1633 JarEntry jarentry = null;
1638 jin = jprovider.getJarInputStream();
1639 for (int i = 0; i < entryCount; i++)
1641 jarentry = jin.getNextJarEntry();
1644 if (jarentry != null && jarentry.getName().endsWith(".xml"))
1646 InputStreamReader in = new InputStreamReader(jin, "UTF-8");
1647 JalviewModel object = new JalviewModel();
1649 Unmarshaller unmar = new Unmarshaller(object);
1650 unmar.setValidation(false);
1651 object = (JalviewModel) unmar.unmarshal(in);
1652 if (true) // !skipViewport(object))
1654 af = LoadFromObject(object, file, true, jprovider);
1655 if (af.viewport.gatherViewsHere)
1657 gatherToThisFrame.put(af.viewport.getSequenceSetId(), af);
1662 else if (jarentry != null)
1664 // Some other file here.
1667 } while (jarentry != null);
1668 resolveFrefedSequences();
1669 } catch (java.io.FileNotFoundException ex)
1671 ex.printStackTrace();
1672 errorMessage = "Couldn't locate Jalview XML file : " + file;
1673 System.err.println("Exception whilst loading jalview XML file : "
1675 } catch (java.net.UnknownHostException ex)
1677 ex.printStackTrace();
1678 errorMessage = "Couldn't locate Jalview XML file : " + file;
1679 System.err.println("Exception whilst loading jalview XML file : "
1681 } catch (Exception ex)
1683 System.err.println("Parsing as Jalview Version 2 file failed.");
1684 ex.printStackTrace(System.err);
1685 if (attemptversion1parse)
1687 // Is Version 1 Jar file?
1690 af = new Jalview2XML_V1(raiseGUI).LoadJalviewAlign(jprovider);
1691 } catch (Exception ex2)
1693 System.err.println("Exception whilst loading as jalviewXMLV1:");
1694 ex2.printStackTrace();
1698 if (Desktop.instance != null)
1700 Desktop.instance.stopLoading();
1704 System.out.println("Successfully loaded archive file");
1707 ex.printStackTrace();
1709 System.err.println("Exception whilst loading jalview XML file : "
1711 } catch (OutOfMemoryError e)
1713 // Don't use the OOM Window here
1714 errorMessage = "Out of memory loading jalview XML file";
1715 System.err.println("Out of memory whilst loading jalview XML file");
1716 e.printStackTrace();
1719 if (Desktop.instance != null)
1721 Desktop.instance.stopLoading();
1724 Enumeration en = gatherToThisFrame.elements();
1725 while (en.hasMoreElements())
1727 Desktop.instance.gatherViews((AlignFrame) en.nextElement());
1729 if (errorMessage != null)
1737 * check errorMessage for a valid error message and raise an error box in the
1738 * GUI or write the current errorMessage to stderr and then clear the error
1741 protected void reportErrors()
1743 reportErrors(false);
1746 protected void reportErrors(final boolean saving)
1748 if (errorMessage != null)
1750 final String finalErrorMessage = errorMessage;
1753 javax.swing.SwingUtilities.invokeLater(new Runnable()
1757 JOptionPane.showInternalMessageDialog(Desktop.desktop,
1758 finalErrorMessage, "Error "
1759 + (saving ? "saving" : "loading")
1760 + " Jalview file", JOptionPane.WARNING_MESSAGE);
1766 System.err.println("Problem loading Jalview file: " + errorMessage);
1769 errorMessage = null;
1772 Hashtable alreadyLoadedPDB;
1775 * when set, local views will be updated from view stored in JalviewXML
1776 * Currently (28th Sep 2008) things will go horribly wrong in vamsas document
1777 * sync if this is set to true.
1779 private boolean updateLocalViews = false;
1781 String loadPDBFile(jarInputStreamProvider jprovider, String pdbId)
1783 if (alreadyLoadedPDB == null)
1784 alreadyLoadedPDB = new Hashtable();
1786 if (alreadyLoadedPDB.containsKey(pdbId))
1787 return alreadyLoadedPDB.get(pdbId).toString();
1791 JarInputStream jin = jprovider.getJarInputStream();
1793 * if (jprovider.startsWith("http://")) { jin = new JarInputStream(new
1794 * URL(jprovider).openStream()); } else { jin = new JarInputStream(new
1795 * FileInputStream(jprovider)); }
1798 JarEntry entry = null;
1801 entry = jin.getNextJarEntry();
1802 } while (entry != null && !entry.getName().equals(pdbId));
1805 BufferedReader in = new BufferedReader(new InputStreamReader(jin));
1806 File outFile = File.createTempFile("jalview_pdb", ".txt");
1807 outFile.deleteOnExit();
1808 PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
1811 while ((data = in.readLine()) != null)
1818 } catch (Exception foo)
1824 alreadyLoadedPDB.put(pdbId, outFile.getAbsolutePath());
1825 return outFile.getAbsolutePath();
1829 warn("Couldn't find PDB file entry in Jalview Jar for " + pdbId);
1831 } catch (Exception ex)
1833 ex.printStackTrace();
1839 private class JvAnnotRow
1841 public JvAnnotRow(int i, AlignmentAnnotation jaa)
1848 * persisted version of annotation row from which to take vis properties
1850 public jalview.datamodel.AlignmentAnnotation template;
1853 * original position of the annotation row in the alignment
1859 * Load alignment frame from jalview XML DOM object
1864 * filename source string
1865 * @param loadTreesAndStructures
1866 * when false only create Viewport
1868 * data source provider
1869 * @return alignment frame created from view stored in DOM
1871 AlignFrame LoadFromObject(JalviewModel object, String file,
1872 boolean loadTreesAndStructures, jarInputStreamProvider jprovider)
1874 SequenceSet vamsasSet = object.getVamsasModel().getSequenceSet(0);
1875 Sequence[] vamsasSeq = vamsasSet.getSequence();
1877 JalviewModelSequence jms = object.getJalviewModelSequence();
1879 Viewport view = jms.getViewport(0);
1880 // ////////////////////////////////
1883 Vector hiddenSeqs = null;
1884 jalview.datamodel.Sequence jseq;
1886 ArrayList tmpseqs = new ArrayList();
1888 boolean multipleView = false;
1890 JSeq[] JSEQ = object.getJalviewModelSequence().getJSeq();
1891 int vi = 0; // counter in vamsasSeq array
1892 for (int i = 0; i < JSEQ.length; i++)
1894 String seqId = JSEQ[i].getId();
1896 if (seqRefIds.get(seqId) != null)
1898 tmpseqs.add((jalview.datamodel.Sequence) seqRefIds.get(seqId));
1899 multipleView = true;
1903 jseq = new jalview.datamodel.Sequence(vamsasSeq[vi].getName(),
1904 vamsasSeq[vi].getSequence());
1905 jseq.setDescription(vamsasSeq[vi].getDescription());
1906 jseq.setStart(JSEQ[i].getStart());
1907 jseq.setEnd(JSEQ[i].getEnd());
1908 jseq.setVamsasId(uniqueSetSuffix + seqId);
1909 seqRefIds.put(vamsasSeq[vi].getId(), jseq);
1914 if (JSEQ[i].getHidden())
1916 if (hiddenSeqs == null)
1918 hiddenSeqs = new Vector();
1921 hiddenSeqs.addElement((jalview.datamodel.Sequence) seqRefIds
1928 // Create the alignment object from the sequence set
1929 // ///////////////////////////////
1930 jalview.datamodel.Sequence[] orderedSeqs = new jalview.datamodel.Sequence[tmpseqs
1933 tmpseqs.toArray(orderedSeqs);
1935 jalview.datamodel.Alignment al = new jalview.datamodel.Alignment(
1938 // / Add the alignment properties
1939 for (int i = 0; i < vamsasSet.getSequenceSetPropertiesCount(); i++)
1941 SequenceSetProperties ssp = vamsasSet.getSequenceSetProperties(i);
1942 al.setProperty(ssp.getKey(), ssp.getValue());
1946 // SequenceFeatures are added to the DatasetSequence,
1947 // so we must create or recover the dataset before loading features
1948 // ///////////////////////////////
1949 if (vamsasSet.getDatasetId() == null || vamsasSet.getDatasetId() == "")
1951 // older jalview projects do not have a dataset id.
1952 al.setDataset(null);
1956 recoverDatasetFor(vamsasSet, al);
1958 // ///////////////////////////////
1960 Hashtable pdbloaded = new Hashtable();
1963 // load sequence features, database references and any associated PDB
1964 // structures for the alignment
1965 for (int i = 0; i < vamsasSeq.length; i++)
1967 if (JSEQ[i].getFeaturesCount() > 0)
1969 Features[] features = JSEQ[i].getFeatures();
1970 for (int f = 0; f < features.length; f++)
1972 jalview.datamodel.SequenceFeature sf = new jalview.datamodel.SequenceFeature(
1973 features[f].getType(), features[f].getDescription(),
1974 features[f].getStatus(), features[f].getBegin(),
1975 features[f].getEnd(), features[f].getFeatureGroup());
1977 sf.setScore(features[f].getScore());
1978 for (int od = 0; od < features[f].getOtherDataCount(); od++)
1980 OtherData keyValue = features[f].getOtherData(od);
1981 if (keyValue.getKey().startsWith("LINK"))
1983 sf.addLink(keyValue.getValue());
1987 sf.setValue(keyValue.getKey(), keyValue.getValue());
1992 al.getSequenceAt(i).getDatasetSequence().addSequenceFeature(sf);
1995 if (vamsasSeq[i].getDBRefCount() > 0)
1997 addDBRefs(al.getSequenceAt(i).getDatasetSequence(), vamsasSeq[i]);
1999 if (JSEQ[i].getPdbidsCount() > 0)
2001 Pdbids[] ids = JSEQ[i].getPdbids();
2002 for (int p = 0; p < ids.length; p++)
2004 jalview.datamodel.PDBEntry entry = new jalview.datamodel.PDBEntry();
2005 entry.setId(ids[p].getId());
2006 entry.setType(ids[p].getType());
2007 if (ids[p].getFile() != null)
2009 if (!pdbloaded.containsKey(ids[p].getFile()))
2011 entry.setFile(loadPDBFile(jprovider, ids[p].getId()));
2015 entry.setFile(pdbloaded.get(ids[p].getId()).toString());
2019 al.getSequenceAt(i).getDatasetSequence().addPDBId(entry);
2023 } // end !multipleview
2025 // ///////////////////////////////
2026 // LOAD SEQUENCE MAPPINGS
2028 if (vamsasSet.getAlcodonFrameCount() > 0)
2030 // TODO Potentially this should only be done once for all views of an
2032 AlcodonFrame[] alc = vamsasSet.getAlcodonFrame();
2033 for (int i = 0; i < alc.length; i++)
2035 jalview.datamodel.AlignedCodonFrame cf = new jalview.datamodel.AlignedCodonFrame(
2036 alc[i].getAlcodonCount());
2037 if (alc[i].getAlcodonCount() > 0)
2039 Alcodon[] alcods = alc[i].getAlcodon();
2040 for (int p = 0; p < cf.codons.length; p++)
2042 if (alcods[p].hasPos1() && alcods[p].hasPos2()
2043 && alcods[p].hasPos3())
2045 // translated codons require three valid positions
2046 cf.codons[p] = new int[3];
2047 cf.codons[p][0] = (int) alcods[p].getPos1();
2048 cf.codons[p][1] = (int) alcods[p].getPos2();
2049 cf.codons[p][2] = (int) alcods[p].getPos3();
2053 cf.codons[p] = null;
2057 if (alc[i].getAlcodMapCount() > 0)
2059 AlcodMap[] maps = alc[i].getAlcodMap();
2060 for (int m = 0; m < maps.length; m++)
2062 SequenceI dnaseq = (SequenceI) seqRefIds
2063 .get(maps[m].getDnasq());
2065 jalview.datamodel.Mapping mapping = null;
2066 // attach to dna sequence reference.
2067 if (maps[m].getMapping() != null)
2069 mapping = addMapping(maps[m].getMapping());
2073 cf.addMap(dnaseq, mapping.getTo(), mapping.getMap());
2078 frefedSequence.add(new Object[]
2079 { maps[m].getDnasq(), cf, mapping });
2083 al.addCodonFrame(cf);
2088 // ////////////////////////////////
2090 ArrayList<JvAnnotRow> autoAlan = new ArrayList<JvAnnotRow>();
2092 * store any annotations which forward reference a group's ID
2094 Hashtable<String, ArrayList<jalview.datamodel.AlignmentAnnotation>> groupAnnotRefs = new Hashtable<String, ArrayList<jalview.datamodel.AlignmentAnnotation>>();
2096 if (vamsasSet.getAnnotationCount() > 0)
2098 Annotation[] an = vamsasSet.getAnnotation();
2100 for (int i = 0; i < an.length; i++)
2103 * test if annotation is automatically calculated for this view only
2105 boolean autoForView = false;
2106 if (an[i].getLabel().equals("Quality")
2107 || an[i].getLabel().equals("Conservation")
2108 || an[i].getLabel().equals("Consensus"))
2110 // Kludge for pre 2.5 projects which lacked the autocalculated flag
2112 if (!an[i].hasAutoCalculated())
2114 an[i].setAutoCalculated(true);
2118 || (an[i].hasAutoCalculated() && an[i].isAutoCalculated()))
2120 // remove ID - we don't recover annotation from other views for
2121 // view-specific annotation
2125 // set visiblity for other annotation in this view
2126 if (an[i].getId() != null
2127 && annotationIds.containsKey(an[i].getId()))
2129 jalview.datamodel.AlignmentAnnotation jda = (jalview.datamodel.AlignmentAnnotation) annotationIds
2130 .get(an[i].getId());
2131 // in principle Visible should always be true for annotation displayed
2132 // in multiple views
2133 if (an[i].hasVisible())
2134 jda.visible = an[i].getVisible();
2136 al.addAnnotation(jda);
2140 // Construct new annotation from model.
2141 AnnotationElement[] ae = an[i].getAnnotationElement();
2142 jalview.datamodel.Annotation[] anot = null;
2144 if (!an[i].getScoreOnly())
2146 anot = new jalview.datamodel.Annotation[al.getWidth()];
2147 for (int aa = 0; aa < ae.length && aa < anot.length; aa++)
2149 if (ae[aa].getPosition() >= anot.length)
2152 anot[ae[aa].getPosition()] = new jalview.datamodel.Annotation(
2154 ae[aa].getDisplayCharacter(), ae[aa].getDescription(),
2155 (ae[aa].getSecondaryStructure() == null || ae[aa]
2156 .getSecondaryStructure().length() == 0) ? ' '
2157 : ae[aa].getSecondaryStructure().charAt(0),
2161 // JBPNote: Consider verifying dataflow for IO of secondary
2162 // structure annotation read from Stockholm files
2163 // this was added to try to ensure that
2164 // if (anot[ae[aa].getPosition()].secondaryStructure>' ')
2166 // anot[ae[aa].getPosition()].displayCharacter = "";
2168 anot[ae[aa].getPosition()].colour = new java.awt.Color(
2169 ae[aa].getColour());
2172 jalview.datamodel.AlignmentAnnotation jaa = null;
2174 if (an[i].getGraph())
2176 float llim = 0, hlim = 0;
2177 // if (autoForView || an[i].isAutoCalculated()) {
2180 jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(),
2181 an[i].getDescription(), anot, llim, hlim,
2182 an[i].getGraphType());
2184 jaa.graphGroup = an[i].getGraphGroup();
2186 if (an[i].getThresholdLine() != null)
2188 jaa.setThreshold(new jalview.datamodel.GraphLine(an[i]
2189 .getThresholdLine().getValue(), an[i]
2190 .getThresholdLine().getLabel(), new java.awt.Color(
2191 an[i].getThresholdLine().getColour())));
2194 if (autoForView || an[i].isAutoCalculated())
2196 // Hardwire the symbol display line to ensure that labels for
2197 // histograms are displayed
2203 jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(),
2204 an[i].getDescription(), anot);
2208 // register new annotation
2209 if (an[i].getId() != null)
2211 annotationIds.put(an[i].getId(), jaa);
2212 jaa.annotationId = an[i].getId();
2214 // recover sequence association
2215 if (an[i].getSequenceRef() != null)
2217 if (al.findName(an[i].getSequenceRef()) != null)
2219 jaa.createSequenceMapping(
2220 al.findName(an[i].getSequenceRef()), 1, true);
2221 al.findName(an[i].getSequenceRef()).addAlignmentAnnotation(
2226 // and make a note of any group association
2227 if (an[i].getGroupRef() != null && an[i].getGroupRef().length() > 0)
2229 ArrayList<jalview.datamodel.AlignmentAnnotation> aal = groupAnnotRefs
2230 .get(an[i].getGroupRef());
2233 aal = new ArrayList<jalview.datamodel.AlignmentAnnotation>();
2234 groupAnnotRefs.put(an[i].getGroupRef(), aal);
2239 if (an[i].hasScore())
2241 jaa.setScore(an[i].getScore());
2243 if (an[i].hasVisible())
2244 jaa.visible = an[i].getVisible();
2246 if (an[i].hasCentreColLabels())
2247 jaa.centreColLabels = an[i].getCentreColLabels();
2249 if (an[i].hasScaleColLabels())
2251 jaa.scaleColLabel = an[i].getScaleColLabels();
2253 if (an[i].hasAutoCalculated() && an[i].isAutoCalculated())
2255 // newer files have an 'autoCalculated' flag and store calculation
2256 // state in viewport properties
2257 jaa.autoCalculated = true; // means annotation will be marked for
2258 // update at end of load.
2260 if (an[i].hasGraphHeight())
2262 jaa.graphHeight = an[i].getGraphHeight();
2264 if (jaa.autoCalculated)
2266 autoAlan.add(new JvAnnotRow(i, jaa));
2269 // if (!autoForView)
2271 // add autocalculated group annotation and any user created annotation
2273 al.addAnnotation(jaa);
2278 // ///////////////////////
2280 // Create alignment markup and styles for this view
2281 if (jms.getJGroupCount() > 0)
2283 JGroup[] groups = jms.getJGroup();
2285 for (int i = 0; i < groups.length; i++)
2287 ColourSchemeI cs = null;
2289 if (groups[i].getColour() != null)
2291 if (groups[i].getColour().startsWith("ucs"))
2293 cs = GetUserColourScheme(jms, groups[i].getColour());
2297 cs = ColourSchemeProperty.getColour(al, groups[i].getColour());
2302 cs.setThreshold(groups[i].getPidThreshold(), true);
2306 Vector seqs = new Vector();
2308 for (int s = 0; s < groups[i].getSeqCount(); s++)
2310 String seqId = groups[i].getSeq(s) + "";
2311 jalview.datamodel.SequenceI ts = (jalview.datamodel.SequenceI) seqRefIds
2316 seqs.addElement(ts);
2320 if (seqs.size() < 1)
2325 jalview.datamodel.SequenceGroup sg = new jalview.datamodel.SequenceGroup(
2326 seqs, groups[i].getName(), cs, groups[i].getDisplayBoxes(),
2327 groups[i].getDisplayText(), groups[i].getColourText(),
2328 groups[i].getStart(), groups[i].getEnd());
2330 sg.setOutlineColour(new java.awt.Color(groups[i].getOutlineColour()));
2332 sg.textColour = new java.awt.Color(groups[i].getTextCol1());
2333 sg.textColour2 = new java.awt.Color(groups[i].getTextCol2());
2334 sg.setShowNonconserved(groups[i].hasShowUnconserved() ? groups[i]
2335 .isShowUnconserved() : false);
2336 sg.thresholdTextColour = groups[i].getTextColThreshold();
2337 if (groups[i].hasShowConsensusHistogram())
2339 sg.setShowConsensusHistogram(groups[i].isShowConsensusHistogram());
2342 if (groups[i].hasShowSequenceLogo())
2344 sg.setshowSequenceLogo(groups[i].isShowSequenceLogo());
2346 if (groups[i].hasIgnoreGapsinConsensus())
2348 sg.setIgnoreGapsConsensus(groups[i].getIgnoreGapsinConsensus());
2350 if (groups[i].getConsThreshold() != 0)
2352 jalview.analysis.Conservation c = new jalview.analysis.Conservation(
2353 "All", ResidueProperties.propHash, 3,
2354 sg.getSequences(null), 0, sg.getWidth() - 1);
2356 c.verdict(false, 25);
2357 sg.cs.setConservation(c);
2360 if (groups[i].getId() != null && groupAnnotRefs.size() > 0)
2362 // re-instate unique group/annotation row reference
2363 ArrayList<jalview.datamodel.AlignmentAnnotation> jaal = groupAnnotRefs
2364 .get(groups[i].getId());
2367 for (jalview.datamodel.AlignmentAnnotation jaa : jaal)
2370 if (jaa.autoCalculated)
2372 // match up and try to set group autocalc alignment row for this
2374 if (jaa.label.startsWith("Consensus for "))
2376 sg.setConsensus(jaa);
2378 // match up and try to set group autocalc alignment row for this
2380 if (jaa.label.startsWith("Conservation for "))
2382 sg.setConservationRow(jaa);
2393 // ///////////////////////////////
2396 // If we just load in the same jar file again, the sequenceSetId
2397 // will be the same, and we end up with multiple references
2398 // to the same sequenceSet. We must modify this id on load
2399 // so that each load of the file gives a unique id
2400 String uniqueSeqSetId = view.getSequenceSetId() + uniqueSetSuffix;
2401 String viewId = (view.getId() == null ? null : view.getId()
2403 AlignFrame af = null;
2404 AlignViewport av = null;
2405 // now check to see if we really need to create a new viewport.
2406 if (multipleView && viewportsAdded.size() == 0)
2408 // We recovered an alignment for which a viewport already exists.
2409 // TODO: fix up any settings necessary for overlaying stored state onto
2410 // state recovered from another document. (may not be necessary).
2411 // we may need a binding from a viewport in memory to one recovered from
2413 // and then recover its containing af to allow the settings to be applied.
2414 // TODO: fix for vamsas demo
2416 .println("About to recover a viewport for existing alignment: Sequence set ID is "
2418 Object seqsetobj = retrieveExistingObj(uniqueSeqSetId);
2419 if (seqsetobj != null)
2421 if (seqsetobj instanceof String)
2423 uniqueSeqSetId = (String) seqsetobj;
2425 .println("Recovered extant sequence set ID mapping for ID : New Sequence set ID is "
2431 .println("Warning : Collision between sequence set ID string and existing jalview object mapping.");
2436 AlignmentPanel ap = null;
2437 boolean isnewview = true;
2440 // Check to see if this alignment already has a view id == viewId
2441 jalview.gui.AlignmentPanel views[] = Desktop
2442 .getAlignmentPanels(uniqueSeqSetId);
2443 if (views != null && views.length > 0)
2445 for (int v = 0; v < views.length; v++)
2447 if (views[v].av.getViewId().equalsIgnoreCase(viewId))
2449 // recover the existing alignpanel, alignframe, viewport
2450 af = views[v].alignFrame;
2453 // TODO: could even skip resetting view settings if we don't want to
2454 // change the local settings from other jalview processes
2463 af = loadViewport(file, JSEQ, hiddenSeqs, al, jms, view,
2464 uniqueSeqSetId, viewId, autoAlan);
2469 // /////////////////////////////////////
2470 if (loadTreesAndStructures && jms.getTreeCount() > 0)
2474 for (int t = 0; t < jms.getTreeCount(); t++)
2477 Tree tree = jms.getTree(t);
2479 TreePanel tp = (TreePanel) retrieveExistingObj(tree.getId());
2482 tp = af.ShowNewickTree(
2483 new jalview.io.NewickFile(tree.getNewick()),
2484 tree.getTitle(), tree.getWidth(), tree.getHeight(),
2485 tree.getXpos(), tree.getYpos());
2486 if (tree.getId() != null)
2488 // perhaps bind the tree id to something ?
2493 // update local tree attributes ?
2494 // TODO: should check if tp has been manipulated by user - if so its
2495 // settings shouldn't be modified
2496 tp.setTitle(tree.getTitle());
2497 tp.setBounds(new Rectangle(tree.getXpos(), tree.getYpos(), tree
2498 .getWidth(), tree.getHeight()));
2499 tp.av = av; // af.viewport; // TODO: verify 'associate with all
2502 tp.treeCanvas.av = av; // af.viewport;
2503 tp.treeCanvas.ap = ap; // af.alignPanel;
2508 warn("There was a problem recovering stored Newick tree: \n"
2509 + tree.getNewick());
2513 tp.fitToWindow.setState(tree.getFitToWindow());
2514 tp.fitToWindow_actionPerformed(null);
2516 if (tree.getFontName() != null)
2518 tp.setTreeFont(new java.awt.Font(tree.getFontName(), tree
2519 .getFontStyle(), tree.getFontSize()));
2523 tp.setTreeFont(new java.awt.Font(view.getFontName(), view
2524 .getFontStyle(), tree.getFontSize()));
2527 tp.showPlaceholders(tree.getMarkUnlinked());
2528 tp.showBootstrap(tree.getShowBootstrap());
2529 tp.showDistances(tree.getShowDistances());
2531 tp.treeCanvas.threshold = tree.getThreshold();
2533 if (tree.getCurrentTree())
2535 af.viewport.setCurrentTree(tp.getTree());
2539 } catch (Exception ex)
2541 ex.printStackTrace();
2545 // //LOAD STRUCTURES
2546 if (loadTreesAndStructures)
2548 // run through all PDB ids on the alignment, and collect mappings between
2549 // jmol view ids and all sequences referring to it
2550 Hashtable<String, Object[]> jmolViewIds = new Hashtable();
2552 for (int i = 0; i < JSEQ.length; i++)
2554 if (JSEQ[i].getPdbidsCount() > 0)
2556 Pdbids[] ids = JSEQ[i].getPdbids();
2557 for (int p = 0; p < ids.length; p++)
2559 for (int s = 0; s < ids[p].getStructureStateCount(); s++)
2561 // check to see if we haven't already created this structure view
2562 String sviewid = (ids[p].getStructureState(s).getViewId() == null) ? null
2563 : ids[p].getStructureState(s).getViewId()
2565 jalview.datamodel.PDBEntry jpdb = new jalview.datamodel.PDBEntry();
2566 // Originally : ids[p].getFile()
2567 // : TODO: verify external PDB file recovery still works in normal
2568 // jalview project load
2569 jpdb.setFile(loadPDBFile(jprovider, ids[p].getId()));
2570 jpdb.setId(ids[p].getId());
2572 int x = ids[p].getStructureState(s).getXpos();
2573 int y = ids[p].getStructureState(s).getYpos();
2574 int width = ids[p].getStructureState(s).getWidth();
2575 int height = ids[p].getStructureState(s).getHeight();
2577 // Probably don't need to do this anymore...
2578 // Desktop.desktop.getComponentAt(x, y);
2579 // TODO: NOW: check that this recovers the PDB file correctly.
2580 String pdbFile = loadPDBFile(jprovider, ids[p].getId());
2581 jalview.datamodel.SequenceI seq = (jalview.datamodel.SequenceI) seqRefIds
2582 .get(JSEQ[i].getId() + "");
2583 if (sviewid == null)
2585 sviewid = "_jalview_pre2_4_" + x + "," + y + "," + width
2588 if (!jmolViewIds.containsKey(sviewid))
2590 jmolViewIds.put(sviewid, new Object[]
2592 { x, y, width, height }, "",
2593 new Hashtable<String, Object[]>(), new boolean[]
2594 { false, false, true } });
2595 // Legacy pre-2.7 conversion JAL-823 :
2596 // do not assume any view has to be linked for colour by
2600 // assemble String[] { pdb files }, String[] { id for each
2601 // file }, orig_fileloc, SequenceI[][] {{ seqs_file 1 }, {
2602 // seqs_file 2}, boolean[] {
2603 // linkAlignPanel,superposeWithAlignpanel}} from hash
2604 Object[] jmoldat = (Object[]) jmolViewIds.get(sviewid);
2605 ((boolean[]) jmoldat[3])[0] |= ids[p].getStructureState(s)
2606 .hasAlignwithAlignPanel() ? ids[p].getStructureState(
2607 s).getAlignwithAlignPanel() : false;
2608 // never colour by linked panel if not specified
2609 ((boolean[]) jmoldat[3])[1] |= ids[p].getStructureState(s)
2610 .hasColourwithAlignPanel() ? ids[p]
2611 .getStructureState(s).getColourwithAlignPanel()
2613 // default for pre-2.7 projects is that Jmol colouring is enabled
2614 ((boolean[]) jmoldat[3])[2] &= ids[p].getStructureState(s)
2615 .hasColourByJmol() ? ids[p].getStructureState(s)
2616 .getColourByJmol() : true;
2618 if (((String) jmoldat[1]).length() < ids[p]
2619 .getStructureState(s).getContent().length())
2622 jmoldat[1] = ids[p].getStructureState(s).getContent();
2625 if (ids[p].getFile() != null)
2627 Object[] seqstrmaps = (Object[]) ((Hashtable) jmoldat[2])
2628 .get(ids[p].getFile());
2629 if (seqstrmaps == null)
2631 ((Hashtable) jmoldat[2]).put(
2632 new File(ids[p].getFile()).toString(),
2633 seqstrmaps = new Object[]
2634 { pdbFile, ids[p].getId(), new Vector(),
2637 if (!((Vector) seqstrmaps[2]).contains(seq))
2639 ((Vector) seqstrmaps[2]).addElement(seq);
2640 // ((Vector)seqstrmaps[3]).addElement(n) :
2641 // in principle, chains
2642 // should be stored here : do we need to
2643 // TODO: store and recover seq/pdb_id :
2649 errorMessage = ("The Jmol views in this project were imported\nfrom an older version of Jalview.\nPlease review the sequence colour associations\nin the Colour by section of the Jmol View menu.\n\nIn the case of problems, see note at\nhttp://issues.jalview.org/browse/JAL-747");
2658 // Instantiate the associated Jmol views
2659 for (Entry<String, Object[]> entry : jmolViewIds.entrySet())
2661 String sviewid = entry.getKey();
2662 Object[] svattrib = entry.getValue();
2663 int[] geom = (int[]) svattrib[0];
2664 String state = (String) svattrib[1];
2665 Hashtable<String, Object[]> oldFiles = (Hashtable<String, Object[]>) svattrib[2];
2666 final boolean useinJmolsuperpos = ((boolean[]) svattrib[3])[0], usetoColourbyseq = ((boolean[]) svattrib[3])[1], jmolColouring = ((boolean[]) svattrib[3])[2];
2667 int x = geom[0], y = geom[1], width = geom[2], height = geom[3];
2668 // collate the pdbfile -> sequence mappings from this view
2669 Vector<String> pdbfilenames = new Vector<String>();
2670 Vector<SequenceI[]> seqmaps = new Vector<SequenceI[]>();
2671 Vector<String> pdbids = new Vector<String>();
2673 // Search to see if we've already created this Jmol view
2674 AppJmol comp = null;
2675 JInternalFrame[] frames = null;
2680 frames = Desktop.desktop.getAllFrames();
2681 } catch (ArrayIndexOutOfBoundsException e)
2683 // occasional No such child exceptions are thrown here...
2688 } catch (Exception f)
2693 } while (frames == null);
2694 // search for any Jmol windows already open from other
2695 // alignment views that exactly match the stored structure state
2696 for (int f = 0; comp == null && f < frames.length; f++)
2698 if (frames[f] instanceof AppJmol)
2701 && ((AppJmol) frames[f]).getViewId().equals(sviewid))
2703 // post jalview 2.4 schema includes structure view id
2704 comp = (AppJmol) frames[f];
2706 else if (frames[f].getX() == x && frames[f].getY() == y
2707 && frames[f].getHeight() == height
2708 && frames[f].getWidth() == width)
2710 comp = (AppJmol) frames[f];
2717 // create a new Jmol window.
2718 // First parse the Jmol state to translate filenames loaded into the
2719 // view, and record the order in which files are shown in the Jmol
2720 // view, so we can add the sequence mappings in same order.
2721 StringBuffer newFileLoc = null;
2722 int cp = 0, ncp, ecp;
2723 while ((ncp = state.indexOf("load ", cp)) > -1)
2725 if (newFileLoc == null)
2727 newFileLoc = new StringBuffer();
2729 newFileLoc.append(state.substring(cp,
2730 ncp = (state.indexOf("\"", ncp + 1) + 1)));
2731 String oldfilenam = state.substring(ncp,
2732 ecp = state.indexOf("\"", ncp));
2733 // recover the new mapping data for this old filename
2734 // have to normalize filename - since Jmol and jalview do filename
2735 // translation differently.
2736 Object[] filedat = oldFiles.get(new File(oldfilenam)
2738 newFileLoc.append(((String) filedat[0]));
2739 pdbfilenames.addElement((String) filedat[0]);
2740 pdbids.addElement((String) filedat[1]);
2741 seqmaps.addElement((SequenceI[]) ((Vector<SequenceI>) filedat[2])
2742 .toArray(new SequenceI[0]));
2743 newFileLoc.append("\"");
2744 cp = ecp + 1; // advance beyond last \" and set cursor so we can
2745 // look for next file statement.
2749 // just append rest of state
2750 newFileLoc.append(state.substring(cp));
2755 .print("Ignoring incomplete Jmol state for PDB ids: ");
2756 newFileLoc = new StringBuffer(state);
2757 newFileLoc.append("; load append ");
2758 for (String id : oldFiles.keySet())
2760 // add this and any other pdb files that should be present in
2762 Object[] filedat = oldFiles.get(id);
2764 newFileLoc.append(((String) filedat[0]));
2765 pdbfilenames.addElement((String) filedat[0]);
2766 pdbids.addElement((String) filedat[1]);
2767 seqmaps.addElement((SequenceI[]) ((Vector<SequenceI>) filedat[2])
2768 .toArray(new SequenceI[0]));
2769 newFileLoc.append(" \"");
2770 newFileLoc.append((String) filedat[0]);
2771 newFileLoc.append("\"");
2774 newFileLoc.append(";");
2777 if (newFileLoc != null)
2779 int histbug = newFileLoc.indexOf("history = ");
2781 int diff = histbug == -1 ? -1 : newFileLoc.indexOf(";",
2783 String val = (diff == -1) ? null : newFileLoc.substring(
2785 if (val != null && val.length() >= 4)
2787 if (val.contains("e"))
2789 if (val.trim().equals("true"))
2797 newFileLoc.replace(histbug, diff, val);
2800 // TODO: assemble String[] { pdb files }, String[] { id for each
2801 // file }, orig_fileloc, SequenceI[][] {{ seqs_file 1 }, {
2802 // seqs_file 2}} from hash
2803 final String[] pdbf = (String[]) pdbfilenames
2804 .toArray(new String[pdbfilenames.size()]), id = (String[]) pdbids
2805 .toArray(new String[pdbids.size()]);
2806 final SequenceI[][] sq = (SequenceI[][]) seqmaps
2807 .toArray(new SequenceI[seqmaps.size()][]);
2808 final String fileloc = newFileLoc.toString(), vid = sviewid;
2809 final AlignFrame alf = af;
2810 final java.awt.Rectangle rect = new java.awt.Rectangle(x, y,
2814 javax.swing.SwingUtilities.invokeAndWait(new Runnable()
2818 AppJmol sview = null;
2821 sview = new AppJmol(pdbf, id, sq, alf.alignPanel,
2822 useinJmolsuperpos, usetoColourbyseq,
2823 jmolColouring, fileloc, rect, vid);
2824 } catch (OutOfMemoryError ex)
2826 new OOMWarning("restoring structure view for PDB id "
2827 + id, (OutOfMemoryError) ex.getCause());
2828 if (sview != null && sview.isVisible())
2830 sview.closeViewer();
2831 sview.setVisible(false);
2837 } catch (InvocationTargetException ex)
2839 warn("Unexpected error when opening Jmol view.", ex);
2841 } catch (InterruptedException e)
2843 // e.printStackTrace();
2849 // if (comp != null)
2851 // NOTE: if the jalview project is part of a shared session then
2852 // view synchronization should/could be done here.
2854 // add mapping for sequences in this view to an already open Jmol
2856 for (String id : oldFiles.keySet())
2858 // add this and any other pdb files that should be present in the
2860 Object[] filedat = oldFiles.get(id);
2861 String pdbFile = (String) filedat[0];
2862 SequenceI[] seq = (SequenceI[]) ((Vector<SequenceI>) filedat[2])
2863 .toArray(new SequenceI[0]);
2864 ((AppJmol) comp).jmb.ssm.setMapping(seq, null, pdbFile,
2865 jalview.io.AppletFormatAdapter.FILE);
2866 ((AppJmol) comp).jmb.addSequenceForStructFile(pdbFile, seq);
2868 // and add the AlignmentPanel's reference to the Jmol view
2869 ((AppJmol) comp).addAlignmentPanel(ap);
2870 if (useinJmolsuperpos)
2872 ((AppJmol) comp).useAlignmentPanelForSuperposition(ap);
2876 ((AppJmol) comp).excludeAlignmentPanelForSuperposition(ap);
2878 if (usetoColourbyseq)
2880 ((AppJmol) comp).useAlignmentPanelForColourbyseq(ap,
2885 ((AppJmol) comp).excludeAlignmentPanelForColourbyseq(ap);
2891 // and finally return.
2895 AlignFrame loadViewport(String file, JSeq[] JSEQ, Vector hiddenSeqs,
2896 Alignment al, JalviewModelSequence jms, Viewport view,
2897 String uniqueSeqSetId, String viewId,
2898 ArrayList<JvAnnotRow> autoAlan)
2900 AlignFrame af = null;
2901 af = new AlignFrame(al, view.getWidth(), view.getHeight(),
2902 uniqueSeqSetId, viewId);
2904 af.setFileName(file, "Jalview");
2906 for (int i = 0; i < JSEQ.length; i++)
2908 af.viewport.setSequenceColour(af.viewport.getAlignment().getSequenceAt(i),
2909 new java.awt.Color(JSEQ[i].getColour()));
2912 af.viewport.gatherViewsHere = view.getGatheredViews();
2914 if (view.getSequenceSetId() != null)
2916 jalview.gui.AlignViewport av = (jalview.gui.AlignViewport) viewportsAdded
2917 .get(uniqueSeqSetId);
2919 af.viewport.setSequenceSetId(uniqueSeqSetId);
2922 // propagate shared settings to this new view
2923 af.viewport.historyList = av.historyList;
2924 af.viewport.redoList = av.redoList;
2928 viewportsAdded.put(uniqueSeqSetId, af.viewport);
2930 // TODO: check if this method can be called repeatedly without
2931 // side-effects if alignpanel already registered.
2932 PaintRefresher.Register(af.alignPanel, uniqueSeqSetId);
2934 // apply Hidden regions to view.
2935 if (hiddenSeqs != null)
2937 for (int s = 0; s < JSEQ.length; s++)
2939 jalview.datamodel.SequenceGroup hidden = new jalview.datamodel.SequenceGroup();
2941 for (int r = 0; r < JSEQ[s].getHiddenSequencesCount(); r++)
2944 al.getSequenceAt(JSEQ[s].getHiddenSequences(r)), false);
2946 af.viewport.hideRepSequences(al.getSequenceAt(s), hidden);
2949 jalview.datamodel.SequenceI[] hseqs = new jalview.datamodel.SequenceI[hiddenSeqs
2952 for (int s = 0; s < hiddenSeqs.size(); s++)
2954 hseqs[s] = (jalview.datamodel.SequenceI) hiddenSeqs.elementAt(s);
2957 af.viewport.hideSequence(hseqs);
2960 // recover view properties and display parameters
2961 if (view.getViewName() != null)
2963 af.viewport.viewName = view.getViewName();
2964 af.setInitialTabVisible();
2966 af.setBounds(view.getXpos(), view.getYpos(), view.getWidth(),
2969 af.viewport.setShowAnnotation(view.getShowAnnotation());
2970 af.viewport.setAbovePIDThreshold(view.getPidSelected());
2972 af.viewport.setColourText(view.getShowColourText());
2974 af.viewport.setConservationSelected(view.getConservationSelected());
2975 af.viewport.setShowJVSuffix(view.getShowFullId());
2976 af.viewport.rightAlignIds = view.getRightAlignIds();
2977 af.viewport.setFont(new java.awt.Font(view.getFontName(), view
2978 .getFontStyle(), view.getFontSize()));
2979 af.alignPanel.fontChanged();
2980 af.viewport.setRenderGaps(view.getRenderGaps());
2981 af.viewport.setWrapAlignment(view.getWrapAlignment());
2982 af.alignPanel.setWrapAlignment(view.getWrapAlignment());
2983 af.viewport.setShowAnnotation(view.getShowAnnotation());
2984 af.alignPanel.setAnnotationVisible(view.getShowAnnotation());
2986 af.viewport.setShowBoxes(view.getShowBoxes());
2988 af.viewport.setShowText(view.getShowText());
2990 af.viewport.textColour = new java.awt.Color(view.getTextCol1());
2991 af.viewport.textColour2 = new java.awt.Color(view.getTextCol2());
2992 af.viewport.thresholdTextColour = view.getTextColThreshold();
2993 af.viewport.setShowUnconserved(view.hasShowUnconserved() ? view
2994 .isShowUnconserved() : false);
2995 af.viewport.setStartRes(view.getStartRes());
2996 af.viewport.setStartSeq(view.getStartSeq());
2998 ColourSchemeI cs = null;
2999 // apply colourschemes
3000 if (view.getBgColour() != null)
3002 if (view.getBgColour().startsWith("ucs"))
3004 cs = GetUserColourScheme(jms, view.getBgColour());
3006 else if (view.getBgColour().startsWith("Annotation"))
3008 // int find annotation
3009 if (af.viewport.getAlignment().getAlignmentAnnotation() != null)
3011 for (int i = 0; i < af.viewport.getAlignment()
3012 .getAlignmentAnnotation().length; i++)
3014 if (af.viewport.getAlignment().getAlignmentAnnotation()[i].label
3015 .equals(view.getAnnotationColours().getAnnotation()))
3017 if (af.viewport.getAlignment().getAlignmentAnnotation()[i]
3018 .getThreshold() == null)
3020 af.viewport.getAlignment().getAlignmentAnnotation()[i]
3021 .setThreshold(new jalview.datamodel.GraphLine(view
3022 .getAnnotationColours().getThreshold(),
3023 "Threshold", java.awt.Color.black)
3028 if (view.getAnnotationColours().getColourScheme()
3031 cs = new AnnotationColourGradient(
3032 af.viewport.getAlignment().getAlignmentAnnotation()[i],
3033 new java.awt.Color(view.getAnnotationColours()
3034 .getMinColour()), new java.awt.Color(view
3035 .getAnnotationColours().getMaxColour()),
3036 view.getAnnotationColours().getAboveThreshold());
3038 else if (view.getAnnotationColours().getColourScheme()
3041 cs = new AnnotationColourGradient(
3042 af.viewport.getAlignment().getAlignmentAnnotation()[i],
3043 GetUserColourScheme(jms, view
3044 .getAnnotationColours().getColourScheme()),
3045 view.getAnnotationColours().getAboveThreshold());
3049 cs = new AnnotationColourGradient(
3050 af.viewport.getAlignment().getAlignmentAnnotation()[i],
3051 ColourSchemeProperty.getColour(al, view
3052 .getAnnotationColours().getColourScheme()),
3053 view.getAnnotationColours().getAboveThreshold());
3056 // Also use these settings for all the groups
3057 if (al.getGroups() != null)
3059 for (int g = 0; g < al.getGroups().size(); g++)
3061 jalview.datamodel.SequenceGroup sg = (jalview.datamodel.SequenceGroup) al
3062 .getGroups().elementAt(g);
3071 * (view.getAnnotationColours().getColourScheme().equals("None"
3072 * )) { sg.cs = new AnnotationColourGradient(
3073 * af.viewport.getAlignment().getAlignmentAnnotation()[i], new
3074 * java.awt.Color(view.getAnnotationColours().
3075 * getMinColour()), new
3076 * java.awt.Color(view.getAnnotationColours().
3078 * view.getAnnotationColours().getAboveThreshold()); } else
3081 sg.cs = new AnnotationColourGradient(
3082 af.viewport.getAlignment().getAlignmentAnnotation()[i],
3083 sg.cs, view.getAnnotationColours()
3084 .getAboveThreshold());
3098 cs = ColourSchemeProperty.getColour(al, view.getBgColour());
3103 cs.setThreshold(view.getPidThreshold(), true);
3104 cs.setConsensus(af.viewport.getSequenceConsensusHash());
3108 af.viewport.setGlobalColourScheme(cs);
3109 af.viewport.setColourAppliesToAllGroups(false);
3111 if (view.getConservationSelected() && cs != null)
3113 cs.setConservationInc(view.getConsThreshold());
3116 af.changeColour(cs);
3118 af.viewport.setColourAppliesToAllGroups(true);
3120 if (view.getShowSequenceFeatures())
3122 af.viewport.showSequenceFeatures = true;
3124 if (view.hasCentreColumnLabels())
3126 af.viewport.setCentreColumnLabels(view.getCentreColumnLabels());
3128 if (view.hasIgnoreGapsinConsensus())
3130 af.viewport.setIgnoreGapsConsensus(view
3131 .getIgnoreGapsinConsensus(), null);
3133 if (view.hasFollowHighlight())
3135 af.viewport.followHighlight = view.getFollowHighlight();
3137 if (view.hasFollowSelection())
3139 af.viewport.followSelection = view.getFollowSelection();
3141 if (view.hasShowConsensusHistogram())
3143 af.viewport.setShowConsensusHistogram(view
3144 .getShowConsensusHistogram());
3148 af.viewport.setShowConsensusHistogram(true);
3150 if (view.hasShowSequenceLogo())
3152 af.viewport.setShowSequenceLogo(view.getShowSequenceLogo());
3156 af.viewport.setShowSequenceLogo(false);
3158 if (view.hasShowDbRefTooltip())
3160 af.viewport.setShowDbRefs(view.getShowDbRefTooltip());
3162 if (view.hasShowNPfeatureTooltip())
3164 af.viewport.setShowNpFeats(view.hasShowNPfeatureTooltip());
3166 if (view.hasShowGroupConsensus())
3168 af.viewport.setShowGroupConsensus(view.getShowGroupConsensus());
3172 af.viewport.setShowGroupConsensus(false);
3174 if (view.hasShowGroupConservation())
3176 af.viewport.setShowGroupConservation(view.getShowGroupConservation());
3180 af.viewport.setShowGroupConservation(false);
3183 // recover featre settings
3184 if (jms.getFeatureSettings() != null)
3186 af.viewport.featuresDisplayed = new Hashtable();
3187 String[] renderOrder = new String[jms.getFeatureSettings()
3188 .getSettingCount()];
3189 for (int fs = 0; fs < jms.getFeatureSettings().getSettingCount(); fs++)
3191 Setting setting = jms.getFeatureSettings().getSetting(fs);
3192 if (setting.hasMincolour())
3194 GraduatedColor gc = setting.hasMin() ? new GraduatedColor(
3195 new java.awt.Color(setting.getMincolour()),
3196 new java.awt.Color(setting.getColour()),
3197 setting.getMin(), setting.getMax()) : new GraduatedColor(
3198 new java.awt.Color(setting.getMincolour()),
3199 new java.awt.Color(setting.getColour()), 0, 1);
3200 if (setting.hasThreshold())
3202 gc.setThresh(setting.getThreshold());
3203 gc.setThreshType(setting.getThreshstate());
3205 gc.setAutoScaled(true); // default
3206 if (setting.hasAutoScale())
3208 gc.setAutoScaled(setting.getAutoScale());
3210 if (setting.hasColourByLabel())
3212 gc.setColourByLabel(setting.getColourByLabel());
3214 // and put in the feature colour table.
3215 af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().setColour(
3216 setting.getType(), gc);
3220 af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().setColour(
3222 new java.awt.Color(setting.getColour()));
3224 renderOrder[fs] = setting.getType();
3225 if (setting.hasOrder())
3226 af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().setOrder(
3227 setting.getType(), setting.getOrder());
3229 af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().setOrder(
3231 fs / jms.getFeatureSettings().getSettingCount());
3232 if (setting.getDisplay())
3234 af.viewport.featuresDisplayed.put(setting.getType(), new Integer(
3235 setting.getColour()));
3238 af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().renderOrder = renderOrder;
3240 af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().featureGroups = fgtable = new Hashtable();
3241 for (int gs = 0; gs < jms.getFeatureSettings().getGroupCount(); gs++)
3243 Group grp = jms.getFeatureSettings().getGroup(gs);
3244 fgtable.put(grp.getName(), new Boolean(grp.getDisplay()));
3248 if (view.getHiddenColumnsCount() > 0)
3250 for (int c = 0; c < view.getHiddenColumnsCount(); c++)
3252 af.viewport.hideColumns(view.getHiddenColumns(c).getStart(), view
3253 .getHiddenColumns(c).getEnd() // +1
3258 af.setMenusFromViewport(af.viewport);
3259 // TODO: we don't need to do this if the viewport is aready visible.
3260 Desktop.addInternalFrame(af, view.getTitle(), view.getWidth(),
3262 af.alignPanel.updateAnnotation(false); // recompute any autoannotation
3263 reorderAutoannotation(af, al, autoAlan);
3267 private void reorderAutoannotation(AlignFrame af, Alignment al,
3268 ArrayList<JvAnnotRow> autoAlan)
3270 // copy over visualization settings for autocalculated annotation in the
3272 if (al.getAlignmentAnnotation() != null)
3275 * Kludge for magic autoannotation names (see JAL-811)
3277 String[] magicNames = new String[]
3278 { "Consensus", "Quality", "Conservation" };
3279 JvAnnotRow nullAnnot = new JvAnnotRow(-1, null);
3280 Hashtable<String, JvAnnotRow> visan = new Hashtable<String, JvAnnotRow>();
3281 for (String nm : magicNames)
3283 visan.put(nm, nullAnnot);
3285 for (JvAnnotRow auan : autoAlan)
3287 visan.put(auan.template.label, auan);
3289 int hSize = al.getAlignmentAnnotation().length;
3290 ArrayList<JvAnnotRow> reorder = new ArrayList<JvAnnotRow>();
3291 for (int h = 0; h < hSize; h++)
3293 jalview.datamodel.AlignmentAnnotation jalan = al
3294 .getAlignmentAnnotation()[h];
3295 if (jalan.autoCalculated)
3297 JvAnnotRow valan = visan.get(jalan.label);
3300 // delete the auto calculated row from the alignment
3301 al.deleteAnnotation(al.getAlignmentAnnotation()[h], false);
3304 if (valan != nullAnnot)
3306 if (jalan != valan.template)
3308 // newly created autoannotation row instance
3309 // so keep a reference to the visible annotation row
3310 // and copy over all relevant attributes
3311 if (valan.template.graphHeight >= 0)
3314 jalan.graphHeight = valan.template.graphHeight;
3316 jalan.visible = valan.template.visible;
3318 reorder.add(new JvAnnotRow(valan.order, jalan));
3323 int s = 0, srt[] = new int[reorder.size()];
3324 JvAnnotRow[] rws = new JvAnnotRow[reorder.size()];
3325 for (JvAnnotRow jvar : reorder)
3328 srt[s++] = jvar.order;
3331 jalview.util.QuickSort.sort(srt, rws);
3332 // and re-insert the annotation at its correct position
3333 for (JvAnnotRow jvar : rws)
3335 al.addAnnotation(jvar.template, jvar.order);
3337 af.alignPanel.adjustAnnotationHeight();
3341 Hashtable skipList = null;
3344 * TODO remove this method
3347 * @return AlignFrame bound to sequenceSetId from view, if one exists. private
3348 * AlignFrame getSkippedFrame(Viewport view) { if (skipList==null) {
3349 * throw new Error("Implementation Error. No skipList defined for this
3350 * Jalview2XML instance."); } return (AlignFrame)
3351 * skipList.get(view.getSequenceSetId()); }
3355 * Check if the Jalview view contained in object should be skipped or not.
3358 * @return true if view's sequenceSetId is a key in skipList
3360 private boolean skipViewport(JalviewModel object)
3362 if (skipList == null)
3367 if (skipList.containsKey(id = object.getJalviewModelSequence()
3368 .getViewport()[0].getSequenceSetId()))
3370 if (Cache.log != null && Cache.log.isDebugEnabled())
3372 Cache.log.debug("Skipping seuqence set id " + id);
3379 public void AddToSkipList(AlignFrame af)
3381 if (skipList == null)
3383 skipList = new Hashtable();
3385 skipList.put(af.getViewport().getSequenceSetId(), af);
3388 public void clearSkipList()
3390 if (skipList != null)
3397 private void recoverDatasetFor(SequenceSet vamsasSet, Alignment al)
3399 jalview.datamodel.Alignment ds = getDatasetFor(vamsasSet.getDatasetId());
3400 Vector dseqs = null;
3403 // create a list of new dataset sequences
3404 dseqs = new Vector();
3406 for (int i = 0, iSize = vamsasSet.getSequenceCount(); i < iSize; i++)
3408 Sequence vamsasSeq = vamsasSet.getSequence(i);
3409 ensureJalviewDatasetSequence(vamsasSeq, ds, dseqs);
3411 // create a new dataset
3414 SequenceI[] dsseqs = new SequenceI[dseqs.size()];
3415 dseqs.copyInto(dsseqs);
3416 ds = new jalview.datamodel.Alignment(dsseqs);
3417 debug("Created new dataset " + vamsasSet.getDatasetId()
3418 + " for alignment " + System.identityHashCode(al));
3419 addDatasetRef(vamsasSet.getDatasetId(), ds);
3421 // set the dataset for the newly imported alignment.
3422 if (al.getDataset() == null)
3431 * sequence definition to create/merge dataset sequence for
3435 * vector to add new dataset sequence to
3437 private void ensureJalviewDatasetSequence(Sequence vamsasSeq,
3438 AlignmentI ds, Vector dseqs)
3440 // JBP TODO: Check this is called for AlCodonFrames to support recovery of
3442 jalview.datamodel.Sequence sq = (jalview.datamodel.Sequence) seqRefIds
3443 .get(vamsasSeq.getId());
3444 jalview.datamodel.SequenceI dsq = null;
3445 if (sq != null && sq.getDatasetSequence() != null)
3447 dsq = (jalview.datamodel.SequenceI) sq.getDatasetSequence();
3450 String sqid = vamsasSeq.getDsseqid();
3453 // need to create or add a new dataset sequence reference to this sequence
3456 dsq = (jalview.datamodel.SequenceI) seqRefIds.get(sqid);
3461 // make a new dataset sequence
3462 dsq = sq.createDatasetSequence();
3465 // make up a new dataset reference for this sequence
3466 sqid = seqHash(dsq);
3468 dsq.setVamsasId(uniqueSetSuffix + sqid);
3469 seqRefIds.put(sqid, dsq);
3474 dseqs.addElement(dsq);
3479 ds.addSequence(dsq);
3485 { // make this dataset sequence sq's dataset sequence
3486 sq.setDatasetSequence(dsq);
3490 // TODO: refactor this as a merge dataset sequence function
3491 // now check that sq (the dataset sequence) sequence really is the union of
3492 // all references to it
3493 // boolean pre = sq.getStart() < dsq.getStart();
3494 // boolean post = sq.getEnd() > dsq.getEnd();
3498 StringBuffer sb = new StringBuffer();
3499 String newres = jalview.analysis.AlignSeq.extractGaps(
3500 jalview.util.Comparison.GapChars, sq.getSequenceAsString());
3501 if (!newres.equalsIgnoreCase(dsq.getSequenceAsString())
3502 && newres.length() > dsq.getLength())
3504 // Update with the longer sequence.
3508 * if (pre) { sb.insert(0, newres .substring(0, dsq.getStart() -
3509 * sq.getStart())); dsq.setStart(sq.getStart()); } if (post) {
3510 * sb.append(newres.substring(newres.length() - sq.getEnd() -
3511 * dsq.getEnd())); dsq.setEnd(sq.getEnd()); }
3513 dsq.setSequence(sb.toString());
3515 // TODO: merges will never happen if we 'know' we have the real dataset
3516 // sequence - this should be detected when id==dssid
3517 System.err.println("DEBUG Notice: Merged dataset sequence"); // ("
3518 // + (pre ? "prepended" : "") + " "
3519 // + (post ? "appended" : ""));
3524 java.util.Hashtable datasetIds = null;
3526 java.util.IdentityHashMap dataset2Ids = null;
3528 private Alignment getDatasetFor(String datasetId)
3530 if (datasetIds == null)
3532 datasetIds = new Hashtable();
3535 if (datasetIds.containsKey(datasetId))
3537 return (Alignment) datasetIds.get(datasetId);
3542 private void addDatasetRef(String datasetId, Alignment dataset)
3544 if (datasetIds == null)
3546 datasetIds = new Hashtable();
3548 datasetIds.put(datasetId, dataset);
3552 * make a new dataset ID for this jalview dataset alignment
3557 private String getDatasetIdRef(jalview.datamodel.Alignment dataset)
3559 if (dataset.getDataset() != null)
3561 warn("Serious issue! Dataset Object passed to getDatasetIdRef is not a Jalview DATASET alignment...");
3563 String datasetId = makeHashCode(dataset, null);
3564 if (datasetId == null)
3566 // make a new datasetId and record it
3567 if (dataset2Ids == null)
3569 dataset2Ids = new IdentityHashMap();
3573 datasetId = (String) dataset2Ids.get(dataset);
3575 if (datasetId == null)
3577 datasetId = "ds" + dataset2Ids.size() + 1;
3578 dataset2Ids.put(dataset, datasetId);
3584 private void addDBRefs(SequenceI datasetSequence, Sequence sequence)
3586 for (int d = 0; d < sequence.getDBRefCount(); d++)
3588 DBRef dr = sequence.getDBRef(d);
3589 jalview.datamodel.DBRefEntry entry = new jalview.datamodel.DBRefEntry(
3590 sequence.getDBRef(d).getSource(), sequence.getDBRef(d)
3591 .getVersion(), sequence.getDBRef(d).getAccessionId());
3592 if (dr.getMapping() != null)
3594 entry.setMap(addMapping(dr.getMapping()));
3596 datasetSequence.addDBRef(entry);
3600 private jalview.datamodel.Mapping addMapping(Mapping m)
3602 SequenceI dsto = null;
3603 // Mapping m = dr.getMapping();
3604 int fr[] = new int[m.getMapListFromCount() * 2];
3605 Enumeration f = m.enumerateMapListFrom();
3606 for (int _i = 0; f.hasMoreElements(); _i += 2)
3608 MapListFrom mf = (MapListFrom) f.nextElement();
3609 fr[_i] = mf.getStart();
3610 fr[_i + 1] = mf.getEnd();
3612 int fto[] = new int[m.getMapListToCount() * 2];
3613 f = m.enumerateMapListTo();
3614 for (int _i = 0; f.hasMoreElements(); _i += 2)
3616 MapListTo mf = (MapListTo) f.nextElement();
3617 fto[_i] = mf.getStart();
3618 fto[_i + 1] = mf.getEnd();
3620 jalview.datamodel.Mapping jmap = new jalview.datamodel.Mapping(dsto,
3621 fr, fto, (int) m.getMapFromUnit(), (int) m.getMapToUnit());
3622 if (m.getMappingChoice() != null)
3624 MappingChoice mc = m.getMappingChoice();
3625 if (mc.getDseqFor() != null)
3627 String dsfor = "" + mc.getDseqFor();
3628 if (seqRefIds.containsKey(dsfor))
3633 jmap.setTo((SequenceI) seqRefIds.get(dsfor));
3637 frefedSequence.add(new Object[]
3644 * local sequence definition
3646 Sequence ms = mc.getSequence();
3647 jalview.datamodel.Sequence djs = null;
3648 String sqid = ms.getDsseqid();
3649 if (sqid != null && sqid.length() > 0)
3652 * recover dataset sequence
3654 djs = (jalview.datamodel.Sequence) seqRefIds.get(sqid);
3659 .println("Warning - making up dataset sequence id for DbRef sequence map reference");
3660 sqid = ((Object) ms).toString(); // make up a new hascode for
3661 // undefined dataset sequence hash
3662 // (unlikely to happen)
3668 * make a new dataset sequence and add it to refIds hash
3670 djs = new jalview.datamodel.Sequence(ms.getName(),
3672 djs.setStart(jmap.getMap().getToLowest());
3673 djs.setEnd(jmap.getMap().getToHighest());
3674 djs.setVamsasId(uniqueSetSuffix + sqid);
3676 seqRefIds.put(sqid, djs);
3679 jalview.bin.Cache.log.debug("about to recurse on addDBRefs.");
3688 public jalview.gui.AlignmentPanel copyAlignPanel(AlignmentPanel ap,
3689 boolean keepSeqRefs)
3692 jalview.schemabinding.version2.JalviewModel jm = SaveState(ap, null,
3698 jm.getJalviewModelSequence().getViewport(0).setSequenceSetId(null);
3702 uniqueSetSuffix = "";
3703 jm.getJalviewModelSequence().getViewport(0).setId(null); // we don't
3708 if (this.frefedSequence == null)
3710 frefedSequence = new Vector();
3713 viewportsAdded = new Hashtable();
3715 AlignFrame af = LoadFromObject(jm, null, false, null);
3716 af.alignPanels.clear();
3717 af.closeMenuItem_actionPerformed(true);
3720 * if(ap.av.getAlignment().getAlignmentAnnotation()!=null) { for(int i=0;
3721 * i<ap.av.getAlignment().getAlignmentAnnotation().length; i++) {
3722 * if(!ap.av.getAlignment().getAlignmentAnnotation()[i].autoCalculated) {
3723 * af.alignPanel.av.getAlignment().getAlignmentAnnotation()[i] =
3724 * ap.av.getAlignment().getAlignmentAnnotation()[i]; } } }
3727 return af.alignPanel;
3731 * flag indicating if hashtables should be cleared on finalization TODO this
3732 * flag may not be necessary
3734 private boolean _cleartables = true;
3736 private Hashtable jvids2vobj;
3741 * @see java.lang.Object#finalize()
3743 protected void finalize() throws Throwable
3745 // really make sure we have no buried refs left.
3750 this.seqRefIds = null;
3751 this.seqsToIds = null;
3755 private void warn(String msg)
3760 private void warn(String msg, Exception e)
3762 if (Cache.log != null)
3766 Cache.log.warn(msg, e);
3770 Cache.log.warn(msg);
3775 System.err.println("Warning: " + msg);
3778 e.printStackTrace();
3783 private void debug(String string)
3785 debug(string, null);
3788 private void debug(String msg, Exception e)
3790 if (Cache.log != null)
3794 Cache.log.debug(msg, e);
3798 Cache.log.debug(msg);
3803 System.err.println("Warning: " + msg);
3806 e.printStackTrace();
3812 * set the object to ID mapping tables used to write/recover objects and XML
3813 * ID strings for the jalview project. If external tables are provided then
3814 * finalize and clearSeqRefs will not clear the tables when the Jalview2XML
3815 * object goes out of scope. - also populates the datasetIds hashtable with
3816 * alignment objects containing dataset sequences
3819 * Map from ID strings to jalview datamodel
3821 * Map from jalview datamodel to ID strings
3825 public void setObjectMappingTables(Hashtable vobj2jv,
3826 IdentityHashMap jv2vobj)
3828 this.jv2vobj = jv2vobj;
3829 this.vobj2jv = vobj2jv;
3830 Iterator ds = jv2vobj.keySet().iterator();
3832 while (ds.hasNext())
3834 Object jvobj = ds.next();
3835 id = jv2vobj.get(jvobj).toString();
3836 if (jvobj instanceof jalview.datamodel.Alignment)
3838 if (((jalview.datamodel.Alignment) jvobj).getDataset() == null)
3840 addDatasetRef(id, (jalview.datamodel.Alignment) jvobj);
3843 else if (jvobj instanceof jalview.datamodel.Sequence)
3845 // register sequence object so the XML parser can recover it.
3846 if (seqRefIds == null)
3848 seqRefIds = new Hashtable();
3850 if (seqsToIds == null)
3852 seqsToIds = new IdentityHashMap();
3854 seqRefIds.put(jv2vobj.get(jvobj).toString(), jvobj);
3855 seqsToIds.put(jvobj, id);
3857 else if (jvobj instanceof jalview.datamodel.AlignmentAnnotation)
3859 if (annotationIds == null)
3861 annotationIds = new Hashtable();
3864 annotationIds.put(anid = jv2vobj.get(jvobj).toString(), jvobj);
3865 jalview.datamodel.AlignmentAnnotation jvann = (jalview.datamodel.AlignmentAnnotation) jvobj;
3866 if (jvann.annotationId == null)
3868 jvann.annotationId = anid;
3870 if (!jvann.annotationId.equals(anid))
3872 // TODO verify that this is the correct behaviour
3873 this.warn("Overriding Annotation ID for " + anid
3874 + " from different id : " + jvann.annotationId);
3875 jvann.annotationId = anid;
3878 else if (jvobj instanceof String)
3880 if (jvids2vobj == null)
3882 jvids2vobj = new Hashtable();
3883 jvids2vobj.put(jvobj, jv2vobj.get(jvobj).toString());
3887 Cache.log.debug("Ignoring " + jvobj.getClass() + " (ID = " + id);
3892 * set the uniqueSetSuffix used to prefix/suffix object IDs for jalview
3893 * objects created from the project archive. If string is null (default for
3894 * construction) then suffix will be set automatically.
3898 public void setUniqueSetSuffix(String string)
3900 uniqueSetSuffix = string;
3905 * uses skipList2 as the skipList for skipping views on sequence sets
3906 * associated with keys in the skipList
3910 public void setSkipList(Hashtable skipList2)
3912 skipList = skipList2;