2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.project;
23 import static jalview.math.RotatableMatrix.Axis.X;
24 import static jalview.math.RotatableMatrix.Axis.Y;
25 import static jalview.math.RotatableMatrix.Axis.Z;
27 import java.awt.Color;
29 import java.awt.FontMetrics;
30 import java.awt.Rectangle;
31 import java.io.BufferedReader;
32 import java.io.ByteArrayInputStream;
34 import java.io.FileInputStream;
35 import java.io.FileOutputStream;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.io.InputStreamReader;
39 import java.io.OutputStream;
40 import java.io.OutputStreamWriter;
41 import java.io.PrintWriter;
42 import java.lang.reflect.InvocationTargetException;
43 import java.math.BigInteger;
44 import java.net.MalformedURLException;
46 import java.util.ArrayList;
47 import java.util.Arrays;
48 import java.util.Collections;
49 import java.util.Enumeration;
50 import java.util.GregorianCalendar;
51 import java.util.HashMap;
52 import java.util.HashSet;
53 import java.util.Hashtable;
54 import java.util.IdentityHashMap;
55 import java.util.Iterator;
56 import java.util.LinkedHashMap;
57 import java.util.List;
58 import java.util.Locale;
60 import java.util.Map.Entry;
62 import java.util.Vector;
63 import java.util.jar.JarEntry;
64 import java.util.jar.JarInputStream;
65 import java.util.jar.JarOutputStream;
67 import javax.swing.JInternalFrame;
68 import javax.swing.SwingUtilities;
69 import javax.xml.bind.JAXBContext;
70 import javax.xml.bind.JAXBElement;
71 import javax.xml.bind.Marshaller;
72 import javax.xml.datatype.DatatypeConfigurationException;
73 import javax.xml.datatype.DatatypeFactory;
74 import javax.xml.datatype.XMLGregorianCalendar;
75 import javax.xml.stream.XMLInputFactory;
76 import javax.xml.stream.XMLStreamReader;
78 import jalview.analysis.Conservation;
79 import jalview.analysis.PCA;
80 import jalview.analysis.scoremodels.ScoreModels;
81 import jalview.analysis.scoremodels.SimilarityParams;
82 import jalview.api.FeatureColourI;
83 import jalview.api.ViewStyleI;
84 import jalview.api.analysis.ScoreModelI;
85 import jalview.api.analysis.SimilarityParamsI;
86 import jalview.api.structures.JalviewStructureDisplayI;
87 import jalview.bin.Cache;
88 import jalview.bin.Console;
89 import jalview.datamodel.AlignedCodonFrame;
90 import jalview.datamodel.Alignment;
91 import jalview.datamodel.AlignmentAnnotation;
92 import jalview.datamodel.AlignmentI;
93 import jalview.datamodel.ContactMatrix;
94 import jalview.datamodel.ContactMatrixI;
95 import jalview.datamodel.DBRefEntry;
96 import jalview.datamodel.GeneLocus;
97 import jalview.datamodel.GraphLine;
98 import jalview.datamodel.PDBEntry;
99 import jalview.datamodel.Point;
100 import jalview.datamodel.RnaViewerModel;
101 import jalview.datamodel.SequenceFeature;
102 import jalview.datamodel.SequenceGroup;
103 import jalview.datamodel.SequenceI;
104 import jalview.datamodel.StructureViewerModel;
105 import jalview.datamodel.StructureViewerModel.StructureData;
106 import jalview.datamodel.features.FeatureMatcher;
107 import jalview.datamodel.features.FeatureMatcherI;
108 import jalview.datamodel.features.FeatureMatcherSet;
109 import jalview.datamodel.features.FeatureMatcherSetI;
110 import jalview.ext.varna.RnaModel;
111 import jalview.gui.AlignFrame;
112 import jalview.gui.AlignViewport;
113 import jalview.gui.AlignmentPanel;
114 import jalview.gui.AppVarna;
115 import jalview.gui.Desktop;
116 import jalview.gui.JvOptionPane;
117 import jalview.gui.OOMWarning;
118 import jalview.gui.OverviewPanel;
119 import jalview.gui.PCAPanel;
120 import jalview.gui.PaintRefresher;
121 import jalview.gui.SplitFrame;
122 import jalview.gui.StructureViewer;
123 import jalview.gui.StructureViewer.ViewerType;
124 import jalview.gui.StructureViewerBase;
125 import jalview.gui.TreePanel;
126 import jalview.io.BackupFiles;
127 import jalview.io.DataSourceType;
128 import jalview.io.FileFormat;
129 import jalview.io.NewickFile;
130 import jalview.math.Matrix;
131 import jalview.math.MatrixI;
132 import jalview.renderer.ResidueShaderI;
133 import jalview.schemes.AnnotationColourGradient;
134 import jalview.schemes.ColourSchemeI;
135 import jalview.schemes.ColourSchemeProperty;
136 import jalview.schemes.FeatureColour;
137 import jalview.schemes.ResidueProperties;
138 import jalview.schemes.UserColourScheme;
139 import jalview.structure.StructureSelectionManager;
140 import jalview.structures.models.AAStructureBindingModel;
141 import jalview.util.Format;
142 import jalview.util.HttpUtils;
143 import jalview.util.MessageManager;
144 import jalview.util.Platform;
145 import jalview.util.StringUtils;
146 import jalview.util.jarInputStreamProvider;
147 import jalview.util.matcher.Condition;
148 import jalview.viewmodel.AlignmentViewport;
149 import jalview.viewmodel.PCAModel;
150 import jalview.viewmodel.ViewportRanges;
151 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
152 import jalview.viewmodel.seqfeatures.FeatureRendererSettings;
153 import jalview.viewmodel.seqfeatures.FeaturesDisplayed;
154 import jalview.ws.datamodel.alphafold.PAEContactMatrix;
155 import jalview.ws.jws2.Jws2Discoverer;
156 import jalview.ws.jws2.dm.AAConSettings;
157 import jalview.ws.jws2.jabaws2.Jws2Instance;
158 import jalview.ws.params.ArgumentI;
159 import jalview.ws.params.AutoCalcSetting;
160 import jalview.ws.params.WsParamSetI;
161 import jalview.xml.binding.jalview.AlcodonFrame;
162 import jalview.xml.binding.jalview.AlcodonFrame.AlcodMap;
163 import jalview.xml.binding.jalview.Annotation;
164 import jalview.xml.binding.jalview.Annotation.ThresholdLine;
165 import jalview.xml.binding.jalview.AnnotationColourScheme;
166 import jalview.xml.binding.jalview.AnnotationElement;
167 import jalview.xml.binding.jalview.DoubleMatrix;
168 import jalview.xml.binding.jalview.DoubleVector;
169 import jalview.xml.binding.jalview.Feature;
170 import jalview.xml.binding.jalview.Feature.OtherData;
171 import jalview.xml.binding.jalview.FeatureMatcherSet.CompoundMatcher;
172 import jalview.xml.binding.jalview.FilterBy;
173 import jalview.xml.binding.jalview.JalviewModel;
174 import jalview.xml.binding.jalview.JalviewModel.FeatureSettings;
175 import jalview.xml.binding.jalview.JalviewModel.FeatureSettings.Group;
176 import jalview.xml.binding.jalview.JalviewModel.FeatureSettings.Setting;
177 import jalview.xml.binding.jalview.JalviewModel.JGroup;
178 import jalview.xml.binding.jalview.JalviewModel.JSeq;
179 import jalview.xml.binding.jalview.JalviewModel.JSeq.Pdbids;
180 import jalview.xml.binding.jalview.JalviewModel.JSeq.Pdbids.StructureState;
181 import jalview.xml.binding.jalview.JalviewModel.JSeq.RnaViewer;
182 import jalview.xml.binding.jalview.JalviewModel.JSeq.RnaViewer.SecondaryStructure;
183 import jalview.xml.binding.jalview.JalviewModel.PcaViewer;
184 import jalview.xml.binding.jalview.JalviewModel.PcaViewer.Axis;
185 import jalview.xml.binding.jalview.JalviewModel.PcaViewer.SeqPointMax;
186 import jalview.xml.binding.jalview.JalviewModel.PcaViewer.SeqPointMin;
187 import jalview.xml.binding.jalview.JalviewModel.PcaViewer.SequencePoint;
188 import jalview.xml.binding.jalview.JalviewModel.Tree;
189 import jalview.xml.binding.jalview.JalviewModel.UserColours;
190 import jalview.xml.binding.jalview.JalviewModel.Viewport;
191 import jalview.xml.binding.jalview.JalviewModel.Viewport.CalcIdParam;
192 import jalview.xml.binding.jalview.JalviewModel.Viewport.HiddenColumns;
193 import jalview.xml.binding.jalview.JalviewModel.Viewport.Overview;
194 import jalview.xml.binding.jalview.JalviewUserColours;
195 import jalview.xml.binding.jalview.JalviewUserColours.Colour;
196 import jalview.xml.binding.jalview.MapListType.MapListFrom;
197 import jalview.xml.binding.jalview.MapListType.MapListTo;
198 import jalview.xml.binding.jalview.Mapping;
199 import jalview.xml.binding.jalview.MatrixType;
200 import jalview.xml.binding.jalview.NoValueColour;
201 import jalview.xml.binding.jalview.ObjectFactory;
202 import jalview.xml.binding.jalview.PcaDataType;
203 import jalview.xml.binding.jalview.Pdbentry.Property;
204 import jalview.xml.binding.jalview.Sequence;
205 import jalview.xml.binding.jalview.Sequence.DBRef;
206 import jalview.xml.binding.jalview.SequenceSet;
207 import jalview.xml.binding.jalview.SequenceSet.SequenceSetProperties;
208 import jalview.xml.binding.jalview.ThresholdType;
209 import jalview.xml.binding.jalview.VAMSAS;
212 * Write out the current jalview desktop state as a Jalview XML stream.
214 * Note: the vamsas objects referred to here are primitive versions of the
215 * VAMSAS project schema elements - they are not the same and most likely never
219 * @version $Revision: 1.134 $
221 public class Jalview2XML
224 // BH 2018 we add the .jvp binary extension to J2S so that
225 // it will declare that binary when we do the file save from the browser
229 Platform.addJ2SBinaryType(".jvp?");
232 private static final String VIEWER_PREFIX = "viewer_";
234 private static final String RNA_PREFIX = "rna_";
236 private static final String UTF_8 = "UTF-8";
239 * used in decision if quit confirmation should be issued
241 private static boolean stateSavedUpToDate = false;
244 * prefix for recovering datasets for alignments with multiple views where
245 * non-existent dataset IDs were written for some views
247 private static final String UNIQSEQSETID = "uniqueSeqSetId.";
249 // use this with nextCounter() to make unique names for entities
250 private int counter = 0;
253 * SequenceI reference -> XML ID string in jalview XML. Populated as XML reps
254 * of sequence objects are created.
256 IdentityHashMap<SequenceI, String> seqsToIds = null;
259 * jalview XML Sequence ID to jalview sequence object reference (both dataset
260 * and alignment sequences. Populated as XML reps of sequence objects are
263 Map<String, SequenceI> seqRefIds = null;
265 Map<String, SequenceI> incompleteSeqs = null;
267 List<SeqFref> frefedSequence = null;
269 boolean raiseGUI = true; // whether errors are raised in dialog boxes or not
272 * Map of reconstructed AlignFrame objects that appear to have come from
273 * SplitFrame objects (have a dna/protein complement view).
275 private Map<Viewport, AlignFrame> splitFrameCandidates = new HashMap<>();
278 * Map from displayed rna structure models to their saved session state jar
281 private Map<RnaModel, String> rnaSessions = new HashMap<>();
284 * A helper method for safely using the value of an optional attribute that
285 * may be null if not present in the XML. Answers the boolean value, or false
291 public static boolean safeBoolean(Boolean b)
293 return b == null ? false : b.booleanValue();
297 * A helper method for safely using the value of an optional attribute that
298 * may be null if not present in the XML. Answers the integer value, or zero
304 public static int safeInt(Integer i)
306 return i == null ? 0 : i.intValue();
310 * A helper method for safely using the value of an optional attribute that
311 * may be null if not present in the XML. Answers the float value, or zero if
317 public static float safeFloat(Float f)
319 return f == null ? 0f : f.floatValue();
323 * create/return unique hash string for sq
326 * @return new or existing unique string for sq
328 String seqHash(SequenceI sq)
330 if (seqsToIds == null)
334 if (seqsToIds.containsKey(sq))
336 return seqsToIds.get(sq);
340 // create sequential key
341 String key = "sq" + (seqsToIds.size() + 1);
342 key = makeHashCode(sq, key); // check we don't have an external reference
344 seqsToIds.put(sq, key);
351 if (seqsToIds == null)
353 seqsToIds = new IdentityHashMap<>();
355 if (seqRefIds == null)
357 seqRefIds = new HashMap<>();
359 if (incompleteSeqs == null)
361 incompleteSeqs = new HashMap<>();
363 if (frefedSequence == null)
365 frefedSequence = new ArrayList<>();
373 public Jalview2XML(boolean raiseGUI)
375 this.raiseGUI = raiseGUI;
379 * base class for resolving forward references to sequences by their ID
384 abstract class SeqFref
390 public SeqFref(String _sref, String type)
396 public String getSref()
401 public SequenceI getSrefSeq()
403 return seqRefIds.get(sref);
406 public boolean isResolvable()
408 return seqRefIds.get(sref) != null;
411 public SequenceI getSrefDatasetSeq()
413 SequenceI sq = seqRefIds.get(sref);
416 while (sq.getDatasetSequence() != null)
418 sq = sq.getDatasetSequence();
425 * @return true if the forward reference was fully resolved
427 abstract boolean resolve();
430 public String toString()
432 return type + " reference to " + sref;
437 * create forward reference for a mapping
443 public SeqFref newMappingRef(final String sref,
444 final jalview.datamodel.Mapping _jmap)
446 SeqFref fref = new SeqFref(sref, "Mapping")
448 public jalview.datamodel.Mapping jmap = _jmap;
453 SequenceI seq = getSrefDatasetSeq();
465 public SeqFref newAlcodMapRef(final String sref,
466 final AlignedCodonFrame _cf,
467 final jalview.datamodel.Mapping _jmap)
470 SeqFref fref = new SeqFref(sref, "Codon Frame")
472 AlignedCodonFrame cf = _cf;
474 public jalview.datamodel.Mapping mp = _jmap;
477 public boolean isResolvable()
479 return super.isResolvable() && mp.getTo() != null;
485 SequenceI seq = getSrefDatasetSeq();
490 cf.addMap(seq, mp.getTo(), mp.getMap());
497 public void resolveFrefedSequences()
499 Iterator<SeqFref> nextFref = frefedSequence.iterator();
500 int toresolve = frefedSequence.size();
501 int unresolved = 0, failedtoresolve = 0;
502 while (nextFref.hasNext())
504 SeqFref ref = nextFref.next();
505 if (ref.isResolvable())
517 } catch (Exception x)
520 "IMPLEMENTATION ERROR: Failed to resolve forward reference for sequence "
533 System.err.println("Jalview Project Import: There were " + unresolved
534 + " forward references left unresolved on the stack.");
536 if (failedtoresolve > 0)
538 System.err.println("SERIOUS! " + failedtoresolve
539 + " resolvable forward references failed to resolve.");
541 if (incompleteSeqs != null && incompleteSeqs.size() > 0)
544 "Jalview Project Import: There are " + incompleteSeqs.size()
545 + " sequences which may have incomplete metadata.");
546 if (incompleteSeqs.size() < 10)
548 for (SequenceI s : incompleteSeqs.values())
550 System.err.println(s.toString());
556 "Too many to report. Skipping output of incomplete sequences.");
562 * This maintains a map of viewports, the key being the seqSetId. Important to
563 * set historyItem and redoList for multiple views
565 Map<String, AlignViewport> viewportsAdded = new HashMap<>();
567 Map<String, AlignmentAnnotation> annotationIds = new HashMap<>();
569 String uniqueSetSuffix = "";
572 * List of pdbfiles added to Jar
574 List<String> pdbfiles = null;
576 // SAVES SEVERAL ALIGNMENT WINDOWS TO SAME JARFILE
577 public void saveState(File statefile)
579 FileOutputStream fos = null;
584 fos = new FileOutputStream(statefile);
586 JarOutputStream jout = new JarOutputStream(fos);
590 } catch (Exception e)
592 Console.error("Couln't write Jalview state to " + statefile, e);
593 // TODO: inform user of the problem - they need to know if their data was
595 if (errorMessage == null)
597 errorMessage = "Did't write Jalview Archive to output file '"
598 + statefile + "' - See console error log for details";
602 errorMessage += "(Didn't write Jalview Archive to output file '"
613 } catch (IOException e)
623 * Writes a jalview project archive to the given Jar output stream.
627 public void saveState(JarOutputStream jout)
629 AlignFrame[] frames = Desktop.getAlignFrames();
631 setStateSavedUpToDate(true);
633 if (Cache.getDefault("DEBUG_DELAY_SAVE", false))
635 int n = debugDelaySave;
639 Console.debug("***** debugging save sleep " + i + "/" + n);
643 } catch (InterruptedException e)
645 // TODO Auto-generated catch block
656 saveAllFrames(Arrays.asList(frames), jout);
660 * core method for storing state for a set of AlignFrames.
663 * - frames involving all data to be exported (including containing
666 * - project output stream
668 private void saveAllFrames(List<AlignFrame> frames, JarOutputStream jout)
670 Hashtable<String, AlignFrame> dsses = new Hashtable<>();
673 * ensure cached data is clear before starting
675 // todo tidy up seqRefIds, seqsToIds initialisation / reset
677 splitFrameCandidates.clear();
682 // NOTE UTF-8 MUST BE USED FOR WRITING UNICODE CHARS
683 // //////////////////////////////////////////////////
685 List<String> shortNames = new ArrayList<>();
686 List<String> viewIds = new ArrayList<>();
689 for (int i = frames.size() - 1; i > -1; i--)
691 AlignFrame af = frames.get(i);
693 if (skipList != null && skipList
694 .containsKey(af.getViewport().getSequenceSetId()))
699 String shortName = makeFilename(af, shortNames);
701 int apSize = af.getAlignPanels().size();
703 for (int ap = 0; ap < apSize; ap++)
705 AlignmentPanel apanel = (AlignmentPanel) af.getAlignPanels()
707 String fileName = apSize == 1 ? shortName : ap + shortName;
708 if (!fileName.endsWith(".xml"))
710 fileName = fileName + ".xml";
713 saveState(apanel, fileName, jout, viewIds);
715 String dssid = getDatasetIdRef(
716 af.getViewport().getAlignment().getDataset());
717 if (!dsses.containsKey(dssid))
719 dsses.put(dssid, af);
724 writeDatasetFor(dsses, "" + jout.hashCode() + " " + uniqueSetSuffix,
730 } catch (Exception foo)
734 } catch (Exception ex)
736 // TODO: inform user of the problem - they need to know if their data was
738 if (errorMessage == null)
740 errorMessage = "Couldn't write Jalview Archive - see error output for details";
742 ex.printStackTrace();
747 * Generates a distinct file name, based on the title of the AlignFrame, by
748 * appending _n for increasing n until an unused name is generated. The new
749 * name (without its extension) is added to the list.
753 * @return the generated name, with .xml extension
755 protected String makeFilename(AlignFrame af, List<String> namesUsed)
757 String shortName = af.getTitle();
759 if (shortName.indexOf(File.separatorChar) > -1)
761 shortName = shortName
762 .substring(shortName.lastIndexOf(File.separatorChar) + 1);
767 while (namesUsed.contains(shortName))
769 if (shortName.endsWith("_" + (count - 1)))
771 shortName = shortName.substring(0, shortName.lastIndexOf("_"));
774 shortName = shortName.concat("_" + count);
778 namesUsed.add(shortName);
780 if (!shortName.endsWith(".xml"))
782 shortName = shortName + ".xml";
787 // USE THIS METHOD TO SAVE A SINGLE ALIGNMENT WINDOW
788 public boolean saveAlignment(AlignFrame af, String jarFile,
793 // create backupfiles object and get new temp filename destination
794 boolean doBackup = BackupFiles.getEnabled();
795 BackupFiles backupfiles = doBackup ? new BackupFiles(jarFile) : null;
796 FileOutputStream fos = new FileOutputStream(
797 doBackup ? backupfiles.getTempFilePath() : jarFile);
799 if (Cache.getDefault("DEBUG_DELAY_SAVE", false))
801 int n = debugDelaySave;
805 Console.debug("***** debugging save sleep " + i + "/" + n);
809 } catch (InterruptedException e)
811 // TODO Auto-generated catch block
818 JarOutputStream jout = new JarOutputStream(fos);
819 List<AlignFrame> frames = new ArrayList<>();
821 // resolve splitframes
822 if (af.getViewport().getCodingComplement() != null)
824 frames = ((SplitFrame) af.getSplitViewContainer()).getAlignFrames();
830 saveAllFrames(frames, jout);
834 } catch (Exception foo)
838 boolean success = true;
842 backupfiles.setWriteSuccess(success);
843 success = backupfiles.rollBackupsAndRenameTempFile();
847 } catch (Exception ex)
849 errorMessage = "Couldn't Write alignment view to Jalview Archive - see error output for details";
850 ex.printStackTrace();
855 private void writeDatasetFor(Hashtable<String, AlignFrame> dsses,
856 String fileName, JarOutputStream jout)
859 for (String dssids : dsses.keySet())
861 AlignFrame _af = dsses.get(dssids);
862 String jfileName = fileName + " Dataset for " + _af.getTitle();
863 if (!jfileName.endsWith(".xml"))
865 jfileName = jfileName + ".xml";
867 saveState(_af.alignPanel, jfileName, true, jout, null);
872 * create a JalviewModel from an alignment view and marshall it to a
876 * panel to create jalview model for
878 * name of alignment panel written to output stream
885 public JalviewModel saveState(AlignmentPanel ap, String fileName,
886 JarOutputStream jout, List<String> viewIds)
888 return saveState(ap, fileName, false, jout, viewIds);
892 * create a JalviewModel from an alignment view and marshall it to a
896 * panel to create jalview model for
898 * name of alignment panel written to output stream
900 * when true, only write the dataset for the alignment, not the data
901 * associated with the view.
907 public JalviewModel saveState(AlignmentPanel ap, String fileName,
908 boolean storeDS, JarOutputStream jout, List<String> viewIds)
912 viewIds = new ArrayList<>();
917 List<UserColourScheme> userColours = new ArrayList<>();
919 AlignViewport av = ap.av;
920 ViewportRanges vpRanges = av.getRanges();
922 final ObjectFactory objectFactory = new ObjectFactory();
923 JalviewModel object = objectFactory.createJalviewModel();
924 object.setVamsasModel(new VAMSAS());
926 // object.setCreationDate(new java.util.Date(System.currentTimeMillis()));
929 GregorianCalendar c = new GregorianCalendar();
930 DatatypeFactory datatypeFactory = DatatypeFactory.newInstance();
931 XMLGregorianCalendar now = datatypeFactory.newXMLGregorianCalendar(c);// gregorianCalendar);
932 object.setCreationDate(now);
933 } catch (DatatypeConfigurationException e)
935 System.err.println("error writing date: " + e.toString());
937 object.setVersion(Cache.getDefault("VERSION", "Development Build"));
940 * rjal is full height alignment, jal is actual alignment with full metadata
941 * but excludes hidden sequences.
943 jalview.datamodel.AlignmentI rjal = av.getAlignment(), jal = rjal;
945 if (av.hasHiddenRows())
947 rjal = jal.getHiddenSequences().getFullAlignment();
950 SequenceSet vamsasSet = new SequenceSet();
952 // JalviewModelSequence jms = new JalviewModelSequence();
954 vamsasSet.setGapChar(jal.getGapCharacter() + "");
956 if (jal.getDataset() != null)
958 // dataset id is the dataset's hashcode
959 vamsasSet.setDatasetId(getDatasetIdRef(jal.getDataset()));
962 // switch jal and the dataset
963 jal = jal.getDataset();
967 if (jal.getProperties() != null)
969 Enumeration en = jal.getProperties().keys();
970 while (en.hasMoreElements())
972 String key = en.nextElement().toString();
973 SequenceSetProperties ssp = new SequenceSetProperties();
975 ssp.setValue(jal.getProperties().get(key).toString());
976 // vamsasSet.addSequenceSetProperties(ssp);
977 vamsasSet.getSequenceSetProperties().add(ssp);
982 Set<String> calcIdSet = new HashSet<>();
983 // record the set of vamsas sequence XML POJO we create.
984 HashMap<String, Sequence> vamsasSetIds = new HashMap<>();
986 for (final SequenceI jds : rjal.getSequences())
988 final SequenceI jdatasq = jds.getDatasetSequence() == null ? jds
989 : jds.getDatasetSequence();
990 String id = seqHash(jds);
991 if (vamsasSetIds.get(id) == null)
993 if (seqRefIds.get(id) != null && !storeDS)
995 // This happens for two reasons: 1. multiple views are being
997 // 2. the hashCode has collided with another sequence's code. This
999 // HAPPEN! (PF00072.15.stk does this)
1000 // JBPNote: Uncomment to debug writing out of files that do not read
1001 // back in due to ArrayOutOfBoundExceptions.
1002 // System.err.println("vamsasSeq backref: "+id+"");
1003 // System.err.println(jds.getName()+"
1004 // "+jds.getStart()+"-"+jds.getEnd()+" "+jds.getSequenceAsString());
1005 // System.err.println("Hashcode: "+seqHash(jds));
1006 // SequenceI rsq = (SequenceI) seqRefIds.get(id + "");
1007 // System.err.println(rsq.getName()+"
1008 // "+rsq.getStart()+"-"+rsq.getEnd()+" "+rsq.getSequenceAsString());
1009 // System.err.println("Hashcode: "+seqHash(rsq));
1013 vamsasSeq = createVamsasSequence(id, jds);
1014 // vamsasSet.addSequence(vamsasSeq);
1015 vamsasSet.getSequence().add(vamsasSeq);
1016 vamsasSetIds.put(id, vamsasSeq);
1017 seqRefIds.put(id, jds);
1021 jseq.setStart(jds.getStart());
1022 jseq.setEnd(jds.getEnd());
1023 jseq.setColour(av.getSequenceColour(jds).getRGB());
1025 jseq.setId(id); // jseq id should be a string not a number
1028 // Store any sequences this sequence represents
1029 if (av.hasHiddenRows())
1031 // use rjal, contains the full height alignment
1033 av.getAlignment().getHiddenSequences().isHidden(jds));
1035 if (av.isHiddenRepSequence(jds))
1037 jalview.datamodel.SequenceI[] reps = av
1038 .getRepresentedSequences(jds).getSequencesInOrder(rjal);
1040 for (int h = 0; h < reps.length; h++)
1044 // jseq.addHiddenSequences(rjal.findIndex(reps[h]));
1045 jseq.getHiddenSequences().add(rjal.findIndex(reps[h]));
1050 // mark sequence as reference - if it is the reference for this view
1051 if (jal.hasSeqrep())
1053 jseq.setViewreference(jds == jal.getSeqrep());
1057 // TODO: omit sequence features from each alignment view's XML dump if we
1058 // are storing dataset
1059 List<SequenceFeature> sfs = jds.getSequenceFeatures();
1060 for (SequenceFeature sf : sfs)
1062 // Features features = new Features();
1063 Feature features = new Feature();
1065 features.setBegin(sf.getBegin());
1066 features.setEnd(sf.getEnd());
1067 features.setDescription(sf.getDescription());
1068 features.setType(sf.getType());
1069 features.setFeatureGroup(sf.getFeatureGroup());
1070 features.setScore(sf.getScore());
1071 if (sf.links != null)
1073 for (int l = 0; l < sf.links.size(); l++)
1075 OtherData keyValue = new OtherData();
1076 keyValue.setKey("LINK_" + l);
1077 keyValue.setValue(sf.links.elementAt(l).toString());
1078 // features.addOtherData(keyValue);
1079 features.getOtherData().add(keyValue);
1082 if (sf.otherDetails != null)
1085 * save feature attributes, which may be simple strings or
1086 * map valued (have sub-attributes)
1088 for (Entry<String, Object> entry : sf.otherDetails.entrySet())
1090 String key = entry.getKey();
1091 Object value = entry.getValue();
1092 if (value instanceof Map<?, ?>)
1094 for (Entry<String, Object> subAttribute : ((Map<String, Object>) value)
1097 OtherData otherData = new OtherData();
1098 otherData.setKey(key);
1099 otherData.setKey2(subAttribute.getKey());
1100 otherData.setValue(subAttribute.getValue().toString());
1101 // features.addOtherData(otherData);
1102 features.getOtherData().add(otherData);
1107 OtherData otherData = new OtherData();
1108 otherData.setKey(key);
1109 otherData.setValue(value.toString());
1110 // features.addOtherData(otherData);
1111 features.getOtherData().add(otherData);
1116 // jseq.addFeatures(features);
1117 jseq.getFeatures().add(features);
1120 if (jdatasq.getAllPDBEntries() != null)
1122 Enumeration<PDBEntry> en = jdatasq.getAllPDBEntries().elements();
1123 while (en.hasMoreElements())
1125 Pdbids pdb = new Pdbids();
1126 jalview.datamodel.PDBEntry entry = en.nextElement();
1128 String pdbId = entry.getId();
1130 pdb.setType(entry.getType());
1133 * Store any structure views associated with this sequence. This
1134 * section copes with duplicate entries in the project, so a dataset
1135 * only view *should* be coped with sensibly.
1137 // This must have been loaded, is it still visible?
1138 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
1139 String matchedFile = null;
1140 for (int f = frames.length - 1; f > -1; f--)
1142 if (frames[f] instanceof StructureViewerBase)
1144 StructureViewerBase viewFrame = (StructureViewerBase) frames[f];
1145 matchedFile = saveStructureViewer(ap, jds, pdb, entry,
1146 viewIds, matchedFile, viewFrame);
1148 * Only store each structure viewer's state once in the project
1149 * jar. First time through only (storeDS==false)
1151 String viewId = viewFrame.getViewId();
1152 String viewerType = viewFrame.getViewerType().toString();
1153 if (!storeDS && !viewIds.contains(viewId))
1155 viewIds.add(viewId);
1156 File viewerState = viewFrame.saveSession();
1157 if (viewerState != null)
1159 copyFileToJar(jout, viewerState.getPath(),
1160 getViewerJarEntryName(viewId), viewerType);
1165 "Failed to save viewer state for " + viewerType);
1171 if (matchedFile != null || entry.getFile() != null)
1173 if (entry.getFile() != null)
1176 matchedFile = entry.getFile();
1178 pdb.setFile(matchedFile); // entry.getFile());
1179 if (pdbfiles == null)
1181 pdbfiles = new ArrayList<>();
1184 if (!pdbfiles.contains(pdbId))
1186 pdbfiles.add(pdbId);
1187 copyFileToJar(jout, matchedFile, pdbId, pdbId);
1191 Enumeration<String> props = entry.getProperties();
1192 if (props.hasMoreElements())
1194 // PdbentryItem item = new PdbentryItem();
1195 while (props.hasMoreElements())
1197 Property prop = new Property();
1198 String key = props.nextElement();
1200 prop.setValue(entry.getProperty(key).toString());
1201 // item.addProperty(prop);
1202 pdb.getProperty().add(prop);
1204 // pdb.addPdbentryItem(item);
1207 // jseq.addPdbids(pdb);
1208 jseq.getPdbids().add(pdb);
1212 saveRnaViewers(jout, jseq, jds, viewIds, ap, storeDS);
1214 // jms.addJSeq(jseq);
1215 object.getJSeq().add(jseq);
1218 if (!storeDS && av.hasHiddenRows())
1220 jal = av.getAlignment();
1224 if (storeDS && jal.getCodonFrames() != null)
1226 List<AlignedCodonFrame> jac = jal.getCodonFrames();
1227 for (AlignedCodonFrame acf : jac)
1229 AlcodonFrame alc = new AlcodonFrame();
1230 if (acf.getProtMappings() != null
1231 && acf.getProtMappings().length > 0)
1233 boolean hasMap = false;
1234 SequenceI[] dnas = acf.getdnaSeqs();
1235 jalview.datamodel.Mapping[] pmaps = acf.getProtMappings();
1236 for (int m = 0; m < pmaps.length; m++)
1238 AlcodMap alcmap = new AlcodMap();
1239 alcmap.setDnasq(seqHash(dnas[m]));
1241 createVamsasMapping(pmaps[m], dnas[m], null, false));
1242 // alc.addAlcodMap(alcmap);
1243 alc.getAlcodMap().add(alcmap);
1248 // vamsasSet.addAlcodonFrame(alc);
1249 vamsasSet.getAlcodonFrame().add(alc);
1252 // TODO: delete this ? dead code from 2.8.3->2.9 ?
1254 // AlcodonFrame alc = new AlcodonFrame();
1255 // vamsasSet.addAlcodonFrame(alc);
1256 // for (int p = 0; p < acf.aaWidth; p++)
1258 // Alcodon cmap = new Alcodon();
1259 // if (acf.codons[p] != null)
1261 // // Null codons indicate a gapped column in the translated peptide
1263 // cmap.setPos1(acf.codons[p][0]);
1264 // cmap.setPos2(acf.codons[p][1]);
1265 // cmap.setPos3(acf.codons[p][2]);
1267 // alc.addAlcodon(cmap);
1269 // if (acf.getProtMappings() != null
1270 // && acf.getProtMappings().length > 0)
1272 // SequenceI[] dnas = acf.getdnaSeqs();
1273 // jalview.datamodel.Mapping[] pmaps = acf.getProtMappings();
1274 // for (int m = 0; m < pmaps.length; m++)
1276 // AlcodMap alcmap = new AlcodMap();
1277 // alcmap.setDnasq(seqHash(dnas[m]));
1278 // alcmap.setMapping(createVamsasMapping(pmaps[m], dnas[m], null,
1280 // alc.addAlcodMap(alcmap);
1287 // /////////////////////////////////
1288 if (!storeDS && av.getCurrentTree() != null)
1290 // FIND ANY ASSOCIATED TREES
1291 // NOT IMPLEMENTED FOR HEADLESS STATE AT PRESENT
1292 if (Desktop.desktop != null)
1294 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
1296 for (int t = 0; t < frames.length; t++)
1298 if (frames[t] instanceof TreePanel)
1300 TreePanel tp = (TreePanel) frames[t];
1302 if (tp.getTreeCanvas().getViewport().getAlignment() == jal)
1304 JalviewModel.Tree tree = new JalviewModel.Tree();
1305 tree.setTitle(tp.getTitle());
1306 tree.setCurrentTree((av.getCurrentTree() == tp.getTree()));
1307 tree.setNewick(tp.getTree().print());
1308 tree.setThreshold(tp.getTreeCanvas().getThreshold());
1310 tree.setFitToWindow(tp.fitToWindow.getState());
1311 tree.setFontName(tp.getTreeFont().getName());
1312 tree.setFontSize(tp.getTreeFont().getSize());
1313 tree.setFontStyle(tp.getTreeFont().getStyle());
1314 tree.setMarkUnlinked(tp.placeholdersMenu.getState());
1316 tree.setShowBootstrap(tp.bootstrapMenu.getState());
1317 tree.setShowDistances(tp.distanceMenu.getState());
1319 tree.setHeight(tp.getHeight());
1320 tree.setWidth(tp.getWidth());
1321 tree.setXpos(tp.getX());
1322 tree.setYpos(tp.getY());
1323 tree.setId(makeHashCode(tp, null));
1324 tree.setLinkToAllViews(
1325 tp.getTreeCanvas().isApplyToAllViews());
1327 // jms.addTree(tree);
1328 object.getTree().add(tree);
1338 if (!storeDS && Desktop.desktop != null)
1340 for (JInternalFrame frame : Desktop.desktop.getAllFrames())
1342 if (frame instanceof PCAPanel)
1344 PCAPanel panel = (PCAPanel) frame;
1345 if (panel.getAlignViewport().getAlignment() == jal)
1347 savePCA(panel, object);
1355 * store forward refs from an annotationRow to any groups
1357 IdentityHashMap<SequenceGroup, String> groupRefs = new IdentityHashMap<>();
1360 for (SequenceI sq : jal.getSequences())
1362 // Store annotation on dataset sequences only
1363 AlignmentAnnotation[] aa = sq.getAnnotation();
1364 if (aa != null && aa.length > 0)
1366 storeAlignmentAnnotation(aa, groupRefs, av, calcIdSet, storeDS,
1373 if (jal.getAlignmentAnnotation() != null)
1375 // Store the annotation shown on the alignment.
1376 AlignmentAnnotation[] aa = jal.getAlignmentAnnotation();
1377 storeAlignmentAnnotation(aa, groupRefs, av, calcIdSet, storeDS,
1382 if (jal.getGroups() != null)
1384 JGroup[] groups = new JGroup[jal.getGroups().size()];
1386 for (jalview.datamodel.SequenceGroup sg : jal.getGroups())
1388 JGroup jGroup = new JGroup();
1389 groups[++i] = jGroup;
1391 jGroup.setStart(sg.getStartRes());
1392 jGroup.setEnd(sg.getEndRes());
1393 jGroup.setName(sg.getName());
1394 if (groupRefs.containsKey(sg))
1396 // group has references so set its ID field
1397 jGroup.setId(groupRefs.get(sg));
1399 ColourSchemeI colourScheme = sg.getColourScheme();
1400 if (colourScheme != null)
1402 ResidueShaderI groupColourScheme = sg.getGroupColourScheme();
1403 if (groupColourScheme.conservationApplied())
1405 jGroup.setConsThreshold(groupColourScheme.getConservationInc());
1407 if (colourScheme instanceof jalview.schemes.UserColourScheme)
1409 jGroup.setColour(setUserColourScheme(colourScheme,
1410 userColours, object));
1414 jGroup.setColour(colourScheme.getSchemeName());
1417 else if (colourScheme instanceof jalview.schemes.AnnotationColourGradient)
1419 jGroup.setColour("AnnotationColourGradient");
1420 jGroup.setAnnotationColours(constructAnnotationColours(
1421 (jalview.schemes.AnnotationColourGradient) colourScheme,
1422 userColours, object));
1424 else if (colourScheme instanceof jalview.schemes.UserColourScheme)
1427 setUserColourScheme(colourScheme, userColours, object));
1431 jGroup.setColour(colourScheme.getSchemeName());
1434 jGroup.setPidThreshold(groupColourScheme.getThreshold());
1437 jGroup.setOutlineColour(sg.getOutlineColour().getRGB());
1438 jGroup.setDisplayBoxes(sg.getDisplayBoxes());
1439 jGroup.setDisplayText(sg.getDisplayText());
1440 jGroup.setColourText(sg.getColourText());
1441 jGroup.setTextCol1(sg.textColour.getRGB());
1442 jGroup.setTextCol2(sg.textColour2.getRGB());
1443 jGroup.setTextColThreshold(sg.thresholdTextColour);
1444 jGroup.setShowUnconserved(sg.getShowNonconserved());
1445 jGroup.setIgnoreGapsinConsensus(sg.getIgnoreGapsConsensus());
1446 jGroup.setShowConsensusHistogram(sg.isShowConsensusHistogram());
1447 jGroup.setShowSequenceLogo(sg.isShowSequenceLogo());
1448 jGroup.setNormaliseSequenceLogo(sg.isNormaliseSequenceLogo());
1449 for (SequenceI seq : sg.getSequences())
1451 // jGroup.addSeq(seqHash(seq));
1452 jGroup.getSeq().add(seqHash(seq));
1456 // jms.setJGroup(groups);
1458 for (JGroup grp : groups)
1460 object.getJGroup().add(grp);
1465 // /////////SAVE VIEWPORT
1466 Viewport view = new Viewport();
1467 view.setTitle(ap.alignFrame.getTitle());
1468 view.setSequenceSetId(
1469 makeHashCode(av.getSequenceSetId(), av.getSequenceSetId()));
1470 view.setId(av.getViewId());
1471 if (av.getCodingComplement() != null)
1473 view.setComplementId(av.getCodingComplement().getViewId());
1475 view.setViewName(av.getViewName());
1476 view.setGatheredViews(av.isGatherViewsHere());
1478 Rectangle size = ap.av.getExplodedGeometry();
1479 Rectangle position = size;
1482 size = ap.alignFrame.getBounds();
1483 if (av.getCodingComplement() != null)
1485 position = ((SplitFrame) ap.alignFrame.getSplitViewContainer())
1493 view.setXpos(position.x);
1494 view.setYpos(position.y);
1496 view.setWidth(size.width);
1497 view.setHeight(size.height);
1499 view.setStartRes(vpRanges.getStartRes());
1500 view.setStartSeq(vpRanges.getStartSeq());
1502 OverviewPanel ov = ap.getOverviewPanel();
1505 Overview overview = new Overview();
1506 overview.setTitle(ov.getTitle());
1507 Rectangle bounds = ov.getFrameBounds();
1508 overview.setXpos(bounds.x);
1509 overview.setYpos(bounds.y);
1510 overview.setWidth(bounds.width);
1511 overview.setHeight(bounds.height);
1512 overview.setShowHidden(ov.isShowHiddenRegions());
1513 overview.setGapColour(ov.getCanvas().getGapColour().getRGB());
1514 overview.setResidueColour(
1515 ov.getCanvas().getResidueColour().getRGB());
1516 overview.setHiddenColour(ov.getCanvas().getHiddenColour().getRGB());
1517 view.setOverview(overview);
1519 if (av.getGlobalColourScheme() instanceof jalview.schemes.UserColourScheme)
1521 view.setBgColour(setUserColourScheme(av.getGlobalColourScheme(),
1522 userColours, object));
1525 .getGlobalColourScheme() instanceof jalview.schemes.AnnotationColourGradient)
1527 AnnotationColourScheme ac = constructAnnotationColours(
1528 (jalview.schemes.AnnotationColourGradient) av
1529 .getGlobalColourScheme(),
1530 userColours, object);
1532 view.setAnnotationColours(ac);
1533 view.setBgColour("AnnotationColourGradient");
1537 view.setBgColour(ColourSchemeProperty
1538 .getColourName(av.getGlobalColourScheme()));
1541 ResidueShaderI vcs = av.getResidueShading();
1542 ColourSchemeI cs = av.getGlobalColourScheme();
1546 if (vcs.conservationApplied())
1548 view.setConsThreshold(vcs.getConservationInc());
1549 if (cs instanceof jalview.schemes.UserColourScheme)
1551 view.setBgColour(setUserColourScheme(cs, userColours, object));
1554 view.setPidThreshold(vcs.getThreshold());
1557 view.setConservationSelected(av.getConservationSelected());
1558 view.setPidSelected(av.getAbovePIDThreshold());
1559 view.setCharHeight(av.getCharHeight());
1560 view.setCharWidth(av.getCharWidth());
1561 final Font font = av.getFont();
1562 view.setFontName(font.getName());
1563 view.setFontSize(font.getSize());
1564 view.setFontStyle(font.getStyle());
1565 view.setScaleProteinAsCdna(av.getViewStyle().isScaleProteinAsCdna());
1566 view.setRenderGaps(av.isRenderGaps());
1567 view.setShowAnnotation(av.isShowAnnotation());
1568 view.setShowBoxes(av.getShowBoxes());
1569 view.setShowColourText(av.getColourText());
1570 view.setShowFullId(av.getShowJVSuffix());
1571 view.setRightAlignIds(av.isRightAlignIds());
1572 view.setShowSequenceFeatures(av.isShowSequenceFeatures());
1573 view.setShowText(av.getShowText());
1574 view.setShowUnconserved(av.getShowUnconserved());
1575 view.setWrapAlignment(av.getWrapAlignment());
1576 view.setTextCol1(av.getTextColour().getRGB());
1577 view.setTextCol2(av.getTextColour2().getRGB());
1578 view.setTextColThreshold(av.getThresholdTextColour());
1579 view.setShowConsensusHistogram(av.isShowConsensusHistogram());
1580 view.setShowSequenceLogo(av.isShowSequenceLogo());
1581 view.setNormaliseSequenceLogo(av.isNormaliseSequenceLogo());
1582 view.setShowGroupConsensus(av.isShowGroupConsensus());
1583 view.setShowGroupConservation(av.isShowGroupConservation());
1584 view.setShowNPfeatureTooltip(av.isShowNPFeats());
1585 view.setShowDbRefTooltip(av.isShowDBRefs());
1586 view.setFollowHighlight(av.isFollowHighlight());
1587 view.setFollowSelection(av.followSelection);
1588 view.setIgnoreGapsinConsensus(av.isIgnoreGapsConsensus());
1589 view.setShowComplementFeatures(av.isShowComplementFeatures());
1590 view.setShowComplementFeaturesOnTop(
1591 av.isShowComplementFeaturesOnTop());
1592 if (av.getFeaturesDisplayed() != null)
1594 FeatureSettings fs = new FeatureSettings();
1596 FeatureRendererModel fr = ap.getSeqPanel().seqCanvas
1597 .getFeatureRenderer();
1598 String[] renderOrder = fr.getRenderOrder().toArray(new String[0]);
1600 Vector<String> settingsAdded = new Vector<>();
1601 if (renderOrder != null)
1603 for (String featureType : renderOrder)
1605 FeatureSettings.Setting setting = new FeatureSettings.Setting();
1606 setting.setType(featureType);
1609 * save any filter for the feature type
1611 FeatureMatcherSetI filter = fr.getFeatureFilter(featureType);
1614 Iterator<FeatureMatcherI> filters = filter.getMatchers()
1616 FeatureMatcherI firstFilter = filters.next();
1617 setting.setMatcherSet(Jalview2XML.marshalFilter(firstFilter,
1618 filters, filter.isAnded()));
1622 * save colour scheme for the feature type
1624 FeatureColourI fcol = fr.getFeatureStyle(featureType);
1625 if (!fcol.isSimpleColour())
1627 setting.setColour(fcol.getMaxColour().getRGB());
1628 setting.setMincolour(fcol.getMinColour().getRGB());
1629 setting.setMin(fcol.getMin());
1630 setting.setMax(fcol.getMax());
1631 setting.setColourByLabel(fcol.isColourByLabel());
1632 if (fcol.isColourByAttribute())
1634 String[] attName = fcol.getAttributeName();
1635 setting.getAttributeName().add(attName[0]);
1636 if (attName.length > 1)
1638 setting.getAttributeName().add(attName[1]);
1641 setting.setAutoScale(fcol.isAutoScaled());
1642 setting.setThreshold(fcol.getThreshold());
1643 Color noColour = fcol.getNoColour();
1644 if (noColour == null)
1646 setting.setNoValueColour(NoValueColour.NONE);
1648 else if (noColour.equals(fcol.getMaxColour()))
1650 setting.setNoValueColour(NoValueColour.MAX);
1654 setting.setNoValueColour(NoValueColour.MIN);
1656 // -1 = No threshold, 0 = Below, 1 = Above
1657 setting.setThreshstate(fcol.isAboveThreshold() ? 1
1658 : (fcol.isBelowThreshold() ? 0 : -1));
1662 setting.setColour(fcol.getColour().getRGB());
1666 av.getFeaturesDisplayed().isVisible(featureType));
1667 float rorder = fr.getOrder(featureType);
1670 setting.setOrder(rorder);
1672 /// fs.addSetting(setting);
1673 fs.getSetting().add(setting);
1674 settingsAdded.addElement(featureType);
1678 // is groups actually supposed to be a map here ?
1679 Iterator<String> en = fr.getFeatureGroups().iterator();
1680 Vector<String> groupsAdded = new Vector<>();
1681 while (en.hasNext())
1683 String grp = en.next();
1684 if (groupsAdded.contains(grp))
1688 Group g = new Group();
1690 g.setDisplay(((Boolean) fr.checkGroupVisibility(grp, false))
1693 fs.getGroup().add(g);
1694 groupsAdded.addElement(grp);
1696 // jms.setFeatureSettings(fs);
1697 object.setFeatureSettings(fs);
1700 if (av.hasHiddenColumns())
1702 jalview.datamodel.HiddenColumns hidden = av.getAlignment()
1703 .getHiddenColumns();
1707 "REPORT BUG: avoided null columnselection bug (DMAM reported). Please contact Jim about this.");
1711 Iterator<int[]> hiddenRegions = hidden.iterator();
1712 while (hiddenRegions.hasNext())
1714 int[] region = hiddenRegions.next();
1715 HiddenColumns hc = new HiddenColumns();
1716 hc.setStart(region[0]);
1717 hc.setEnd(region[1]);
1718 // view.addHiddenColumns(hc);
1719 view.getHiddenColumns().add(hc);
1723 if (calcIdSet.size() > 0)
1725 for (String calcId : calcIdSet)
1727 if (calcId.trim().length() > 0)
1729 CalcIdParam cidp = createCalcIdParam(calcId, av);
1730 // Some calcIds have no parameters.
1733 // view.addCalcIdParam(cidp);
1734 view.getCalcIdParam().add(cidp);
1740 // jms.addViewport(view);
1741 object.getViewport().add(view);
1743 // object.setJalviewModelSequence(jms);
1744 // object.getVamsasModel().addSequenceSet(vamsasSet);
1745 object.getVamsasModel().getSequenceSet().add(vamsasSet);
1747 if (jout != null && fileName != null)
1749 // We may not want to write the object to disk,
1750 // eg we can copy the alignViewport to a new view object
1751 // using save and then load
1754 fileName = fileName.replace('\\', '/');
1755 System.out.println("Writing jar entry " + fileName);
1756 JarEntry entry = new JarEntry(fileName);
1757 jout.putNextEntry(entry);
1758 PrintWriter pout = new PrintWriter(
1759 new OutputStreamWriter(jout, UTF_8));
1760 JAXBContext jaxbContext = JAXBContext
1761 .newInstance(JalviewModel.class);
1762 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
1764 // output pretty printed
1765 // jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
1766 jaxbMarshaller.marshal(
1767 new ObjectFactory().createJalviewModel(object), pout);
1769 // jaxbMarshaller.marshal(object, pout);
1770 // marshaller.marshal(object);
1773 } catch (Exception ex)
1775 // TODO: raise error in GUI if marshalling failed.
1776 System.err.println("Error writing Jalview project");
1777 ex.printStackTrace();
1784 * Writes PCA viewer attributes and computed values to an XML model object and
1785 * adds it to the JalviewModel. Any exceptions are reported by logging.
1787 protected void savePCA(PCAPanel panel, JalviewModel object)
1791 PcaViewer viewer = new PcaViewer();
1792 viewer.setHeight(panel.getHeight());
1793 viewer.setWidth(panel.getWidth());
1794 viewer.setXpos(panel.getX());
1795 viewer.setYpos(panel.getY());
1796 viewer.setTitle(panel.getTitle());
1797 PCAModel pcaModel = panel.getPcaModel();
1798 viewer.setScoreModelName(pcaModel.getScoreModelName());
1799 viewer.setXDim(panel.getSelectedDimensionIndex(X));
1800 viewer.setYDim(panel.getSelectedDimensionIndex(Y));
1801 viewer.setZDim(panel.getSelectedDimensionIndex(Z));
1803 panel.getRotatableCanvas().getBackgroundColour().getRGB());
1804 viewer.setScaleFactor(panel.getRotatableCanvas().getScaleFactor());
1805 float[] spMin = panel.getRotatableCanvas().getSeqMin();
1806 SeqPointMin spmin = new SeqPointMin();
1807 spmin.setXPos(spMin[0]);
1808 spmin.setYPos(spMin[1]);
1809 spmin.setZPos(spMin[2]);
1810 viewer.setSeqPointMin(spmin);
1811 float[] spMax = panel.getRotatableCanvas().getSeqMax();
1812 SeqPointMax spmax = new SeqPointMax();
1813 spmax.setXPos(spMax[0]);
1814 spmax.setYPos(spMax[1]);
1815 spmax.setZPos(spMax[2]);
1816 viewer.setSeqPointMax(spmax);
1817 viewer.setShowLabels(panel.getRotatableCanvas().isShowLabels());
1818 viewer.setLinkToAllViews(
1819 panel.getRotatableCanvas().isApplyToAllViews());
1820 SimilarityParamsI sp = pcaModel.getSimilarityParameters();
1821 viewer.setIncludeGaps(sp.includeGaps());
1822 viewer.setMatchGaps(sp.matchGaps());
1823 viewer.setIncludeGappedColumns(sp.includeGappedColumns());
1824 viewer.setDenominateByShortestLength(sp.denominateByShortestLength());
1827 * sequence points on display
1829 for (jalview.datamodel.SequencePoint spt : pcaModel
1830 .getSequencePoints())
1832 SequencePoint point = new SequencePoint();
1833 point.setSequenceRef(seqHash(spt.getSequence()));
1834 point.setXPos(spt.coord.x);
1835 point.setYPos(spt.coord.y);
1836 point.setZPos(spt.coord.z);
1837 viewer.getSequencePoint().add(point);
1841 * (end points of) axes on display
1843 for (Point p : panel.getRotatableCanvas().getAxisEndPoints())
1846 Axis axis = new Axis();
1850 viewer.getAxis().add(axis);
1854 * raw PCA data (note we are not restoring PCA inputs here -
1855 * alignment view, score model, similarity parameters)
1857 PcaDataType data = new PcaDataType();
1858 viewer.setPcaData(data);
1859 PCA pca = pcaModel.getPcaData();
1861 DoubleMatrix pm = new DoubleMatrix();
1862 saveDoubleMatrix(pca.getPairwiseScores(), pm);
1863 data.setPairwiseMatrix(pm);
1865 DoubleMatrix tm = new DoubleMatrix();
1866 saveDoubleMatrix(pca.getTridiagonal(), tm);
1867 data.setTridiagonalMatrix(tm);
1869 DoubleMatrix eigenMatrix = new DoubleMatrix();
1870 data.setEigenMatrix(eigenMatrix);
1871 saveDoubleMatrix(pca.getEigenmatrix(), eigenMatrix);
1873 object.getPcaViewer().add(viewer);
1874 } catch (Throwable t)
1876 Console.error("Error saving PCA: " + t.getMessage());
1881 * Stores values from a matrix into an XML element, including (if present) the
1886 * @see #loadDoubleMatrix(DoubleMatrix)
1888 protected void saveDoubleMatrix(MatrixI m, DoubleMatrix xmlMatrix)
1890 xmlMatrix.setRows(m.height());
1891 xmlMatrix.setColumns(m.width());
1892 for (int i = 0; i < m.height(); i++)
1894 DoubleVector row = new DoubleVector();
1895 for (int j = 0; j < m.width(); j++)
1897 row.getV().add(m.getValue(i, j));
1899 xmlMatrix.getRow().add(row);
1901 if (m.getD() != null)
1903 DoubleVector dVector = new DoubleVector();
1904 for (double d : m.getD())
1906 dVector.getV().add(d);
1908 xmlMatrix.setD(dVector);
1910 if (m.getE() != null)
1912 DoubleVector eVector = new DoubleVector();
1913 for (double e : m.getE())
1915 eVector.getV().add(e);
1917 xmlMatrix.setE(eVector);
1922 * Loads XML matrix data into a new Matrix object, including the D and/or E
1923 * vectors (if present)
1927 * @see Jalview2XML#saveDoubleMatrix(MatrixI, DoubleMatrix)
1929 protected MatrixI loadDoubleMatrix(DoubleMatrix mData)
1931 int rows = mData.getRows();
1932 double[][] vals = new double[rows][];
1934 for (int i = 0; i < rows; i++)
1936 List<Double> dVector = mData.getRow().get(i).getV();
1937 vals[i] = new double[dVector.size()];
1939 for (Double d : dVector)
1945 MatrixI m = new Matrix(vals);
1947 if (mData.getD() != null)
1949 List<Double> dVector = mData.getD().getV();
1950 double[] vec = new double[dVector.size()];
1952 for (Double d : dVector)
1958 if (mData.getE() != null)
1960 List<Double> dVector = mData.getE().getV();
1961 double[] vec = new double[dVector.size()];
1963 for (Double d : dVector)
1974 * Save any Varna viewers linked to this sequence. Writes an rnaViewer element
1975 * for each viewer, with
1977 * <li>viewer geometry (position, size, split pane divider location)</li>
1978 * <li>index of the selected structure in the viewer (currently shows gapped
1980 * <li>the id of the annotation holding RNA secondary structure</li>
1981 * <li>(currently only one SS is shown per viewer, may be more in future)</li>
1983 * Varna viewer state is also written out (in native Varna XML) to separate
1984 * project jar entries. A separate entry is written for each RNA structure
1985 * displayed, with the naming convention
1987 * <li>rna_viewId_sequenceId_annotationId_[gapped|trimmed]</li>
1995 * @param storeDataset
1997 protected void saveRnaViewers(JarOutputStream jout, JSeq jseq,
1998 final SequenceI jds, List<String> viewIds, AlignmentPanel ap,
1999 boolean storeDataset)
2001 if (Desktop.desktop == null)
2005 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
2006 for (int f = frames.length - 1; f > -1; f--)
2008 if (frames[f] instanceof AppVarna)
2010 AppVarna varna = (AppVarna) frames[f];
2012 * link the sequence to every viewer that is showing it and is linked to
2013 * its alignment panel
2015 if (varna.isListeningFor(jds) && ap == varna.getAlignmentPanel())
2017 String viewId = varna.getViewId();
2018 RnaViewer rna = new RnaViewer();
2019 rna.setViewId(viewId);
2020 rna.setTitle(varna.getTitle());
2021 rna.setXpos(varna.getX());
2022 rna.setYpos(varna.getY());
2023 rna.setWidth(varna.getWidth());
2024 rna.setHeight(varna.getHeight());
2025 rna.setDividerLocation(varna.getDividerLocation());
2026 rna.setSelectedRna(varna.getSelectedIndex());
2027 // jseq.addRnaViewer(rna);
2028 jseq.getRnaViewer().add(rna);
2031 * Store each Varna panel's state once in the project per sequence.
2032 * First time through only (storeDataset==false)
2034 // boolean storeSessions = false;
2035 // String sequenceViewId = viewId + seqsToIds.get(jds);
2036 // if (!storeDataset && !viewIds.contains(sequenceViewId))
2038 // viewIds.add(sequenceViewId);
2039 // storeSessions = true;
2041 for (RnaModel model : varna.getModels())
2043 if (model.seq == jds)
2046 * VARNA saves each view (sequence or alignment secondary
2047 * structure, gapped or trimmed) as a separate XML file
2049 String jarEntryName = rnaSessions.get(model);
2050 if (jarEntryName == null)
2053 String varnaStateFile = varna.getStateInfo(model.rna);
2054 jarEntryName = RNA_PREFIX + viewId + "_" + nextCounter();
2055 copyFileToJar(jout, varnaStateFile, jarEntryName, "Varna");
2056 rnaSessions.put(model, jarEntryName);
2058 SecondaryStructure ss = new SecondaryStructure();
2059 String annotationId = varna.getAnnotation(jds).annotationId;
2060 ss.setAnnotationId(annotationId);
2061 ss.setViewerState(jarEntryName);
2062 ss.setGapped(model.gapped);
2063 ss.setTitle(model.title);
2064 // rna.addSecondaryStructure(ss);
2065 rna.getSecondaryStructure().add(ss);
2074 * Copy the contents of a file to a new entry added to the output jar
2078 * @param jarEntryName
2080 * additional identifying info to log to the console
2082 protected void copyFileToJar(JarOutputStream jout, String infilePath,
2083 String jarEntryName, String msg)
2085 try (InputStream is = new FileInputStream(infilePath))
2087 File file = new File(infilePath);
2088 if (file.exists() && jout != null)
2091 "Writing jar entry " + jarEntryName + " (" + msg + ")");
2092 jout.putNextEntry(new JarEntry(jarEntryName));
2095 // dis = new DataInputStream(new FileInputStream(file));
2096 // byte[] data = new byte[(int) file.length()];
2097 // dis.readFully(data);
2098 // writeJarEntry(jout, jarEntryName, data);
2100 } catch (Exception ex)
2102 ex.printStackTrace();
2107 * Copies input to output, in 4K buffers; handles any data (text or binary)
2111 * @throws IOException
2113 protected void copyAll(InputStream in, OutputStream out)
2116 byte[] buffer = new byte[4096];
2118 while ((bytesRead = in.read(buffer)) != -1)
2120 out.write(buffer, 0, bytesRead);
2125 * Save the state of a structure viewer
2130 * the archive XML element under which to save the state
2133 * @param matchedFile
2137 protected String saveStructureViewer(AlignmentPanel ap, SequenceI jds,
2138 Pdbids pdb, PDBEntry entry, List<String> viewIds,
2139 String matchedFile, StructureViewerBase viewFrame)
2141 final AAStructureBindingModel bindingModel = viewFrame.getBinding();
2144 * Look for any bindings for this viewer to the PDB file of interest
2145 * (including part matches excluding chain id)
2147 for (int peid = 0; peid < bindingModel.getPdbCount(); peid++)
2149 final PDBEntry pdbentry = bindingModel.getPdbEntry(peid);
2150 final String pdbId = pdbentry.getId();
2151 if (!pdbId.equals(entry.getId()) && !(entry.getId().length() > 4
2152 && entry.getId().toLowerCase(Locale.ROOT)
2153 .startsWith(pdbId.toLowerCase(Locale.ROOT))))
2156 * not interested in a binding to a different PDB entry here
2160 if (matchedFile == null)
2162 matchedFile = pdbentry.getFile();
2164 else if (!matchedFile.equals(pdbentry.getFile()))
2167 "Probably lost some PDB-Sequence mappings for this structure file (which apparently has same PDB Entry code): "
2168 + pdbentry.getFile());
2172 // can get at it if the ID
2173 // match is ambiguous (e.g.
2176 for (int smap = 0; smap < viewFrame.getBinding()
2177 .getSequence()[peid].length; smap++)
2179 // if (jal.findIndex(jmol.jmb.sequence[peid][smap]) > -1)
2180 if (jds == viewFrame.getBinding().getSequence()[peid][smap])
2182 StructureState state = new StructureState();
2183 state.setVisible(true);
2184 state.setXpos(viewFrame.getX());
2185 state.setYpos(viewFrame.getY());
2186 state.setWidth(viewFrame.getWidth());
2187 state.setHeight(viewFrame.getHeight());
2188 final String viewId = viewFrame.getViewId();
2189 state.setViewId(viewId);
2190 state.setAlignwithAlignPanel(viewFrame.isUsedforaligment(ap));
2191 state.setColourwithAlignPanel(viewFrame.isUsedForColourBy(ap));
2192 state.setColourByJmol(viewFrame.isColouredByViewer());
2193 state.setType(viewFrame.getViewerType().toString());
2194 // pdb.addStructureState(state);
2195 pdb.getStructureState().add(state);
2203 * Populates the AnnotationColourScheme xml for save. This captures the
2204 * settings of the options in the 'Colour by Annotation' dialog.
2207 * @param userColours
2211 private AnnotationColourScheme constructAnnotationColours(
2212 AnnotationColourGradient acg, List<UserColourScheme> userColours,
2215 AnnotationColourScheme ac = new AnnotationColourScheme();
2216 ac.setAboveThreshold(acg.getAboveThreshold());
2217 ac.setThreshold(acg.getAnnotationThreshold());
2218 // 2.10.2 save annotationId (unique) not annotation label
2219 ac.setAnnotation(acg.getAnnotation().annotationId);
2220 if (acg.getBaseColour() instanceof UserColourScheme)
2223 setUserColourScheme(acg.getBaseColour(), userColours, jm));
2228 ColourSchemeProperty.getColourName(acg.getBaseColour()));
2231 ac.setMaxColour(acg.getMaxColour().getRGB());
2232 ac.setMinColour(acg.getMinColour().getRGB());
2233 ac.setPerSequence(acg.isSeqAssociated());
2234 ac.setPredefinedColours(acg.isPredefinedColours());
2238 private void storeAlignmentAnnotation(AlignmentAnnotation[] aa,
2239 IdentityHashMap<SequenceGroup, String> groupRefs,
2240 AlignmentViewport av, Set<String> calcIdSet, boolean storeDS,
2241 SequenceSet vamsasSet)
2244 for (int i = 0; i < aa.length; i++)
2246 Annotation an = new Annotation();
2248 AlignmentAnnotation annotation = aa[i];
2249 if (annotation.annotationId != null)
2251 annotationIds.put(annotation.annotationId, annotation);
2254 an.setId(annotation.annotationId);
2256 an.setVisible(annotation.visible);
2258 an.setDescription(annotation.description);
2260 if (annotation.sequenceRef != null)
2262 // 2.9 JAL-1781 xref on sequence id rather than name
2263 an.setSequenceRef(seqsToIds.get(annotation.sequenceRef));
2265 if (annotation.groupRef != null)
2267 String groupIdr = groupRefs.get(annotation.groupRef);
2268 if (groupIdr == null)
2270 // make a locally unique String
2271 groupRefs.put(annotation.groupRef,
2272 groupIdr = ("" + System.currentTimeMillis()
2273 + annotation.groupRef.getName()
2274 + groupRefs.size()));
2276 an.setGroupRef(groupIdr.toString());
2279 // store all visualization attributes for annotation
2280 an.setGraphHeight(annotation.graphHeight);
2281 an.setCentreColLabels(annotation.centreColLabels);
2282 an.setScaleColLabels(annotation.scaleColLabel);
2283 an.setShowAllColLabels(annotation.showAllColLabels);
2284 an.setBelowAlignment(annotation.belowAlignment);
2286 if (annotation.graph > 0)
2289 an.setGraphType(annotation.graph);
2290 an.setGraphGroup(annotation.graphGroup);
2291 if (annotation.getThreshold() != null)
2293 ThresholdLine line = new ThresholdLine();
2294 line.setLabel(annotation.getThreshold().label);
2295 line.setValue(annotation.getThreshold().value);
2296 line.setColour(annotation.getThreshold().colour.getRGB());
2297 an.setThresholdLine(line);
2299 if (annotation.graph==AlignmentAnnotation.CONTACT_MAP)
2301 if (annotation.sequenceRef.getContactMaps()!=null)
2303 ContactMatrixI cm = annotation.sequenceRef.getContactMatrixFor(annotation);
2306 MatrixType xmlmat = new MatrixType();
2307 xmlmat.setType(cm.getType());
2308 xmlmat.setRows(BigInteger.valueOf(cm.getWidth()));
2309 xmlmat.setCols(BigInteger.valueOf(cm.getHeight()));
2310 xmlmat.setValue(ContactMatrix.contactToFloatString(cm));
2311 an.getContactmatrix().add(xmlmat);
2321 an.setLabel(annotation.label);
2323 if (annotation == av.getAlignmentQualityAnnot()
2324 || annotation == av.getAlignmentConservationAnnotation()
2325 || annotation == av.getAlignmentConsensusAnnotation()
2326 || annotation.autoCalculated)
2328 // new way of indicating autocalculated annotation -
2329 an.setAutoCalculated(annotation.autoCalculated);
2331 if (annotation.hasScore())
2333 an.setScore(annotation.getScore());
2336 if (annotation.getCalcId() != null)
2338 calcIdSet.add(annotation.getCalcId());
2339 an.setCalcId(annotation.getCalcId());
2341 if (annotation.hasProperties())
2343 for (String pr : annotation.getProperties())
2345 jalview.xml.binding.jalview.Annotation.Property prop = new jalview.xml.binding.jalview.Annotation.Property();
2347 prop.setValue(annotation.getProperty(pr));
2348 // an.addProperty(prop);
2349 an.getProperty().add(prop);
2353 AnnotationElement ae;
2354 if (annotation.annotations != null)
2356 an.setScoreOnly(false);
2357 for (int a = 0; a < annotation.annotations.length; a++)
2359 if ((annotation == null) || (annotation.annotations[a] == null))
2364 ae = new AnnotationElement();
2365 if (annotation.annotations[a].description != null)
2367 ae.setDescription(annotation.annotations[a].description);
2369 if (annotation.annotations[a].displayCharacter != null)
2371 ae.setDisplayCharacter(
2372 annotation.annotations[a].displayCharacter);
2375 if (!Float.isNaN(annotation.annotations[a].value))
2377 ae.setValue(annotation.annotations[a].value);
2381 if (annotation.annotations[a].secondaryStructure > ' ')
2383 ae.setSecondaryStructure(
2384 annotation.annotations[a].secondaryStructure + "");
2387 if (annotation.annotations[a].colour != null
2388 && annotation.annotations[a].colour != java.awt.Color.black)
2390 ae.setColour(annotation.annotations[a].colour.getRGB());
2393 // an.addAnnotationElement(ae);
2394 an.getAnnotationElement().add(ae);
2395 if (annotation.autoCalculated)
2397 // only write one non-null entry into the annotation row -
2398 // sufficient to get the visualization attributes necessary to
2406 an.setScoreOnly(true);
2408 if (!storeDS || (storeDS && !annotation.autoCalculated))
2410 // skip autocalculated annotation - these are only provided for
2412 // vamsasSet.addAnnotation(an);
2413 vamsasSet.getAnnotation().add(an);
2419 private CalcIdParam createCalcIdParam(String calcId, AlignViewport av)
2421 AutoCalcSetting settings = av.getCalcIdSettingsFor(calcId);
2422 if (settings != null)
2424 CalcIdParam vCalcIdParam = new CalcIdParam();
2425 vCalcIdParam.setCalcId(calcId);
2426 // vCalcIdParam.addServiceURL(settings.getServiceURI());
2427 vCalcIdParam.getServiceURL().add(settings.getServiceURI());
2428 // generic URI allowing a third party to resolve another instance of the
2429 // service used for this calculation
2430 for (String url : settings.getServiceURLs())
2432 // vCalcIdParam.addServiceURL(urls);
2433 vCalcIdParam.getServiceURL().add(url);
2435 vCalcIdParam.setVersion("1.0");
2436 if (settings.getPreset() != null)
2438 WsParamSetI setting = settings.getPreset();
2439 vCalcIdParam.setName(setting.getName());
2440 vCalcIdParam.setDescription(setting.getDescription());
2444 vCalcIdParam.setName("");
2445 vCalcIdParam.setDescription("Last used parameters");
2447 // need to be able to recover 1) settings 2) user-defined presets or
2448 // recreate settings from preset 3) predefined settings provided by
2449 // service - or settings that can be transferred (or discarded)
2450 vCalcIdParam.setParameters(
2451 settings.getWsParamFile().replace("\n", "|\\n|"));
2452 vCalcIdParam.setAutoUpdate(settings.isAutoUpdate());
2453 // todo - decide if updateImmediately is needed for any projects.
2455 return vCalcIdParam;
2460 private boolean recoverCalcIdParam(CalcIdParam calcIdParam,
2463 if (calcIdParam.getVersion().equals("1.0"))
2465 final String[] calcIds = calcIdParam.getServiceURL()
2466 .toArray(new String[0]);
2467 Jws2Instance service = Jws2Discoverer.getDiscoverer()
2468 .getPreferredServiceFor(calcIds);
2469 if (service != null)
2471 WsParamSetI parmSet = null;
2474 parmSet = service.getParamStore().parseServiceParameterFile(
2475 calcIdParam.getName(), calcIdParam.getDescription(),
2477 calcIdParam.getParameters().replace("|\\n|", "\n"));
2478 } catch (IOException x)
2480 Console.warn("Couldn't parse parameter data for "
2481 + calcIdParam.getCalcId(), x);
2484 List<ArgumentI> argList = null;
2485 if (calcIdParam.getName().length() > 0)
2487 parmSet = service.getParamStore()
2488 .getPreset(calcIdParam.getName());
2489 if (parmSet != null)
2491 // TODO : check we have a good match with settings in AACon -
2492 // otherwise we'll need to create a new preset
2497 argList = parmSet.getArguments();
2500 AAConSettings settings = new AAConSettings(
2501 calcIdParam.isAutoUpdate(), service, parmSet, argList);
2502 av.setCalcIdSettingsFor(calcIdParam.getCalcId(), settings,
2503 calcIdParam.isNeedsUpdate());
2509 "Cannot resolve a service for the parameters used in this project. Try configuring a JABAWS server.");
2513 throw new Error(MessageManager.formatMessage(
2514 "error.unsupported_version_calcIdparam", new Object[]
2515 { calcIdParam.toString() }));
2519 * External mapping between jalview objects and objects yielding a valid and
2520 * unique object ID string. This is null for normal Jalview project IO, but
2521 * non-null when a jalview project is being read or written as part of a
2524 IdentityHashMap jv2vobj = null;
2527 * Construct a unique ID for jvobj using either existing bindings or if none
2528 * exist, the result of the hashcode call for the object.
2531 * jalview data object
2532 * @return unique ID for referring to jvobj
2534 private String makeHashCode(Object jvobj, String altCode)
2536 if (jv2vobj != null)
2538 Object id = jv2vobj.get(jvobj);
2541 return id.toString();
2543 // check string ID mappings
2544 if (jvids2vobj != null && jvobj instanceof String)
2546 id = jvids2vobj.get(jvobj);
2550 return id.toString();
2552 // give up and warn that something has gone wrong
2554 "Cannot find ID for object in external mapping : " + jvobj);
2560 * return local jalview object mapped to ID, if it exists
2564 * @return null or object bound to idcode
2566 private Object retrieveExistingObj(String idcode)
2568 if (idcode != null && vobj2jv != null)
2570 return vobj2jv.get(idcode);
2576 * binding from ID strings from external mapping table to jalview data model
2579 private Hashtable vobj2jv;
2581 private Sequence createVamsasSequence(String id, SequenceI jds)
2583 return createVamsasSequence(true, id, jds, null);
2586 private Sequence createVamsasSequence(boolean recurse, String id,
2587 SequenceI jds, SequenceI parentseq)
2589 Sequence vamsasSeq = new Sequence();
2590 vamsasSeq.setId(id);
2591 vamsasSeq.setName(jds.getName());
2592 vamsasSeq.setSequence(jds.getSequenceAsString());
2593 vamsasSeq.setDescription(jds.getDescription());
2594 List<DBRefEntry> dbrefs = null;
2595 if (jds.getDatasetSequence() != null)
2597 vamsasSeq.setDsseqid(seqHash(jds.getDatasetSequence()));
2601 // seqId==dsseqid so we can tell which sequences really are
2602 // dataset sequences only
2603 vamsasSeq.setDsseqid(id);
2604 dbrefs = jds.getDBRefs();
2605 if (parentseq == null)
2612 * save any dbrefs; special subclass GeneLocus is flagged as 'locus'
2616 for (int d = 0, nd = dbrefs.size(); d < nd; d++)
2618 DBRef dbref = new DBRef();
2619 DBRefEntry ref = dbrefs.get(d);
2620 dbref.setSource(ref.getSource());
2621 dbref.setVersion(ref.getVersion());
2622 dbref.setAccessionId(ref.getAccessionId());
2623 dbref.setCanonical(ref.isCanonical());
2624 if (ref instanceof GeneLocus)
2626 dbref.setLocus(true);
2630 Mapping mp = createVamsasMapping(ref.getMap(), parentseq, jds,
2632 dbref.setMapping(mp);
2634 vamsasSeq.getDBRef().add(dbref);
2640 private Mapping createVamsasMapping(jalview.datamodel.Mapping jmp,
2641 SequenceI parentseq, SequenceI jds, boolean recurse)
2644 if (jmp.getMap() != null)
2648 jalview.util.MapList mlst = jmp.getMap();
2649 List<int[]> r = mlst.getFromRanges();
2650 for (int[] range : r)
2652 MapListFrom mfrom = new MapListFrom();
2653 mfrom.setStart(range[0]);
2654 mfrom.setEnd(range[1]);
2655 // mp.addMapListFrom(mfrom);
2656 mp.getMapListFrom().add(mfrom);
2658 r = mlst.getToRanges();
2659 for (int[] range : r)
2661 MapListTo mto = new MapListTo();
2662 mto.setStart(range[0]);
2663 mto.setEnd(range[1]);
2664 // mp.addMapListTo(mto);
2665 mp.getMapListTo().add(mto);
2667 mp.setMapFromUnit(BigInteger.valueOf(mlst.getFromRatio()));
2668 mp.setMapToUnit(BigInteger.valueOf(mlst.getToRatio()));
2669 if (jmp.getTo() != null)
2671 // MappingChoice mpc = new MappingChoice();
2673 // check/create ID for the sequence referenced by getTo()
2676 SequenceI ps = null;
2677 if (parentseq != jmp.getTo()
2678 && parentseq.getDatasetSequence() != jmp.getTo())
2680 // chaining dbref rather than a handshaking one
2681 jmpid = seqHash(ps = jmp.getTo());
2685 jmpid = seqHash(ps = parentseq);
2687 // mpc.setDseqFor(jmpid);
2688 mp.setDseqFor(jmpid);
2689 if (!seqRefIds.containsKey(jmpid))
2691 Console.debug("creatign new DseqFor ID");
2692 seqRefIds.put(jmpid, ps);
2696 Console.debug("reusing DseqFor ID");
2699 // mp.setMappingChoice(mpc);
2705 String setUserColourScheme(jalview.schemes.ColourSchemeI cs,
2706 List<UserColourScheme> userColours, JalviewModel jm)
2709 jalview.schemes.UserColourScheme ucs = (jalview.schemes.UserColourScheme) cs;
2710 boolean newucs = false;
2711 if (!userColours.contains(ucs))
2713 userColours.add(ucs);
2716 id = "ucs" + userColours.indexOf(ucs);
2719 // actually create the scheme's entry in the XML model
2720 java.awt.Color[] colours = ucs.getColours();
2721 UserColours uc = new UserColours();
2722 // UserColourScheme jbucs = new UserColourScheme();
2723 JalviewUserColours jbucs = new JalviewUserColours();
2725 for (int i = 0; i < colours.length; i++)
2727 Colour col = new Colour();
2728 col.setName(ResidueProperties.aa[i]);
2729 col.setRGB(jalview.util.Format.getHexString(colours[i]));
2730 // jbucs.addColour(col);
2731 jbucs.getColour().add(col);
2733 if (ucs.getLowerCaseColours() != null)
2735 colours = ucs.getLowerCaseColours();
2736 for (int i = 0; i < colours.length; i++)
2738 Colour col = new Colour();
2739 col.setName(ResidueProperties.aa[i].toLowerCase(Locale.ROOT));
2740 col.setRGB(jalview.util.Format.getHexString(colours[i]));
2741 // jbucs.addColour(col);
2742 jbucs.getColour().add(col);
2747 uc.setUserColourScheme(jbucs);
2748 // jm.addUserColours(uc);
2749 jm.getUserColours().add(uc);
2755 jalview.schemes.UserColourScheme getUserColourScheme(JalviewModel jm,
2758 List<UserColours> uc = jm.getUserColours();
2759 UserColours colours = null;
2761 for (int i = 0; i < uc.length; i++)
2763 if (uc[i].getId().equals(id))
2770 for (UserColours c : uc)
2772 if (c.getId().equals(id))
2779 java.awt.Color[] newColours = new java.awt.Color[24];
2781 for (int i = 0; i < 24; i++)
2783 newColours[i] = new java.awt.Color(Integer.parseInt(
2784 // colours.getUserColourScheme().getColour(i).getRGB(), 16));
2785 colours.getUserColourScheme().getColour().get(i).getRGB(),
2789 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(
2792 if (colours.getUserColourScheme().getColour().size()/*Count()*/ > 24)
2794 newColours = new java.awt.Color[23];
2795 for (int i = 0; i < 23; i++)
2797 newColours[i] = new java.awt.Color(
2798 Integer.parseInt(colours.getUserColourScheme().getColour()
2799 .get(i + 24).getRGB(), 16));
2801 ucs.setLowerCaseColours(newColours);
2808 * contains last error message (if any) encountered by XML loader.
2810 String errorMessage = null;
2813 * flag to control whether the Jalview2XML_V1 parser should be deferred to if
2814 * exceptions are raised during project XML parsing
2816 public boolean attemptversion1parse = false;
2819 * Load a jalview project archive from a jar file
2822 * - HTTP URL or filename
2824 public AlignFrame loadJalviewAlign(final Object file)
2827 jalview.gui.AlignFrame af = null;
2831 // create list to store references for any new Jmol viewers created
2832 newStructureViewers = new Vector<>();
2833 // UNMARSHALLER SEEMS TO CLOSE JARINPUTSTREAM, MOST ANNOYING
2834 // Workaround is to make sure caller implements the JarInputStreamProvider
2836 // so we can re-open the jar input stream for each entry.
2838 jarInputStreamProvider jprovider = createjarInputStreamProvider(file);
2839 af = loadJalviewAlign(jprovider);
2842 af.setMenusForViewport();
2844 } catch (MalformedURLException e)
2846 errorMessage = "Invalid URL format for '" + file + "'";
2852 SwingUtilities.invokeAndWait(new Runnable()
2857 setLoadingFinishedForNewStructureViewers();
2860 } catch (Exception x)
2862 System.err.println("Error loading alignment: " + x.getMessage());
2868 @SuppressWarnings("unused")
2869 private jarInputStreamProvider createjarInputStreamProvider(
2870 final Object ofile) throws MalformedURLException
2873 // BH 2018 allow for bytes already attached to File object
2876 String file = (ofile instanceof File
2877 ? ((File) ofile).getCanonicalPath()
2878 : ofile.toString());
2879 byte[] bytes = Platform.isJS() ? Platform.getFileBytes((File) ofile)
2882 errorMessage = null;
2883 uniqueSetSuffix = null;
2885 viewportsAdded.clear();
2886 frefedSequence = null;
2888 if (HttpUtils.startsWithHttpOrHttps(file))
2890 url = new URL(file);
2892 final URL _url = url;
2893 return new jarInputStreamProvider()
2897 public JarInputStream getJarInputStream() throws IOException
2901 // System.out.println("Jalview2XML: opening byte jarInputStream for
2902 // bytes.length=" + bytes.length);
2903 return new JarInputStream(new ByteArrayInputStream(bytes));
2907 // System.out.println("Jalview2XML: opening url jarInputStream for "
2909 return new JarInputStream(_url.openStream());
2913 // System.out.println("Jalview2XML: opening file jarInputStream for
2915 return new JarInputStream(new FileInputStream(file));
2920 public String getFilename()
2925 } catch (IOException e)
2927 e.printStackTrace();
2933 * Recover jalview session from a jalview project archive. Caller may
2934 * initialise uniqueSetSuffix, seqRefIds, viewportsAdded and frefedSequence
2935 * themselves. Any null fields will be initialised with default values,
2936 * non-null fields are left alone.
2941 public AlignFrame loadJalviewAlign(final jarInputStreamProvider jprovider)
2943 errorMessage = null;
2944 if (uniqueSetSuffix == null)
2946 uniqueSetSuffix = System.currentTimeMillis() % 100000 + "";
2948 if (seqRefIds == null)
2952 AlignFrame af = null, _af = null;
2953 IdentityHashMap<AlignmentI, AlignmentI> importedDatasets = new IdentityHashMap<>();
2954 Map<String, AlignFrame> gatherToThisFrame = new HashMap<>();
2955 final String file = jprovider.getFilename();
2958 JarInputStream jin = null;
2959 JarEntry jarentry = null;
2964 jin = jprovider.getJarInputStream();
2965 for (int i = 0; i < entryCount; i++)
2967 jarentry = jin.getNextJarEntry();
2970 if (jarentry != null && jarentry.getName().endsWith(".xml"))
2972 JAXBContext jc = JAXBContext
2973 .newInstance("jalview.xml.binding.jalview");
2974 XMLStreamReader streamReader = XMLInputFactory.newInstance()
2975 .createXMLStreamReader(jin);
2976 javax.xml.bind.Unmarshaller um = jc.createUnmarshaller();
2977 JAXBElement<JalviewModel> jbe = um.unmarshal(streamReader,
2978 JalviewModel.class);
2979 JalviewModel object = jbe.getValue();
2981 if (true) // !skipViewport(object))
2983 _af = loadFromObject(object, file, true, jprovider);
2984 if (_af != null && object.getViewport().size() > 0)
2985 // getJalviewModelSequence().getViewportCount() > 0)
2989 // store a reference to the first view
2992 if (_af.getViewport().isGatherViewsHere())
2994 // if this is a gathered view, keep its reference since
2995 // after gathering views, only this frame will remain
2997 gatherToThisFrame.put(_af.getViewport().getSequenceSetId(),
3000 // Save dataset to register mappings once all resolved
3001 importedDatasets.put(
3002 af.getViewport().getAlignment().getDataset(),
3003 af.getViewport().getAlignment().getDataset());
3008 else if (jarentry != null)
3010 // Some other file here.
3013 } while (jarentry != null);
3015 resolveFrefedSequences();
3016 } catch (IOException ex)
3018 ex.printStackTrace();
3019 errorMessage = "Couldn't locate Jalview XML file : " + file;
3021 "Exception whilst loading jalview XML file : " + ex + "\n");
3022 } catch (Exception ex)
3024 System.err.println("Parsing as Jalview Version 2 file failed.");
3025 ex.printStackTrace(System.err);
3026 if (attemptversion1parse)
3028 // used to attempt to parse as V1 castor-generated xml
3030 if (Desktop.instance != null)
3032 Desktop.instance.stopLoading();
3036 System.out.println("Successfully loaded archive file");
3039 ex.printStackTrace();
3042 "Exception whilst loading jalview XML file : " + ex + "\n");
3043 } catch (OutOfMemoryError e)
3045 // Don't use the OOM Window here
3046 errorMessage = "Out of memory loading jalview XML file";
3047 System.err.println("Out of memory whilst loading jalview XML file");
3048 e.printStackTrace();
3052 * Regather multiple views (with the same sequence set id) to the frame (if
3053 * any) that is flagged as the one to gather to, i.e. convert them to tabbed
3054 * views instead of separate frames. Note this doesn't restore a state where
3055 * some expanded views in turn have tabbed views - the last "first tab" read
3056 * in will play the role of gatherer for all.
3058 for (AlignFrame fr : gatherToThisFrame.values())
3060 Desktop.instance.gatherViews(fr);
3063 restoreSplitFrames();
3064 for (AlignmentI ds : importedDatasets.keySet())
3066 if (ds.getCodonFrames() != null)
3068 StructureSelectionManager
3069 .getStructureSelectionManager(Desktop.instance)
3070 .registerMappings(ds.getCodonFrames());
3073 if (errorMessage != null)
3078 if (Desktop.instance != null)
3080 Desktop.instance.stopLoading();
3087 * Try to reconstruct and display SplitFrame windows, where each contains
3088 * complementary dna and protein alignments. Done by pairing up AlignFrame
3089 * objects (created earlier) which have complementary viewport ids associated.
3091 protected void restoreSplitFrames()
3093 List<SplitFrame> gatherTo = new ArrayList<>();
3094 List<AlignFrame> addedToSplitFrames = new ArrayList<>();
3095 Map<String, AlignFrame> dna = new HashMap<>();
3098 * Identify the DNA alignments
3100 for (Entry<Viewport, AlignFrame> candidate : splitFrameCandidates
3103 AlignFrame af = candidate.getValue();
3104 if (af.getViewport().getAlignment().isNucleotide())
3106 dna.put(candidate.getKey().getId(), af);
3111 * Try to match up the protein complements
3113 for (Entry<Viewport, AlignFrame> candidate : splitFrameCandidates
3116 AlignFrame af = candidate.getValue();
3117 if (!af.getViewport().getAlignment().isNucleotide())
3119 String complementId = candidate.getKey().getComplementId();
3120 // only non-null complements should be in the Map
3121 if (complementId != null && dna.containsKey(complementId))
3123 final AlignFrame dnaFrame = dna.get(complementId);
3124 SplitFrame sf = createSplitFrame(dnaFrame, af);
3125 addedToSplitFrames.add(dnaFrame);
3126 addedToSplitFrames.add(af);
3127 dnaFrame.setMenusForViewport();
3128 af.setMenusForViewport();
3129 if (af.getViewport().isGatherViewsHere())
3138 * Open any that we failed to pair up (which shouldn't happen!) as
3139 * standalone AlignFrame's.
3141 for (Entry<Viewport, AlignFrame> candidate : splitFrameCandidates
3144 AlignFrame af = candidate.getValue();
3145 if (!addedToSplitFrames.contains(af))
3147 Viewport view = candidate.getKey();
3148 Desktop.addInternalFrame(af, view.getTitle(),
3149 safeInt(view.getWidth()), safeInt(view.getHeight()));
3150 af.setMenusForViewport();
3151 System.err.println("Failed to restore view " + view.getTitle()
3152 + " to split frame");
3157 * Gather back into tabbed views as flagged.
3159 for (SplitFrame sf : gatherTo)
3161 Desktop.instance.gatherViews(sf);
3164 splitFrameCandidates.clear();
3168 * Construct and display one SplitFrame holding DNA and protein alignments.
3171 * @param proteinFrame
3174 protected SplitFrame createSplitFrame(AlignFrame dnaFrame,
3175 AlignFrame proteinFrame)
3177 SplitFrame splitFrame = new SplitFrame(dnaFrame, proteinFrame);
3178 String title = MessageManager.getString("label.linked_view_title");
3179 int width = (int) dnaFrame.getBounds().getWidth();
3180 int height = (int) (dnaFrame.getBounds().getHeight()
3181 + proteinFrame.getBounds().getHeight() + 50);
3184 * SplitFrame location is saved to both enclosed frames
3186 splitFrame.setLocation(dnaFrame.getX(), dnaFrame.getY());
3187 Desktop.addInternalFrame(splitFrame, title, width, height);
3190 * And compute cDNA consensus (couldn't do earlier with consensus as
3191 * mappings were not yet present)
3193 proteinFrame.getViewport().alignmentChanged(proteinFrame.alignPanel);
3199 * check errorMessage for a valid error message and raise an error box in the
3200 * GUI or write the current errorMessage to stderr and then clear the error
3203 protected void reportErrors()
3205 reportErrors(false);
3208 protected void reportErrors(final boolean saving)
3210 if (errorMessage != null)
3212 final String finalErrorMessage = errorMessage;
3215 javax.swing.SwingUtilities.invokeLater(new Runnable()
3220 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
3222 "Error " + (saving ? "saving" : "loading")
3224 JvOptionPane.WARNING_MESSAGE);
3230 System.err.println("Problem loading Jalview file: " + errorMessage);
3233 errorMessage = null;
3236 Map<String, String> alreadyLoadedPDB = new HashMap<>();
3239 * when set, local views will be updated from view stored in JalviewXML
3240 * Currently (28th Sep 2008) things will go horribly wrong in vamsas document
3241 * sync if this is set to true.
3243 private final boolean updateLocalViews = false;
3246 * Returns the path to a temporary file holding the PDB file for the given PDB
3247 * id. The first time of asking, searches for a file of that name in the
3248 * Jalview project jar, and copies it to a new temporary file. Any repeat
3249 * requests just return the path to the file previously created.
3255 String loadPDBFile(jarInputStreamProvider jprovider, String pdbId,
3258 if (alreadyLoadedPDB.containsKey(pdbId))
3260 return alreadyLoadedPDB.get(pdbId).toString();
3263 String tempFile = copyJarEntry(jprovider, pdbId, "jalview_pdb",
3265 if (tempFile != null)
3267 alreadyLoadedPDB.put(pdbId, tempFile);
3273 * Copies the jar entry of given name to a new temporary file and returns the
3274 * path to the file, or null if the entry is not found.
3277 * @param jarEntryName
3279 * a prefix for the temporary file name, must be at least three
3281 * @param suffixModel
3282 * null or original file - so new file can be given the same suffix
3286 protected String copyJarEntry(jarInputStreamProvider jprovider,
3287 String jarEntryName, String prefix, String suffixModel)
3289 String suffix = ".tmp";
3290 if (suffixModel == null)
3292 suffixModel = jarEntryName;
3294 int sfpos = suffixModel.lastIndexOf(".");
3295 if (sfpos > -1 && sfpos < (suffixModel.length() - 1))
3297 suffix = "." + suffixModel.substring(sfpos + 1);
3300 try (JarInputStream jin = jprovider.getJarInputStream())
3302 JarEntry entry = null;
3305 entry = jin.getNextJarEntry();
3306 } while (entry != null && !entry.getName().equals(jarEntryName));
3310 // in = new BufferedReader(new InputStreamReader(jin, UTF_8));
3311 File outFile = File.createTempFile(prefix, suffix);
3312 outFile.deleteOnExit();
3313 try (OutputStream os = new FileOutputStream(outFile))
3317 String t = outFile.getAbsolutePath();
3323 "Couldn't find entry in Jalview Jar for " + jarEntryName);
3325 } catch (Exception ex)
3327 ex.printStackTrace();
3333 private class JvAnnotRow
3335 public JvAnnotRow(int i, AlignmentAnnotation jaa)
3342 * persisted version of annotation row from which to take vis properties
3344 public jalview.datamodel.AlignmentAnnotation template;
3347 * original position of the annotation row in the alignment
3353 * Load alignment frame from jalview XML DOM object
3355 * @param jalviewModel
3358 * filename source string
3359 * @param loadTreesAndStructures
3360 * when false only create Viewport
3362 * data source provider
3363 * @return alignment frame created from view stored in DOM
3365 AlignFrame loadFromObject(JalviewModel jalviewModel, String file,
3366 boolean loadTreesAndStructures, jarInputStreamProvider jprovider)
3368 SequenceSet vamsasSet = jalviewModel.getVamsasModel().getSequenceSet()
3370 List<Sequence> vamsasSeqs = vamsasSet.getSequence();
3372 // JalviewModelSequence jms = object.getJalviewModelSequence();
3374 // Viewport view = (jms.getViewportCount() > 0) ? jms.getViewport(0)
3376 Viewport view = (jalviewModel.getViewport().size() > 0)
3377 ? jalviewModel.getViewport().get(0)
3380 // ////////////////////////////////
3381 // INITIALISE ALIGNMENT SEQUENCESETID AND VIEWID
3384 // If we just load in the same jar file again, the sequenceSetId
3385 // will be the same, and we end up with multiple references
3386 // to the same sequenceSet. We must modify this id on load
3387 // so that each load of the file gives a unique id
3390 * used to resolve correct alignment dataset for alignments with multiple
3393 String uniqueSeqSetId = null;
3394 String viewId = null;
3397 uniqueSeqSetId = view.getSequenceSetId() + uniqueSetSuffix;
3398 viewId = (view.getId() == null ? null
3399 : view.getId() + uniqueSetSuffix);
3402 // ////////////////////////////////
3405 List<SequenceI> hiddenSeqs = null;
3407 List<SequenceI> tmpseqs = new ArrayList<>();
3409 boolean multipleView = false;
3410 SequenceI referenceseqForView = null;
3411 // JSeq[] jseqs = object.getJalviewModelSequence().getJSeq();
3412 List<JSeq> jseqs = jalviewModel.getJSeq();
3413 int vi = 0; // counter in vamsasSeq array
3414 for (int i = 0; i < jseqs.size(); i++)
3416 JSeq jseq = jseqs.get(i);
3417 String seqId = jseq.getId();
3419 SequenceI tmpSeq = seqRefIds.get(seqId);
3422 if (!incompleteSeqs.containsKey(seqId))
3424 // may not need this check, but keep it for at least 2.9,1 release
3425 if (tmpSeq.getStart() != jseq.getStart()
3426 || tmpSeq.getEnd() != jseq.getEnd())
3428 System.err.println(String.format(
3429 "Warning JAL-2154 regression: updating start/end for sequence %s from %d/%d to %d/%d",
3430 tmpSeq.getName(), tmpSeq.getStart(), tmpSeq.getEnd(),
3431 jseq.getStart(), jseq.getEnd()));
3436 incompleteSeqs.remove(seqId);
3438 if (vamsasSeqs.size() > vi
3439 && vamsasSeqs.get(vi).getId().equals(seqId))
3441 // most likely we are reading a dataset XML document so
3442 // update from vamsasSeq section of XML for this sequence
3443 tmpSeq.setName(vamsasSeqs.get(vi).getName());
3444 tmpSeq.setDescription(vamsasSeqs.get(vi).getDescription());
3445 tmpSeq.setSequence(vamsasSeqs.get(vi).getSequence());
3450 // reading multiple views, so vamsasSeq set is a subset of JSeq
3451 multipleView = true;
3453 tmpSeq.setStart(jseq.getStart());
3454 tmpSeq.setEnd(jseq.getEnd());
3455 tmpseqs.add(tmpSeq);
3459 Sequence vamsasSeq = vamsasSeqs.get(vi);
3460 tmpSeq = new jalview.datamodel.Sequence(vamsasSeq.getName(),
3461 vamsasSeq.getSequence());
3462 tmpSeq.setDescription(vamsasSeq.getDescription());
3463 tmpSeq.setStart(jseq.getStart());
3464 tmpSeq.setEnd(jseq.getEnd());
3465 tmpSeq.setVamsasId(uniqueSetSuffix + seqId);
3466 seqRefIds.put(vamsasSeq.getId(), tmpSeq);
3467 tmpseqs.add(tmpSeq);
3471 if (safeBoolean(jseq.isViewreference()))
3473 referenceseqForView = tmpseqs.get(tmpseqs.size() - 1);
3476 if (jseq.isHidden() != null && jseq.isHidden().booleanValue())
3478 if (hiddenSeqs == null)
3480 hiddenSeqs = new ArrayList<>();
3483 hiddenSeqs.add(tmpSeq);
3488 // Create the alignment object from the sequence set
3489 // ///////////////////////////////
3490 SequenceI[] orderedSeqs = tmpseqs
3491 .toArray(new SequenceI[tmpseqs.size()]);
3493 AlignmentI al = null;
3494 // so we must create or recover the dataset alignment before going further
3495 // ///////////////////////////////
3496 if (vamsasSet.getDatasetId() == null || vamsasSet.getDatasetId() == "")
3498 // older jalview projects do not have a dataset - so creat alignment and
3500 al = new Alignment(orderedSeqs);
3501 al.setDataset(null);
3505 boolean isdsal = jalviewModel.getViewport().isEmpty();
3508 // we are importing a dataset record, so
3509 // recover reference to an alignment already materialsed as dataset
3510 al = getDatasetFor(vamsasSet.getDatasetId());
3514 // materialse the alignment
3515 al = new Alignment(orderedSeqs);
3519 addDatasetRef(vamsasSet.getDatasetId(), al);
3522 // finally, verify all data in vamsasSet is actually present in al
3523 // passing on flag indicating if it is actually a stored dataset
3524 recoverDatasetFor(vamsasSet, al, isdsal, uniqueSeqSetId);
3527 if (referenceseqForView != null)
3529 al.setSeqrep(referenceseqForView);
3531 // / Add the alignment properties
3532 for (int i = 0; i < vamsasSet.getSequenceSetProperties().size(); i++)
3534 SequenceSetProperties ssp = vamsasSet.getSequenceSetProperties()
3536 al.setProperty(ssp.getKey(), ssp.getValue());
3539 // ///////////////////////////////
3541 Hashtable pdbloaded = new Hashtable(); // TODO nothing writes to this??
3544 // load sequence features, database references and any associated PDB
3545 // structures for the alignment
3547 // prior to 2.10, this part would only be executed the first time a
3548 // sequence was encountered, but not afterwards.
3549 // now, for 2.10 projects, this is also done if the xml doc includes
3550 // dataset sequences not actually present in any particular view.
3552 for (int i = 0; i < vamsasSeqs.size(); i++)
3554 JSeq jseq = jseqs.get(i);
3555 if (jseq.getFeatures().size() > 0)
3557 List<Feature> features = jseq.getFeatures();
3558 for (int f = 0; f < features.size(); f++)
3560 Feature feat = features.get(f);
3561 SequenceFeature sf = new SequenceFeature(feat.getType(),
3562 feat.getDescription(), feat.getBegin(), feat.getEnd(),
3563 safeFloat(feat.getScore()), feat.getFeatureGroup());
3564 sf.setStatus(feat.getStatus());
3567 * load any feature attributes - include map-valued attributes
3569 Map<String, Map<String, String>> mapAttributes = new HashMap<>();
3570 for (int od = 0; od < feat.getOtherData().size(); od++)
3572 OtherData keyValue = feat.getOtherData().get(od);
3573 String attributeName = keyValue.getKey();
3574 String attributeValue = keyValue.getValue();
3575 if (attributeName.startsWith("LINK"))
3577 sf.addLink(attributeValue);
3581 String subAttribute = keyValue.getKey2();
3582 if (subAttribute == null)
3584 // simple string-valued attribute
3585 sf.setValue(attributeName, attributeValue);
3589 // attribute 'key' has sub-attribute 'key2'
3590 if (!mapAttributes.containsKey(attributeName))
3592 mapAttributes.put(attributeName, new HashMap<>());
3594 mapAttributes.get(attributeName).put(subAttribute,
3599 for (Entry<String, Map<String, String>> mapAttribute : mapAttributes
3602 sf.setValue(mapAttribute.getKey(), mapAttribute.getValue());
3605 // adds feature to datasequence's feature set (since Jalview 2.10)
3606 al.getSequenceAt(i).addSequenceFeature(sf);
3609 if (vamsasSeqs.get(i).getDBRef().size() > 0)
3611 // adds dbrefs to datasequence's set (since Jalview 2.10)
3613 al.getSequenceAt(i).getDatasetSequence() == null
3614 ? al.getSequenceAt(i)
3615 : al.getSequenceAt(i).getDatasetSequence(),
3618 if (jseq.getPdbids().size() > 0)
3620 List<Pdbids> ids = jseq.getPdbids();
3621 for (int p = 0; p < ids.size(); p++)
3623 Pdbids pdbid = ids.get(p);
3624 jalview.datamodel.PDBEntry entry = new jalview.datamodel.PDBEntry();
3625 entry.setId(pdbid.getId());
3626 if (pdbid.getType() != null)
3628 if (PDBEntry.Type.getType(pdbid.getType()) != null)
3630 entry.setType(PDBEntry.Type.getType(pdbid.getType()));
3634 entry.setType(PDBEntry.Type.FILE);
3637 // jprovider is null when executing 'New View'
3638 if (pdbid.getFile() != null && jprovider != null)
3640 if (!pdbloaded.containsKey(pdbid.getFile()))
3642 entry.setFile(loadPDBFile(jprovider, pdbid.getId(),
3647 entry.setFile(pdbloaded.get(pdbid.getId()).toString());
3651 if (pdbid.getPdbentryItem() != null)
3653 for (PdbentryItem item : pdbid.getPdbentryItem())
3655 for (Property pr : item.getProperty())
3657 entry.setProperty(pr.getName(), pr.getValue());
3662 for (Property prop : pdbid.getProperty())
3664 entry.setProperty(prop.getName(), prop.getValue());
3666 StructureSelectionManager
3667 .getStructureSelectionManager(Desktop.instance)
3668 .registerPDBEntry(entry);
3669 // adds PDBEntry to datasequence's set (since Jalview 2.10)
3670 if (al.getSequenceAt(i).getDatasetSequence() != null)
3672 al.getSequenceAt(i).getDatasetSequence().addPDBId(entry);
3676 al.getSequenceAt(i).addPDBId(entry);
3681 } // end !multipleview
3683 // ///////////////////////////////
3684 // LOAD SEQUENCE MAPPINGS
3686 if (vamsasSet.getAlcodonFrame().size() > 0)
3688 // TODO Potentially this should only be done once for all views of an
3690 List<AlcodonFrame> alc = vamsasSet.getAlcodonFrame();
3691 for (int i = 0; i < alc.size(); i++)
3693 AlignedCodonFrame cf = new AlignedCodonFrame();
3694 if (alc.get(i).getAlcodMap().size() > 0)
3696 List<AlcodMap> maps = alc.get(i).getAlcodMap();
3697 for (int m = 0; m < maps.size(); m++)
3699 AlcodMap map = maps.get(m);
3700 SequenceI dnaseq = seqRefIds.get(map.getDnasq());
3702 jalview.datamodel.Mapping mapping = null;
3703 // attach to dna sequence reference.
3704 if (map.getMapping() != null)
3706 mapping = addMapping(map.getMapping());
3707 if (dnaseq != null && mapping.getTo() != null)
3709 cf.addMap(dnaseq, mapping.getTo(), mapping.getMap());
3715 .add(newAlcodMapRef(map.getDnasq(), cf, mapping));
3719 al.addCodonFrame(cf);
3724 // ////////////////////////////////
3726 List<JvAnnotRow> autoAlan = new ArrayList<>();
3729 * store any annotations which forward reference a group's ID
3731 Map<String, List<AlignmentAnnotation>> groupAnnotRefs = new Hashtable<>();
3733 if (vamsasSet.getAnnotation().size()/*Count()*/ > 0)
3735 List<Annotation> an = vamsasSet.getAnnotation();
3737 for (int i = 0; i < an.size(); i++)
3739 Annotation annotation = an.get(i);
3742 * test if annotation is automatically calculated for this view only
3744 boolean autoForView = false;
3745 if (annotation.getLabel().equals("Quality")
3746 || annotation.getLabel().equals("Conservation")
3747 || annotation.getLabel().equals("Consensus"))
3749 // Kludge for pre 2.5 projects which lacked the autocalculated flag
3751 // JAXB has no has() test; schema defaults value to false
3752 // if (!annotation.hasAutoCalculated())
3754 // annotation.setAutoCalculated(true);
3757 if (autoForView || annotation.isAutoCalculated())
3759 // remove ID - we don't recover annotation from other views for
3760 // view-specific annotation
3761 annotation.setId(null);
3764 // set visibility for other annotation in this view
3765 String annotationId = annotation.getId();
3766 if (annotationId != null && annotationIds.containsKey(annotationId))
3768 AlignmentAnnotation jda = annotationIds.get(annotationId);
3769 // in principle Visible should always be true for annotation displayed
3770 // in multiple views
3771 if (annotation.isVisible() != null)
3773 jda.visible = annotation.isVisible();
3776 al.addAnnotation(jda);
3780 // Construct new annotation from model.
3781 List<AnnotationElement> ae = annotation.getAnnotationElement();
3782 jalview.datamodel.Annotation[] anot = null;
3783 java.awt.Color firstColour = null;
3785 if (!annotation.isScoreOnly())
3787 anot = new jalview.datamodel.Annotation[al.getWidth()];
3788 for (int aa = 0; aa < ae.size() && aa < anot.length; aa++)
3790 AnnotationElement annElement = ae.get(aa);
3791 anpos = annElement.getPosition();
3793 if (anpos >= anot.length)
3798 float value = safeFloat(annElement.getValue());
3799 anot[anpos] = new jalview.datamodel.Annotation(
3800 annElement.getDisplayCharacter(),
3801 annElement.getDescription(),
3802 (annElement.getSecondaryStructure() == null
3803 || annElement.getSecondaryStructure()
3807 .getSecondaryStructure()
3810 anot[anpos].colour = new Color(safeInt(annElement.getColour()));
3811 if (firstColour == null)
3813 firstColour = anot[anpos].colour;
3817 jalview.datamodel.AlignmentAnnotation jaa = null;
3819 if (annotation.isGraph())
3821 float llim = 0, hlim = 0;
3822 // if (autoForView || an[i].isAutoCalculated()) {
3825 jaa = new jalview.datamodel.AlignmentAnnotation(
3826 annotation.getLabel(), annotation.getDescription(), anot,
3827 llim, hlim, safeInt(annotation.getGraphType()));
3829 jaa.graphGroup = safeInt(annotation.getGraphGroup());
3830 jaa._linecolour = firstColour;
3831 if (annotation.getThresholdLine() != null)
3833 jaa.setThreshold(new jalview.datamodel.GraphLine(
3834 safeFloat(annotation.getThresholdLine().getValue()),
3835 annotation.getThresholdLine().getLabel(),
3836 new java.awt.Color(safeInt(
3837 annotation.getThresholdLine().getColour()))));
3839 if (autoForView || annotation.isAutoCalculated())
3841 // Hardwire the symbol display line to ensure that labels for
3842 // histograms are displayed
3848 jaa = new jalview.datamodel.AlignmentAnnotation(
3849 annotation.getLabel(), annotation.getDescription(), anot);
3850 jaa._linecolour = firstColour;
3852 // register new annotation
3853 if (annotation.getId() != null)
3855 annotationIds.put(annotation.getId(), jaa);
3856 jaa.annotationId = annotation.getId();
3858 // recover sequence association
3859 String sequenceRef = annotation.getSequenceRef();
3860 if (sequenceRef != null)
3862 // from 2.9 sequenceRef is to sequence id (JAL-1781)
3863 SequenceI sequence = seqRefIds.get(sequenceRef);
3864 if (sequence == null)
3866 // in pre-2.9 projects sequence ref is to sequence name
3867 sequence = al.findName(sequenceRef);
3869 if (sequence != null)
3871 jaa.createSequenceMapping(sequence, 1, true);
3872 sequence.addAlignmentAnnotation(jaa);
3875 // and make a note of any group association
3876 if (annotation.getGroupRef() != null
3877 && annotation.getGroupRef().length() > 0)
3879 List<jalview.datamodel.AlignmentAnnotation> aal = groupAnnotRefs
3880 .get(annotation.getGroupRef());
3883 aal = new ArrayList<>();
3884 groupAnnotRefs.put(annotation.getGroupRef(), aal);
3889 if (annotation.getScore() != null)
3891 jaa.setScore(annotation.getScore().doubleValue());
3893 if (annotation.isVisible() != null)
3895 jaa.visible = annotation.isVisible().booleanValue();
3898 if (annotation.isCentreColLabels() != null)
3900 jaa.centreColLabels = annotation.isCentreColLabels()
3904 if (annotation.isScaleColLabels() != null)
3906 jaa.scaleColLabel = annotation.isScaleColLabels().booleanValue();
3908 if (annotation.isAutoCalculated())
3910 // newer files have an 'autoCalculated' flag and store calculation
3911 // state in viewport properties
3912 jaa.autoCalculated = true; // means annotation will be marked for
3913 // update at end of load.
3915 if (annotation.getGraphHeight() != null)
3917 jaa.graphHeight = annotation.getGraphHeight().intValue();
3919 jaa.belowAlignment = annotation.isBelowAlignment();
3920 jaa.setCalcId(annotation.getCalcId());
3921 if (annotation.getProperty().size() > 0)
3923 for (Annotation.Property prop : annotation.getProperty())
3925 jaa.setProperty(prop.getName(), prop.getValue());
3928 if (jaa.graph == AlignmentAnnotation.CONTACT_MAP)
3930 if (annotation.getContactmatrix() != null
3931 && annotation.getContactmatrix().size() > 0)
3933 for (MatrixType xmlmat : annotation.getContactmatrix())
3935 if (PAEContactMatrix.PAEMATRIX.equals(xmlmat.getType()))
3937 if (!xmlmat.getRows().equals(xmlmat.getCols()))
3939 Console.error("Can't handle non square PAE Matrices");
3943 float[][] elements = ContactMatrix
3944 .fromFloatStringToContacts(xmlmat.getValue(),
3945 xmlmat.getCols().intValue(),
3946 xmlmat.getRows().intValue());
3948 PAEContactMatrix newpae = new PAEContactMatrix(
3949 jaa.sequenceRef, elements);
3950 jaa.sequenceRef.addContactListFor(jaa, newpae);
3955 Console.error("Ignoring CONTACT_MAP annotation with type "
3956 + xmlmat.getType());
3962 if (jaa.autoCalculated)
3964 autoAlan.add(new JvAnnotRow(i, jaa));
3967 // if (!autoForView)
3969 // add autocalculated group annotation and any user created annotation
3971 al.addAnnotation(jaa);
3975 // ///////////////////////
3977 // Create alignment markup and styles for this view
3978 if (jalviewModel.getJGroup().size() > 0)
3980 List<JGroup> groups = jalviewModel.getJGroup();
3981 boolean addAnnotSchemeGroup = false;
3982 for (int i = 0; i < groups.size(); i++)
3984 JGroup jGroup = groups.get(i);
3985 ColourSchemeI cs = null;
3986 if (jGroup.getColour() != null)
3988 if (jGroup.getColour().startsWith("ucs"))
3990 cs = getUserColourScheme(jalviewModel, jGroup.getColour());
3992 else if (jGroup.getColour().equals("AnnotationColourGradient")
3993 && jGroup.getAnnotationColours() != null)
3995 addAnnotSchemeGroup = true;
3999 cs = ColourSchemeProperty.getColourScheme(null, al,
4000 jGroup.getColour());
4003 int pidThreshold = safeInt(jGroup.getPidThreshold());
4005 Vector<SequenceI> seqs = new Vector<>();
4007 for (int s = 0; s < jGroup.getSeq().size(); s++)
4009 String seqId = jGroup.getSeq().get(s);
4010 SequenceI ts = seqRefIds.get(seqId);
4014 seqs.addElement(ts);
4018 if (seqs.size() < 1)
4023 SequenceGroup sg = new SequenceGroup(seqs, jGroup.getName(), cs,
4024 safeBoolean(jGroup.isDisplayBoxes()),
4025 safeBoolean(jGroup.isDisplayText()),
4026 safeBoolean(jGroup.isColourText()),
4027 safeInt(jGroup.getStart()), safeInt(jGroup.getEnd()));
4028 sg.getGroupColourScheme().setThreshold(pidThreshold, true);
4029 sg.getGroupColourScheme()
4030 .setConservationInc(safeInt(jGroup.getConsThreshold()));
4031 sg.setOutlineColour(new Color(safeInt(jGroup.getOutlineColour())));
4033 sg.textColour = new Color(safeInt(jGroup.getTextCol1()));
4034 sg.textColour2 = new Color(safeInt(jGroup.getTextCol2()));
4035 sg.setShowNonconserved(safeBoolean(jGroup.isShowUnconserved()));
4036 sg.thresholdTextColour = safeInt(jGroup.getTextColThreshold());
4037 // attributes with a default in the schema are never null
4038 sg.setShowConsensusHistogram(jGroup.isShowConsensusHistogram());
4039 sg.setshowSequenceLogo(jGroup.isShowSequenceLogo());
4040 sg.setNormaliseSequenceLogo(jGroup.isNormaliseSequenceLogo());
4041 sg.setIgnoreGapsConsensus(jGroup.isIgnoreGapsinConsensus());
4042 if (jGroup.getConsThreshold() != null
4043 && jGroup.getConsThreshold().intValue() != 0)
4045 Conservation c = new Conservation("All", sg.getSequences(null), 0,
4048 c.verdict(false, 25);
4049 sg.cs.setConservation(c);
4052 if (jGroup.getId() != null && groupAnnotRefs.size() > 0)
4054 // re-instate unique group/annotation row reference
4055 List<AlignmentAnnotation> jaal = groupAnnotRefs
4056 .get(jGroup.getId());
4059 for (AlignmentAnnotation jaa : jaal)
4062 if (jaa.autoCalculated)
4064 // match up and try to set group autocalc alignment row for this
4066 if (jaa.label.startsWith("Consensus for "))
4068 sg.setConsensus(jaa);
4070 // match up and try to set group autocalc alignment row for this
4072 if (jaa.label.startsWith("Conservation for "))
4074 sg.setConservationRow(jaa);
4081 if (addAnnotSchemeGroup)
4083 // reconstruct the annotation colourscheme
4085 constructAnnotationColour(jGroup.getAnnotationColours(),
4086 null, al, jalviewModel, false));
4092 // only dataset in this model, so just return.
4095 // ///////////////////////////////
4098 AlignFrame af = null;
4099 AlignViewport av = null;
4100 // now check to see if we really need to create a new viewport.
4101 if (multipleView && viewportsAdded.size() == 0)
4103 // We recovered an alignment for which a viewport already exists.
4104 // TODO: fix up any settings necessary for overlaying stored state onto
4105 // state recovered from another document. (may not be necessary).
4106 // we may need a binding from a viewport in memory to one recovered from
4108 // and then recover its containing af to allow the settings to be applied.
4109 // TODO: fix for vamsas demo
4111 "About to recover a viewport for existing alignment: Sequence set ID is "
4113 Object seqsetobj = retrieveExistingObj(uniqueSeqSetId);
4114 if (seqsetobj != null)
4116 if (seqsetobj instanceof String)
4118 uniqueSeqSetId = (String) seqsetobj;
4120 "Recovered extant sequence set ID mapping for ID : New Sequence set ID is "
4126 "Warning : Collision between sequence set ID string and existing jalview object mapping.");
4132 * indicate that annotation colours are applied across all groups (pre
4133 * Jalview 2.8.1 behaviour)
4135 boolean doGroupAnnColour = Jalview2XML.isVersionStringLaterThan("2.8.1",
4136 jalviewModel.getVersion());
4138 AlignmentPanel ap = null;
4139 boolean isnewview = true;
4142 // Check to see if this alignment already has a view id == viewId
4143 jalview.gui.AlignmentPanel views[] = Desktop
4144 .getAlignmentPanels(uniqueSeqSetId);
4145 if (views != null && views.length > 0)
4147 for (int v = 0; v < views.length; v++)
4149 if (views[v].av.getViewId().equalsIgnoreCase(viewId))
4151 // recover the existing alignpanel, alignframe, viewport
4152 af = views[v].alignFrame;
4155 // TODO: could even skip resetting view settings if we don't want to
4156 // change the local settings from other jalview processes
4165 af = loadViewport(file, jseqs, hiddenSeqs, al, jalviewModel, view,
4166 uniqueSeqSetId, viewId, autoAlan);
4167 av = af.getViewport();
4172 * Load any trees, PDB structures and viewers, Overview
4174 * Not done if flag is false (when this method is used for New View)
4176 if (loadTreesAndStructures)
4178 loadTrees(jalviewModel, view, af, av, ap);
4179 loadPCAViewers(jalviewModel, ap);
4180 loadPDBStructures(jprovider, jseqs, af, ap);
4181 loadRnaViewers(jprovider, jseqs, ap);
4182 loadOverview(view, jalviewModel.getVersion(), af);
4184 // and finally return.
4189 * Load Overview window, restoring colours, 'show hidden regions' flag, title
4190 * and geometry as saved
4195 protected void loadOverview(Viewport view, String version, AlignFrame af)
4197 if (!isVersionStringLaterThan("2.11.3",
4198 version) && view.getOverview()==null)
4203 * first close any Overview that was opened automatically
4204 * (if so configured in Preferences) so that the view is
4205 * restored in the same state as saved
4207 af.alignPanel.closeOverviewPanel();
4209 Overview overview = view.getOverview();
4210 if (overview != null)
4212 OverviewPanel overviewPanel = af
4213 .openOverviewPanel(overview.isShowHidden());
4214 overviewPanel.setTitle(overview.getTitle());
4215 overviewPanel.setFrameBounds(overview.getXpos(), overview.getYpos(),
4216 overview.getWidth(), overview.getHeight());
4217 Color gap = new Color(overview.getGapColour());
4218 Color residue = new Color(overview.getResidueColour());
4219 Color hidden = new Color(overview.getHiddenColour());
4220 overviewPanel.getCanvas().setColours(gap, residue, hidden);
4225 * Instantiate and link any saved RNA (Varna) viewers. The state of the Varna
4226 * panel is restored from separate jar entries, two (gapped and trimmed) per
4227 * sequence and secondary structure.
4229 * Currently each viewer shows just one sequence and structure (gapped and
4230 * trimmed), however this method is designed to support multiple sequences or
4231 * structures in viewers if wanted in future.
4237 private void loadRnaViewers(jarInputStreamProvider jprovider,
4238 List<JSeq> jseqs, AlignmentPanel ap)
4241 * scan the sequences for references to viewers; create each one the first
4242 * time it is referenced, add Rna models to existing viewers
4244 for (JSeq jseq : jseqs)
4246 for (int i = 0; i < jseq.getRnaViewer().size(); i++)
4248 RnaViewer viewer = jseq.getRnaViewer().get(i);
4249 AppVarna appVarna = findOrCreateVarnaViewer(viewer, uniqueSetSuffix,
4252 for (int j = 0; j < viewer.getSecondaryStructure().size(); j++)
4254 SecondaryStructure ss = viewer.getSecondaryStructure().get(j);
4255 SequenceI seq = seqRefIds.get(jseq.getId());
4256 AlignmentAnnotation ann = this.annotationIds
4257 .get(ss.getAnnotationId());
4260 * add the structure to the Varna display (with session state copied
4261 * from the jar to a temporary file)
4263 boolean gapped = safeBoolean(ss.isGapped());
4264 String rnaTitle = ss.getTitle();
4265 String sessionState = ss.getViewerState();
4266 String tempStateFile = copyJarEntry(jprovider, sessionState,
4268 RnaModel rna = new RnaModel(rnaTitle, ann, seq, null, gapped);
4269 appVarna.addModelSession(rna, rnaTitle, tempStateFile);
4271 appVarna.setInitialSelection(safeInt(viewer.getSelectedRna()));
4277 * Locate and return an already instantiated matching AppVarna, or create one
4281 * @param viewIdSuffix
4285 protected AppVarna findOrCreateVarnaViewer(RnaViewer viewer,
4286 String viewIdSuffix, AlignmentPanel ap)
4289 * on each load a suffix is appended to the saved viewId, to avoid conflicts
4290 * if load is repeated
4292 String postLoadId = viewer.getViewId() + viewIdSuffix;
4293 for (JInternalFrame frame : getAllFrames())
4295 if (frame instanceof AppVarna)
4297 AppVarna varna = (AppVarna) frame;
4298 if (postLoadId.equals(varna.getViewId()))
4300 // this viewer is already instantiated
4301 // could in future here add ap as another 'parent' of the
4302 // AppVarna window; currently just 1-to-many
4309 * viewer not found - make it
4311 RnaViewerModel model = new RnaViewerModel(postLoadId, viewer.getTitle(),
4312 safeInt(viewer.getXpos()), safeInt(viewer.getYpos()),
4313 safeInt(viewer.getWidth()), safeInt(viewer.getHeight()),
4314 safeInt(viewer.getDividerLocation()));
4315 AppVarna varna = new AppVarna(model, ap);
4321 * Load any saved trees
4329 protected void loadTrees(JalviewModel jm, Viewport view, AlignFrame af,
4330 AlignViewport av, AlignmentPanel ap)
4332 // TODO result of automated refactoring - are all these parameters needed?
4335 for (int t = 0; t < jm.getTree().size(); t++)
4338 Tree tree = jm.getTree().get(t);
4340 TreePanel tp = (TreePanel) retrieveExistingObj(tree.getId());
4343 tp = af.showNewickTree(new NewickFile(tree.getNewick()),
4344 tree.getTitle(), safeInt(tree.getWidth()),
4345 safeInt(tree.getHeight()), safeInt(tree.getXpos()),
4346 safeInt(tree.getYpos()));
4347 if (tree.getId() != null)
4349 // perhaps bind the tree id to something ?
4354 // update local tree attributes ?
4355 // TODO: should check if tp has been manipulated by user - if so its
4356 // settings shouldn't be modified
4357 tp.setTitle(tree.getTitle());
4358 tp.setBounds(new Rectangle(safeInt(tree.getXpos()),
4359 safeInt(tree.getYpos()), safeInt(tree.getWidth()),
4360 safeInt(tree.getHeight())));
4361 tp.setViewport(av); // af.viewport;
4362 // TODO: verify 'associate with all views' works still
4363 tp.getTreeCanvas().setViewport(av); // af.viewport;
4364 tp.getTreeCanvas().setAssociatedPanel(ap); // af.alignPanel;
4366 tp.getTreeCanvas().setApplyToAllViews(tree.isLinkToAllViews());
4370 "There was a problem recovering stored Newick tree: \n"
4371 + tree.getNewick());
4375 tp.fitToWindow.setState(safeBoolean(tree.isFitToWindow()));
4376 tp.fitToWindow_actionPerformed(null);
4378 if (tree.getFontName() != null)
4381 new Font(tree.getFontName(), safeInt(tree.getFontStyle()),
4382 safeInt(tree.getFontSize())));
4387 new Font(view.getFontName(), safeInt(view.getFontStyle()),
4388 safeInt(view.getFontSize())));
4391 tp.showPlaceholders(safeBoolean(tree.isMarkUnlinked()));
4392 tp.showBootstrap(safeBoolean(tree.isShowBootstrap()));
4393 tp.showDistances(safeBoolean(tree.isShowDistances()));
4395 tp.getTreeCanvas().setThreshold(safeFloat(tree.getThreshold()));
4397 if (safeBoolean(tree.isCurrentTree()))
4399 af.getViewport().setCurrentTree(tp.getTree());
4403 } catch (Exception ex)
4405 ex.printStackTrace();
4410 * Load and link any saved structure viewers.
4417 protected void loadPDBStructures(jarInputStreamProvider jprovider,
4418 List<JSeq> jseqs, AlignFrame af, AlignmentPanel ap)
4421 * Run through all PDB ids on the alignment, and collect mappings between
4422 * distinct view ids and all sequences referring to that view.
4424 Map<String, StructureViewerModel> structureViewers = new LinkedHashMap<>();
4426 for (int i = 0; i < jseqs.size(); i++)
4428 JSeq jseq = jseqs.get(i);
4429 if (jseq.getPdbids().size() > 0)
4431 List<Pdbids> ids = jseq.getPdbids();
4432 for (int p = 0; p < ids.size(); p++)
4434 Pdbids pdbid = ids.get(p);
4435 final int structureStateCount = pdbid.getStructureState().size();
4436 for (int s = 0; s < structureStateCount; s++)
4438 // check to see if we haven't already created this structure view
4439 final StructureState structureState = pdbid.getStructureState()
4441 String sviewid = (structureState.getViewId() == null) ? null
4442 : structureState.getViewId() + uniqueSetSuffix;
4443 jalview.datamodel.PDBEntry jpdb = new jalview.datamodel.PDBEntry();
4444 // Originally : pdbid.getFile()
4445 // : TODO: verify external PDB file recovery still works in normal
4446 // jalview project load
4448 loadPDBFile(jprovider, pdbid.getId(), pdbid.getFile()));
4449 jpdb.setId(pdbid.getId());
4451 int x = safeInt(structureState.getXpos());
4452 int y = safeInt(structureState.getYpos());
4453 int width = safeInt(structureState.getWidth());
4454 int height = safeInt(structureState.getHeight());
4456 // Probably don't need to do this anymore...
4457 // Desktop.desktop.getComponentAt(x, y);
4458 // TODO: NOW: check that this recovers the PDB file correctly.
4459 String pdbFile = loadPDBFile(jprovider, pdbid.getId(),
4461 jalview.datamodel.SequenceI seq = seqRefIds
4462 .get(jseq.getId() + "");
4463 if (sviewid == null)
4465 sviewid = "_jalview_pre2_4_" + x + "," + y + "," + width + ","
4468 if (!structureViewers.containsKey(sviewid))
4470 String viewerType = structureState.getType();
4471 if (viewerType == null) // pre Jalview 2.9
4473 viewerType = ViewerType.JMOL.toString();
4475 structureViewers.put(sviewid,
4476 new StructureViewerModel(x, y, width, height, false,
4477 false, true, structureState.getViewId(),
4479 // Legacy pre-2.7 conversion JAL-823 :
4480 // do not assume any view has to be linked for colour by
4484 // assemble String[] { pdb files }, String[] { id for each
4485 // file }, orig_fileloc, SequenceI[][] {{ seqs_file 1 }, {
4486 // seqs_file 2}, boolean[] {
4487 // linkAlignPanel,superposeWithAlignpanel}} from hash
4488 StructureViewerModel jmoldat = structureViewers.get(sviewid);
4489 jmoldat.setAlignWithPanel(jmoldat.isAlignWithPanel()
4490 || structureState.isAlignwithAlignPanel());
4493 * Default colour by linked panel to false if not specified (e.g.
4494 * for pre-2.7 projects)
4496 boolean colourWithAlignPanel = jmoldat.isColourWithAlignPanel();
4497 colourWithAlignPanel |= structureState.isColourwithAlignPanel();
4498 jmoldat.setColourWithAlignPanel(colourWithAlignPanel);
4501 * Default colour by viewer to true if not specified (e.g. for
4504 boolean colourByViewer = jmoldat.isColourByViewer();
4505 colourByViewer &= structureState.isColourByJmol();
4506 jmoldat.setColourByViewer(colourByViewer);
4508 if (jmoldat.getStateData().length() < structureState.getValue()
4509 /*Content()*/.length())
4511 jmoldat.setStateData(structureState.getValue());// Content());
4513 if (pdbid.getFile() != null)
4515 File mapkey = new File(pdbid.getFile());
4516 StructureData seqstrmaps = jmoldat.getFileData().get(mapkey);
4517 if (seqstrmaps == null)
4519 jmoldat.getFileData().put(mapkey,
4520 seqstrmaps = jmoldat.new StructureData(pdbFile,
4523 if (!seqstrmaps.getSeqList().contains(seq))
4525 seqstrmaps.getSeqList().add(seq);
4531 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");
4532 Console.warn(errorMessage);
4538 // Instantiate the associated structure views
4539 for (Entry<String, StructureViewerModel> entry : structureViewers
4544 createOrLinkStructureViewer(entry, af, ap, jprovider);
4545 } catch (Exception e)
4548 "Error loading structure viewer: " + e.getMessage());
4549 // failed - try the next one
4561 protected void createOrLinkStructureViewer(
4562 Entry<String, StructureViewerModel> viewerData, AlignFrame af,
4563 AlignmentPanel ap, jarInputStreamProvider jprovider)
4565 final StructureViewerModel stateData = viewerData.getValue();
4568 * Search for any viewer windows already open from other alignment views
4569 * that exactly match the stored structure state
4571 StructureViewerBase comp = findMatchingViewer(viewerData);
4575 linkStructureViewer(ap, comp, stateData);
4579 String type = stateData.getType();
4582 ViewerType viewerType = ViewerType.valueOf(type);
4583 createStructureViewer(viewerType, viewerData, af, jprovider);
4584 } catch (IllegalArgumentException | NullPointerException e)
4586 // TODO JAL-3619 show error dialog / offer an alternative viewer
4587 Console.error("Invalid structure viewer type: " + type);
4592 * Generates a name for the entry in the project jar file to hold state
4593 * information for a structure viewer
4598 protected String getViewerJarEntryName(String viewId)
4600 return VIEWER_PREFIX + viewId;
4604 * Returns any open frame that matches given structure viewer data. The match
4605 * is based on the unique viewId, or (for older project versions) the frame's
4611 protected StructureViewerBase findMatchingViewer(
4612 Entry<String, StructureViewerModel> viewerData)
4614 final String sviewid = viewerData.getKey();
4615 final StructureViewerModel svattrib = viewerData.getValue();
4616 StructureViewerBase comp = null;
4617 JInternalFrame[] frames = getAllFrames();
4618 for (JInternalFrame frame : frames)
4620 if (frame instanceof StructureViewerBase)
4623 * Post jalview 2.4 schema includes structure view id
4625 if (sviewid != null && ((StructureViewerBase) frame).getViewId()
4628 comp = (StructureViewerBase) frame;
4629 break; // break added in 2.9
4632 * Otherwise test for matching position and size of viewer frame
4634 else if (frame.getX() == svattrib.getX()
4635 && frame.getY() == svattrib.getY()
4636 && frame.getHeight() == svattrib.getHeight()
4637 && frame.getWidth() == svattrib.getWidth())
4639 comp = (StructureViewerBase) frame;
4640 // no break in faint hope of an exact match on viewId
4648 * Link an AlignmentPanel to an existing structure viewer.
4653 * @param useinViewerSuperpos
4654 * @param usetoColourbyseq
4655 * @param viewerColouring
4657 protected void linkStructureViewer(AlignmentPanel ap,
4658 StructureViewerBase viewer, StructureViewerModel stateData)
4660 // NOTE: if the jalview project is part of a shared session then
4661 // view synchronization should/could be done here.
4663 final boolean useinViewerSuperpos = stateData.isAlignWithPanel();
4664 final boolean usetoColourbyseq = stateData.isColourWithAlignPanel();
4665 final boolean viewerColouring = stateData.isColourByViewer();
4666 Map<File, StructureData> oldFiles = stateData.getFileData();
4669 * Add mapping for sequences in this view to an already open viewer
4671 final AAStructureBindingModel binding = viewer.getBinding();
4672 for (File id : oldFiles.keySet())
4674 // add this and any other pdb files that should be present in the
4676 StructureData filedat = oldFiles.get(id);
4677 String pdbFile = filedat.getFilePath();
4678 SequenceI[] seq = filedat.getSeqList().toArray(new SequenceI[0]);
4679 binding.getSsm().setMapping(seq, null, pdbFile, DataSourceType.FILE,
4681 binding.addSequenceForStructFile(pdbFile, seq);
4683 // and add the AlignmentPanel's reference to the view panel
4684 viewer.addAlignmentPanel(ap);
4685 if (useinViewerSuperpos)
4687 viewer.useAlignmentPanelForSuperposition(ap);
4691 viewer.excludeAlignmentPanelForSuperposition(ap);
4693 if (usetoColourbyseq)
4695 viewer.useAlignmentPanelForColourbyseq(ap, !viewerColouring);
4699 viewer.excludeAlignmentPanelForColourbyseq(ap);
4704 * Get all frames within the Desktop.
4708 protected JInternalFrame[] getAllFrames()
4710 JInternalFrame[] frames = null;
4711 // TODO is this necessary - is it safe - risk of hanging?
4716 frames = Desktop.desktop.getAllFrames();
4717 } catch (ArrayIndexOutOfBoundsException e)
4719 // occasional No such child exceptions are thrown here...
4723 } catch (InterruptedException f)
4727 } while (frames == null);
4732 * Answers true if 'version' is equal to or later than 'supported', where each
4733 * is formatted as major/minor versions like "2.8.3" or "2.3.4b1" for bugfix
4734 * changes. Development and test values for 'version' are leniently treated
4738 * - minimum version we are comparing against
4740 * - version of data being processsed
4741 * @return true if version is equal to or later than supported
4743 public static boolean isVersionStringLaterThan(String supported,
4746 if (supported == null || version == null
4747 || version.equalsIgnoreCase("DEVELOPMENT BUILD")
4748 || version.equalsIgnoreCase("Test")
4749 || version.equalsIgnoreCase("AUTOMATED BUILD"))
4751 System.err.println("Assuming project file with "
4752 + (version == null ? "null" : version)
4753 + " is compatible with Jalview version " + supported);
4758 return StringUtils.compareVersions(version, supported, "b") >= 0;
4762 Vector<JalviewStructureDisplayI> newStructureViewers = null;
4764 protected void addNewStructureViewer(JalviewStructureDisplayI sview)
4766 if (newStructureViewers != null)
4768 sview.getBinding().setFinishedLoadingFromArchive(false);
4769 newStructureViewers.add(sview);
4773 protected void setLoadingFinishedForNewStructureViewers()
4775 if (newStructureViewers != null)
4777 for (JalviewStructureDisplayI sview : newStructureViewers)
4779 sview.getBinding().setFinishedLoadingFromArchive(true);
4781 newStructureViewers.clear();
4782 newStructureViewers = null;
4786 AlignFrame loadViewport(String file, List<JSeq> JSEQ,
4787 List<SequenceI> hiddenSeqs, AlignmentI al, JalviewModel jm,
4788 Viewport view, String uniqueSeqSetId, String viewId,
4789 List<JvAnnotRow> autoAlan)
4791 AlignFrame af = null;
4792 af = new AlignFrame(al, safeInt(view.getWidth()),
4793 safeInt(view.getHeight()), uniqueSeqSetId, viewId)
4797 // protected void processKeyEvent(java.awt.event.KeyEvent e) {
4798 // System.out.println("Jalview2XML AF " + e);
4799 // super.processKeyEvent(e);
4806 af.setFileName(file, FileFormat.Jalview);
4808 final AlignViewport viewport = af.getViewport();
4809 for (int i = 0; i < JSEQ.size(); i++)
4811 int colour = safeInt(JSEQ.get(i).getColour());
4812 viewport.setSequenceColour(viewport.getAlignment().getSequenceAt(i),
4818 viewport.setColourByReferenceSeq(true);
4819 viewport.setDisplayReferenceSeq(true);
4822 viewport.setGatherViewsHere(safeBoolean(view.isGatheredViews()));
4824 if (view.getSequenceSetId() != null)
4826 AlignmentViewport av = viewportsAdded.get(uniqueSeqSetId);
4828 viewport.setSequenceSetId(uniqueSeqSetId);
4831 // propagate shared settings to this new view
4832 viewport.setHistoryList(av.getHistoryList());
4833 viewport.setRedoList(av.getRedoList());
4837 viewportsAdded.put(uniqueSeqSetId, viewport);
4839 // TODO: check if this method can be called repeatedly without
4840 // side-effects if alignpanel already registered.
4841 PaintRefresher.Register(af.alignPanel, uniqueSeqSetId);
4843 // apply Hidden regions to view.
4844 if (hiddenSeqs != null)
4846 for (int s = 0; s < JSEQ.size(); s++)
4848 SequenceGroup hidden = new SequenceGroup();
4849 boolean isRepresentative = false;
4850 for (int r = 0; r < JSEQ.get(s).getHiddenSequences().size(); r++)
4852 isRepresentative = true;
4853 SequenceI sequenceToHide = al
4854 .getSequenceAt(JSEQ.get(s).getHiddenSequences().get(r));
4855 hidden.addSequence(sequenceToHide, false);
4856 // remove from hiddenSeqs list so we don't try to hide it twice
4857 hiddenSeqs.remove(sequenceToHide);
4859 if (isRepresentative)
4861 SequenceI representativeSequence = al.getSequenceAt(s);
4862 hidden.addSequence(representativeSequence, false);
4863 viewport.hideRepSequences(representativeSequence, hidden);
4867 SequenceI[] hseqs = hiddenSeqs
4868 .toArray(new SequenceI[hiddenSeqs.size()]);
4869 viewport.hideSequence(hseqs);
4872 // recover view properties and display parameters
4874 viewport.setShowAnnotation(safeBoolean(view.isShowAnnotation()));
4875 viewport.setAbovePIDThreshold(safeBoolean(view.isPidSelected()));
4876 final int pidThreshold = safeInt(view.getPidThreshold());
4877 viewport.setThreshold(pidThreshold);
4879 viewport.setColourText(safeBoolean(view.isShowColourText()));
4881 viewport.setConservationSelected(
4882 safeBoolean(view.isConservationSelected()));
4883 viewport.setIncrement(safeInt(view.getConsThreshold()));
4884 viewport.setShowJVSuffix(safeBoolean(view.isShowFullId()));
4885 viewport.setRightAlignIds(safeBoolean(view.isRightAlignIds()));
4886 viewport.setFont(new Font(view.getFontName(),
4887 safeInt(view.getFontStyle()), safeInt(view.getFontSize())),
4888 (view.getCharWidth()!=null) ? false : true);
4889 if (view.getCharWidth()!=null)
4891 viewport.setCharWidth(view.getCharWidth());
4892 viewport.setCharHeight(view.getCharHeight());
4894 ViewStyleI vs = viewport.getViewStyle();
4895 vs.setScaleProteinAsCdna(view.isScaleProteinAsCdna());
4896 viewport.setViewStyle(vs);
4897 // TODO: allow custom charWidth/Heights to be restored by updating them
4898 // after setting font - which means set above to false
4899 viewport.setRenderGaps(safeBoolean(view.isRenderGaps()));
4900 viewport.setWrapAlignment(safeBoolean(view.isWrapAlignment()));
4901 viewport.setShowAnnotation(safeBoolean(view.isShowAnnotation()));
4903 viewport.setShowBoxes(safeBoolean(view.isShowBoxes()));
4905 viewport.setShowText(safeBoolean(view.isShowText()));
4907 viewport.setTextColour(new Color(safeInt(view.getTextCol1())));
4908 viewport.setTextColour2(new Color(safeInt(view.getTextCol2())));
4909 viewport.setThresholdTextColour(safeInt(view.getTextColThreshold()));
4910 viewport.setShowUnconserved(view.isShowUnconserved());
4911 viewport.getRanges().setStartRes(safeInt(view.getStartRes()));
4913 if (view.getViewName() != null)
4915 viewport.setViewName(view.getViewName());
4916 af.setInitialTabVisible();
4918 af.setBounds(safeInt(view.getXpos()), safeInt(view.getYpos()),
4919 safeInt(view.getWidth()), safeInt(view.getHeight()));
4920 // startSeq set in af.alignPanel.updateLayout below
4921 af.alignPanel.updateLayout();
4922 ColourSchemeI cs = null;
4923 // apply colourschemes
4924 if (view.getBgColour() != null)
4926 if (view.getBgColour().startsWith("ucs"))
4928 cs = getUserColourScheme(jm, view.getBgColour());
4930 else if (view.getBgColour().startsWith("Annotation"))
4932 AnnotationColourScheme viewAnnColour = view.getAnnotationColours();
4933 cs = constructAnnotationColour(viewAnnColour, af, al, jm, true);
4940 cs = ColourSchemeProperty.getColourScheme(af.getViewport(), al,
4941 view.getBgColour());
4946 * turn off 'alignment colour applies to all groups'
4947 * while restoring global colour scheme
4949 viewport.setColourAppliesToAllGroups(false);
4950 viewport.setGlobalColourScheme(cs);
4951 viewport.getResidueShading().setThreshold(pidThreshold,
4952 view.isIgnoreGapsinConsensus());
4953 viewport.getResidueShading()
4954 .setConsensus(viewport.getSequenceConsensusHash());
4955 if (safeBoolean(view.isConservationSelected()) && cs != null)
4957 viewport.getResidueShading()
4958 .setConservationInc(safeInt(view.getConsThreshold()));
4960 af.changeColour(cs);
4961 viewport.setColourAppliesToAllGroups(true);
4963 viewport.setShowSequenceFeatures(
4964 safeBoolean(view.isShowSequenceFeatures()));
4966 viewport.setCentreColumnLabels(view.isCentreColumnLabels());
4967 viewport.setIgnoreGapsConsensus(view.isIgnoreGapsinConsensus(), null);
4968 viewport.setFollowHighlight(view.isFollowHighlight());
4969 viewport.followSelection = view.isFollowSelection();
4970 viewport.setShowConsensusHistogram(view.isShowConsensusHistogram());
4971 viewport.setShowSequenceLogo(view.isShowSequenceLogo());
4972 viewport.setNormaliseSequenceLogo(view.isNormaliseSequenceLogo());
4973 viewport.setShowDBRefs(safeBoolean(view.isShowDbRefTooltip()));
4974 viewport.setShowNPFeats(safeBoolean(view.isShowNPfeatureTooltip()));
4975 viewport.setShowGroupConsensus(view.isShowGroupConsensus());
4976 viewport.setShowGroupConservation(view.isShowGroupConservation());
4977 viewport.setShowComplementFeatures(view.isShowComplementFeatures());
4978 viewport.setShowComplementFeaturesOnTop(
4979 view.isShowComplementFeaturesOnTop());
4981 // recover feature settings
4982 if (jm.getFeatureSettings() != null)
4984 FeatureRendererModel fr = af.alignPanel.getSeqPanel().seqCanvas
4985 .getFeatureRenderer();
4986 FeaturesDisplayed fdi;
4987 viewport.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
4988 String[] renderOrder = new String[jm.getFeatureSettings().getSetting()
4990 Map<String, FeatureColourI> featureColours = new Hashtable<>();
4991 Map<String, Float> featureOrder = new Hashtable<>();
4993 for (int fs = 0; fs < jm.getFeatureSettings().getSetting()
4996 Setting setting = jm.getFeatureSettings().getSetting().get(fs);
4997 String featureType = setting.getType();
5000 * restore feature filters (if any)
5002 jalview.xml.binding.jalview.FeatureMatcherSet filters = setting
5004 if (filters != null)
5006 FeatureMatcherSetI filter = Jalview2XML.parseFilter(featureType,
5008 if (!filter.isEmpty())
5010 fr.setFeatureFilter(featureType, filter);
5015 * restore feature colour scheme
5017 Color maxColour = new Color(setting.getColour());
5018 if (setting.getMincolour() != null)
5021 * minColour is always set unless a simple colour
5022 * (including for colour by label though it doesn't use it)
5024 Color minColour = new Color(setting.getMincolour().intValue());
5025 Color noValueColour = minColour;
5026 NoValueColour noColour = setting.getNoValueColour();
5027 if (noColour == NoValueColour.NONE)
5029 noValueColour = null;
5031 else if (noColour == NoValueColour.MAX)
5033 noValueColour = maxColour;
5035 float min = safeFloat(safeFloat(setting.getMin()));
5036 float max = setting.getMax() == null ? 1f
5037 : setting.getMax().floatValue();
5038 FeatureColourI gc = new FeatureColour(maxColour, minColour,
5039 maxColour, noValueColour, min, max);
5040 if (setting.getAttributeName().size() > 0)
5042 gc.setAttributeName(setting.getAttributeName().toArray(
5043 new String[setting.getAttributeName().size()]));
5045 if (setting.getThreshold() != null)
5047 gc.setThreshold(setting.getThreshold().floatValue());
5048 int threshstate = safeInt(setting.getThreshstate());
5049 // -1 = None, 0 = Below, 1 = Above threshold
5050 if (threshstate == 0)
5052 gc.setBelowThreshold(true);
5054 else if (threshstate == 1)
5056 gc.setAboveThreshold(true);
5059 gc.setAutoScaled(true); // default
5060 if (setting.isAutoScale() != null)
5062 gc.setAutoScaled(setting.isAutoScale());
5064 if (setting.isColourByLabel() != null)
5066 gc.setColourByLabel(setting.isColourByLabel());
5068 // and put in the feature colour table.
5069 featureColours.put(featureType, gc);
5073 featureColours.put(featureType, new FeatureColour(maxColour));
5075 renderOrder[fs] = featureType;
5076 if (setting.getOrder() != null)
5078 featureOrder.put(featureType, setting.getOrder().floatValue());
5082 featureOrder.put(featureType, Float.valueOf(
5083 fs / jm.getFeatureSettings().getSetting().size()));
5085 if (safeBoolean(setting.isDisplay()))
5087 fdi.setVisible(featureType);
5090 Map<String, Boolean> fgtable = new Hashtable<>();
5091 for (int gs = 0; gs < jm.getFeatureSettings().getGroup().size(); gs++)
5093 Group grp = jm.getFeatureSettings().getGroup().get(gs);
5094 fgtable.put(grp.getName(), Boolean.valueOf(grp.isDisplay()));
5096 // FeatureRendererSettings frs = new FeatureRendererSettings(renderOrder,
5097 // fgtable, featureColours, jms.getFeatureSettings().hasTransparency() ?
5098 // jms.getFeatureSettings().getTransparency() : 0.0, featureOrder);
5099 FeatureRendererSettings frs = new FeatureRendererSettings(renderOrder,
5100 fgtable, featureColours, 1.0f, featureOrder);
5101 fr.transferSettings(frs);
5104 if (view.getHiddenColumns().size() > 0)
5106 for (int c = 0; c < view.getHiddenColumns().size(); c++)
5108 final HiddenColumns hc = view.getHiddenColumns().get(c);
5109 viewport.hideColumns(safeInt(hc.getStart()),
5110 safeInt(hc.getEnd()) /* +1 */);
5113 if (view.getCalcIdParam() != null)
5115 for (CalcIdParam calcIdParam : view.getCalcIdParam())
5117 if (calcIdParam != null)
5119 if (recoverCalcIdParam(calcIdParam, viewport))
5124 Console.warn("Couldn't recover parameters for "
5125 + calcIdParam.getCalcId());
5130 af.setMenusFromViewport(viewport);
5131 af.setTitle(view.getTitle());
5132 // TODO: we don't need to do this if the viewport is aready visible.
5134 * Add the AlignFrame to the desktop (it may be 'gathered' later), unless it
5135 * has a 'cdna/protein complement' view, in which case save it in order to
5136 * populate a SplitFrame once all views have been read in.
5138 String complementaryViewId = view.getComplementId();
5139 if (complementaryViewId == null)
5141 Desktop.addInternalFrame(af, view.getTitle(),
5142 safeInt(view.getWidth()), safeInt(view.getHeight()));
5143 // recompute any autoannotation
5144 af.alignPanel.updateAnnotation(false, true);
5145 reorderAutoannotation(af, al, autoAlan);
5146 af.alignPanel.alignmentChanged();
5150 splitFrameCandidates.put(view, af);
5157 * Reads saved data to restore Colour by Annotation settings
5159 * @param viewAnnColour
5163 * @param checkGroupAnnColour
5166 private ColourSchemeI constructAnnotationColour(
5167 AnnotationColourScheme viewAnnColour, AlignFrame af,
5168 AlignmentI al, JalviewModel model, boolean checkGroupAnnColour)
5170 boolean propagateAnnColour = false;
5171 AlignmentI annAlignment = af != null ? af.getViewport().getAlignment()
5173 if (checkGroupAnnColour && al.getGroups() != null
5174 && al.getGroups().size() > 0)
5176 // pre 2.8.1 behaviour
5177 // check to see if we should transfer annotation colours
5178 propagateAnnColour = true;
5179 for (SequenceGroup sg : al.getGroups())
5181 if (sg.getColourScheme() instanceof AnnotationColourGradient)
5183 propagateAnnColour = false;
5189 * 2.10.2- : saved annotationId is AlignmentAnnotation.annotationId
5191 String annotationId = viewAnnColour.getAnnotation();
5192 AlignmentAnnotation matchedAnnotation = annotationIds.get(annotationId);
5195 * pre 2.10.2: saved annotationId is AlignmentAnnotation.label
5197 if (matchedAnnotation == null
5198 && annAlignment.getAlignmentAnnotation() != null)
5200 for (int i = 0; i < annAlignment.getAlignmentAnnotation().length; i++)
5203 .equals(annAlignment.getAlignmentAnnotation()[i].label))
5205 matchedAnnotation = annAlignment.getAlignmentAnnotation()[i];
5210 if (matchedAnnotation == null)
5212 System.err.println("Failed to match annotation colour scheme for "
5216 // belt-and-braces create a threshold line if the
5217 // colourscheme needs one but the matchedAnnotation doesn't have one
5218 if (safeInt(viewAnnColour.getAboveThreshold()) != 0
5219 && matchedAnnotation.getThreshold() == null)
5221 matchedAnnotation.setThreshold(
5222 new GraphLine(safeFloat(viewAnnColour.getThreshold()),
5223 "Threshold", Color.black));
5226 AnnotationColourGradient cs = null;
5227 if (viewAnnColour.getColourScheme().equals("None"))
5229 cs = new AnnotationColourGradient(matchedAnnotation,
5230 new Color(safeInt(viewAnnColour.getMinColour())),
5231 new Color(safeInt(viewAnnColour.getMaxColour())),
5232 safeInt(viewAnnColour.getAboveThreshold()));
5234 else if (viewAnnColour.getColourScheme().startsWith("ucs"))
5236 cs = new AnnotationColourGradient(matchedAnnotation,
5237 getUserColourScheme(model, viewAnnColour.getColourScheme()),
5238 safeInt(viewAnnColour.getAboveThreshold()));
5242 cs = new AnnotationColourGradient(matchedAnnotation,
5243 ColourSchemeProperty.getColourScheme(af.getViewport(), al,
5244 viewAnnColour.getColourScheme()),
5245 safeInt(viewAnnColour.getAboveThreshold()));
5248 boolean perSequenceOnly = safeBoolean(viewAnnColour.isPerSequence());
5249 boolean useOriginalColours = safeBoolean(
5250 viewAnnColour.isPredefinedColours());
5251 cs.setSeqAssociated(perSequenceOnly);
5252 cs.setPredefinedColours(useOriginalColours);
5254 if (propagateAnnColour && al.getGroups() != null)
5256 // Also use these settings for all the groups
5257 for (int g = 0; g < al.getGroups().size(); g++)
5259 SequenceGroup sg = al.getGroups().get(g);
5260 if (sg.getGroupColourScheme() == null)
5265 AnnotationColourGradient groupScheme = new AnnotationColourGradient(
5266 matchedAnnotation, sg.getColourScheme(),
5267 safeInt(viewAnnColour.getAboveThreshold()));
5268 sg.setColourScheme(groupScheme);
5269 groupScheme.setSeqAssociated(perSequenceOnly);
5270 groupScheme.setPredefinedColours(useOriginalColours);
5276 private void reorderAutoannotation(AlignFrame af, AlignmentI al,
5277 List<JvAnnotRow> autoAlan)
5279 // copy over visualization settings for autocalculated annotation in the
5281 if (al.getAlignmentAnnotation() != null)
5284 * Kludge for magic autoannotation names (see JAL-811)
5286 String[] magicNames = new String[] { "Consensus", "Quality",
5288 JvAnnotRow nullAnnot = new JvAnnotRow(-1, null);
5289 Hashtable<String, JvAnnotRow> visan = new Hashtable<>();
5290 for (String nm : magicNames)
5292 visan.put(nm, nullAnnot);
5294 for (JvAnnotRow auan : autoAlan)
5296 visan.put(auan.template.label
5297 + (auan.template.getCalcId() == null ? ""
5298 : "\t" + auan.template.getCalcId()),
5301 int hSize = al.getAlignmentAnnotation().length;
5302 List<JvAnnotRow> reorder = new ArrayList<>();
5303 // work through any autoCalculated annotation already on the view
5304 // removing it if it should be placed in a different location on the
5305 // annotation panel.
5306 List<String> remains = new ArrayList<>(visan.keySet());
5307 for (int h = 0; h < hSize; h++)
5309 jalview.datamodel.AlignmentAnnotation jalan = al
5310 .getAlignmentAnnotation()[h];
5311 if (jalan.autoCalculated)
5314 JvAnnotRow valan = visan.get(k = jalan.label);
5315 if (jalan.getCalcId() != null)
5317 valan = visan.get(k = jalan.label + "\t" + jalan.getCalcId());
5322 // delete the auto calculated row from the alignment
5323 al.deleteAnnotation(jalan, false);
5327 if (valan != nullAnnot)
5329 if (jalan != valan.template)
5331 // newly created autoannotation row instance
5332 // so keep a reference to the visible annotation row
5333 // and copy over all relevant attributes
5334 if (valan.template.graphHeight >= 0)
5337 jalan.graphHeight = valan.template.graphHeight;
5339 jalan.visible = valan.template.visible;
5341 reorder.add(new JvAnnotRow(valan.order, jalan));
5346 // Add any (possibly stale) autocalculated rows that were not appended to
5347 // the view during construction
5348 for (String other : remains)
5350 JvAnnotRow othera = visan.get(other);
5351 if (othera != nullAnnot && othera.template.getCalcId() != null
5352 && othera.template.getCalcId().length() > 0)
5354 reorder.add(othera);
5357 // now put the automatic annotation in its correct place
5358 int s = 0, srt[] = new int[reorder.size()];
5359 JvAnnotRow[] rws = new JvAnnotRow[reorder.size()];
5360 for (JvAnnotRow jvar : reorder)
5363 srt[s++] = jvar.order;
5366 jalview.util.QuickSort.sort(srt, rws);
5367 // and re-insert the annotation at its correct position
5368 for (JvAnnotRow jvar : rws)
5370 al.addAnnotation(jvar.template, jvar.order);
5372 af.alignPanel.adjustAnnotationHeight();
5376 Hashtable skipList = null;
5379 * TODO remove this method
5382 * @return AlignFrame bound to sequenceSetId from view, if one exists. private
5383 * AlignFrame getSkippedFrame(Viewport view) { if (skipList==null) {
5384 * throw new Error("Implementation Error. No skipList defined for this
5385 * Jalview2XML instance."); } return (AlignFrame)
5386 * skipList.get(view.getSequenceSetId()); }
5390 * Check if the Jalview view contained in object should be skipped or not.
5393 * @return true if view's sequenceSetId is a key in skipList
5395 private boolean skipViewport(JalviewModel object)
5397 if (skipList == null)
5401 String id = object.getViewport().get(0).getSequenceSetId();
5402 if (skipList.containsKey(id))
5404 Console.debug("Skipping seuqence set id " + id);
5410 public void addToSkipList(AlignFrame af)
5412 if (skipList == null)
5414 skipList = new Hashtable();
5416 skipList.put(af.getViewport().getSequenceSetId(), af);
5419 public void clearSkipList()
5421 if (skipList != null)
5428 private void recoverDatasetFor(SequenceSet vamsasSet, AlignmentI al,
5429 boolean ignoreUnrefed, String uniqueSeqSetId)
5431 jalview.datamodel.AlignmentI ds = getDatasetFor(
5432 vamsasSet.getDatasetId());
5433 AlignmentI xtant_ds = ds;
5434 if (xtant_ds == null)
5436 // good chance we are about to create a new dataset, but check if we've
5437 // seen some of the dataset sequence IDs before.
5438 // TODO: skip this check if we are working with project generated by
5439 // version 2.11 or later
5440 xtant_ds = checkIfHasDataset(vamsasSet.getSequence());
5441 if (xtant_ds != null)
5444 addDatasetRef(vamsasSet.getDatasetId(), ds);
5447 Vector<SequenceI> dseqs = null;
5450 // recovering an alignment View
5451 AlignmentI seqSetDS = getDatasetFor(UNIQSEQSETID + uniqueSeqSetId);
5452 if (seqSetDS != null)
5454 if (ds != null && ds != seqSetDS)
5457 "JAL-3171 regression: Overwriting a dataset reference for an alignment"
5458 + " - CDS/Protein crossreference data may be lost");
5459 if (xtant_ds != null)
5461 // This can only happen if the unique sequence set ID was bound to a
5462 // dataset that did not contain any of the sequences in the view
5463 // currently being restored.
5465 "JAL-3171 SERIOUS! TOTAL CONFUSION - please consider contacting the Jalview Development team so they can investigate why your project caused this message to be displayed.");
5469 addDatasetRef(vamsasSet.getDatasetId(), ds);
5474 // try even harder to restore dataset
5475 AlignmentI xtantDS = checkIfHasDataset(vamsasSet.getSequence());
5476 // create a list of new dataset sequences
5477 dseqs = new Vector<>();
5479 for (int i = 0, iSize = vamsasSet.getSequence().size(); i < iSize; i++)
5481 Sequence vamsasSeq = vamsasSet.getSequence().get(i);
5482 ensureJalviewDatasetSequence(vamsasSeq, ds, dseqs, ignoreUnrefed, i);
5484 // create a new dataset
5487 SequenceI[] dsseqs = new SequenceI[dseqs.size()];
5488 dseqs.copyInto(dsseqs);
5489 ds = new jalview.datamodel.Alignment(dsseqs);
5490 Console.debug("Created new dataset " + vamsasSet.getDatasetId()
5491 + " for alignment " + System.identityHashCode(al));
5492 addDatasetRef(vamsasSet.getDatasetId(), ds);
5494 // set the dataset for the newly imported alignment.
5495 if (al.getDataset() == null && !ignoreUnrefed)
5498 // register dataset for the alignment's uniqueSeqSetId for legacy projects
5499 addDatasetRef(UNIQSEQSETID + uniqueSeqSetId, ds);
5501 updateSeqDatasetBinding(vamsasSet.getSequence(), ds);
5505 * XML dataset sequence ID to materialised dataset reference
5507 HashMap<String, AlignmentI> seqToDataset = new HashMap<>();
5510 * @return the first materialised dataset reference containing a dataset
5511 * sequence referenced in the given view
5513 * - sequences from the view
5515 AlignmentI checkIfHasDataset(List<Sequence> list)
5517 for (Sequence restoredSeq : list)
5519 AlignmentI datasetFor = seqToDataset.get(restoredSeq.getDsseqid());
5520 if (datasetFor != null)
5529 * Register ds as the containing dataset for the dataset sequences referenced
5530 * by sequences in list
5533 * - sequences in a view
5536 void updateSeqDatasetBinding(List<Sequence> list, AlignmentI ds)
5538 for (Sequence restoredSeq : list)
5540 AlignmentI prevDS = seqToDataset.put(restoredSeq.getDsseqid(), ds);
5541 if (prevDS != null && prevDS != ds)
5543 Console.warn("Dataset sequence appears in many datasets: "
5544 + restoredSeq.getDsseqid());
5545 // TODO: try to merge!
5553 * sequence definition to create/merge dataset sequence for
5557 * vector to add new dataset sequence to
5558 * @param ignoreUnrefed
5559 * - when true, don't create new sequences from vamsasSeq if it's id
5560 * doesn't already have an asssociated Jalview sequence.
5562 * - used to reorder the sequence in the alignment according to the
5563 * vamsasSeq array ordering, to preserve ordering of dataset
5565 private void ensureJalviewDatasetSequence(Sequence vamsasSeq,
5566 AlignmentI ds, Vector<SequenceI> dseqs, boolean ignoreUnrefed,
5569 // JBP TODO: Check this is called for AlCodonFrames to support recovery of
5571 SequenceI sq = seqRefIds.get(vamsasSeq.getId());
5572 boolean reorder = false;
5573 SequenceI dsq = null;
5574 if (sq != null && sq.getDatasetSequence() != null)
5576 dsq = sq.getDatasetSequence();
5582 if (sq == null && ignoreUnrefed)
5586 String sqid = vamsasSeq.getDsseqid();
5589 // need to create or add a new dataset sequence reference to this sequence
5592 dsq = seqRefIds.get(sqid);
5597 // make a new dataset sequence
5598 dsq = sq.createDatasetSequence();
5601 // make up a new dataset reference for this sequence
5602 sqid = seqHash(dsq);
5604 dsq.setVamsasId(uniqueSetSuffix + sqid);
5605 seqRefIds.put(sqid, dsq);
5610 dseqs.addElement(dsq);
5615 ds.addSequence(dsq);
5621 { // make this dataset sequence sq's dataset sequence
5622 sq.setDatasetSequence(dsq);
5623 // and update the current dataset alignment
5628 if (!dseqs.contains(dsq))
5635 if (ds.findIndex(dsq) < 0)
5637 ds.addSequence(dsq);
5644 // TODO: refactor this as a merge dataset sequence function
5645 // now check that sq (the dataset sequence) sequence really is the union of
5646 // all references to it
5647 // boolean pre = sq.getStart() < dsq.getStart();
5648 // boolean post = sq.getEnd() > dsq.getEnd();
5652 // StringBuffer sb = new StringBuffer();
5653 String newres = jalview.analysis.AlignSeq.extractGaps(
5654 jalview.util.Comparison.GapChars, sq.getSequenceAsString());
5655 if (!newres.equalsIgnoreCase(dsq.getSequenceAsString())
5656 && newres.length() > dsq.getLength())
5658 // Update with the longer sequence.
5662 * if (pre) { sb.insert(0, newres .substring(0, dsq.getStart() -
5663 * sq.getStart())); dsq.setStart(sq.getStart()); } if (post) {
5664 * sb.append(newres.substring(newres.length() - sq.getEnd() -
5665 * dsq.getEnd())); dsq.setEnd(sq.getEnd()); }
5667 dsq.setSequence(newres);
5669 // TODO: merges will never happen if we 'know' we have the real dataset
5670 // sequence - this should be detected when id==dssid
5672 "DEBUG Notice: Merged dataset sequence (if you see this often, post at http://issues.jalview.org/browse/JAL-1474)"); // ("
5673 // + (pre ? "prepended" : "") + " "
5674 // + (post ? "appended" : ""));
5679 // sequence refs are identical. We may need to update the existing dataset
5680 // alignment with this one, though.
5681 if (ds != null && dseqs == null)
5683 int opos = ds.findIndex(dsq);
5684 SequenceI tseq = null;
5685 if (opos != -1 && vseqpos != opos)
5687 // remove from old position
5688 ds.deleteSequence(dsq);
5690 if (vseqpos < ds.getHeight())
5692 if (vseqpos != opos)
5694 // save sequence at destination position
5695 tseq = ds.getSequenceAt(vseqpos);
5696 ds.replaceSequenceAt(vseqpos, dsq);
5697 ds.addSequence(tseq);
5702 ds.addSequence(dsq);
5709 * TODO use AlignmentI here and in related methods - needs
5710 * AlignmentI.getDataset() changed to return AlignmentI instead of Alignment
5712 Hashtable<String, AlignmentI> datasetIds = null;
5714 IdentityHashMap<AlignmentI, String> dataset2Ids = null;
5716 private AlignmentI getDatasetFor(String datasetId)
5718 if (datasetIds == null)
5720 datasetIds = new Hashtable<>();
5723 if (datasetIds.containsKey(datasetId))
5725 return datasetIds.get(datasetId);
5730 private void addDatasetRef(String datasetId, AlignmentI dataset)
5732 if (datasetIds == null)
5734 datasetIds = new Hashtable<>();
5736 datasetIds.put(datasetId, dataset);
5740 * make a new dataset ID for this jalview dataset alignment
5745 private String getDatasetIdRef(AlignmentI dataset)
5747 if (dataset.getDataset() != null)
5750 "Serious issue! Dataset Object passed to getDatasetIdRef is not a Jalview DATASET alignment...");
5752 String datasetId = makeHashCode(dataset, null);
5753 if (datasetId == null)
5755 // make a new datasetId and record it
5756 if (dataset2Ids == null)
5758 dataset2Ids = new IdentityHashMap<>();
5762 datasetId = dataset2Ids.get(dataset);
5764 if (datasetId == null)
5766 datasetId = "ds" + dataset2Ids.size() + 1;
5767 dataset2Ids.put(dataset, datasetId);
5774 * Add any saved DBRefEntry's to the sequence. An entry flagged as 'locus' is
5775 * constructed as a special subclass GeneLocus.
5777 * @param datasetSequence
5780 private void addDBRefs(SequenceI datasetSequence, Sequence sequence)
5782 for (int d = 0; d < sequence.getDBRef().size(); d++)
5784 DBRef dr = sequence.getDBRef().get(d);
5788 entry = new GeneLocus(dr.getSource(), dr.getVersion(),
5789 dr.getAccessionId());
5793 entry = new DBRefEntry(dr.getSource(), dr.getVersion(),
5794 dr.getAccessionId());
5796 if (dr.getMapping() != null)
5798 entry.setMap(addMapping(dr.getMapping()));
5800 entry.setCanonical(dr.isCanonical());
5801 datasetSequence.addDBRef(entry);
5805 private jalview.datamodel.Mapping addMapping(Mapping m)
5807 SequenceI dsto = null;
5808 // Mapping m = dr.getMapping();
5809 int fr[] = new int[m.getMapListFrom().size() * 2];
5810 Iterator<MapListFrom> from = m.getMapListFrom().iterator();// enumerateMapListFrom();
5811 for (int _i = 0; from.hasNext(); _i += 2)
5813 MapListFrom mf = from.next();
5814 fr[_i] = mf.getStart();
5815 fr[_i + 1] = mf.getEnd();
5817 int fto[] = new int[m.getMapListTo().size() * 2];
5818 Iterator<MapListTo> to = m.getMapListTo().iterator();// enumerateMapListTo();
5819 for (int _i = 0; to.hasNext(); _i += 2)
5821 MapListTo mf = to.next();
5822 fto[_i] = mf.getStart();
5823 fto[_i + 1] = mf.getEnd();
5825 jalview.datamodel.Mapping jmap = new jalview.datamodel.Mapping(dsto, fr,
5826 fto, m.getMapFromUnit().intValue(),
5827 m.getMapToUnit().intValue());
5830 * (optional) choice of dseqFor or Sequence
5832 if (m.getDseqFor() != null)
5834 String dsfor = m.getDseqFor();
5835 if (seqRefIds.containsKey(dsfor))
5840 jmap.setTo(seqRefIds.get(dsfor));
5844 frefedSequence.add(newMappingRef(dsfor, jmap));
5847 else if (m.getSequence() != null)
5850 * local sequence definition
5852 Sequence ms = m.getSequence();
5853 SequenceI djs = null;
5854 String sqid = ms.getDsseqid();
5855 if (sqid != null && sqid.length() > 0)
5858 * recover dataset sequence
5860 djs = seqRefIds.get(sqid);
5865 "Warning - making up dataset sequence id for DbRef sequence map reference");
5866 sqid = ((Object) ms).toString(); // make up a new hascode for
5867 // undefined dataset sequence hash
5868 // (unlikely to happen)
5874 * make a new dataset sequence and add it to refIds hash
5876 djs = new jalview.datamodel.Sequence(ms.getName(),
5878 djs.setStart(jmap.getMap().getToLowest());
5879 djs.setEnd(jmap.getMap().getToHighest());
5880 djs.setVamsasId(uniqueSetSuffix + sqid);
5882 incompleteSeqs.put(sqid, djs);
5883 seqRefIds.put(sqid, djs);
5886 Console.debug("about to recurse on addDBRefs.");
5895 * Provides a 'copy' of an alignment view (on action New View) by 'saving' the
5896 * view as XML (but not to file), and then reloading it
5901 public AlignmentPanel copyAlignPanel(AlignmentPanel ap)
5904 JalviewModel jm = saveState(ap, null, null, null);
5907 jm.getVamsasModel().getSequenceSet().get(0).getDatasetId(),
5908 ap.getAlignment().getDataset());
5910 uniqueSetSuffix = "";
5911 // jm.getJalviewModelSequence().getViewport(0).setId(null);
5912 jm.getViewport().get(0).setId(null);
5913 // we don't overwrite the view we just copied
5915 if (this.frefedSequence == null)
5917 frefedSequence = new Vector<>();
5920 viewportsAdded.clear();
5922 AlignFrame af = loadFromObject(jm, null, false, null);
5923 af.getAlignPanels().clear();
5924 af.closeMenuItem_actionPerformed(true);
5927 * if(ap.av.getAlignment().getAlignmentAnnotation()!=null) { for(int i=0;
5928 * i<ap.av.getAlignment().getAlignmentAnnotation().length; i++) {
5929 * if(!ap.av.getAlignment().getAlignmentAnnotation()[i].autoCalculated) {
5930 * af.alignPanel.av.getAlignment().getAlignmentAnnotation()[i] =
5931 * ap.av.getAlignment().getAlignmentAnnotation()[i]; } } }
5934 return af.alignPanel;
5937 private Hashtable jvids2vobj;
5940 * set the object to ID mapping tables used to write/recover objects and XML
5941 * ID strings for the jalview project. If external tables are provided then
5942 * finalize and clearSeqRefs will not clear the tables when the Jalview2XML
5943 * object goes out of scope. - also populates the datasetIds hashtable with
5944 * alignment objects containing dataset sequences
5947 * Map from ID strings to jalview datamodel
5949 * Map from jalview datamodel to ID strings
5953 public void setObjectMappingTables(Hashtable vobj2jv,
5954 IdentityHashMap jv2vobj)
5956 this.jv2vobj = jv2vobj;
5957 this.vobj2jv = vobj2jv;
5958 Iterator ds = jv2vobj.keySet().iterator();
5960 while (ds.hasNext())
5962 Object jvobj = ds.next();
5963 id = jv2vobj.get(jvobj).toString();
5964 if (jvobj instanceof jalview.datamodel.Alignment)
5966 if (((jalview.datamodel.Alignment) jvobj).getDataset() == null)
5968 addDatasetRef(id, (jalview.datamodel.Alignment) jvobj);
5971 else if (jvobj instanceof jalview.datamodel.Sequence)
5973 // register sequence object so the XML parser can recover it.
5974 if (seqRefIds == null)
5976 seqRefIds = new HashMap<>();
5978 if (seqsToIds == null)
5980 seqsToIds = new IdentityHashMap<>();
5982 seqRefIds.put(jv2vobj.get(jvobj).toString(), (SequenceI) jvobj);
5983 seqsToIds.put((SequenceI) jvobj, id);
5985 else if (jvobj instanceof jalview.datamodel.AlignmentAnnotation)
5988 AlignmentAnnotation jvann = (AlignmentAnnotation) jvobj;
5989 annotationIds.put(anid = jv2vobj.get(jvobj).toString(), jvann);
5990 if (jvann.annotationId == null)
5992 jvann.annotationId = anid;
5994 if (!jvann.annotationId.equals(anid))
5996 // TODO verify that this is the correct behaviour
5997 Console.warn("Overriding Annotation ID for " + anid
5998 + " from different id : " + jvann.annotationId);
5999 jvann.annotationId = anid;
6002 else if (jvobj instanceof String)
6004 if (jvids2vobj == null)
6006 jvids2vobj = new Hashtable();
6007 jvids2vobj.put(jvobj, jv2vobj.get(jvobj).toString());
6012 Console.debug("Ignoring " + jvobj.getClass() + " (ID = " + id);
6018 * set the uniqueSetSuffix used to prefix/suffix object IDs for jalview
6019 * objects created from the project archive. If string is null (default for
6020 * construction) then suffix will be set automatically.
6024 public void setUniqueSetSuffix(String string)
6026 uniqueSetSuffix = string;
6031 * uses skipList2 as the skipList for skipping views on sequence sets
6032 * associated with keys in the skipList
6036 public void setSkipList(Hashtable skipList2)
6038 skipList = skipList2;
6042 * Reads the jar entry of given name and returns its contents, or null if the
6043 * entry is not found.
6046 * @param jarEntryName
6049 protected String readJarEntry(jarInputStreamProvider jprovider,
6050 String jarEntryName)
6052 String result = null;
6053 BufferedReader in = null;
6058 * Reopen the jar input stream and traverse its entries to find a matching
6061 JarInputStream jin = jprovider.getJarInputStream();
6062 JarEntry entry = null;
6065 entry = jin.getNextJarEntry();
6066 } while (entry != null && !entry.getName().equals(jarEntryName));
6070 StringBuilder out = new StringBuilder(256);
6071 in = new BufferedReader(new InputStreamReader(jin, UTF_8));
6074 while ((data = in.readLine()) != null)
6078 result = out.toString();
6083 "Couldn't find entry in Jalview Jar for " + jarEntryName);
6085 } catch (Exception ex)
6087 ex.printStackTrace();
6095 } catch (IOException e)
6106 * Returns an incrementing counter (0, 1, 2...)
6110 private synchronized int nextCounter()
6116 * Loads any saved PCA viewers
6121 protected void loadPCAViewers(JalviewModel model, AlignmentPanel ap)
6125 List<PcaViewer> pcaviewers = model.getPcaViewer();
6126 for (PcaViewer viewer : pcaviewers)
6128 String modelName = viewer.getScoreModelName();
6129 SimilarityParamsI params = new SimilarityParams(
6130 viewer.isIncludeGappedColumns(), viewer.isMatchGaps(),
6131 viewer.isIncludeGaps(),
6132 viewer.isDenominateByShortestLength());
6135 * create the panel (without computing the PCA)
6137 PCAPanel panel = new PCAPanel(ap, modelName, params);
6139 panel.setTitle(viewer.getTitle());
6140 panel.setBounds(new Rectangle(viewer.getXpos(), viewer.getYpos(),
6141 viewer.getWidth(), viewer.getHeight()));
6143 boolean showLabels = viewer.isShowLabels();
6144 panel.setShowLabels(showLabels);
6145 panel.getRotatableCanvas().setShowLabels(showLabels);
6146 panel.getRotatableCanvas()
6147 .setBgColour(new Color(viewer.getBgColour()));
6148 panel.getRotatableCanvas()
6149 .setApplyToAllViews(viewer.isLinkToAllViews());
6152 * load PCA output data
6154 ScoreModelI scoreModel = ScoreModels.getInstance()
6155 .getScoreModel(modelName, ap);
6156 PCA pca = new PCA(null, scoreModel, params);
6157 PcaDataType pcaData = viewer.getPcaData();
6159 MatrixI pairwise = loadDoubleMatrix(pcaData.getPairwiseMatrix());
6160 pca.setPairwiseScores(pairwise);
6162 MatrixI triDiag = loadDoubleMatrix(pcaData.getTridiagonalMatrix());
6163 pca.setTridiagonal(triDiag);
6165 MatrixI result = loadDoubleMatrix(pcaData.getEigenMatrix());
6166 pca.setEigenmatrix(result);
6168 panel.getPcaModel().setPCA(pca);
6171 * we haven't saved the input data! (JAL-2647 to do)
6173 panel.setInputData(null);
6176 * add the sequence points for the PCA display
6178 List<jalview.datamodel.SequencePoint> seqPoints = new ArrayList<>();
6179 for (SequencePoint sp : viewer.getSequencePoint())
6181 String seqId = sp.getSequenceRef();
6182 SequenceI seq = seqRefIds.get(seqId);
6185 throw new IllegalStateException(
6186 "Unmatched seqref for PCA: " + seqId);
6188 Point pt = new Point(sp.getXPos(), sp.getYPos(), sp.getZPos());
6189 jalview.datamodel.SequencePoint seqPoint = new jalview.datamodel.SequencePoint(
6191 seqPoints.add(seqPoint);
6193 panel.getRotatableCanvas().setPoints(seqPoints, seqPoints.size());
6196 * set min-max ranges and scale after setPoints (which recomputes them)
6198 panel.getRotatableCanvas().setScaleFactor(viewer.getScaleFactor());
6199 SeqPointMin spMin = viewer.getSeqPointMin();
6200 float[] min = new float[] { spMin.getXPos(), spMin.getYPos(),
6202 SeqPointMax spMax = viewer.getSeqPointMax();
6203 float[] max = new float[] { spMax.getXPos(), spMax.getYPos(),
6205 panel.getRotatableCanvas().setSeqMinMax(min, max);
6207 // todo: hold points list in PCAModel only
6208 panel.getPcaModel().setSequencePoints(seqPoints);
6210 panel.setSelectedDimensionIndex(viewer.getXDim(), X);
6211 panel.setSelectedDimensionIndex(viewer.getYDim(), Y);
6212 panel.setSelectedDimensionIndex(viewer.getZDim(), Z);
6214 // is this duplication needed?
6215 panel.setTop(seqPoints.size() - 1);
6216 panel.getPcaModel().setTop(seqPoints.size() - 1);
6219 * add the axes' end points for the display
6221 for (int i = 0; i < 3; i++)
6223 Axis axis = viewer.getAxis().get(i);
6224 panel.getRotatableCanvas().getAxisEndPoints()[i] = new Point(
6225 axis.getXPos(), axis.getYPos(), axis.getZPos());
6228 Desktop.addInternalFrame(panel, MessageManager.formatMessage(
6229 "label.calc_title", "PCA", modelName), 475, 450);
6231 } catch (Exception ex)
6233 Console.error("Error loading PCA: " + ex.toString());
6238 * Creates a new structure viewer window
6245 protected void createStructureViewer(ViewerType viewerType,
6246 final Entry<String, StructureViewerModel> viewerData,
6247 AlignFrame af, jarInputStreamProvider jprovider)
6249 final StructureViewerModel viewerModel = viewerData.getValue();
6250 String sessionFilePath = null;
6252 if (viewerType == ViewerType.JMOL)
6254 sessionFilePath = rewriteJmolSession(viewerModel, jprovider);
6258 String viewerJarEntryName = getViewerJarEntryName(
6259 viewerModel.getViewId());
6260 sessionFilePath = copyJarEntry(jprovider, viewerJarEntryName,
6261 "viewerSession", ".tmp");
6263 final String sessionPath = sessionFilePath;
6264 final String sviewid = viewerData.getKey();
6267 SwingUtilities.invokeAndWait(new Runnable()
6272 JalviewStructureDisplayI sview = null;
6275 sview = StructureViewer.createView(viewerType, af.alignPanel,
6276 viewerModel, sessionPath, sviewid);
6277 addNewStructureViewer(sview);
6278 } catch (OutOfMemoryError ex)
6280 new OOMWarning("Restoring structure view for " + viewerType,
6281 (OutOfMemoryError) ex.getCause());
6282 if (sview != null && sview.isVisible())
6284 sview.closeViewer(false);
6285 sview.setVisible(false);
6291 } catch (InvocationTargetException | InterruptedException ex)
6293 Console.warn("Unexpected error when opening " + viewerType
6294 + " structure viewer", ex);
6299 * Rewrites a Jmol session script, saves it to a temporary file, and returns
6300 * the path of the file. "load file" commands are rewritten to change the
6301 * original PDB file names to those created as the Jalview project is loaded.
6307 private String rewriteJmolSession(StructureViewerModel svattrib,
6308 jarInputStreamProvider jprovider)
6310 String state = svattrib.getStateData(); // Jalview < 2.9
6311 if (state == null || state.isEmpty()) // Jalview >= 2.9
6313 String jarEntryName = getViewerJarEntryName(svattrib.getViewId());
6314 state = readJarEntry(jprovider, jarEntryName);
6316 // TODO or simpler? for each key in oldFiles,
6317 // replace key.getPath() in state with oldFiles.get(key).getFilePath()
6318 // (allowing for different path escapings)
6319 StringBuilder rewritten = new StringBuilder(state.length());
6320 int cp = 0, ncp, ecp;
6321 Map<File, StructureData> oldFiles = svattrib.getFileData();
6322 while ((ncp = state.indexOf("load ", cp)) > -1)
6326 // look for next filename in load statement
6327 rewritten.append(state.substring(cp,
6328 ncp = (state.indexOf("\"", ncp + 1) + 1)));
6329 String oldfilenam = state.substring(ncp,
6330 ecp = state.indexOf("\"", ncp));
6331 // recover the new mapping data for this old filename
6332 // have to normalize filename - since Jmol and jalview do
6333 // filename translation differently.
6334 StructureData filedat = oldFiles.get(new File(oldfilenam));
6335 if (filedat == null)
6337 String reformatedOldFilename = oldfilenam.replaceAll("/", "\\\\");
6338 filedat = oldFiles.get(new File(reformatedOldFilename));
6340 rewritten.append(Platform.escapeBackslashes(filedat.getFilePath()));
6341 rewritten.append("\"");
6342 cp = ecp + 1; // advance beyond last \" and set cursor so we can
6343 // look for next file statement.
6344 } while ((ncp = state.indexOf("/*file*/", cp)) > -1);
6348 // just append rest of state
6349 rewritten.append(state.substring(cp));
6353 System.err.print("Ignoring incomplete Jmol state for PDB ids: ");
6354 rewritten = new StringBuilder(state);
6355 rewritten.append("; load append ");
6356 for (File id : oldFiles.keySet())
6358 // add pdb files that should be present in the viewer
6359 StructureData filedat = oldFiles.get(id);
6360 rewritten.append(" \"").append(filedat.getFilePath()).append("\"");
6362 rewritten.append(";");
6365 if (rewritten.length() == 0)
6369 final String history = "history = ";
6370 int historyIndex = rewritten.indexOf(history);
6371 if (historyIndex > -1)
6374 * change "history = [true|false];" to "history = [1|0];"
6376 historyIndex += history.length();
6377 String val = rewritten.substring(historyIndex, historyIndex + 5);
6378 if (val.startsWith("true"))
6380 rewritten.replace(historyIndex, historyIndex + 4, "1");
6382 else if (val.startsWith("false"))
6384 rewritten.replace(historyIndex, historyIndex + 5, "0");
6390 File tmp = File.createTempFile("viewerSession", ".tmp");
6391 try (OutputStream os = new FileOutputStream(tmp))
6393 InputStream is = new ByteArrayInputStream(
6394 rewritten.toString().getBytes());
6396 return tmp.getAbsolutePath();
6398 } catch (IOException e)
6400 Console.error("Error restoring Jmol session: " + e.toString());
6406 * Populates an XML model of the feature colour scheme for one feature type
6408 * @param featureType
6412 public static Colour marshalColour(String featureType,
6413 FeatureColourI fcol)
6415 Colour col = new Colour();
6416 if (fcol.isSimpleColour())
6418 col.setRGB(Format.getHexString(fcol.getColour()));
6422 col.setRGB(Format.getHexString(fcol.getMaxColour()));
6423 col.setMin(fcol.getMin());
6424 col.setMax(fcol.getMax());
6425 col.setMinRGB(jalview.util.Format.getHexString(fcol.getMinColour()));
6426 col.setAutoScale(fcol.isAutoScaled());
6427 col.setThreshold(fcol.getThreshold());
6428 col.setColourByLabel(fcol.isColourByLabel());
6429 col.setThreshType(fcol.isAboveThreshold() ? ThresholdType.ABOVE
6430 : (fcol.isBelowThreshold() ? ThresholdType.BELOW
6431 : ThresholdType.NONE));
6432 if (fcol.isColourByAttribute())
6434 final String[] attName = fcol.getAttributeName();
6435 col.getAttributeName().add(attName[0]);
6436 if (attName.length > 1)
6438 col.getAttributeName().add(attName[1]);
6441 Color noColour = fcol.getNoColour();
6442 if (noColour == null)
6444 col.setNoValueColour(NoValueColour.NONE);
6446 else if (noColour == fcol.getMaxColour())
6448 col.setNoValueColour(NoValueColour.MAX);
6452 col.setNoValueColour(NoValueColour.MIN);
6455 col.setName(featureType);
6460 * Populates an XML model of the feature filter(s) for one feature type
6462 * @param firstMatcher
6463 * the first (or only) match condition)
6465 * remaining match conditions (if any)
6467 * if true, conditions are and-ed, else or-ed
6469 public static jalview.xml.binding.jalview.FeatureMatcherSet marshalFilter(
6470 FeatureMatcherI firstMatcher, Iterator<FeatureMatcherI> filters,
6473 jalview.xml.binding.jalview.FeatureMatcherSet result = new jalview.xml.binding.jalview.FeatureMatcherSet();
6475 if (filters.hasNext())
6480 CompoundMatcher compound = new CompoundMatcher();
6481 compound.setAnd(and);
6482 jalview.xml.binding.jalview.FeatureMatcherSet matcher1 = marshalFilter(
6483 firstMatcher, Collections.emptyIterator(), and);
6484 // compound.addMatcherSet(matcher1);
6485 compound.getMatcherSet().add(matcher1);
6486 FeatureMatcherI nextMatcher = filters.next();
6487 jalview.xml.binding.jalview.FeatureMatcherSet matcher2 = marshalFilter(
6488 nextMatcher, filters, and);
6489 // compound.addMatcherSet(matcher2);
6490 compound.getMatcherSet().add(matcher2);
6491 result.setCompoundMatcher(compound);
6496 * single condition matcher
6498 // MatchCondition matcherModel = new MatchCondition();
6499 jalview.xml.binding.jalview.FeatureMatcher matcherModel = new jalview.xml.binding.jalview.FeatureMatcher();
6500 matcherModel.setCondition(
6501 firstMatcher.getMatcher().getCondition().getStableName());
6502 matcherModel.setValue(firstMatcher.getMatcher().getPattern());
6503 if (firstMatcher.isByAttribute())
6505 matcherModel.setBy(FilterBy.BY_ATTRIBUTE);
6506 // matcherModel.setAttributeName(firstMatcher.getAttribute());
6507 String[] attName = firstMatcher.getAttribute();
6508 matcherModel.getAttributeName().add(attName[0]); // attribute
6509 if (attName.length > 1)
6511 matcherModel.getAttributeName().add(attName[1]); // sub-attribute
6514 else if (firstMatcher.isByLabel())
6516 matcherModel.setBy(FilterBy.BY_LABEL);
6518 else if (firstMatcher.isByScore())
6520 matcherModel.setBy(FilterBy.BY_SCORE);
6522 result.setMatchCondition(matcherModel);
6529 * Loads one XML model of a feature filter to a Jalview object
6531 * @param featureType
6532 * @param matcherSetModel
6535 public static FeatureMatcherSetI parseFilter(String featureType,
6536 jalview.xml.binding.jalview.FeatureMatcherSet matcherSetModel)
6538 FeatureMatcherSetI result = new FeatureMatcherSet();
6541 parseFilterConditions(result, matcherSetModel, true);
6542 } catch (IllegalStateException e)
6544 // mixing AND and OR conditions perhaps
6546 String.format("Error reading filter conditions for '%s': %s",
6547 featureType, e.getMessage()));
6548 // return as much as was parsed up to the error
6555 * Adds feature match conditions to matcherSet as unmarshalled from XML
6556 * (possibly recursively for compound conditions)
6559 * @param matcherSetModel
6561 * if true, multiple conditions are AND-ed, else they are OR-ed
6562 * @throws IllegalStateException
6563 * if AND and OR conditions are mixed
6565 protected static void parseFilterConditions(FeatureMatcherSetI matcherSet,
6566 jalview.xml.binding.jalview.FeatureMatcherSet matcherSetModel,
6569 jalview.xml.binding.jalview.FeatureMatcher mc = matcherSetModel
6570 .getMatchCondition();
6576 FilterBy filterBy = mc.getBy();
6577 Condition cond = Condition.fromString(mc.getCondition());
6578 String pattern = mc.getValue();
6579 FeatureMatcherI matchCondition = null;
6580 if (filterBy == FilterBy.BY_LABEL)
6582 matchCondition = FeatureMatcher.byLabel(cond, pattern);
6584 else if (filterBy == FilterBy.BY_SCORE)
6586 matchCondition = FeatureMatcher.byScore(cond, pattern);
6589 else if (filterBy == FilterBy.BY_ATTRIBUTE)
6591 final List<String> attributeName = mc.getAttributeName();
6592 String[] attNames = attributeName
6593 .toArray(new String[attributeName.size()]);
6594 matchCondition = FeatureMatcher.byAttribute(cond, pattern,
6599 * note this throws IllegalStateException if AND-ing to a
6600 * previously OR-ed compound condition, or vice versa
6604 matcherSet.and(matchCondition);
6608 matcherSet.or(matchCondition);
6614 * compound condition
6616 List<jalview.xml.binding.jalview.FeatureMatcherSet> matchers = matcherSetModel
6617 .getCompoundMatcher().getMatcherSet();
6618 boolean anded = matcherSetModel.getCompoundMatcher().isAnd();
6619 if (matchers.size() == 2)
6621 parseFilterConditions(matcherSet, matchers.get(0), anded);
6622 parseFilterConditions(matcherSet, matchers.get(1), anded);
6626 System.err.println("Malformed compound filter condition");
6632 * Loads one XML model of a feature colour to a Jalview object
6634 * @param colourModel
6637 public static FeatureColourI parseColour(Colour colourModel)
6639 FeatureColourI colour = null;
6641 if (colourModel.getMax() != null)
6643 Color mincol = null;
6644 Color maxcol = null;
6645 Color noValueColour = null;
6649 mincol = new Color(Integer.parseInt(colourModel.getMinRGB(), 16));
6650 maxcol = new Color(Integer.parseInt(colourModel.getRGB(), 16));
6651 } catch (Exception e)
6653 Console.warn("Couldn't parse out graduated feature color.", e);
6656 NoValueColour noCol = colourModel.getNoValueColour();
6657 if (noCol == NoValueColour.MIN)
6659 noValueColour = mincol;
6661 else if (noCol == NoValueColour.MAX)
6663 noValueColour = maxcol;
6666 colour = new FeatureColour(maxcol, mincol, maxcol, noValueColour,
6667 safeFloat(colourModel.getMin()),
6668 safeFloat(colourModel.getMax()));
6669 final List<String> attributeName = colourModel.getAttributeName();
6670 String[] attributes = attributeName
6671 .toArray(new String[attributeName.size()]);
6672 if (attributes != null && attributes.length > 0)
6674 colour.setAttributeName(attributes);
6676 if (colourModel.isAutoScale() != null)
6678 colour.setAutoScaled(colourModel.isAutoScale().booleanValue());
6680 if (colourModel.isColourByLabel() != null)
6682 colour.setColourByLabel(
6683 colourModel.isColourByLabel().booleanValue());
6685 if (colourModel.getThreshold() != null)
6687 colour.setThreshold(colourModel.getThreshold().floatValue());
6689 ThresholdType ttyp = colourModel.getThreshType();
6690 if (ttyp == ThresholdType.ABOVE)
6692 colour.setAboveThreshold(true);
6694 else if (ttyp == ThresholdType.BELOW)
6696 colour.setBelowThreshold(true);
6701 Color color = new Color(Integer.parseInt(colourModel.getRGB(), 16));
6702 colour = new FeatureColour(color);
6708 public static void setStateSavedUpToDate(boolean s)
6710 Console.debug("Setting overall stateSavedUpToDate to " + s);
6711 stateSavedUpToDate = s;
6714 public static boolean stateSavedUpToDate()
6716 Console.debug("Returning overall stateSavedUpToDate value: "
6717 + stateSavedUpToDate);
6718 return stateSavedUpToDate;
6721 public static boolean allSavedUpToDate()
6723 if (stateSavedUpToDate()) // nothing happened since last project save
6726 AlignFrame[] frames = Desktop.getAlignFrames();
6729 for (int i = 0; i < frames.length; i++)
6731 if (frames[i] == null)
6733 if (!frames[i].getViewport().savedUpToDate())
6734 return false; // at least one alignment is not individually saved
6740 // used for debugging and tests
6741 private static int debugDelaySave = 20;
6743 public static void setDebugDelaySave(int n)