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.Rectangle;
30 import java.io.BufferedReader;
31 import java.io.ByteArrayInputStream;
33 import java.io.FileInputStream;
34 import java.io.FileOutputStream;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.io.InputStreamReader;
38 import java.io.OutputStream;
39 import java.io.OutputStreamWriter;
40 import java.io.PrintWriter;
41 import java.lang.reflect.InvocationTargetException;
42 import java.math.BigInteger;
43 import java.net.MalformedURLException;
45 import java.util.ArrayList;
46 import java.util.Arrays;
47 import java.util.Collections;
48 import java.util.Enumeration;
49 import java.util.GregorianCalendar;
50 import java.util.HashMap;
51 import java.util.HashSet;
52 import java.util.Hashtable;
53 import java.util.IdentityHashMap;
54 import java.util.Iterator;
55 import java.util.LinkedHashMap;
56 import java.util.List;
57 import java.util.Locale;
59 import java.util.Map.Entry;
61 import java.util.Vector;
62 import java.util.jar.JarEntry;
63 import java.util.jar.JarInputStream;
64 import java.util.jar.JarOutputStream;
66 import javax.swing.JInternalFrame;
67 import javax.swing.SwingUtilities;
68 import javax.xml.bind.JAXBContext;
69 import javax.xml.bind.JAXBElement;
70 import javax.xml.bind.Marshaller;
71 import javax.xml.datatype.DatatypeConfigurationException;
72 import javax.xml.datatype.DatatypeFactory;
73 import javax.xml.datatype.XMLGregorianCalendar;
74 import javax.xml.stream.XMLInputFactory;
75 import javax.xml.stream.XMLStreamReader;
77 import jalview.analysis.Conservation;
78 import jalview.analysis.PCA;
79 import jalview.analysis.scoremodels.ScoreModels;
80 import jalview.analysis.scoremodels.SimilarityParams;
81 import jalview.api.FeatureColourI;
82 import jalview.api.ViewStyleI;
83 import jalview.api.analysis.ScoreModelI;
84 import jalview.api.analysis.SimilarityParamsI;
85 import jalview.api.structures.JalviewStructureDisplayI;
86 import jalview.bin.Cache;
87 import jalview.bin.Console;
88 import jalview.datamodel.AlignedCodonFrame;
89 import jalview.datamodel.Alignment;
90 import jalview.datamodel.AlignmentAnnotation;
91 import jalview.datamodel.AlignmentI;
92 import jalview.datamodel.DBRefEntry;
93 import jalview.datamodel.GeneLocus;
94 import jalview.datamodel.GraphLine;
95 import jalview.datamodel.PDBEntry;
96 import jalview.datamodel.Point;
97 import jalview.datamodel.RnaViewerModel;
98 import jalview.datamodel.SequenceFeature;
99 import jalview.datamodel.SequenceGroup;
100 import jalview.datamodel.SequenceI;
101 import jalview.datamodel.StructureViewerModel;
102 import jalview.datamodel.StructureViewerModel.StructureData;
103 import jalview.datamodel.features.FeatureMatcher;
104 import jalview.datamodel.features.FeatureMatcherI;
105 import jalview.datamodel.features.FeatureMatcherSet;
106 import jalview.datamodel.features.FeatureMatcherSetI;
107 import jalview.ext.varna.RnaModel;
108 import jalview.gui.AlignFrame;
109 import jalview.gui.AlignViewport;
110 import jalview.gui.AlignmentPanel;
111 import jalview.gui.AppVarna;
112 import jalview.gui.Desktop;
113 import jalview.gui.JvOptionPane;
114 import jalview.gui.OOMWarning;
115 import jalview.gui.PCAPanel;
116 import jalview.gui.PaintRefresher;
117 import jalview.gui.SplitFrame;
118 import jalview.gui.StructureViewer;
119 import jalview.gui.StructureViewer.ViewerType;
120 import jalview.gui.StructureViewerBase;
121 import jalview.gui.TreePanel;
122 import jalview.io.BackupFiles;
123 import jalview.io.DataSourceType;
124 import jalview.io.FileFormat;
125 import jalview.io.NewickFile;
126 import jalview.math.Matrix;
127 import jalview.math.MatrixI;
128 import jalview.renderer.ResidueShaderI;
129 import jalview.schemes.AnnotationColourGradient;
130 import jalview.schemes.ColourSchemeI;
131 import jalview.schemes.ColourSchemeProperty;
132 import jalview.schemes.FeatureColour;
133 import jalview.schemes.ResidueProperties;
134 import jalview.schemes.UserColourScheme;
135 import jalview.structure.StructureSelectionManager;
136 import jalview.structures.models.AAStructureBindingModel;
137 import jalview.util.Format;
138 import jalview.util.HttpUtils;
139 import jalview.util.MessageManager;
140 import jalview.util.Platform;
141 import jalview.util.StringUtils;
142 import jalview.util.jarInputStreamProvider;
143 import jalview.util.matcher.Condition;
144 import jalview.viewmodel.AlignmentViewport;
145 import jalview.viewmodel.PCAModel;
146 import jalview.viewmodel.ViewportRanges;
147 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
148 import jalview.viewmodel.seqfeatures.FeatureRendererSettings;
149 import jalview.viewmodel.seqfeatures.FeaturesDisplayed;
150 import jalview.ws.jws2.Jws2Discoverer;
151 import jalview.ws.jws2.dm.AAConSettings;
152 import jalview.ws.jws2.jabaws2.Jws2Instance;
153 import jalview.ws.params.ArgumentI;
154 import jalview.ws.params.AutoCalcSetting;
155 import jalview.ws.params.WsParamSetI;
156 import jalview.xml.binding.jalview.AlcodonFrame;
157 import jalview.xml.binding.jalview.AlcodonFrame.AlcodMap;
158 import jalview.xml.binding.jalview.Annotation;
159 import jalview.xml.binding.jalview.Annotation.ThresholdLine;
160 import jalview.xml.binding.jalview.AnnotationColourScheme;
161 import jalview.xml.binding.jalview.AnnotationElement;
162 import jalview.xml.binding.jalview.DoubleMatrix;
163 import jalview.xml.binding.jalview.DoubleVector;
164 import jalview.xml.binding.jalview.Feature;
165 import jalview.xml.binding.jalview.Feature.OtherData;
166 import jalview.xml.binding.jalview.FeatureMatcherSet.CompoundMatcher;
167 import jalview.xml.binding.jalview.FilterBy;
168 import jalview.xml.binding.jalview.JalviewModel;
169 import jalview.xml.binding.jalview.JalviewModel.FeatureSettings;
170 import jalview.xml.binding.jalview.JalviewModel.FeatureSettings.Group;
171 import jalview.xml.binding.jalview.JalviewModel.FeatureSettings.Setting;
172 import jalview.xml.binding.jalview.JalviewModel.JGroup;
173 import jalview.xml.binding.jalview.JalviewModel.JSeq;
174 import jalview.xml.binding.jalview.JalviewModel.JSeq.Pdbids;
175 import jalview.xml.binding.jalview.JalviewModel.JSeq.Pdbids.StructureState;
176 import jalview.xml.binding.jalview.JalviewModel.JSeq.RnaViewer;
177 import jalview.xml.binding.jalview.JalviewModel.JSeq.RnaViewer.SecondaryStructure;
178 import jalview.xml.binding.jalview.JalviewModel.PcaViewer;
179 import jalview.xml.binding.jalview.JalviewModel.PcaViewer.Axis;
180 import jalview.xml.binding.jalview.JalviewModel.PcaViewer.SeqPointMax;
181 import jalview.xml.binding.jalview.JalviewModel.PcaViewer.SeqPointMin;
182 import jalview.xml.binding.jalview.JalviewModel.PcaViewer.SequencePoint;
183 import jalview.xml.binding.jalview.JalviewModel.Tree;
184 import jalview.xml.binding.jalview.JalviewModel.UserColours;
185 import jalview.xml.binding.jalview.JalviewModel.Viewport;
186 import jalview.xml.binding.jalview.JalviewModel.Viewport.CalcIdParam;
187 import jalview.xml.binding.jalview.JalviewModel.Viewport.HiddenColumns;
188 import jalview.xml.binding.jalview.JalviewUserColours;
189 import jalview.xml.binding.jalview.JalviewUserColours.Colour;
190 import jalview.xml.binding.jalview.MapListType.MapListFrom;
191 import jalview.xml.binding.jalview.MapListType.MapListTo;
192 import jalview.xml.binding.jalview.Mapping;
193 import jalview.xml.binding.jalview.NoValueColour;
194 import jalview.xml.binding.jalview.ObjectFactory;
195 import jalview.xml.binding.jalview.PcaDataType;
196 import jalview.xml.binding.jalview.Pdbentry.Property;
197 import jalview.xml.binding.jalview.Sequence;
198 import jalview.xml.binding.jalview.Sequence.DBRef;
199 import jalview.xml.binding.jalview.SequenceSet;
200 import jalview.xml.binding.jalview.SequenceSet.SequenceSetProperties;
201 import jalview.xml.binding.jalview.ThresholdType;
202 import jalview.xml.binding.jalview.VAMSAS;
205 * Write out the current jalview desktop state as a Jalview XML stream.
207 * Note: the vamsas objects referred to here are primitive versions of the
208 * VAMSAS project schema elements - they are not the same and most likely never
212 * @version $Revision: 1.134 $
214 public class Jalview2XML
217 // BH 2018 we add the .jvp binary extension to J2S so that
218 // it will declare that binary when we do the file save from the browser
222 Platform.addJ2SBinaryType(".jvp?");
225 private static final String VIEWER_PREFIX = "viewer_";
227 private static final String RNA_PREFIX = "rna_";
229 private static final String UTF_8 = "UTF-8";
232 * used in decision if quit confirmation should be issued
234 private static boolean stateSavedUpToDate = false;
237 * prefix for recovering datasets for alignments with multiple views where
238 * non-existent dataset IDs were written for some views
240 private static final String UNIQSEQSETID = "uniqueSeqSetId.";
242 // use this with nextCounter() to make unique names for entities
243 private int counter = 0;
246 * SequenceI reference -> XML ID string in jalview XML. Populated as XML reps
247 * of sequence objects are created.
249 IdentityHashMap<SequenceI, String> seqsToIds = null;
252 * jalview XML Sequence ID to jalview sequence object reference (both dataset
253 * and alignment sequences. Populated as XML reps of sequence objects are
256 Map<String, SequenceI> seqRefIds = null;
258 Map<String, SequenceI> incompleteSeqs = null;
260 List<SeqFref> frefedSequence = null;
262 boolean raiseGUI = true; // whether errors are raised in dialog boxes or not
265 * Map of reconstructed AlignFrame objects that appear to have come from
266 * SplitFrame objects (have a dna/protein complement view).
268 private Map<Viewport, AlignFrame> splitFrameCandidates = new HashMap<>();
271 * Map from displayed rna structure models to their saved session state jar
274 private Map<RnaModel, String> rnaSessions = new HashMap<>();
277 * A helper method for safely using the value of an optional attribute that
278 * may be null if not present in the XML. Answers the boolean value, or false
284 public static boolean safeBoolean(Boolean b)
286 return b == null ? false : b.booleanValue();
290 * A helper method for safely using the value of an optional attribute that
291 * may be null if not present in the XML. Answers the integer value, or zero
297 public static int safeInt(Integer i)
299 return i == null ? 0 : i.intValue();
303 * A helper method for safely using the value of an optional attribute that
304 * may be null if not present in the XML. Answers the float value, or zero if
310 public static float safeFloat(Float f)
312 return f == null ? 0f : f.floatValue();
316 * create/return unique hash string for sq
319 * @return new or existing unique string for sq
321 String seqHash(SequenceI sq)
323 if (seqsToIds == null)
327 if (seqsToIds.containsKey(sq))
329 return seqsToIds.get(sq);
333 // create sequential key
334 String key = "sq" + (seqsToIds.size() + 1);
335 key = makeHashCode(sq, key); // check we don't have an external reference
337 seqsToIds.put(sq, key);
344 if (seqsToIds == null)
346 seqsToIds = new IdentityHashMap<>();
348 if (seqRefIds == null)
350 seqRefIds = new HashMap<>();
352 if (incompleteSeqs == null)
354 incompleteSeqs = new HashMap<>();
356 if (frefedSequence == null)
358 frefedSequence = new ArrayList<>();
366 public Jalview2XML(boolean raiseGUI)
368 this.raiseGUI = raiseGUI;
372 * base class for resolving forward references to sequences by their ID
377 abstract class SeqFref
383 public SeqFref(String _sref, String type)
389 public String getSref()
394 public SequenceI getSrefSeq()
396 return seqRefIds.get(sref);
399 public boolean isResolvable()
401 return seqRefIds.get(sref) != null;
404 public SequenceI getSrefDatasetSeq()
406 SequenceI sq = seqRefIds.get(sref);
409 while (sq.getDatasetSequence() != null)
411 sq = sq.getDatasetSequence();
418 * @return true if the forward reference was fully resolved
420 abstract boolean resolve();
423 public String toString()
425 return type + " reference to " + sref;
430 * create forward reference for a mapping
436 public SeqFref newMappingRef(final String sref,
437 final jalview.datamodel.Mapping _jmap)
439 SeqFref fref = new SeqFref(sref, "Mapping")
441 public jalview.datamodel.Mapping jmap = _jmap;
446 SequenceI seq = getSrefDatasetSeq();
458 public SeqFref newAlcodMapRef(final String sref,
459 final AlignedCodonFrame _cf,
460 final jalview.datamodel.Mapping _jmap)
463 SeqFref fref = new SeqFref(sref, "Codon Frame")
465 AlignedCodonFrame cf = _cf;
467 public jalview.datamodel.Mapping mp = _jmap;
470 public boolean isResolvable()
472 return super.isResolvable() && mp.getTo() != null;
478 SequenceI seq = getSrefDatasetSeq();
483 cf.addMap(seq, mp.getTo(), mp.getMap());
490 public void resolveFrefedSequences()
492 Iterator<SeqFref> nextFref = frefedSequence.iterator();
493 int toresolve = frefedSequence.size();
494 int unresolved = 0, failedtoresolve = 0;
495 while (nextFref.hasNext())
497 SeqFref ref = nextFref.next();
498 if (ref.isResolvable())
510 } catch (Exception x)
513 "IMPLEMENTATION ERROR: Failed to resolve forward reference for sequence "
526 System.err.println("Jalview Project Import: There were " + unresolved
527 + " forward references left unresolved on the stack.");
529 if (failedtoresolve > 0)
531 System.err.println("SERIOUS! " + failedtoresolve
532 + " resolvable forward references failed to resolve.");
534 if (incompleteSeqs != null && incompleteSeqs.size() > 0)
537 "Jalview Project Import: There are " + incompleteSeqs.size()
538 + " sequences which may have incomplete metadata.");
539 if (incompleteSeqs.size() < 10)
541 for (SequenceI s : incompleteSeqs.values())
543 System.err.println(s.toString());
549 "Too many to report. Skipping output of incomplete sequences.");
555 * This maintains a map of viewports, the key being the seqSetId. Important to
556 * set historyItem and redoList for multiple views
558 Map<String, AlignViewport> viewportsAdded = new HashMap<>();
560 Map<String, AlignmentAnnotation> annotationIds = new HashMap<>();
562 String uniqueSetSuffix = "";
565 * List of pdbfiles added to Jar
567 List<String> pdbfiles = null;
569 // SAVES SEVERAL ALIGNMENT WINDOWS TO SAME JARFILE
570 public void saveState(File statefile)
572 FileOutputStream fos = null;
577 fos = new FileOutputStream(statefile);
579 JarOutputStream jout = new JarOutputStream(fos);
583 } catch (Exception e)
585 Console.error("Couln't write Jalview state to " + statefile, e);
586 // TODO: inform user of the problem - they need to know if their data was
588 if (errorMessage == null)
590 errorMessage = "Did't write Jalview Archive to output file '"
591 + statefile + "' - See console error log for details";
595 errorMessage += "(Didn't write Jalview Archive to output file '"
606 } catch (IOException e)
616 * Writes a jalview project archive to the given Jar output stream.
620 public void saveState(JarOutputStream jout)
622 AlignFrame[] frames = Desktop.getAlignFrames();
624 setStateSavedUpToDate(true);
626 if (Cache.getDefault("DEBUG_DELAY_SAVE", false))
628 int n = debugDelaySave;
632 Console.debug("***** debugging save sleep " + i + "/" + n);
636 } catch (InterruptedException e)
638 // TODO Auto-generated catch block
649 saveAllFrames(Arrays.asList(frames), jout);
653 * core method for storing state for a set of AlignFrames.
656 * - frames involving all data to be exported (including containing
659 * - project output stream
661 private void saveAllFrames(List<AlignFrame> frames, JarOutputStream jout)
663 Hashtable<String, AlignFrame> dsses = new Hashtable<>();
666 * ensure cached data is clear before starting
668 // todo tidy up seqRefIds, seqsToIds initialisation / reset
670 splitFrameCandidates.clear();
675 // NOTE UTF-8 MUST BE USED FOR WRITING UNICODE CHARS
676 // //////////////////////////////////////////////////
678 List<String> shortNames = new ArrayList<>();
679 List<String> viewIds = new ArrayList<>();
682 for (int i = frames.size() - 1; i > -1; i--)
684 AlignFrame af = frames.get(i);
686 if (skipList != null && skipList
687 .containsKey(af.getViewport().getSequenceSetId()))
692 String shortName = makeFilename(af, shortNames);
694 int apSize = af.getAlignPanels().size();
696 for (int ap = 0; ap < apSize; ap++)
698 AlignmentPanel apanel = (AlignmentPanel) af.getAlignPanels()
700 String fileName = apSize == 1 ? shortName : ap + shortName;
701 if (!fileName.endsWith(".xml"))
703 fileName = fileName + ".xml";
706 saveState(apanel, fileName, jout, viewIds);
708 String dssid = getDatasetIdRef(
709 af.getViewport().getAlignment().getDataset());
710 if (!dsses.containsKey(dssid))
712 dsses.put(dssid, af);
717 writeDatasetFor(dsses, "" + jout.hashCode() + " " + uniqueSetSuffix,
723 } catch (Exception foo)
727 } catch (Exception ex)
729 // TODO: inform user of the problem - they need to know if their data was
731 if (errorMessage == null)
733 errorMessage = "Couldn't write Jalview Archive - see error output for details";
735 ex.printStackTrace();
740 * Generates a distinct file name, based on the title of the AlignFrame, by
741 * appending _n for increasing n until an unused name is generated. The new
742 * name (without its extension) is added to the list.
746 * @return the generated name, with .xml extension
748 protected String makeFilename(AlignFrame af, List<String> namesUsed)
750 String shortName = af.getTitle();
752 if (shortName.indexOf(File.separatorChar) > -1)
754 shortName = shortName
755 .substring(shortName.lastIndexOf(File.separatorChar) + 1);
760 while (namesUsed.contains(shortName))
762 if (shortName.endsWith("_" + (count - 1)))
764 shortName = shortName.substring(0, shortName.lastIndexOf("_"));
767 shortName = shortName.concat("_" + count);
771 namesUsed.add(shortName);
773 if (!shortName.endsWith(".xml"))
775 shortName = shortName + ".xml";
780 // USE THIS METHOD TO SAVE A SINGLE ALIGNMENT WINDOW
781 public boolean saveAlignment(AlignFrame af, String jarFile,
786 // create backupfiles object and get new temp filename destination
787 boolean doBackup = BackupFiles.getEnabled();
788 BackupFiles backupfiles = doBackup ? new BackupFiles(jarFile) : null;
789 FileOutputStream fos = new FileOutputStream(
790 doBackup ? backupfiles.getTempFilePath() : jarFile);
792 if (Cache.getDefault("DEBUG_DELAY_SAVE", false))
794 int n = debugDelaySave;
798 Console.debug("***** debugging save sleep " + i + "/" + n);
802 } catch (InterruptedException e)
804 // TODO Auto-generated catch block
811 JarOutputStream jout = new JarOutputStream(fos);
812 List<AlignFrame> frames = new ArrayList<>();
814 // resolve splitframes
815 if (af.getViewport().getCodingComplement() != null)
817 frames = ((SplitFrame) af.getSplitViewContainer()).getAlignFrames();
823 saveAllFrames(frames, jout);
827 } catch (Exception foo)
831 boolean success = true;
835 backupfiles.setWriteSuccess(success);
836 success = backupfiles.rollBackupsAndRenameTempFile();
840 } catch (Exception ex)
842 errorMessage = "Couldn't Write alignment view to Jalview Archive - see error output for details";
843 ex.printStackTrace();
848 private void writeDatasetFor(Hashtable<String, AlignFrame> dsses,
849 String fileName, JarOutputStream jout)
852 for (String dssids : dsses.keySet())
854 AlignFrame _af = dsses.get(dssids);
855 String jfileName = fileName + " Dataset for " + _af.getTitle();
856 if (!jfileName.endsWith(".xml"))
858 jfileName = jfileName + ".xml";
860 saveState(_af.alignPanel, jfileName, true, jout, null);
865 * create a JalviewModel from an alignment view and marshall it to a
869 * panel to create jalview model for
871 * name of alignment panel written to output stream
878 public JalviewModel saveState(AlignmentPanel ap, String fileName,
879 JarOutputStream jout, List<String> viewIds)
881 return saveState(ap, fileName, false, jout, viewIds);
885 * create a JalviewModel from an alignment view and marshall it to a
889 * panel to create jalview model for
891 * name of alignment panel written to output stream
893 * when true, only write the dataset for the alignment, not the data
894 * associated with the view.
900 public JalviewModel saveState(AlignmentPanel ap, String fileName,
901 boolean storeDS, JarOutputStream jout, List<String> viewIds)
905 viewIds = new ArrayList<>();
910 List<UserColourScheme> userColours = new ArrayList<>();
912 AlignViewport av = ap.av;
913 ViewportRanges vpRanges = av.getRanges();
915 final ObjectFactory objectFactory = new ObjectFactory();
916 JalviewModel object = objectFactory.createJalviewModel();
917 object.setVamsasModel(new VAMSAS());
919 // object.setCreationDate(new java.util.Date(System.currentTimeMillis()));
922 GregorianCalendar c = new GregorianCalendar();
923 DatatypeFactory datatypeFactory = DatatypeFactory.newInstance();
924 XMLGregorianCalendar now = datatypeFactory.newXMLGregorianCalendar(c);// gregorianCalendar);
925 object.setCreationDate(now);
926 } catch (DatatypeConfigurationException e)
928 System.err.println("error writing date: " + e.toString());
930 object.setVersion(Cache.getDefault("VERSION", "Development Build"));
933 * rjal is full height alignment, jal is actual alignment with full metadata
934 * but excludes hidden sequences.
936 jalview.datamodel.AlignmentI rjal = av.getAlignment(), jal = rjal;
938 if (av.hasHiddenRows())
940 rjal = jal.getHiddenSequences().getFullAlignment();
943 SequenceSet vamsasSet = new SequenceSet();
945 // JalviewModelSequence jms = new JalviewModelSequence();
947 vamsasSet.setGapChar(jal.getGapCharacter() + "");
949 if (jal.getDataset() != null)
951 // dataset id is the dataset's hashcode
952 vamsasSet.setDatasetId(getDatasetIdRef(jal.getDataset()));
955 // switch jal and the dataset
956 jal = jal.getDataset();
960 if (jal.getProperties() != null)
962 Enumeration en = jal.getProperties().keys();
963 while (en.hasMoreElements())
965 String key = en.nextElement().toString();
966 SequenceSetProperties ssp = new SequenceSetProperties();
968 ssp.setValue(jal.getProperties().get(key).toString());
969 // vamsasSet.addSequenceSetProperties(ssp);
970 vamsasSet.getSequenceSetProperties().add(ssp);
975 Set<String> calcIdSet = new HashSet<>();
976 // record the set of vamsas sequence XML POJO we create.
977 HashMap<String, Sequence> vamsasSetIds = new HashMap<>();
979 for (final SequenceI jds : rjal.getSequences())
981 final SequenceI jdatasq = jds.getDatasetSequence() == null ? jds
982 : jds.getDatasetSequence();
983 String id = seqHash(jds);
984 if (vamsasSetIds.get(id) == null)
986 if (seqRefIds.get(id) != null && !storeDS)
988 // This happens for two reasons: 1. multiple views are being
990 // 2. the hashCode has collided with another sequence's code. This
992 // HAPPEN! (PF00072.15.stk does this)
993 // JBPNote: Uncomment to debug writing out of files that do not read
994 // back in due to ArrayOutOfBoundExceptions.
995 // System.err.println("vamsasSeq backref: "+id+"");
996 // System.err.println(jds.getName()+"
997 // "+jds.getStart()+"-"+jds.getEnd()+" "+jds.getSequenceAsString());
998 // System.err.println("Hashcode: "+seqHash(jds));
999 // SequenceI rsq = (SequenceI) seqRefIds.get(id + "");
1000 // System.err.println(rsq.getName()+"
1001 // "+rsq.getStart()+"-"+rsq.getEnd()+" "+rsq.getSequenceAsString());
1002 // System.err.println("Hashcode: "+seqHash(rsq));
1006 vamsasSeq = createVamsasSequence(id, jds);
1007 // vamsasSet.addSequence(vamsasSeq);
1008 vamsasSet.getSequence().add(vamsasSeq);
1009 vamsasSetIds.put(id, vamsasSeq);
1010 seqRefIds.put(id, jds);
1014 jseq.setStart(jds.getStart());
1015 jseq.setEnd(jds.getEnd());
1016 jseq.setColour(av.getSequenceColour(jds).getRGB());
1018 jseq.setId(id); // jseq id should be a string not a number
1021 // Store any sequences this sequence represents
1022 if (av.hasHiddenRows())
1024 // use rjal, contains the full height alignment
1026 av.getAlignment().getHiddenSequences().isHidden(jds));
1028 if (av.isHiddenRepSequence(jds))
1030 jalview.datamodel.SequenceI[] reps = av
1031 .getRepresentedSequences(jds).getSequencesInOrder(rjal);
1033 for (int h = 0; h < reps.length; h++)
1037 // jseq.addHiddenSequences(rjal.findIndex(reps[h]));
1038 jseq.getHiddenSequences().add(rjal.findIndex(reps[h]));
1043 // mark sequence as reference - if it is the reference for this view
1044 if (jal.hasSeqrep())
1046 jseq.setViewreference(jds == jal.getSeqrep());
1050 // TODO: omit sequence features from each alignment view's XML dump if we
1051 // are storing dataset
1052 List<SequenceFeature> sfs = jds.getSequenceFeatures();
1053 for (SequenceFeature sf : sfs)
1055 // Features features = new Features();
1056 Feature features = new Feature();
1058 features.setBegin(sf.getBegin());
1059 features.setEnd(sf.getEnd());
1060 features.setDescription(sf.getDescription());
1061 features.setType(sf.getType());
1062 features.setFeatureGroup(sf.getFeatureGroup());
1063 features.setScore(sf.getScore());
1064 if (sf.links != null)
1066 for (int l = 0; l < sf.links.size(); l++)
1068 OtherData keyValue = new OtherData();
1069 keyValue.setKey("LINK_" + l);
1070 keyValue.setValue(sf.links.elementAt(l).toString());
1071 // features.addOtherData(keyValue);
1072 features.getOtherData().add(keyValue);
1075 if (sf.otherDetails != null)
1078 * save feature attributes, which may be simple strings or
1079 * map valued (have sub-attributes)
1081 for (Entry<String, Object> entry : sf.otherDetails.entrySet())
1083 String key = entry.getKey();
1084 Object value = entry.getValue();
1085 if (value instanceof Map<?, ?>)
1087 for (Entry<String, Object> subAttribute : ((Map<String, Object>) value)
1090 OtherData otherData = new OtherData();
1091 otherData.setKey(key);
1092 otherData.setKey2(subAttribute.getKey());
1093 otherData.setValue(subAttribute.getValue().toString());
1094 // features.addOtherData(otherData);
1095 features.getOtherData().add(otherData);
1100 OtherData otherData = new OtherData();
1101 otherData.setKey(key);
1102 otherData.setValue(value.toString());
1103 // features.addOtherData(otherData);
1104 features.getOtherData().add(otherData);
1109 // jseq.addFeatures(features);
1110 jseq.getFeatures().add(features);
1113 if (jdatasq.getAllPDBEntries() != null)
1115 Enumeration<PDBEntry> en = jdatasq.getAllPDBEntries().elements();
1116 while (en.hasMoreElements())
1118 Pdbids pdb = new Pdbids();
1119 jalview.datamodel.PDBEntry entry = en.nextElement();
1121 String pdbId = entry.getId();
1123 pdb.setType(entry.getType());
1126 * Store any structure views associated with this sequence. This
1127 * section copes with duplicate entries in the project, so a dataset
1128 * only view *should* be coped with sensibly.
1130 // This must have been loaded, is it still visible?
1131 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
1132 String matchedFile = null;
1133 for (int f = frames.length - 1; f > -1; f--)
1135 if (frames[f] instanceof StructureViewerBase)
1137 StructureViewerBase viewFrame = (StructureViewerBase) frames[f];
1138 matchedFile = saveStructureViewer(ap, jds, pdb, entry,
1139 viewIds, matchedFile, viewFrame);
1141 * Only store each structure viewer's state once in the project
1142 * jar. First time through only (storeDS==false)
1144 String viewId = viewFrame.getViewId();
1145 String viewerType = viewFrame.getViewerType().toString();
1146 if (!storeDS && !viewIds.contains(viewId))
1148 viewIds.add(viewId);
1149 File viewerState = viewFrame.saveSession();
1150 if (viewerState != null)
1152 copyFileToJar(jout, viewerState.getPath(),
1153 getViewerJarEntryName(viewId), viewerType);
1158 "Failed to save viewer state for " + viewerType);
1164 if (matchedFile != null || entry.getFile() != null)
1166 if (entry.getFile() != null)
1169 matchedFile = entry.getFile();
1171 pdb.setFile(matchedFile); // entry.getFile());
1172 if (pdbfiles == null)
1174 pdbfiles = new ArrayList<>();
1177 if (!pdbfiles.contains(pdbId))
1179 pdbfiles.add(pdbId);
1180 copyFileToJar(jout, matchedFile, pdbId, pdbId);
1184 Enumeration<String> props = entry.getProperties();
1185 if (props.hasMoreElements())
1187 // PdbentryItem item = new PdbentryItem();
1188 while (props.hasMoreElements())
1190 Property prop = new Property();
1191 String key = props.nextElement();
1193 prop.setValue(entry.getProperty(key).toString());
1194 // item.addProperty(prop);
1195 pdb.getProperty().add(prop);
1197 // pdb.addPdbentryItem(item);
1200 // jseq.addPdbids(pdb);
1201 jseq.getPdbids().add(pdb);
1205 saveRnaViewers(jout, jseq, jds, viewIds, ap, storeDS);
1207 // jms.addJSeq(jseq);
1208 object.getJSeq().add(jseq);
1211 if (!storeDS && av.hasHiddenRows())
1213 jal = av.getAlignment();
1217 if (storeDS && jal.getCodonFrames() != null)
1219 List<AlignedCodonFrame> jac = jal.getCodonFrames();
1220 for (AlignedCodonFrame acf : jac)
1222 AlcodonFrame alc = new AlcodonFrame();
1223 if (acf.getProtMappings() != null
1224 && acf.getProtMappings().length > 0)
1226 boolean hasMap = false;
1227 SequenceI[] dnas = acf.getdnaSeqs();
1228 jalview.datamodel.Mapping[] pmaps = acf.getProtMappings();
1229 for (int m = 0; m < pmaps.length; m++)
1231 AlcodMap alcmap = new AlcodMap();
1232 alcmap.setDnasq(seqHash(dnas[m]));
1234 createVamsasMapping(pmaps[m], dnas[m], null, false));
1235 // alc.addAlcodMap(alcmap);
1236 alc.getAlcodMap().add(alcmap);
1241 // vamsasSet.addAlcodonFrame(alc);
1242 vamsasSet.getAlcodonFrame().add(alc);
1245 // TODO: delete this ? dead code from 2.8.3->2.9 ?
1247 // AlcodonFrame alc = new AlcodonFrame();
1248 // vamsasSet.addAlcodonFrame(alc);
1249 // for (int p = 0; p < acf.aaWidth; p++)
1251 // Alcodon cmap = new Alcodon();
1252 // if (acf.codons[p] != null)
1254 // // Null codons indicate a gapped column in the translated peptide
1256 // cmap.setPos1(acf.codons[p][0]);
1257 // cmap.setPos2(acf.codons[p][1]);
1258 // cmap.setPos3(acf.codons[p][2]);
1260 // alc.addAlcodon(cmap);
1262 // if (acf.getProtMappings() != null
1263 // && acf.getProtMappings().length > 0)
1265 // SequenceI[] dnas = acf.getdnaSeqs();
1266 // jalview.datamodel.Mapping[] pmaps = acf.getProtMappings();
1267 // for (int m = 0; m < pmaps.length; m++)
1269 // AlcodMap alcmap = new AlcodMap();
1270 // alcmap.setDnasq(seqHash(dnas[m]));
1271 // alcmap.setMapping(createVamsasMapping(pmaps[m], dnas[m], null,
1273 // alc.addAlcodMap(alcmap);
1280 // /////////////////////////////////
1281 if (!storeDS && av.getCurrentTree() != null)
1283 // FIND ANY ASSOCIATED TREES
1284 // NOT IMPLEMENTED FOR HEADLESS STATE AT PRESENT
1285 if (Desktop.desktop != null)
1287 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
1289 for (int t = 0; t < frames.length; t++)
1291 if (frames[t] instanceof TreePanel)
1293 TreePanel tp = (TreePanel) frames[t];
1295 if (tp.getTreeCanvas().getViewport().getAlignment() == jal)
1297 JalviewModel.Tree tree = new JalviewModel.Tree();
1298 tree.setTitle(tp.getTitle());
1299 tree.setCurrentTree((av.getCurrentTree() == tp.getTree()));
1300 tree.setNewick(tp.getTree().print());
1301 tree.setThreshold(tp.getTreeCanvas().getThreshold());
1303 tree.setFitToWindow(tp.fitToWindow.getState());
1304 tree.setFontName(tp.getTreeFont().getName());
1305 tree.setFontSize(tp.getTreeFont().getSize());
1306 tree.setFontStyle(tp.getTreeFont().getStyle());
1307 tree.setMarkUnlinked(tp.placeholdersMenu.getState());
1309 tree.setShowBootstrap(tp.bootstrapMenu.getState());
1310 tree.setShowDistances(tp.distanceMenu.getState());
1312 tree.setHeight(tp.getHeight());
1313 tree.setWidth(tp.getWidth());
1314 tree.setXpos(tp.getX());
1315 tree.setYpos(tp.getY());
1316 tree.setId(makeHashCode(tp, null));
1317 tree.setLinkToAllViews(
1318 tp.getTreeCanvas().isApplyToAllViews());
1320 // jms.addTree(tree);
1321 object.getTree().add(tree);
1331 if (!storeDS && Desktop.desktop != null)
1333 for (JInternalFrame frame : Desktop.desktop.getAllFrames())
1335 if (frame instanceof PCAPanel)
1337 PCAPanel panel = (PCAPanel) frame;
1338 if (panel.getAlignViewport().getAlignment() == jal)
1340 savePCA(panel, object);
1348 * store forward refs from an annotationRow to any groups
1350 IdentityHashMap<SequenceGroup, String> groupRefs = new IdentityHashMap<>();
1353 for (SequenceI sq : jal.getSequences())
1355 // Store annotation on dataset sequences only
1356 AlignmentAnnotation[] aa = sq.getAnnotation();
1357 if (aa != null && aa.length > 0)
1359 storeAlignmentAnnotation(aa, groupRefs, av, calcIdSet, storeDS,
1366 if (jal.getAlignmentAnnotation() != null)
1368 // Store the annotation shown on the alignment.
1369 AlignmentAnnotation[] aa = jal.getAlignmentAnnotation();
1370 storeAlignmentAnnotation(aa, groupRefs, av, calcIdSet, storeDS,
1375 if (jal.getGroups() != null)
1377 JGroup[] groups = new JGroup[jal.getGroups().size()];
1379 for (jalview.datamodel.SequenceGroup sg : jal.getGroups())
1381 JGroup jGroup = new JGroup();
1382 groups[++i] = jGroup;
1384 jGroup.setStart(sg.getStartRes());
1385 jGroup.setEnd(sg.getEndRes());
1386 jGroup.setName(sg.getName());
1387 if (groupRefs.containsKey(sg))
1389 // group has references so set its ID field
1390 jGroup.setId(groupRefs.get(sg));
1392 ColourSchemeI colourScheme = sg.getColourScheme();
1393 if (colourScheme != null)
1395 ResidueShaderI groupColourScheme = sg.getGroupColourScheme();
1396 if (groupColourScheme.conservationApplied())
1398 jGroup.setConsThreshold(groupColourScheme.getConservationInc());
1400 if (colourScheme instanceof jalview.schemes.UserColourScheme)
1402 jGroup.setColour(setUserColourScheme(colourScheme,
1403 userColours, object));
1407 jGroup.setColour(colourScheme.getSchemeName());
1410 else if (colourScheme instanceof jalview.schemes.AnnotationColourGradient)
1412 jGroup.setColour("AnnotationColourGradient");
1413 jGroup.setAnnotationColours(constructAnnotationColours(
1414 (jalview.schemes.AnnotationColourGradient) colourScheme,
1415 userColours, object));
1417 else if (colourScheme instanceof jalview.schemes.UserColourScheme)
1420 setUserColourScheme(colourScheme, userColours, object));
1424 jGroup.setColour(colourScheme.getSchemeName());
1427 jGroup.setPidThreshold(groupColourScheme.getThreshold());
1430 jGroup.setOutlineColour(sg.getOutlineColour().getRGB());
1431 jGroup.setDisplayBoxes(sg.getDisplayBoxes());
1432 jGroup.setDisplayText(sg.getDisplayText());
1433 jGroup.setColourText(sg.getColourText());
1434 jGroup.setTextCol1(sg.textColour.getRGB());
1435 jGroup.setTextCol2(sg.textColour2.getRGB());
1436 jGroup.setTextColThreshold(sg.thresholdTextColour);
1437 jGroup.setShowUnconserved(sg.getShowNonconserved());
1438 jGroup.setIgnoreGapsinConsensus(sg.getIgnoreGapsConsensus());
1439 jGroup.setShowConsensusHistogram(sg.isShowConsensusHistogram());
1440 jGroup.setShowSequenceLogo(sg.isShowSequenceLogo());
1441 jGroup.setNormaliseSequenceLogo(sg.isNormaliseSequenceLogo());
1442 for (SequenceI seq : sg.getSequences())
1444 // jGroup.addSeq(seqHash(seq));
1445 jGroup.getSeq().add(seqHash(seq));
1449 // jms.setJGroup(groups);
1451 for (JGroup grp : groups)
1453 object.getJGroup().add(grp);
1458 // /////////SAVE VIEWPORT
1459 Viewport view = new Viewport();
1460 view.setTitle(ap.alignFrame.getTitle());
1461 view.setSequenceSetId(
1462 makeHashCode(av.getSequenceSetId(), av.getSequenceSetId()));
1463 view.setId(av.getViewId());
1464 if (av.getCodingComplement() != null)
1466 view.setComplementId(av.getCodingComplement().getViewId());
1468 view.setViewName(av.getViewName());
1469 view.setGatheredViews(av.isGatherViewsHere());
1471 Rectangle size = ap.av.getExplodedGeometry();
1472 Rectangle position = size;
1475 size = ap.alignFrame.getBounds();
1476 if (av.getCodingComplement() != null)
1478 position = ((SplitFrame) ap.alignFrame.getSplitViewContainer())
1486 view.setXpos(position.x);
1487 view.setYpos(position.y);
1489 view.setWidth(size.width);
1490 view.setHeight(size.height);
1492 view.setStartRes(vpRanges.getStartRes());
1493 view.setStartSeq(vpRanges.getStartSeq());
1495 if (av.getGlobalColourScheme() instanceof jalview.schemes.UserColourScheme)
1497 view.setBgColour(setUserColourScheme(av.getGlobalColourScheme(),
1498 userColours, object));
1501 .getGlobalColourScheme() instanceof jalview.schemes.AnnotationColourGradient)
1503 AnnotationColourScheme ac = constructAnnotationColours(
1504 (jalview.schemes.AnnotationColourGradient) av
1505 .getGlobalColourScheme(),
1506 userColours, object);
1508 view.setAnnotationColours(ac);
1509 view.setBgColour("AnnotationColourGradient");
1513 view.setBgColour(ColourSchemeProperty
1514 .getColourName(av.getGlobalColourScheme()));
1517 ResidueShaderI vcs = av.getResidueShading();
1518 ColourSchemeI cs = av.getGlobalColourScheme();
1522 if (vcs.conservationApplied())
1524 view.setConsThreshold(vcs.getConservationInc());
1525 if (cs instanceof jalview.schemes.UserColourScheme)
1527 view.setBgColour(setUserColourScheme(cs, userColours, object));
1530 view.setPidThreshold(vcs.getThreshold());
1533 view.setConservationSelected(av.getConservationSelected());
1534 view.setPidSelected(av.getAbovePIDThreshold());
1535 final Font font = av.getFont();
1536 view.setFontName(font.getName());
1537 view.setFontSize(font.getSize());
1538 view.setFontStyle(font.getStyle());
1539 view.setScaleProteinAsCdna(av.getViewStyle().isScaleProteinAsCdna());
1540 view.setRenderGaps(av.isRenderGaps());
1541 view.setShowAnnotation(av.isShowAnnotation());
1542 view.setShowBoxes(av.getShowBoxes());
1543 view.setShowColourText(av.getColourText());
1544 view.setShowFullId(av.getShowJVSuffix());
1545 view.setRightAlignIds(av.isRightAlignIds());
1546 view.setShowSequenceFeatures(av.isShowSequenceFeatures());
1547 view.setShowText(av.getShowText());
1548 view.setShowUnconserved(av.getShowUnconserved());
1549 view.setWrapAlignment(av.getWrapAlignment());
1550 view.setTextCol1(av.getTextColour().getRGB());
1551 view.setTextCol2(av.getTextColour2().getRGB());
1552 view.setTextColThreshold(av.getThresholdTextColour());
1553 view.setShowConsensusHistogram(av.isShowConsensusHistogram());
1554 view.setShowSequenceLogo(av.isShowSequenceLogo());
1555 view.setNormaliseSequenceLogo(av.isNormaliseSequenceLogo());
1556 view.setShowGroupConsensus(av.isShowGroupConsensus());
1557 view.setShowGroupConservation(av.isShowGroupConservation());
1558 view.setShowNPfeatureTooltip(av.isShowNPFeats());
1559 view.setShowDbRefTooltip(av.isShowDBRefs());
1560 view.setFollowHighlight(av.isFollowHighlight());
1561 view.setFollowSelection(av.followSelection);
1562 view.setIgnoreGapsinConsensus(av.isIgnoreGapsConsensus());
1563 view.setShowComplementFeatures(av.isShowComplementFeatures());
1564 view.setShowComplementFeaturesOnTop(
1565 av.isShowComplementFeaturesOnTop());
1566 if (av.getFeaturesDisplayed() != null)
1568 FeatureSettings fs = new FeatureSettings();
1570 FeatureRendererModel fr = ap.getSeqPanel().seqCanvas
1571 .getFeatureRenderer();
1572 String[] renderOrder = fr.getRenderOrder().toArray(new String[0]);
1574 Vector<String> settingsAdded = new Vector<>();
1575 if (renderOrder != null)
1577 for (String featureType : renderOrder)
1579 FeatureSettings.Setting setting = new FeatureSettings.Setting();
1580 setting.setType(featureType);
1583 * save any filter for the feature type
1585 FeatureMatcherSetI filter = fr.getFeatureFilter(featureType);
1588 Iterator<FeatureMatcherI> filters = filter.getMatchers()
1590 FeatureMatcherI firstFilter = filters.next();
1591 setting.setMatcherSet(Jalview2XML.marshalFilter(firstFilter,
1592 filters, filter.isAnded()));
1596 * save colour scheme for the feature type
1598 FeatureColourI fcol = fr.getFeatureStyle(featureType);
1599 if (!fcol.isSimpleColour())
1601 setting.setColour(fcol.getMaxColour().getRGB());
1602 setting.setMincolour(fcol.getMinColour().getRGB());
1603 setting.setMin(fcol.getMin());
1604 setting.setMax(fcol.getMax());
1605 setting.setColourByLabel(fcol.isColourByLabel());
1606 if (fcol.isColourByAttribute())
1608 String[] attName = fcol.getAttributeName();
1609 setting.getAttributeName().add(attName[0]);
1610 if (attName.length > 1)
1612 setting.getAttributeName().add(attName[1]);
1615 setting.setAutoScale(fcol.isAutoScaled());
1616 setting.setThreshold(fcol.getThreshold());
1617 Color noColour = fcol.getNoColour();
1618 if (noColour == null)
1620 setting.setNoValueColour(NoValueColour.NONE);
1622 else if (noColour.equals(fcol.getMaxColour()))
1624 setting.setNoValueColour(NoValueColour.MAX);
1628 setting.setNoValueColour(NoValueColour.MIN);
1630 // -1 = No threshold, 0 = Below, 1 = Above
1631 setting.setThreshstate(fcol.isAboveThreshold() ? 1
1632 : (fcol.isBelowThreshold() ? 0 : -1));
1636 setting.setColour(fcol.getColour().getRGB());
1640 av.getFeaturesDisplayed().isVisible(featureType));
1641 float rorder = fr.getOrder(featureType);
1644 setting.setOrder(rorder);
1646 /// fs.addSetting(setting);
1647 fs.getSetting().add(setting);
1648 settingsAdded.addElement(featureType);
1652 // is groups actually supposed to be a map here ?
1653 Iterator<String> en = fr.getFeatureGroups().iterator();
1654 Vector<String> groupsAdded = new Vector<>();
1655 while (en.hasNext())
1657 String grp = en.next();
1658 if (groupsAdded.contains(grp))
1662 Group g = new Group();
1664 g.setDisplay(((Boolean) fr.checkGroupVisibility(grp, false))
1667 fs.getGroup().add(g);
1668 groupsAdded.addElement(grp);
1670 // jms.setFeatureSettings(fs);
1671 object.setFeatureSettings(fs);
1674 if (av.hasHiddenColumns())
1676 jalview.datamodel.HiddenColumns hidden = av.getAlignment()
1677 .getHiddenColumns();
1681 "REPORT BUG: avoided null columnselection bug (DMAM reported). Please contact Jim about this.");
1685 Iterator<int[]> hiddenRegions = hidden.iterator();
1686 while (hiddenRegions.hasNext())
1688 int[] region = hiddenRegions.next();
1689 HiddenColumns hc = new HiddenColumns();
1690 hc.setStart(region[0]);
1691 hc.setEnd(region[1]);
1692 // view.addHiddenColumns(hc);
1693 view.getHiddenColumns().add(hc);
1697 if (calcIdSet.size() > 0)
1699 for (String calcId : calcIdSet)
1701 if (calcId.trim().length() > 0)
1703 CalcIdParam cidp = createCalcIdParam(calcId, av);
1704 // Some calcIds have no parameters.
1707 // view.addCalcIdParam(cidp);
1708 view.getCalcIdParam().add(cidp);
1714 // jms.addViewport(view);
1715 object.getViewport().add(view);
1717 // object.setJalviewModelSequence(jms);
1718 // object.getVamsasModel().addSequenceSet(vamsasSet);
1719 object.getVamsasModel().getSequenceSet().add(vamsasSet);
1721 if (jout != null && fileName != null)
1723 // We may not want to write the object to disk,
1724 // eg we can copy the alignViewport to a new view object
1725 // using save and then load
1728 fileName = fileName.replace('\\', '/');
1729 System.out.println("Writing jar entry " + fileName);
1730 JarEntry entry = new JarEntry(fileName);
1731 jout.putNextEntry(entry);
1732 PrintWriter pout = new PrintWriter(
1733 new OutputStreamWriter(jout, UTF_8));
1734 JAXBContext jaxbContext = JAXBContext
1735 .newInstance(JalviewModel.class);
1736 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
1738 // output pretty printed
1739 // jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
1740 jaxbMarshaller.marshal(
1741 new ObjectFactory().createJalviewModel(object), pout);
1743 // jaxbMarshaller.marshal(object, pout);
1744 // marshaller.marshal(object);
1747 } catch (Exception ex)
1749 // TODO: raise error in GUI if marshalling failed.
1750 System.err.println("Error writing Jalview project");
1751 ex.printStackTrace();
1758 * Writes PCA viewer attributes and computed values to an XML model object and
1759 * adds it to the JalviewModel. Any exceptions are reported by logging.
1761 protected void savePCA(PCAPanel panel, JalviewModel object)
1765 PcaViewer viewer = new PcaViewer();
1766 viewer.setHeight(panel.getHeight());
1767 viewer.setWidth(panel.getWidth());
1768 viewer.setXpos(panel.getX());
1769 viewer.setYpos(panel.getY());
1770 viewer.setTitle(panel.getTitle());
1771 PCAModel pcaModel = panel.getPcaModel();
1772 viewer.setScoreModelName(pcaModel.getScoreModelName());
1773 viewer.setXDim(panel.getSelectedDimensionIndex(X));
1774 viewer.setYDim(panel.getSelectedDimensionIndex(Y));
1775 viewer.setZDim(panel.getSelectedDimensionIndex(Z));
1777 panel.getRotatableCanvas().getBackgroundColour().getRGB());
1778 viewer.setScaleFactor(panel.getRotatableCanvas().getScaleFactor());
1779 float[] spMin = panel.getRotatableCanvas().getSeqMin();
1780 SeqPointMin spmin = new SeqPointMin();
1781 spmin.setXPos(spMin[0]);
1782 spmin.setYPos(spMin[1]);
1783 spmin.setZPos(spMin[2]);
1784 viewer.setSeqPointMin(spmin);
1785 float[] spMax = panel.getRotatableCanvas().getSeqMax();
1786 SeqPointMax spmax = new SeqPointMax();
1787 spmax.setXPos(spMax[0]);
1788 spmax.setYPos(spMax[1]);
1789 spmax.setZPos(spMax[2]);
1790 viewer.setSeqPointMax(spmax);
1791 viewer.setShowLabels(panel.getRotatableCanvas().isShowLabels());
1792 viewer.setLinkToAllViews(
1793 panel.getRotatableCanvas().isApplyToAllViews());
1794 SimilarityParamsI sp = pcaModel.getSimilarityParameters();
1795 viewer.setIncludeGaps(sp.includeGaps());
1796 viewer.setMatchGaps(sp.matchGaps());
1797 viewer.setIncludeGappedColumns(sp.includeGappedColumns());
1798 viewer.setDenominateByShortestLength(sp.denominateByShortestLength());
1801 * sequence points on display
1803 for (jalview.datamodel.SequencePoint spt : pcaModel
1804 .getSequencePoints())
1806 SequencePoint point = new SequencePoint();
1807 point.setSequenceRef(seqHash(spt.getSequence()));
1808 point.setXPos(spt.coord.x);
1809 point.setYPos(spt.coord.y);
1810 point.setZPos(spt.coord.z);
1811 viewer.getSequencePoint().add(point);
1815 * (end points of) axes on display
1817 for (Point p : panel.getRotatableCanvas().getAxisEndPoints())
1820 Axis axis = new Axis();
1824 viewer.getAxis().add(axis);
1828 * raw PCA data (note we are not restoring PCA inputs here -
1829 * alignment view, score model, similarity parameters)
1831 PcaDataType data = new PcaDataType();
1832 viewer.setPcaData(data);
1833 PCA pca = pcaModel.getPcaData();
1835 DoubleMatrix pm = new DoubleMatrix();
1836 saveDoubleMatrix(pca.getPairwiseScores(), pm);
1837 data.setPairwiseMatrix(pm);
1839 DoubleMatrix tm = new DoubleMatrix();
1840 saveDoubleMatrix(pca.getTridiagonal(), tm);
1841 data.setTridiagonalMatrix(tm);
1843 DoubleMatrix eigenMatrix = new DoubleMatrix();
1844 data.setEigenMatrix(eigenMatrix);
1845 saveDoubleMatrix(pca.getEigenmatrix(), eigenMatrix);
1847 object.getPcaViewer().add(viewer);
1848 } catch (Throwable t)
1850 Console.error("Error saving PCA: " + t.getMessage());
1855 * Stores values from a matrix into an XML element, including (if present) the
1860 * @see #loadDoubleMatrix(DoubleMatrix)
1862 protected void saveDoubleMatrix(MatrixI m, DoubleMatrix xmlMatrix)
1864 xmlMatrix.setRows(m.height());
1865 xmlMatrix.setColumns(m.width());
1866 for (int i = 0; i < m.height(); i++)
1868 DoubleVector row = new DoubleVector();
1869 for (int j = 0; j < m.width(); j++)
1871 row.getV().add(m.getValue(i, j));
1873 xmlMatrix.getRow().add(row);
1875 if (m.getD() != null)
1877 DoubleVector dVector = new DoubleVector();
1878 for (double d : m.getD())
1880 dVector.getV().add(d);
1882 xmlMatrix.setD(dVector);
1884 if (m.getE() != null)
1886 DoubleVector eVector = new DoubleVector();
1887 for (double e : m.getE())
1889 eVector.getV().add(e);
1891 xmlMatrix.setE(eVector);
1896 * Loads XML matrix data into a new Matrix object, including the D and/or E
1897 * vectors (if present)
1901 * @see Jalview2XML#saveDoubleMatrix(MatrixI, DoubleMatrix)
1903 protected MatrixI loadDoubleMatrix(DoubleMatrix mData)
1905 int rows = mData.getRows();
1906 double[][] vals = new double[rows][];
1908 for (int i = 0; i < rows; i++)
1910 List<Double> dVector = mData.getRow().get(i).getV();
1911 vals[i] = new double[dVector.size()];
1913 for (Double d : dVector)
1919 MatrixI m = new Matrix(vals);
1921 if (mData.getD() != null)
1923 List<Double> dVector = mData.getD().getV();
1924 double[] vec = new double[dVector.size()];
1926 for (Double d : dVector)
1932 if (mData.getE() != null)
1934 List<Double> dVector = mData.getE().getV();
1935 double[] vec = new double[dVector.size()];
1937 for (Double d : dVector)
1948 * Save any Varna viewers linked to this sequence. Writes an rnaViewer element
1949 * for each viewer, with
1951 * <li>viewer geometry (position, size, split pane divider location)</li>
1952 * <li>index of the selected structure in the viewer (currently shows gapped
1954 * <li>the id of the annotation holding RNA secondary structure</li>
1955 * <li>(currently only one SS is shown per viewer, may be more in future)</li>
1957 * Varna viewer state is also written out (in native Varna XML) to separate
1958 * project jar entries. A separate entry is written for each RNA structure
1959 * displayed, with the naming convention
1961 * <li>rna_viewId_sequenceId_annotationId_[gapped|trimmed]</li>
1969 * @param storeDataset
1971 protected void saveRnaViewers(JarOutputStream jout, JSeq jseq,
1972 final SequenceI jds, List<String> viewIds, AlignmentPanel ap,
1973 boolean storeDataset)
1975 if (Desktop.desktop == null)
1979 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
1980 for (int f = frames.length - 1; f > -1; f--)
1982 if (frames[f] instanceof AppVarna)
1984 AppVarna varna = (AppVarna) frames[f];
1986 * link the sequence to every viewer that is showing it and is linked to
1987 * its alignment panel
1989 if (varna.isListeningFor(jds) && ap == varna.getAlignmentPanel())
1991 String viewId = varna.getViewId();
1992 RnaViewer rna = new RnaViewer();
1993 rna.setViewId(viewId);
1994 rna.setTitle(varna.getTitle());
1995 rna.setXpos(varna.getX());
1996 rna.setYpos(varna.getY());
1997 rna.setWidth(varna.getWidth());
1998 rna.setHeight(varna.getHeight());
1999 rna.setDividerLocation(varna.getDividerLocation());
2000 rna.setSelectedRna(varna.getSelectedIndex());
2001 // jseq.addRnaViewer(rna);
2002 jseq.getRnaViewer().add(rna);
2005 * Store each Varna panel's state once in the project per sequence.
2006 * First time through only (storeDataset==false)
2008 // boolean storeSessions = false;
2009 // String sequenceViewId = viewId + seqsToIds.get(jds);
2010 // if (!storeDataset && !viewIds.contains(sequenceViewId))
2012 // viewIds.add(sequenceViewId);
2013 // storeSessions = true;
2015 for (RnaModel model : varna.getModels())
2017 if (model.seq == jds)
2020 * VARNA saves each view (sequence or alignment secondary
2021 * structure, gapped or trimmed) as a separate XML file
2023 String jarEntryName = rnaSessions.get(model);
2024 if (jarEntryName == null)
2027 String varnaStateFile = varna.getStateInfo(model.rna);
2028 jarEntryName = RNA_PREFIX + viewId + "_" + nextCounter();
2029 copyFileToJar(jout, varnaStateFile, jarEntryName, "Varna");
2030 rnaSessions.put(model, jarEntryName);
2032 SecondaryStructure ss = new SecondaryStructure();
2033 String annotationId = varna.getAnnotation(jds).annotationId;
2034 ss.setAnnotationId(annotationId);
2035 ss.setViewerState(jarEntryName);
2036 ss.setGapped(model.gapped);
2037 ss.setTitle(model.title);
2038 // rna.addSecondaryStructure(ss);
2039 rna.getSecondaryStructure().add(ss);
2048 * Copy the contents of a file to a new entry added to the output jar
2052 * @param jarEntryName
2054 * additional identifying info to log to the console
2056 protected void copyFileToJar(JarOutputStream jout, String infilePath,
2057 String jarEntryName, String msg)
2059 try (InputStream is = new FileInputStream(infilePath))
2061 File file = new File(infilePath);
2062 if (file.exists() && jout != null)
2065 "Writing jar entry " + jarEntryName + " (" + msg + ")");
2066 jout.putNextEntry(new JarEntry(jarEntryName));
2069 // dis = new DataInputStream(new FileInputStream(file));
2070 // byte[] data = new byte[(int) file.length()];
2071 // dis.readFully(data);
2072 // writeJarEntry(jout, jarEntryName, data);
2074 } catch (Exception ex)
2076 ex.printStackTrace();
2081 * Copies input to output, in 4K buffers; handles any data (text or binary)
2085 * @throws IOException
2087 protected void copyAll(InputStream in, OutputStream out)
2090 byte[] buffer = new byte[4096];
2092 while ((bytesRead = in.read(buffer)) != -1)
2094 out.write(buffer, 0, bytesRead);
2099 * Save the state of a structure viewer
2104 * the archive XML element under which to save the state
2107 * @param matchedFile
2111 protected String saveStructureViewer(AlignmentPanel ap, SequenceI jds,
2112 Pdbids pdb, PDBEntry entry, List<String> viewIds,
2113 String matchedFile, StructureViewerBase viewFrame)
2115 final AAStructureBindingModel bindingModel = viewFrame.getBinding();
2118 * Look for any bindings for this viewer to the PDB file of interest
2119 * (including part matches excluding chain id)
2121 for (int peid = 0; peid < bindingModel.getPdbCount(); peid++)
2123 final PDBEntry pdbentry = bindingModel.getPdbEntry(peid);
2124 final String pdbId = pdbentry.getId();
2125 if (!pdbId.equals(entry.getId()) && !(entry.getId().length() > 4
2126 && entry.getId().toLowerCase(Locale.ROOT)
2127 .startsWith(pdbId.toLowerCase(Locale.ROOT))))
2130 * not interested in a binding to a different PDB entry here
2134 if (matchedFile == null)
2136 matchedFile = pdbentry.getFile();
2138 else if (!matchedFile.equals(pdbentry.getFile()))
2141 "Probably lost some PDB-Sequence mappings for this structure file (which apparently has same PDB Entry code): "
2142 + pdbentry.getFile());
2146 // can get at it if the ID
2147 // match is ambiguous (e.g.
2150 for (int smap = 0; smap < viewFrame.getBinding()
2151 .getSequence()[peid].length; smap++)
2153 // if (jal.findIndex(jmol.jmb.sequence[peid][smap]) > -1)
2154 if (jds == viewFrame.getBinding().getSequence()[peid][smap])
2156 StructureState state = new StructureState();
2157 state.setVisible(true);
2158 state.setXpos(viewFrame.getX());
2159 state.setYpos(viewFrame.getY());
2160 state.setWidth(viewFrame.getWidth());
2161 state.setHeight(viewFrame.getHeight());
2162 final String viewId = viewFrame.getViewId();
2163 state.setViewId(viewId);
2164 state.setAlignwithAlignPanel(viewFrame.isUsedforaligment(ap));
2165 state.setColourwithAlignPanel(viewFrame.isUsedForColourBy(ap));
2166 state.setColourByJmol(viewFrame.isColouredByViewer());
2167 state.setType(viewFrame.getViewerType().toString());
2168 // pdb.addStructureState(state);
2169 pdb.getStructureState().add(state);
2177 * Populates the AnnotationColourScheme xml for save. This captures the
2178 * settings of the options in the 'Colour by Annotation' dialog.
2181 * @param userColours
2185 private AnnotationColourScheme constructAnnotationColours(
2186 AnnotationColourGradient acg, List<UserColourScheme> userColours,
2189 AnnotationColourScheme ac = new AnnotationColourScheme();
2190 ac.setAboveThreshold(acg.getAboveThreshold());
2191 ac.setThreshold(acg.getAnnotationThreshold());
2192 // 2.10.2 save annotationId (unique) not annotation label
2193 ac.setAnnotation(acg.getAnnotation().annotationId);
2194 if (acg.getBaseColour() instanceof UserColourScheme)
2197 setUserColourScheme(acg.getBaseColour(), userColours, jm));
2202 ColourSchemeProperty.getColourName(acg.getBaseColour()));
2205 ac.setMaxColour(acg.getMaxColour().getRGB());
2206 ac.setMinColour(acg.getMinColour().getRGB());
2207 ac.setPerSequence(acg.isSeqAssociated());
2208 ac.setPredefinedColours(acg.isPredefinedColours());
2212 private void storeAlignmentAnnotation(AlignmentAnnotation[] aa,
2213 IdentityHashMap<SequenceGroup, String> groupRefs,
2214 AlignmentViewport av, Set<String> calcIdSet, boolean storeDS,
2215 SequenceSet vamsasSet)
2218 for (int i = 0; i < aa.length; i++)
2220 Annotation an = new Annotation();
2222 AlignmentAnnotation annotation = aa[i];
2223 if (annotation.annotationId != null)
2225 annotationIds.put(annotation.annotationId, annotation);
2228 an.setId(annotation.annotationId);
2230 an.setVisible(annotation.visible);
2232 an.setDescription(annotation.description);
2234 if (annotation.sequenceRef != null)
2236 // 2.9 JAL-1781 xref on sequence id rather than name
2237 an.setSequenceRef(seqsToIds.get(annotation.sequenceRef));
2239 if (annotation.groupRef != null)
2241 String groupIdr = groupRefs.get(annotation.groupRef);
2242 if (groupIdr == null)
2244 // make a locally unique String
2245 groupRefs.put(annotation.groupRef,
2246 groupIdr = ("" + System.currentTimeMillis()
2247 + annotation.groupRef.getName()
2248 + groupRefs.size()));
2250 an.setGroupRef(groupIdr.toString());
2253 // store all visualization attributes for annotation
2254 an.setGraphHeight(annotation.graphHeight);
2255 an.setCentreColLabels(annotation.centreColLabels);
2256 an.setScaleColLabels(annotation.scaleColLabel);
2257 an.setShowAllColLabels(annotation.showAllColLabels);
2258 an.setBelowAlignment(annotation.belowAlignment);
2260 if (annotation.graph > 0)
2263 an.setGraphType(annotation.graph);
2264 an.setGraphGroup(annotation.graphGroup);
2265 if (annotation.getThreshold() != null)
2267 ThresholdLine line = new ThresholdLine();
2268 line.setLabel(annotation.getThreshold().label);
2269 line.setValue(annotation.getThreshold().value);
2270 line.setColour(annotation.getThreshold().colour.getRGB());
2271 an.setThresholdLine(line);
2279 an.setLabel(annotation.label);
2281 if (annotation == av.getAlignmentQualityAnnot()
2282 || annotation == av.getAlignmentConservationAnnotation()
2283 || annotation == av.getAlignmentConsensusAnnotation()
2284 || annotation.autoCalculated)
2286 // new way of indicating autocalculated annotation -
2287 an.setAutoCalculated(annotation.autoCalculated);
2289 if (annotation.hasScore())
2291 an.setScore(annotation.getScore());
2294 if (annotation.getCalcId() != null)
2296 calcIdSet.add(annotation.getCalcId());
2297 an.setCalcId(annotation.getCalcId());
2299 if (annotation.hasProperties())
2301 for (String pr : annotation.getProperties())
2303 jalview.xml.binding.jalview.Annotation.Property prop = new jalview.xml.binding.jalview.Annotation.Property();
2305 prop.setValue(annotation.getProperty(pr));
2306 // an.addProperty(prop);
2307 an.getProperty().add(prop);
2311 AnnotationElement ae;
2312 if (annotation.annotations != null)
2314 an.setScoreOnly(false);
2315 for (int a = 0; a < annotation.annotations.length; a++)
2317 if ((annotation == null) || (annotation.annotations[a] == null))
2322 ae = new AnnotationElement();
2323 if (annotation.annotations[a].description != null)
2325 ae.setDescription(annotation.annotations[a].description);
2327 if (annotation.annotations[a].displayCharacter != null)
2329 ae.setDisplayCharacter(
2330 annotation.annotations[a].displayCharacter);
2333 if (!Float.isNaN(annotation.annotations[a].value))
2335 ae.setValue(annotation.annotations[a].value);
2339 if (annotation.annotations[a].secondaryStructure > ' ')
2341 ae.setSecondaryStructure(
2342 annotation.annotations[a].secondaryStructure + "");
2345 if (annotation.annotations[a].colour != null
2346 && annotation.annotations[a].colour != java.awt.Color.black)
2348 ae.setColour(annotation.annotations[a].colour.getRGB());
2351 // an.addAnnotationElement(ae);
2352 an.getAnnotationElement().add(ae);
2353 if (annotation.autoCalculated)
2355 // only write one non-null entry into the annotation row -
2356 // sufficient to get the visualization attributes necessary to
2364 an.setScoreOnly(true);
2366 if (!storeDS || (storeDS && !annotation.autoCalculated))
2368 // skip autocalculated annotation - these are only provided for
2370 // vamsasSet.addAnnotation(an);
2371 vamsasSet.getAnnotation().add(an);
2377 private CalcIdParam createCalcIdParam(String calcId, AlignViewport av)
2379 AutoCalcSetting settings = av.getCalcIdSettingsFor(calcId);
2380 if (settings != null)
2382 CalcIdParam vCalcIdParam = new CalcIdParam();
2383 vCalcIdParam.setCalcId(calcId);
2384 // vCalcIdParam.addServiceURL(settings.getServiceURI());
2385 vCalcIdParam.getServiceURL().add(settings.getServiceURI());
2386 // generic URI allowing a third party to resolve another instance of the
2387 // service used for this calculation
2388 for (String url : settings.getServiceURLs())
2390 // vCalcIdParam.addServiceURL(urls);
2391 vCalcIdParam.getServiceURL().add(url);
2393 vCalcIdParam.setVersion("1.0");
2394 if (settings.getPreset() != null)
2396 WsParamSetI setting = settings.getPreset();
2397 vCalcIdParam.setName(setting.getName());
2398 vCalcIdParam.setDescription(setting.getDescription());
2402 vCalcIdParam.setName("");
2403 vCalcIdParam.setDescription("Last used parameters");
2405 // need to be able to recover 1) settings 2) user-defined presets or
2406 // recreate settings from preset 3) predefined settings provided by
2407 // service - or settings that can be transferred (or discarded)
2408 vCalcIdParam.setParameters(
2409 settings.getWsParamFile().replace("\n", "|\\n|"));
2410 vCalcIdParam.setAutoUpdate(settings.isAutoUpdate());
2411 // todo - decide if updateImmediately is needed for any projects.
2413 return vCalcIdParam;
2418 private boolean recoverCalcIdParam(CalcIdParam calcIdParam,
2421 if (calcIdParam.getVersion().equals("1.0"))
2423 final String[] calcIds = calcIdParam.getServiceURL()
2424 .toArray(new String[0]);
2425 Jws2Instance service = Jws2Discoverer.getDiscoverer()
2426 .getPreferredServiceFor(calcIds);
2427 if (service != null)
2429 WsParamSetI parmSet = null;
2432 parmSet = service.getParamStore().parseServiceParameterFile(
2433 calcIdParam.getName(), calcIdParam.getDescription(),
2435 calcIdParam.getParameters().replace("|\\n|", "\n"));
2436 } catch (IOException x)
2438 Console.warn("Couldn't parse parameter data for "
2439 + calcIdParam.getCalcId(), x);
2442 List<ArgumentI> argList = null;
2443 if (calcIdParam.getName().length() > 0)
2445 parmSet = service.getParamStore()
2446 .getPreset(calcIdParam.getName());
2447 if (parmSet != null)
2449 // TODO : check we have a good match with settings in AACon -
2450 // otherwise we'll need to create a new preset
2455 argList = parmSet.getArguments();
2458 AAConSettings settings = new AAConSettings(
2459 calcIdParam.isAutoUpdate(), service, parmSet, argList);
2460 av.setCalcIdSettingsFor(calcIdParam.getCalcId(), settings,
2461 calcIdParam.isNeedsUpdate());
2467 "Cannot resolve a service for the parameters used in this project. Try configuring a JABAWS server.");
2471 throw new Error(MessageManager.formatMessage(
2472 "error.unsupported_version_calcIdparam", new Object[]
2473 { calcIdParam.toString() }));
2477 * External mapping between jalview objects and objects yielding a valid and
2478 * unique object ID string. This is null for normal Jalview project IO, but
2479 * non-null when a jalview project is being read or written as part of a
2482 IdentityHashMap jv2vobj = null;
2485 * Construct a unique ID for jvobj using either existing bindings or if none
2486 * exist, the result of the hashcode call for the object.
2489 * jalview data object
2490 * @return unique ID for referring to jvobj
2492 private String makeHashCode(Object jvobj, String altCode)
2494 if (jv2vobj != null)
2496 Object id = jv2vobj.get(jvobj);
2499 return id.toString();
2501 // check string ID mappings
2502 if (jvids2vobj != null && jvobj instanceof String)
2504 id = jvids2vobj.get(jvobj);
2508 return id.toString();
2510 // give up and warn that something has gone wrong
2512 "Cannot find ID for object in external mapping : " + jvobj);
2518 * return local jalview object mapped to ID, if it exists
2522 * @return null or object bound to idcode
2524 private Object retrieveExistingObj(String idcode)
2526 if (idcode != null && vobj2jv != null)
2528 return vobj2jv.get(idcode);
2534 * binding from ID strings from external mapping table to jalview data model
2537 private Hashtable vobj2jv;
2539 private Sequence createVamsasSequence(String id, SequenceI jds)
2541 return createVamsasSequence(true, id, jds, null);
2544 private Sequence createVamsasSequence(boolean recurse, String id,
2545 SequenceI jds, SequenceI parentseq)
2547 Sequence vamsasSeq = new Sequence();
2548 vamsasSeq.setId(id);
2549 vamsasSeq.setName(jds.getName());
2550 vamsasSeq.setSequence(jds.getSequenceAsString());
2551 vamsasSeq.setDescription(jds.getDescription());
2552 List<DBRefEntry> dbrefs = null;
2553 if (jds.getDatasetSequence() != null)
2555 vamsasSeq.setDsseqid(seqHash(jds.getDatasetSequence()));
2559 // seqId==dsseqid so we can tell which sequences really are
2560 // dataset sequences only
2561 vamsasSeq.setDsseqid(id);
2562 dbrefs = jds.getDBRefs();
2563 if (parentseq == null)
2570 * save any dbrefs; special subclass GeneLocus is flagged as 'locus'
2574 for (int d = 0, nd = dbrefs.size(); d < nd; d++)
2576 DBRef dbref = new DBRef();
2577 DBRefEntry ref = dbrefs.get(d);
2578 dbref.setSource(ref.getSource());
2579 dbref.setVersion(ref.getVersion());
2580 dbref.setAccessionId(ref.getAccessionId());
2581 dbref.setCanonical(ref.isCanonical());
2582 if (ref instanceof GeneLocus)
2584 dbref.setLocus(true);
2588 Mapping mp = createVamsasMapping(ref.getMap(), parentseq, jds,
2590 dbref.setMapping(mp);
2592 vamsasSeq.getDBRef().add(dbref);
2598 private Mapping createVamsasMapping(jalview.datamodel.Mapping jmp,
2599 SequenceI parentseq, SequenceI jds, boolean recurse)
2602 if (jmp.getMap() != null)
2606 jalview.util.MapList mlst = jmp.getMap();
2607 List<int[]> r = mlst.getFromRanges();
2608 for (int[] range : r)
2610 MapListFrom mfrom = new MapListFrom();
2611 mfrom.setStart(range[0]);
2612 mfrom.setEnd(range[1]);
2613 // mp.addMapListFrom(mfrom);
2614 mp.getMapListFrom().add(mfrom);
2616 r = mlst.getToRanges();
2617 for (int[] range : r)
2619 MapListTo mto = new MapListTo();
2620 mto.setStart(range[0]);
2621 mto.setEnd(range[1]);
2622 // mp.addMapListTo(mto);
2623 mp.getMapListTo().add(mto);
2625 mp.setMapFromUnit(BigInteger.valueOf(mlst.getFromRatio()));
2626 mp.setMapToUnit(BigInteger.valueOf(mlst.getToRatio()));
2627 if (jmp.getTo() != null)
2629 // MappingChoice mpc = new MappingChoice();
2631 // check/create ID for the sequence referenced by getTo()
2634 SequenceI ps = null;
2635 if (parentseq != jmp.getTo()
2636 && parentseq.getDatasetSequence() != jmp.getTo())
2638 // chaining dbref rather than a handshaking one
2639 jmpid = seqHash(ps = jmp.getTo());
2643 jmpid = seqHash(ps = parentseq);
2645 // mpc.setDseqFor(jmpid);
2646 mp.setDseqFor(jmpid);
2647 if (!seqRefIds.containsKey(jmpid))
2649 Console.debug("creatign new DseqFor ID");
2650 seqRefIds.put(jmpid, ps);
2654 Console.debug("reusing DseqFor ID");
2657 // mp.setMappingChoice(mpc);
2663 String setUserColourScheme(jalview.schemes.ColourSchemeI cs,
2664 List<UserColourScheme> userColours, JalviewModel jm)
2667 jalview.schemes.UserColourScheme ucs = (jalview.schemes.UserColourScheme) cs;
2668 boolean newucs = false;
2669 if (!userColours.contains(ucs))
2671 userColours.add(ucs);
2674 id = "ucs" + userColours.indexOf(ucs);
2677 // actually create the scheme's entry in the XML model
2678 java.awt.Color[] colours = ucs.getColours();
2679 UserColours uc = new UserColours();
2680 // UserColourScheme jbucs = new UserColourScheme();
2681 JalviewUserColours jbucs = new JalviewUserColours();
2683 for (int i = 0; i < colours.length; i++)
2685 Colour col = new Colour();
2686 col.setName(ResidueProperties.aa[i]);
2687 col.setRGB(jalview.util.Format.getHexString(colours[i]));
2688 // jbucs.addColour(col);
2689 jbucs.getColour().add(col);
2691 if (ucs.getLowerCaseColours() != null)
2693 colours = ucs.getLowerCaseColours();
2694 for (int i = 0; i < colours.length; i++)
2696 Colour col = new Colour();
2697 col.setName(ResidueProperties.aa[i].toLowerCase(Locale.ROOT));
2698 col.setRGB(jalview.util.Format.getHexString(colours[i]));
2699 // jbucs.addColour(col);
2700 jbucs.getColour().add(col);
2705 uc.setUserColourScheme(jbucs);
2706 // jm.addUserColours(uc);
2707 jm.getUserColours().add(uc);
2713 jalview.schemes.UserColourScheme getUserColourScheme(JalviewModel jm,
2716 List<UserColours> uc = jm.getUserColours();
2717 UserColours colours = null;
2719 for (int i = 0; i < uc.length; i++)
2721 if (uc[i].getId().equals(id))
2728 for (UserColours c : uc)
2730 if (c.getId().equals(id))
2737 java.awt.Color[] newColours = new java.awt.Color[24];
2739 for (int i = 0; i < 24; i++)
2741 newColours[i] = new java.awt.Color(Integer.parseInt(
2742 // colours.getUserColourScheme().getColour(i).getRGB(), 16));
2743 colours.getUserColourScheme().getColour().get(i).getRGB(),
2747 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(
2750 if (colours.getUserColourScheme().getColour().size()/*Count()*/ > 24)
2752 newColours = new java.awt.Color[23];
2753 for (int i = 0; i < 23; i++)
2755 newColours[i] = new java.awt.Color(
2756 Integer.parseInt(colours.getUserColourScheme().getColour()
2757 .get(i + 24).getRGB(), 16));
2759 ucs.setLowerCaseColours(newColours);
2766 * contains last error message (if any) encountered by XML loader.
2768 String errorMessage = null;
2771 * flag to control whether the Jalview2XML_V1 parser should be deferred to if
2772 * exceptions are raised during project XML parsing
2774 public boolean attemptversion1parse = false;
2777 * Load a jalview project archive from a jar file
2780 * - HTTP URL or filename
2782 public AlignFrame loadJalviewAlign(final Object file)
2785 jalview.gui.AlignFrame af = null;
2789 // create list to store references for any new Jmol viewers created
2790 newStructureViewers = new Vector<>();
2791 // UNMARSHALLER SEEMS TO CLOSE JARINPUTSTREAM, MOST ANNOYING
2792 // Workaround is to make sure caller implements the JarInputStreamProvider
2794 // so we can re-open the jar input stream for each entry.
2796 jarInputStreamProvider jprovider = createjarInputStreamProvider(file);
2797 af = loadJalviewAlign(jprovider);
2800 af.setMenusForViewport();
2802 } catch (MalformedURLException e)
2804 errorMessage = "Invalid URL format for '" + file + "'";
2810 SwingUtilities.invokeAndWait(new Runnable()
2815 setLoadingFinishedForNewStructureViewers();
2818 } catch (Exception x)
2820 System.err.println("Error loading alignment: " + x.getMessage());
2826 @SuppressWarnings("unused")
2827 private jarInputStreamProvider createjarInputStreamProvider(
2828 final Object ofile) throws MalformedURLException
2831 // BH 2018 allow for bytes already attached to File object
2834 String file = (ofile instanceof File
2835 ? ((File) ofile).getCanonicalPath()
2836 : ofile.toString());
2837 byte[] bytes = Platform.isJS() ? Platform.getFileBytes((File) ofile)
2840 errorMessage = null;
2841 uniqueSetSuffix = null;
2843 viewportsAdded.clear();
2844 frefedSequence = null;
2846 if (HttpUtils.startsWithHttpOrHttps(file))
2848 url = new URL(file);
2850 final URL _url = url;
2851 return new jarInputStreamProvider()
2855 public JarInputStream getJarInputStream() throws IOException
2859 // System.out.println("Jalview2XML: opening byte jarInputStream for
2860 // bytes.length=" + bytes.length);
2861 return new JarInputStream(new ByteArrayInputStream(bytes));
2865 // System.out.println("Jalview2XML: opening url jarInputStream for "
2867 return new JarInputStream(_url.openStream());
2871 // System.out.println("Jalview2XML: opening file jarInputStream for
2873 return new JarInputStream(new FileInputStream(file));
2878 public String getFilename()
2883 } catch (IOException e)
2885 e.printStackTrace();
2891 * Recover jalview session from a jalview project archive. Caller may
2892 * initialise uniqueSetSuffix, seqRefIds, viewportsAdded and frefedSequence
2893 * themselves. Any null fields will be initialised with default values,
2894 * non-null fields are left alone.
2899 public AlignFrame loadJalviewAlign(final jarInputStreamProvider jprovider)
2901 errorMessage = null;
2902 if (uniqueSetSuffix == null)
2904 uniqueSetSuffix = System.currentTimeMillis() % 100000 + "";
2906 if (seqRefIds == null)
2910 AlignFrame af = null, _af = null;
2911 IdentityHashMap<AlignmentI, AlignmentI> importedDatasets = new IdentityHashMap<>();
2912 Map<String, AlignFrame> gatherToThisFrame = new HashMap<>();
2913 final String file = jprovider.getFilename();
2916 JarInputStream jin = null;
2917 JarEntry jarentry = null;
2922 jin = jprovider.getJarInputStream();
2923 for (int i = 0; i < entryCount; i++)
2925 jarentry = jin.getNextJarEntry();
2928 if (jarentry != null && jarentry.getName().endsWith(".xml"))
2930 JAXBContext jc = JAXBContext
2931 .newInstance("jalview.xml.binding.jalview");
2932 XMLStreamReader streamReader = XMLInputFactory.newInstance()
2933 .createXMLStreamReader(jin);
2934 javax.xml.bind.Unmarshaller um = jc.createUnmarshaller();
2935 JAXBElement<JalviewModel> jbe = um.unmarshal(streamReader,
2936 JalviewModel.class);
2937 JalviewModel object = jbe.getValue();
2939 if (true) // !skipViewport(object))
2941 _af = loadFromObject(object, file, true, jprovider);
2942 if (_af != null && object.getViewport().size() > 0)
2943 // getJalviewModelSequence().getViewportCount() > 0)
2947 // store a reference to the first view
2950 if (_af.getViewport().isGatherViewsHere())
2952 // if this is a gathered view, keep its reference since
2953 // after gathering views, only this frame will remain
2955 gatherToThisFrame.put(_af.getViewport().getSequenceSetId(),
2958 // Save dataset to register mappings once all resolved
2959 importedDatasets.put(
2960 af.getViewport().getAlignment().getDataset(),
2961 af.getViewport().getAlignment().getDataset());
2966 else if (jarentry != null)
2968 // Some other file here.
2971 } while (jarentry != null);
2973 resolveFrefedSequences();
2974 } catch (IOException ex)
2976 ex.printStackTrace();
2977 errorMessage = "Couldn't locate Jalview XML file : " + file;
2979 "Exception whilst loading jalview XML file : " + ex + "\n");
2980 } catch (Exception ex)
2982 System.err.println("Parsing as Jalview Version 2 file failed.");
2983 ex.printStackTrace(System.err);
2984 if (attemptversion1parse)
2986 // used to attempt to parse as V1 castor-generated xml
2988 if (Desktop.instance != null)
2990 Desktop.instance.stopLoading();
2994 System.out.println("Successfully loaded archive file");
2997 ex.printStackTrace();
3000 "Exception whilst loading jalview XML file : " + ex + "\n");
3001 } catch (OutOfMemoryError e)
3003 // Don't use the OOM Window here
3004 errorMessage = "Out of memory loading jalview XML file";
3005 System.err.println("Out of memory whilst loading jalview XML file");
3006 e.printStackTrace();
3010 * Regather multiple views (with the same sequence set id) to the frame (if
3011 * any) that is flagged as the one to gather to, i.e. convert them to tabbed
3012 * views instead of separate frames. Note this doesn't restore a state where
3013 * some expanded views in turn have tabbed views - the last "first tab" read
3014 * in will play the role of gatherer for all.
3016 for (AlignFrame fr : gatherToThisFrame.values())
3018 Desktop.instance.gatherViews(fr);
3021 restoreSplitFrames();
3022 for (AlignmentI ds : importedDatasets.keySet())
3024 if (ds.getCodonFrames() != null)
3026 StructureSelectionManager
3027 .getStructureSelectionManager(Desktop.instance)
3028 .registerMappings(ds.getCodonFrames());
3031 if (errorMessage != null)
3036 if (Desktop.instance != null)
3038 Desktop.instance.stopLoading();
3045 * Try to reconstruct and display SplitFrame windows, where each contains
3046 * complementary dna and protein alignments. Done by pairing up AlignFrame
3047 * objects (created earlier) which have complementary viewport ids associated.
3049 protected void restoreSplitFrames()
3051 List<SplitFrame> gatherTo = new ArrayList<>();
3052 List<AlignFrame> addedToSplitFrames = new ArrayList<>();
3053 Map<String, AlignFrame> dna = new HashMap<>();
3056 * Identify the DNA alignments
3058 for (Entry<Viewport, AlignFrame> candidate : splitFrameCandidates
3061 AlignFrame af = candidate.getValue();
3062 if (af.getViewport().getAlignment().isNucleotide())
3064 dna.put(candidate.getKey().getId(), af);
3069 * Try to match up the protein complements
3071 for (Entry<Viewport, AlignFrame> candidate : splitFrameCandidates
3074 AlignFrame af = candidate.getValue();
3075 if (!af.getViewport().getAlignment().isNucleotide())
3077 String complementId = candidate.getKey().getComplementId();
3078 // only non-null complements should be in the Map
3079 if (complementId != null && dna.containsKey(complementId))
3081 final AlignFrame dnaFrame = dna.get(complementId);
3082 SplitFrame sf = createSplitFrame(dnaFrame, af);
3083 addedToSplitFrames.add(dnaFrame);
3084 addedToSplitFrames.add(af);
3085 dnaFrame.setMenusForViewport();
3086 af.setMenusForViewport();
3087 if (af.getViewport().isGatherViewsHere())
3096 * Open any that we failed to pair up (which shouldn't happen!) as
3097 * standalone AlignFrame's.
3099 for (Entry<Viewport, AlignFrame> candidate : splitFrameCandidates
3102 AlignFrame af = candidate.getValue();
3103 if (!addedToSplitFrames.contains(af))
3105 Viewport view = candidate.getKey();
3106 Desktop.addInternalFrame(af, view.getTitle(),
3107 safeInt(view.getWidth()), safeInt(view.getHeight()));
3108 af.setMenusForViewport();
3109 System.err.println("Failed to restore view " + view.getTitle()
3110 + " to split frame");
3115 * Gather back into tabbed views as flagged.
3117 for (SplitFrame sf : gatherTo)
3119 Desktop.instance.gatherViews(sf);
3122 splitFrameCandidates.clear();
3126 * Construct and display one SplitFrame holding DNA and protein alignments.
3129 * @param proteinFrame
3132 protected SplitFrame createSplitFrame(AlignFrame dnaFrame,
3133 AlignFrame proteinFrame)
3135 SplitFrame splitFrame = new SplitFrame(dnaFrame, proteinFrame);
3136 String title = MessageManager.getString("label.linked_view_title");
3137 int width = (int) dnaFrame.getBounds().getWidth();
3138 int height = (int) (dnaFrame.getBounds().getHeight()
3139 + proteinFrame.getBounds().getHeight() + 50);
3142 * SplitFrame location is saved to both enclosed frames
3144 splitFrame.setLocation(dnaFrame.getX(), dnaFrame.getY());
3145 Desktop.addInternalFrame(splitFrame, title, width, height);
3148 * And compute cDNA consensus (couldn't do earlier with consensus as
3149 * mappings were not yet present)
3151 proteinFrame.getViewport().alignmentChanged(proteinFrame.alignPanel);
3157 * check errorMessage for a valid error message and raise an error box in the
3158 * GUI or write the current errorMessage to stderr and then clear the error
3161 protected void reportErrors()
3163 reportErrors(false);
3166 protected void reportErrors(final boolean saving)
3168 if (errorMessage != null)
3170 final String finalErrorMessage = errorMessage;
3173 javax.swing.SwingUtilities.invokeLater(new Runnable()
3178 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
3180 "Error " + (saving ? "saving" : "loading")
3182 JvOptionPane.WARNING_MESSAGE);
3188 System.err.println("Problem loading Jalview file: " + errorMessage);
3191 errorMessage = null;
3194 Map<String, String> alreadyLoadedPDB = new HashMap<>();
3197 * when set, local views will be updated from view stored in JalviewXML
3198 * Currently (28th Sep 2008) things will go horribly wrong in vamsas document
3199 * sync if this is set to true.
3201 private final boolean updateLocalViews = false;
3204 * Returns the path to a temporary file holding the PDB file for the given PDB
3205 * id. The first time of asking, searches for a file of that name in the
3206 * Jalview project jar, and copies it to a new temporary file. Any repeat
3207 * requests just return the path to the file previously created.
3213 String loadPDBFile(jarInputStreamProvider jprovider, String pdbId,
3216 if (alreadyLoadedPDB.containsKey(pdbId))
3218 return alreadyLoadedPDB.get(pdbId).toString();
3221 String tempFile = copyJarEntry(jprovider, pdbId, "jalview_pdb",
3223 if (tempFile != null)
3225 alreadyLoadedPDB.put(pdbId, tempFile);
3231 * Copies the jar entry of given name to a new temporary file and returns the
3232 * path to the file, or null if the entry is not found.
3235 * @param jarEntryName
3237 * a prefix for the temporary file name, must be at least three
3239 * @param suffixModel
3240 * null or original file - so new file can be given the same suffix
3244 protected String copyJarEntry(jarInputStreamProvider jprovider,
3245 String jarEntryName, String prefix, String suffixModel)
3247 String suffix = ".tmp";
3248 if (suffixModel == null)
3250 suffixModel = jarEntryName;
3252 int sfpos = suffixModel.lastIndexOf(".");
3253 if (sfpos > -1 && sfpos < (suffixModel.length() - 1))
3255 suffix = "." + suffixModel.substring(sfpos + 1);
3258 try (JarInputStream jin = jprovider.getJarInputStream())
3260 JarEntry entry = null;
3263 entry = jin.getNextJarEntry();
3264 } while (entry != null && !entry.getName().equals(jarEntryName));
3268 // in = new BufferedReader(new InputStreamReader(jin, UTF_8));
3269 File outFile = File.createTempFile(prefix, suffix);
3270 outFile.deleteOnExit();
3271 try (OutputStream os = new FileOutputStream(outFile))
3275 String t = outFile.getAbsolutePath();
3281 "Couldn't find entry in Jalview Jar for " + jarEntryName);
3283 } catch (Exception ex)
3285 ex.printStackTrace();
3291 private class JvAnnotRow
3293 public JvAnnotRow(int i, AlignmentAnnotation jaa)
3300 * persisted version of annotation row from which to take vis properties
3302 public jalview.datamodel.AlignmentAnnotation template;
3305 * original position of the annotation row in the alignment
3311 * Load alignment frame from jalview XML DOM object
3313 * @param jalviewModel
3316 * filename source string
3317 * @param loadTreesAndStructures
3318 * when false only create Viewport
3320 * data source provider
3321 * @return alignment frame created from view stored in DOM
3323 AlignFrame loadFromObject(JalviewModel jalviewModel, String file,
3324 boolean loadTreesAndStructures, jarInputStreamProvider jprovider)
3326 SequenceSet vamsasSet = jalviewModel.getVamsasModel().getSequenceSet()
3328 List<Sequence> vamsasSeqs = vamsasSet.getSequence();
3330 // JalviewModelSequence jms = object.getJalviewModelSequence();
3332 // Viewport view = (jms.getViewportCount() > 0) ? jms.getViewport(0)
3334 Viewport view = (jalviewModel.getViewport().size() > 0)
3335 ? jalviewModel.getViewport().get(0)
3338 // ////////////////////////////////
3339 // INITIALISE ALIGNMENT SEQUENCESETID AND VIEWID
3342 // If we just load in the same jar file again, the sequenceSetId
3343 // will be the same, and we end up with multiple references
3344 // to the same sequenceSet. We must modify this id on load
3345 // so that each load of the file gives a unique id
3348 * used to resolve correct alignment dataset for alignments with multiple
3351 String uniqueSeqSetId = null;
3352 String viewId = null;
3355 uniqueSeqSetId = view.getSequenceSetId() + uniqueSetSuffix;
3356 viewId = (view.getId() == null ? null
3357 : view.getId() + uniqueSetSuffix);
3360 // ////////////////////////////////
3363 List<SequenceI> hiddenSeqs = null;
3365 List<SequenceI> tmpseqs = new ArrayList<>();
3367 boolean multipleView = false;
3368 SequenceI referenceseqForView = null;
3369 // JSeq[] jseqs = object.getJalviewModelSequence().getJSeq();
3370 List<JSeq> jseqs = jalviewModel.getJSeq();
3371 int vi = 0; // counter in vamsasSeq array
3372 for (int i = 0; i < jseqs.size(); i++)
3374 JSeq jseq = jseqs.get(i);
3375 String seqId = jseq.getId();
3377 SequenceI tmpSeq = seqRefIds.get(seqId);
3380 if (!incompleteSeqs.containsKey(seqId))
3382 // may not need this check, but keep it for at least 2.9,1 release
3383 if (tmpSeq.getStart() != jseq.getStart()
3384 || tmpSeq.getEnd() != jseq.getEnd())
3386 System.err.println(String.format(
3387 "Warning JAL-2154 regression: updating start/end for sequence %s from %d/%d to %d/%d",
3388 tmpSeq.getName(), tmpSeq.getStart(), tmpSeq.getEnd(),
3389 jseq.getStart(), jseq.getEnd()));
3394 incompleteSeqs.remove(seqId);
3396 if (vamsasSeqs.size() > vi
3397 && vamsasSeqs.get(vi).getId().equals(seqId))
3399 // most likely we are reading a dataset XML document so
3400 // update from vamsasSeq section of XML for this sequence
3401 tmpSeq.setName(vamsasSeqs.get(vi).getName());
3402 tmpSeq.setDescription(vamsasSeqs.get(vi).getDescription());
3403 tmpSeq.setSequence(vamsasSeqs.get(vi).getSequence());
3408 // reading multiple views, so vamsasSeq set is a subset of JSeq
3409 multipleView = true;
3411 tmpSeq.setStart(jseq.getStart());
3412 tmpSeq.setEnd(jseq.getEnd());
3413 tmpseqs.add(tmpSeq);
3417 Sequence vamsasSeq = vamsasSeqs.get(vi);
3418 tmpSeq = new jalview.datamodel.Sequence(vamsasSeq.getName(),
3419 vamsasSeq.getSequence());
3420 tmpSeq.setDescription(vamsasSeq.getDescription());
3421 tmpSeq.setStart(jseq.getStart());
3422 tmpSeq.setEnd(jseq.getEnd());
3423 tmpSeq.setVamsasId(uniqueSetSuffix + seqId);
3424 seqRefIds.put(vamsasSeq.getId(), tmpSeq);
3425 tmpseqs.add(tmpSeq);
3429 if (safeBoolean(jseq.isViewreference()))
3431 referenceseqForView = tmpseqs.get(tmpseqs.size() - 1);
3434 if (jseq.isHidden() != null && jseq.isHidden().booleanValue())
3436 if (hiddenSeqs == null)
3438 hiddenSeqs = new ArrayList<>();
3441 hiddenSeqs.add(tmpSeq);
3446 // Create the alignment object from the sequence set
3447 // ///////////////////////////////
3448 SequenceI[] orderedSeqs = tmpseqs
3449 .toArray(new SequenceI[tmpseqs.size()]);
3451 AlignmentI al = null;
3452 // so we must create or recover the dataset alignment before going further
3453 // ///////////////////////////////
3454 if (vamsasSet.getDatasetId() == null || vamsasSet.getDatasetId() == "")
3456 // older jalview projects do not have a dataset - so creat alignment and
3458 al = new Alignment(orderedSeqs);
3459 al.setDataset(null);
3463 boolean isdsal = jalviewModel.getViewport().isEmpty();
3466 // we are importing a dataset record, so
3467 // recover reference to an alignment already materialsed as dataset
3468 al = getDatasetFor(vamsasSet.getDatasetId());
3472 // materialse the alignment
3473 al = new Alignment(orderedSeqs);
3477 addDatasetRef(vamsasSet.getDatasetId(), al);
3480 // finally, verify all data in vamsasSet is actually present in al
3481 // passing on flag indicating if it is actually a stored dataset
3482 recoverDatasetFor(vamsasSet, al, isdsal, uniqueSeqSetId);
3485 if (referenceseqForView != null)
3487 al.setSeqrep(referenceseqForView);
3489 // / Add the alignment properties
3490 for (int i = 0; i < vamsasSet.getSequenceSetProperties().size(); i++)
3492 SequenceSetProperties ssp = vamsasSet.getSequenceSetProperties()
3494 al.setProperty(ssp.getKey(), ssp.getValue());
3497 // ///////////////////////////////
3499 Hashtable pdbloaded = new Hashtable(); // TODO nothing writes to this??
3502 // load sequence features, database references and any associated PDB
3503 // structures for the alignment
3505 // prior to 2.10, this part would only be executed the first time a
3506 // sequence was encountered, but not afterwards.
3507 // now, for 2.10 projects, this is also done if the xml doc includes
3508 // dataset sequences not actually present in any particular view.
3510 for (int i = 0; i < vamsasSeqs.size(); i++)
3512 JSeq jseq = jseqs.get(i);
3513 if (jseq.getFeatures().size() > 0)
3515 List<Feature> features = jseq.getFeatures();
3516 for (int f = 0; f < features.size(); f++)
3518 Feature feat = features.get(f);
3519 SequenceFeature sf = new SequenceFeature(feat.getType(),
3520 feat.getDescription(), feat.getBegin(), feat.getEnd(),
3521 safeFloat(feat.getScore()), feat.getFeatureGroup());
3522 sf.setStatus(feat.getStatus());
3525 * load any feature attributes - include map-valued attributes
3527 Map<String, Map<String, String>> mapAttributes = new HashMap<>();
3528 for (int od = 0; od < feat.getOtherData().size(); od++)
3530 OtherData keyValue = feat.getOtherData().get(od);
3531 String attributeName = keyValue.getKey();
3532 String attributeValue = keyValue.getValue();
3533 if (attributeName.startsWith("LINK"))
3535 sf.addLink(attributeValue);
3539 String subAttribute = keyValue.getKey2();
3540 if (subAttribute == null)
3542 // simple string-valued attribute
3543 sf.setValue(attributeName, attributeValue);
3547 // attribute 'key' has sub-attribute 'key2'
3548 if (!mapAttributes.containsKey(attributeName))
3550 mapAttributes.put(attributeName, new HashMap<>());
3552 mapAttributes.get(attributeName).put(subAttribute,
3557 for (Entry<String, Map<String, String>> mapAttribute : mapAttributes
3560 sf.setValue(mapAttribute.getKey(), mapAttribute.getValue());
3563 // adds feature to datasequence's feature set (since Jalview 2.10)
3564 al.getSequenceAt(i).addSequenceFeature(sf);
3567 if (vamsasSeqs.get(i).getDBRef().size() > 0)
3569 // adds dbrefs to datasequence's set (since Jalview 2.10)
3571 al.getSequenceAt(i).getDatasetSequence() == null
3572 ? al.getSequenceAt(i)
3573 : al.getSequenceAt(i).getDatasetSequence(),
3576 if (jseq.getPdbids().size() > 0)
3578 List<Pdbids> ids = jseq.getPdbids();
3579 for (int p = 0; p < ids.size(); p++)
3581 Pdbids pdbid = ids.get(p);
3582 jalview.datamodel.PDBEntry entry = new jalview.datamodel.PDBEntry();
3583 entry.setId(pdbid.getId());
3584 if (pdbid.getType() != null)
3586 if (PDBEntry.Type.getType(pdbid.getType()) != null)
3588 entry.setType(PDBEntry.Type.getType(pdbid.getType()));
3592 entry.setType(PDBEntry.Type.FILE);
3595 // jprovider is null when executing 'New View'
3596 if (pdbid.getFile() != null && jprovider != null)
3598 if (!pdbloaded.containsKey(pdbid.getFile()))
3600 entry.setFile(loadPDBFile(jprovider, pdbid.getId(),
3605 entry.setFile(pdbloaded.get(pdbid.getId()).toString());
3609 if (pdbid.getPdbentryItem() != null)
3611 for (PdbentryItem item : pdbid.getPdbentryItem())
3613 for (Property pr : item.getProperty())
3615 entry.setProperty(pr.getName(), pr.getValue());
3620 for (Property prop : pdbid.getProperty())
3622 entry.setProperty(prop.getName(), prop.getValue());
3624 StructureSelectionManager
3625 .getStructureSelectionManager(Desktop.instance)
3626 .registerPDBEntry(entry);
3627 // adds PDBEntry to datasequence's set (since Jalview 2.10)
3628 if (al.getSequenceAt(i).getDatasetSequence() != null)
3630 al.getSequenceAt(i).getDatasetSequence().addPDBId(entry);
3634 al.getSequenceAt(i).addPDBId(entry);
3639 } // end !multipleview
3641 // ///////////////////////////////
3642 // LOAD SEQUENCE MAPPINGS
3644 if (vamsasSet.getAlcodonFrame().size() > 0)
3646 // TODO Potentially this should only be done once for all views of an
3648 List<AlcodonFrame> alc = vamsasSet.getAlcodonFrame();
3649 for (int i = 0; i < alc.size(); i++)
3651 AlignedCodonFrame cf = new AlignedCodonFrame();
3652 if (alc.get(i).getAlcodMap().size() > 0)
3654 List<AlcodMap> maps = alc.get(i).getAlcodMap();
3655 for (int m = 0; m < maps.size(); m++)
3657 AlcodMap map = maps.get(m);
3658 SequenceI dnaseq = seqRefIds.get(map.getDnasq());
3660 jalview.datamodel.Mapping mapping = null;
3661 // attach to dna sequence reference.
3662 if (map.getMapping() != null)
3664 mapping = addMapping(map.getMapping());
3665 if (dnaseq != null && mapping.getTo() != null)
3667 cf.addMap(dnaseq, mapping.getTo(), mapping.getMap());
3673 .add(newAlcodMapRef(map.getDnasq(), cf, mapping));
3677 al.addCodonFrame(cf);
3682 // ////////////////////////////////
3684 List<JvAnnotRow> autoAlan = new ArrayList<>();
3687 * store any annotations which forward reference a group's ID
3689 Map<String, List<AlignmentAnnotation>> groupAnnotRefs = new Hashtable<>();
3691 if (vamsasSet.getAnnotation().size()/*Count()*/ > 0)
3693 List<Annotation> an = vamsasSet.getAnnotation();
3695 for (int i = 0; i < an.size(); i++)
3697 Annotation annotation = an.get(i);
3700 * test if annotation is automatically calculated for this view only
3702 boolean autoForView = false;
3703 if (annotation.getLabel().equals("Quality")
3704 || annotation.getLabel().equals("Conservation")
3705 || annotation.getLabel().equals("Consensus"))
3707 // Kludge for pre 2.5 projects which lacked the autocalculated flag
3709 // JAXB has no has() test; schema defaults value to false
3710 // if (!annotation.hasAutoCalculated())
3712 // annotation.setAutoCalculated(true);
3715 if (autoForView || annotation.isAutoCalculated())
3717 // remove ID - we don't recover annotation from other views for
3718 // view-specific annotation
3719 annotation.setId(null);
3722 // set visibility for other annotation in this view
3723 String annotationId = annotation.getId();
3724 if (annotationId != null && annotationIds.containsKey(annotationId))
3726 AlignmentAnnotation jda = annotationIds.get(annotationId);
3727 // in principle Visible should always be true for annotation displayed
3728 // in multiple views
3729 if (annotation.isVisible() != null)
3731 jda.visible = annotation.isVisible();
3734 al.addAnnotation(jda);
3738 // Construct new annotation from model.
3739 List<AnnotationElement> ae = annotation.getAnnotationElement();
3740 jalview.datamodel.Annotation[] anot = null;
3741 java.awt.Color firstColour = null;
3743 if (!annotation.isScoreOnly())
3745 anot = new jalview.datamodel.Annotation[al.getWidth()];
3746 for (int aa = 0; aa < ae.size() && aa < anot.length; aa++)
3748 AnnotationElement annElement = ae.get(aa);
3749 anpos = annElement.getPosition();
3751 if (anpos >= anot.length)
3756 float value = safeFloat(annElement.getValue());
3757 anot[anpos] = new jalview.datamodel.Annotation(
3758 annElement.getDisplayCharacter(),
3759 annElement.getDescription(),
3760 (annElement.getSecondaryStructure() == null
3761 || annElement.getSecondaryStructure()
3765 .getSecondaryStructure()
3768 anot[anpos].colour = new Color(safeInt(annElement.getColour()));
3769 if (firstColour == null)
3771 firstColour = anot[anpos].colour;
3775 jalview.datamodel.AlignmentAnnotation jaa = null;
3777 if (annotation.isGraph())
3779 float llim = 0, hlim = 0;
3780 // if (autoForView || an[i].isAutoCalculated()) {
3783 jaa = new jalview.datamodel.AlignmentAnnotation(
3784 annotation.getLabel(), annotation.getDescription(), anot,
3785 llim, hlim, safeInt(annotation.getGraphType()));
3787 jaa.graphGroup = safeInt(annotation.getGraphGroup());
3788 jaa._linecolour = firstColour;
3789 if (annotation.getThresholdLine() != null)
3791 jaa.setThreshold(new jalview.datamodel.GraphLine(
3792 safeFloat(annotation.getThresholdLine().getValue()),
3793 annotation.getThresholdLine().getLabel(),
3794 new java.awt.Color(safeInt(
3795 annotation.getThresholdLine().getColour()))));
3797 if (autoForView || annotation.isAutoCalculated())
3799 // Hardwire the symbol display line to ensure that labels for
3800 // histograms are displayed
3806 jaa = new jalview.datamodel.AlignmentAnnotation(
3807 annotation.getLabel(), annotation.getDescription(), anot);
3808 jaa._linecolour = firstColour;
3810 // register new annotation
3811 if (annotation.getId() != null)
3813 annotationIds.put(annotation.getId(), jaa);
3814 jaa.annotationId = annotation.getId();
3816 // recover sequence association
3817 String sequenceRef = annotation.getSequenceRef();
3818 if (sequenceRef != null)
3820 // from 2.9 sequenceRef is to sequence id (JAL-1781)
3821 SequenceI sequence = seqRefIds.get(sequenceRef);
3822 if (sequence == null)
3824 // in pre-2.9 projects sequence ref is to sequence name
3825 sequence = al.findName(sequenceRef);
3827 if (sequence != null)
3829 jaa.createSequenceMapping(sequence, 1, true);
3830 sequence.addAlignmentAnnotation(jaa);
3833 // and make a note of any group association
3834 if (annotation.getGroupRef() != null
3835 && annotation.getGroupRef().length() > 0)
3837 List<jalview.datamodel.AlignmentAnnotation> aal = groupAnnotRefs
3838 .get(annotation.getGroupRef());
3841 aal = new ArrayList<>();
3842 groupAnnotRefs.put(annotation.getGroupRef(), aal);
3847 if (annotation.getScore() != null)
3849 jaa.setScore(annotation.getScore().doubleValue());
3851 if (annotation.isVisible() != null)
3853 jaa.visible = annotation.isVisible().booleanValue();
3856 if (annotation.isCentreColLabels() != null)
3858 jaa.centreColLabels = annotation.isCentreColLabels()
3862 if (annotation.isScaleColLabels() != null)
3864 jaa.scaleColLabel = annotation.isScaleColLabels().booleanValue();
3866 if (annotation.isAutoCalculated())
3868 // newer files have an 'autoCalculated' flag and store calculation
3869 // state in viewport properties
3870 jaa.autoCalculated = true; // means annotation will be marked for
3871 // update at end of load.
3873 if (annotation.getGraphHeight() != null)
3875 jaa.graphHeight = annotation.getGraphHeight().intValue();
3877 jaa.belowAlignment = annotation.isBelowAlignment();
3878 jaa.setCalcId(annotation.getCalcId());
3879 if (annotation.getProperty().size() > 0)
3881 for (Annotation.Property prop : annotation.getProperty())
3883 jaa.setProperty(prop.getName(), prop.getValue());
3886 if (jaa.autoCalculated)
3888 autoAlan.add(new JvAnnotRow(i, jaa));
3891 // if (!autoForView)
3893 // add autocalculated group annotation and any user created annotation
3895 al.addAnnotation(jaa);
3899 // ///////////////////////
3901 // Create alignment markup and styles for this view
3902 if (jalviewModel.getJGroup().size() > 0)
3904 List<JGroup> groups = jalviewModel.getJGroup();
3905 boolean addAnnotSchemeGroup = false;
3906 for (int i = 0; i < groups.size(); i++)
3908 JGroup jGroup = groups.get(i);
3909 ColourSchemeI cs = null;
3910 if (jGroup.getColour() != null)
3912 if (jGroup.getColour().startsWith("ucs"))
3914 cs = getUserColourScheme(jalviewModel, jGroup.getColour());
3916 else if (jGroup.getColour().equals("AnnotationColourGradient")
3917 && jGroup.getAnnotationColours() != null)
3919 addAnnotSchemeGroup = true;
3923 cs = ColourSchemeProperty.getColourScheme(null, al,
3924 jGroup.getColour());
3927 int pidThreshold = safeInt(jGroup.getPidThreshold());
3929 Vector<SequenceI> seqs = new Vector<>();
3931 for (int s = 0; s < jGroup.getSeq().size(); s++)
3933 String seqId = jGroup.getSeq().get(s);
3934 SequenceI ts = seqRefIds.get(seqId);
3938 seqs.addElement(ts);
3942 if (seqs.size() < 1)
3947 SequenceGroup sg = new SequenceGroup(seqs, jGroup.getName(), cs,
3948 safeBoolean(jGroup.isDisplayBoxes()),
3949 safeBoolean(jGroup.isDisplayText()),
3950 safeBoolean(jGroup.isColourText()),
3951 safeInt(jGroup.getStart()), safeInt(jGroup.getEnd()));
3952 sg.getGroupColourScheme().setThreshold(pidThreshold, true);
3953 sg.getGroupColourScheme()
3954 .setConservationInc(safeInt(jGroup.getConsThreshold()));
3955 sg.setOutlineColour(new Color(safeInt(jGroup.getOutlineColour())));
3957 sg.textColour = new Color(safeInt(jGroup.getTextCol1()));
3958 sg.textColour2 = new Color(safeInt(jGroup.getTextCol2()));
3959 sg.setShowNonconserved(safeBoolean(jGroup.isShowUnconserved()));
3960 sg.thresholdTextColour = safeInt(jGroup.getTextColThreshold());
3961 // attributes with a default in the schema are never null
3962 sg.setShowConsensusHistogram(jGroup.isShowConsensusHistogram());
3963 sg.setshowSequenceLogo(jGroup.isShowSequenceLogo());
3964 sg.setNormaliseSequenceLogo(jGroup.isNormaliseSequenceLogo());
3965 sg.setIgnoreGapsConsensus(jGroup.isIgnoreGapsinConsensus());
3966 if (jGroup.getConsThreshold() != null
3967 && jGroup.getConsThreshold().intValue() != 0)
3969 Conservation c = new Conservation("All", sg.getSequences(null), 0,
3972 c.verdict(false, 25);
3973 sg.cs.setConservation(c);
3976 if (jGroup.getId() != null && groupAnnotRefs.size() > 0)
3978 // re-instate unique group/annotation row reference
3979 List<AlignmentAnnotation> jaal = groupAnnotRefs
3980 .get(jGroup.getId());
3983 for (AlignmentAnnotation jaa : jaal)
3986 if (jaa.autoCalculated)
3988 // match up and try to set group autocalc alignment row for this
3990 if (jaa.label.startsWith("Consensus for "))
3992 sg.setConsensus(jaa);
3994 // match up and try to set group autocalc alignment row for this
3996 if (jaa.label.startsWith("Conservation for "))
3998 sg.setConservationRow(jaa);
4005 if (addAnnotSchemeGroup)
4007 // reconstruct the annotation colourscheme
4009 constructAnnotationColour(jGroup.getAnnotationColours(),
4010 null, al, jalviewModel, false));
4016 // only dataset in this model, so just return.
4019 // ///////////////////////////////
4022 AlignFrame af = null;
4023 AlignViewport av = null;
4024 // now check to see if we really need to create a new viewport.
4025 if (multipleView && viewportsAdded.size() == 0)
4027 // We recovered an alignment for which a viewport already exists.
4028 // TODO: fix up any settings necessary for overlaying stored state onto
4029 // state recovered from another document. (may not be necessary).
4030 // we may need a binding from a viewport in memory to one recovered from
4032 // and then recover its containing af to allow the settings to be applied.
4033 // TODO: fix for vamsas demo
4035 "About to recover a viewport for existing alignment: Sequence set ID is "
4037 Object seqsetobj = retrieveExistingObj(uniqueSeqSetId);
4038 if (seqsetobj != null)
4040 if (seqsetobj instanceof String)
4042 uniqueSeqSetId = (String) seqsetobj;
4044 "Recovered extant sequence set ID mapping for ID : New Sequence set ID is "
4050 "Warning : Collision between sequence set ID string and existing jalview object mapping.");
4056 * indicate that annotation colours are applied across all groups (pre
4057 * Jalview 2.8.1 behaviour)
4059 boolean doGroupAnnColour = Jalview2XML.isVersionStringLaterThan("2.8.1",
4060 jalviewModel.getVersion());
4062 AlignmentPanel ap = null;
4063 boolean isnewview = true;
4066 // Check to see if this alignment already has a view id == viewId
4067 jalview.gui.AlignmentPanel views[] = Desktop
4068 .getAlignmentPanels(uniqueSeqSetId);
4069 if (views != null && views.length > 0)
4071 for (int v = 0; v < views.length; v++)
4073 if (views[v].av.getViewId().equalsIgnoreCase(viewId))
4075 // recover the existing alignpanel, alignframe, viewport
4076 af = views[v].alignFrame;
4079 // TODO: could even skip resetting view settings if we don't want to
4080 // change the local settings from other jalview processes
4089 af = loadViewport(file, jseqs, hiddenSeqs, al, jalviewModel, view,
4090 uniqueSeqSetId, viewId, autoAlan);
4091 av = af.getViewport();
4096 * Load any trees, PDB structures and viewers
4098 * Not done if flag is false (when this method is used for New View)
4100 if (loadTreesAndStructures)
4102 loadTrees(jalviewModel, view, af, av, ap);
4103 loadPCAViewers(jalviewModel, ap);
4104 loadPDBStructures(jprovider, jseqs, af, ap);
4105 loadRnaViewers(jprovider, jseqs, ap);
4107 // and finally return.
4112 * Instantiate and link any saved RNA (Varna) viewers. The state of the Varna
4113 * panel is restored from separate jar entries, two (gapped and trimmed) per
4114 * sequence and secondary structure.
4116 * Currently each viewer shows just one sequence and structure (gapped and
4117 * trimmed), however this method is designed to support multiple sequences or
4118 * structures in viewers if wanted in future.
4124 private void loadRnaViewers(jarInputStreamProvider jprovider,
4125 List<JSeq> jseqs, AlignmentPanel ap)
4128 * scan the sequences for references to viewers; create each one the first
4129 * time it is referenced, add Rna models to existing viewers
4131 for (JSeq jseq : jseqs)
4133 for (int i = 0; i < jseq.getRnaViewer().size(); i++)
4135 RnaViewer viewer = jseq.getRnaViewer().get(i);
4136 AppVarna appVarna = findOrCreateVarnaViewer(viewer, uniqueSetSuffix,
4139 for (int j = 0; j < viewer.getSecondaryStructure().size(); j++)
4141 SecondaryStructure ss = viewer.getSecondaryStructure().get(j);
4142 SequenceI seq = seqRefIds.get(jseq.getId());
4143 AlignmentAnnotation ann = this.annotationIds
4144 .get(ss.getAnnotationId());
4147 * add the structure to the Varna display (with session state copied
4148 * from the jar to a temporary file)
4150 boolean gapped = safeBoolean(ss.isGapped());
4151 String rnaTitle = ss.getTitle();
4152 String sessionState = ss.getViewerState();
4153 String tempStateFile = copyJarEntry(jprovider, sessionState,
4155 RnaModel rna = new RnaModel(rnaTitle, ann, seq, null, gapped);
4156 appVarna.addModelSession(rna, rnaTitle, tempStateFile);
4158 appVarna.setInitialSelection(safeInt(viewer.getSelectedRna()));
4164 * Locate and return an already instantiated matching AppVarna, or create one
4168 * @param viewIdSuffix
4172 protected AppVarna findOrCreateVarnaViewer(RnaViewer viewer,
4173 String viewIdSuffix, AlignmentPanel ap)
4176 * on each load a suffix is appended to the saved viewId, to avoid conflicts
4177 * if load is repeated
4179 String postLoadId = viewer.getViewId() + viewIdSuffix;
4180 for (JInternalFrame frame : getAllFrames())
4182 if (frame instanceof AppVarna)
4184 AppVarna varna = (AppVarna) frame;
4185 if (postLoadId.equals(varna.getViewId()))
4187 // this viewer is already instantiated
4188 // could in future here add ap as another 'parent' of the
4189 // AppVarna window; currently just 1-to-many
4196 * viewer not found - make it
4198 RnaViewerModel model = new RnaViewerModel(postLoadId, viewer.getTitle(),
4199 safeInt(viewer.getXpos()), safeInt(viewer.getYpos()),
4200 safeInt(viewer.getWidth()), safeInt(viewer.getHeight()),
4201 safeInt(viewer.getDividerLocation()));
4202 AppVarna varna = new AppVarna(model, ap);
4208 * Load any saved trees
4216 protected void loadTrees(JalviewModel jm, Viewport view, AlignFrame af,
4217 AlignViewport av, AlignmentPanel ap)
4219 // TODO result of automated refactoring - are all these parameters needed?
4222 for (int t = 0; t < jm.getTree().size(); t++)
4225 Tree tree = jm.getTree().get(t);
4227 TreePanel tp = (TreePanel) retrieveExistingObj(tree.getId());
4230 tp = af.showNewickTree(new NewickFile(tree.getNewick()),
4231 tree.getTitle(), safeInt(tree.getWidth()),
4232 safeInt(tree.getHeight()), safeInt(tree.getXpos()),
4233 safeInt(tree.getYpos()));
4234 if (tree.getId() != null)
4236 // perhaps bind the tree id to something ?
4241 // update local tree attributes ?
4242 // TODO: should check if tp has been manipulated by user - if so its
4243 // settings shouldn't be modified
4244 tp.setTitle(tree.getTitle());
4245 tp.setBounds(new Rectangle(safeInt(tree.getXpos()),
4246 safeInt(tree.getYpos()), safeInt(tree.getWidth()),
4247 safeInt(tree.getHeight())));
4248 tp.setViewport(av); // af.viewport;
4249 // TODO: verify 'associate with all views' works still
4250 tp.getTreeCanvas().setViewport(av); // af.viewport;
4251 tp.getTreeCanvas().setAssociatedPanel(ap); // af.alignPanel;
4253 tp.getTreeCanvas().setApplyToAllViews(tree.isLinkToAllViews());
4257 "There was a problem recovering stored Newick tree: \n"
4258 + tree.getNewick());
4262 tp.fitToWindow.setState(safeBoolean(tree.isFitToWindow()));
4263 tp.fitToWindow_actionPerformed(null);
4265 if (tree.getFontName() != null)
4268 new Font(tree.getFontName(), safeInt(tree.getFontStyle()),
4269 safeInt(tree.getFontSize())));
4274 new Font(view.getFontName(), safeInt(view.getFontStyle()),
4275 safeInt(view.getFontSize())));
4278 tp.showPlaceholders(safeBoolean(tree.isMarkUnlinked()));
4279 tp.showBootstrap(safeBoolean(tree.isShowBootstrap()));
4280 tp.showDistances(safeBoolean(tree.isShowDistances()));
4282 tp.getTreeCanvas().setThreshold(safeFloat(tree.getThreshold()));
4284 if (safeBoolean(tree.isCurrentTree()))
4286 af.getViewport().setCurrentTree(tp.getTree());
4290 } catch (Exception ex)
4292 ex.printStackTrace();
4297 * Load and link any saved structure viewers.
4304 protected void loadPDBStructures(jarInputStreamProvider jprovider,
4305 List<JSeq> jseqs, AlignFrame af, AlignmentPanel ap)
4308 * Run through all PDB ids on the alignment, and collect mappings between
4309 * distinct view ids and all sequences referring to that view.
4311 Map<String, StructureViewerModel> structureViewers = new LinkedHashMap<>();
4313 for (int i = 0; i < jseqs.size(); i++)
4315 JSeq jseq = jseqs.get(i);
4316 if (jseq.getPdbids().size() > 0)
4318 List<Pdbids> ids = jseq.getPdbids();
4319 for (int p = 0; p < ids.size(); p++)
4321 Pdbids pdbid = ids.get(p);
4322 final int structureStateCount = pdbid.getStructureState().size();
4323 for (int s = 0; s < structureStateCount; s++)
4325 // check to see if we haven't already created this structure view
4326 final StructureState structureState = pdbid.getStructureState()
4328 String sviewid = (structureState.getViewId() == null) ? null
4329 : structureState.getViewId() + uniqueSetSuffix;
4330 jalview.datamodel.PDBEntry jpdb = new jalview.datamodel.PDBEntry();
4331 // Originally : pdbid.getFile()
4332 // : TODO: verify external PDB file recovery still works in normal
4333 // jalview project load
4335 loadPDBFile(jprovider, pdbid.getId(), pdbid.getFile()));
4336 jpdb.setId(pdbid.getId());
4338 int x = safeInt(structureState.getXpos());
4339 int y = safeInt(structureState.getYpos());
4340 int width = safeInt(structureState.getWidth());
4341 int height = safeInt(structureState.getHeight());
4343 // Probably don't need to do this anymore...
4344 // Desktop.desktop.getComponentAt(x, y);
4345 // TODO: NOW: check that this recovers the PDB file correctly.
4346 String pdbFile = loadPDBFile(jprovider, pdbid.getId(),
4348 jalview.datamodel.SequenceI seq = seqRefIds
4349 .get(jseq.getId() + "");
4350 if (sviewid == null)
4352 sviewid = "_jalview_pre2_4_" + x + "," + y + "," + width + ","
4355 if (!structureViewers.containsKey(sviewid))
4357 String viewerType = structureState.getType();
4358 if (viewerType == null) // pre Jalview 2.9
4360 viewerType = ViewerType.JMOL.toString();
4362 structureViewers.put(sviewid,
4363 new StructureViewerModel(x, y, width, height, false,
4364 false, true, structureState.getViewId(),
4366 // Legacy pre-2.7 conversion JAL-823 :
4367 // do not assume any view has to be linked for colour by
4371 // assemble String[] { pdb files }, String[] { id for each
4372 // file }, orig_fileloc, SequenceI[][] {{ seqs_file 1 }, {
4373 // seqs_file 2}, boolean[] {
4374 // linkAlignPanel,superposeWithAlignpanel}} from hash
4375 StructureViewerModel jmoldat = structureViewers.get(sviewid);
4376 jmoldat.setAlignWithPanel(jmoldat.isAlignWithPanel()
4377 || structureState.isAlignwithAlignPanel());
4380 * Default colour by linked panel to false if not specified (e.g.
4381 * for pre-2.7 projects)
4383 boolean colourWithAlignPanel = jmoldat.isColourWithAlignPanel();
4384 colourWithAlignPanel |= structureState.isColourwithAlignPanel();
4385 jmoldat.setColourWithAlignPanel(colourWithAlignPanel);
4388 * Default colour by viewer to true if not specified (e.g. for
4391 boolean colourByViewer = jmoldat.isColourByViewer();
4392 colourByViewer &= structureState.isColourByJmol();
4393 jmoldat.setColourByViewer(colourByViewer);
4395 if (jmoldat.getStateData().length() < structureState.getValue()
4396 /*Content()*/.length())
4398 jmoldat.setStateData(structureState.getValue());// Content());
4400 if (pdbid.getFile() != null)
4402 File mapkey = new File(pdbid.getFile());
4403 StructureData seqstrmaps = jmoldat.getFileData().get(mapkey);
4404 if (seqstrmaps == null)
4406 jmoldat.getFileData().put(mapkey,
4407 seqstrmaps = jmoldat.new StructureData(pdbFile,
4410 if (!seqstrmaps.getSeqList().contains(seq))
4412 seqstrmaps.getSeqList().add(seq);
4418 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");
4419 Console.warn(errorMessage);
4425 // Instantiate the associated structure views
4426 for (Entry<String, StructureViewerModel> entry : structureViewers
4431 createOrLinkStructureViewer(entry, af, ap, jprovider);
4432 } catch (Exception e)
4435 "Error loading structure viewer: " + e.getMessage());
4436 // failed - try the next one
4448 protected void createOrLinkStructureViewer(
4449 Entry<String, StructureViewerModel> viewerData, AlignFrame af,
4450 AlignmentPanel ap, jarInputStreamProvider jprovider)
4452 final StructureViewerModel stateData = viewerData.getValue();
4455 * Search for any viewer windows already open from other alignment views
4456 * that exactly match the stored structure state
4458 StructureViewerBase comp = findMatchingViewer(viewerData);
4462 linkStructureViewer(ap, comp, stateData);
4466 String type = stateData.getType();
4469 ViewerType viewerType = ViewerType.valueOf(type);
4470 createStructureViewer(viewerType, viewerData, af, jprovider);
4471 } catch (IllegalArgumentException | NullPointerException e)
4473 // TODO JAL-3619 show error dialog / offer an alternative viewer
4474 Console.error("Invalid structure viewer type: " + type);
4479 * Generates a name for the entry in the project jar file to hold state
4480 * information for a structure viewer
4485 protected String getViewerJarEntryName(String viewId)
4487 return VIEWER_PREFIX + viewId;
4491 * Returns any open frame that matches given structure viewer data. The match
4492 * is based on the unique viewId, or (for older project versions) the frame's
4498 protected StructureViewerBase findMatchingViewer(
4499 Entry<String, StructureViewerModel> viewerData)
4501 final String sviewid = viewerData.getKey();
4502 final StructureViewerModel svattrib = viewerData.getValue();
4503 StructureViewerBase comp = null;
4504 JInternalFrame[] frames = getAllFrames();
4505 for (JInternalFrame frame : frames)
4507 if (frame instanceof StructureViewerBase)
4510 * Post jalview 2.4 schema includes structure view id
4512 if (sviewid != null && ((StructureViewerBase) frame).getViewId()
4515 comp = (StructureViewerBase) frame;
4516 break; // break added in 2.9
4519 * Otherwise test for matching position and size of viewer frame
4521 else if (frame.getX() == svattrib.getX()
4522 && frame.getY() == svattrib.getY()
4523 && frame.getHeight() == svattrib.getHeight()
4524 && frame.getWidth() == svattrib.getWidth())
4526 comp = (StructureViewerBase) frame;
4527 // no break in faint hope of an exact match on viewId
4535 * Link an AlignmentPanel to an existing structure viewer.
4540 * @param useinViewerSuperpos
4541 * @param usetoColourbyseq
4542 * @param viewerColouring
4544 protected void linkStructureViewer(AlignmentPanel ap,
4545 StructureViewerBase viewer, StructureViewerModel stateData)
4547 // NOTE: if the jalview project is part of a shared session then
4548 // view synchronization should/could be done here.
4550 final boolean useinViewerSuperpos = stateData.isAlignWithPanel();
4551 final boolean usetoColourbyseq = stateData.isColourWithAlignPanel();
4552 final boolean viewerColouring = stateData.isColourByViewer();
4553 Map<File, StructureData> oldFiles = stateData.getFileData();
4556 * Add mapping for sequences in this view to an already open viewer
4558 final AAStructureBindingModel binding = viewer.getBinding();
4559 for (File id : oldFiles.keySet())
4561 // add this and any other pdb files that should be present in the
4563 StructureData filedat = oldFiles.get(id);
4564 String pdbFile = filedat.getFilePath();
4565 SequenceI[] seq = filedat.getSeqList().toArray(new SequenceI[0]);
4566 binding.getSsm().setMapping(seq, null, pdbFile, DataSourceType.FILE,
4568 binding.addSequenceForStructFile(pdbFile, seq);
4570 // and add the AlignmentPanel's reference to the view panel
4571 viewer.addAlignmentPanel(ap);
4572 if (useinViewerSuperpos)
4574 viewer.useAlignmentPanelForSuperposition(ap);
4578 viewer.excludeAlignmentPanelForSuperposition(ap);
4580 if (usetoColourbyseq)
4582 viewer.useAlignmentPanelForColourbyseq(ap, !viewerColouring);
4586 viewer.excludeAlignmentPanelForColourbyseq(ap);
4591 * Get all frames within the Desktop.
4595 protected JInternalFrame[] getAllFrames()
4597 JInternalFrame[] frames = null;
4598 // TODO is this necessary - is it safe - risk of hanging?
4603 frames = Desktop.desktop.getAllFrames();
4604 } catch (ArrayIndexOutOfBoundsException e)
4606 // occasional No such child exceptions are thrown here...
4610 } catch (InterruptedException f)
4614 } while (frames == null);
4619 * Answers true if 'version' is equal to or later than 'supported', where each
4620 * is formatted as major/minor versions like "2.8.3" or "2.3.4b1" for bugfix
4621 * changes. Development and test values for 'version' are leniently treated
4625 * - minimum version we are comparing against
4627 * - version of data being processsed
4630 public static boolean isVersionStringLaterThan(String supported,
4633 if (supported == null || version == null
4634 || version.equalsIgnoreCase("DEVELOPMENT BUILD")
4635 || version.equalsIgnoreCase("Test")
4636 || version.equalsIgnoreCase("AUTOMATED BUILD"))
4638 System.err.println("Assuming project file with "
4639 + (version == null ? "null" : version)
4640 + " is compatible with Jalview version " + supported);
4645 return StringUtils.compareVersions(version, supported, "b") >= 0;
4649 Vector<JalviewStructureDisplayI> newStructureViewers = null;
4651 protected void addNewStructureViewer(JalviewStructureDisplayI sview)
4653 if (newStructureViewers != null)
4655 sview.getBinding().setFinishedLoadingFromArchive(false);
4656 newStructureViewers.add(sview);
4660 protected void setLoadingFinishedForNewStructureViewers()
4662 if (newStructureViewers != null)
4664 for (JalviewStructureDisplayI sview : newStructureViewers)
4666 sview.getBinding().setFinishedLoadingFromArchive(true);
4668 newStructureViewers.clear();
4669 newStructureViewers = null;
4673 AlignFrame loadViewport(String file, List<JSeq> JSEQ,
4674 List<SequenceI> hiddenSeqs, AlignmentI al, JalviewModel jm,
4675 Viewport view, String uniqueSeqSetId, String viewId,
4676 List<JvAnnotRow> autoAlan)
4678 AlignFrame af = null;
4679 af = new AlignFrame(al, safeInt(view.getWidth()),
4680 safeInt(view.getHeight()), uniqueSeqSetId, viewId)
4684 // protected void processKeyEvent(java.awt.event.KeyEvent e) {
4685 // System.out.println("Jalview2XML AF " + e);
4686 // super.processKeyEvent(e);
4693 af.setFileName(file, FileFormat.Jalview);
4695 final AlignViewport viewport = af.getViewport();
4696 for (int i = 0; i < JSEQ.size(); i++)
4698 int colour = safeInt(JSEQ.get(i).getColour());
4699 viewport.setSequenceColour(viewport.getAlignment().getSequenceAt(i),
4705 viewport.setColourByReferenceSeq(true);
4706 viewport.setDisplayReferenceSeq(true);
4709 viewport.setGatherViewsHere(safeBoolean(view.isGatheredViews()));
4711 if (view.getSequenceSetId() != null)
4713 AlignmentViewport av = viewportsAdded.get(uniqueSeqSetId);
4715 viewport.setSequenceSetId(uniqueSeqSetId);
4718 // propagate shared settings to this new view
4719 viewport.setHistoryList(av.getHistoryList());
4720 viewport.setRedoList(av.getRedoList());
4724 viewportsAdded.put(uniqueSeqSetId, viewport);
4726 // TODO: check if this method can be called repeatedly without
4727 // side-effects if alignpanel already registered.
4728 PaintRefresher.Register(af.alignPanel, uniqueSeqSetId);
4730 // apply Hidden regions to view.
4731 if (hiddenSeqs != null)
4733 for (int s = 0; s < JSEQ.size(); s++)
4735 SequenceGroup hidden = new SequenceGroup();
4736 boolean isRepresentative = false;
4737 for (int r = 0; r < JSEQ.get(s).getHiddenSequences().size(); r++)
4739 isRepresentative = true;
4740 SequenceI sequenceToHide = al
4741 .getSequenceAt(JSEQ.get(s).getHiddenSequences().get(r));
4742 hidden.addSequence(sequenceToHide, false);
4743 // remove from hiddenSeqs list so we don't try to hide it twice
4744 hiddenSeqs.remove(sequenceToHide);
4746 if (isRepresentative)
4748 SequenceI representativeSequence = al.getSequenceAt(s);
4749 hidden.addSequence(representativeSequence, false);
4750 viewport.hideRepSequences(representativeSequence, hidden);
4754 SequenceI[] hseqs = hiddenSeqs
4755 .toArray(new SequenceI[hiddenSeqs.size()]);
4756 viewport.hideSequence(hseqs);
4759 // recover view properties and display parameters
4761 viewport.setShowAnnotation(safeBoolean(view.isShowAnnotation()));
4762 viewport.setAbovePIDThreshold(safeBoolean(view.isPidSelected()));
4763 final int pidThreshold = safeInt(view.getPidThreshold());
4764 viewport.setThreshold(pidThreshold);
4766 viewport.setColourText(safeBoolean(view.isShowColourText()));
4768 viewport.setConservationSelected(
4769 safeBoolean(view.isConservationSelected()));
4770 viewport.setIncrement(safeInt(view.getConsThreshold()));
4771 viewport.setShowJVSuffix(safeBoolean(view.isShowFullId()));
4772 viewport.setRightAlignIds(safeBoolean(view.isRightAlignIds()));
4773 viewport.setFont(new Font(view.getFontName(),
4774 safeInt(view.getFontStyle()), safeInt(view.getFontSize())),
4776 ViewStyleI vs = viewport.getViewStyle();
4777 vs.setScaleProteinAsCdna(view.isScaleProteinAsCdna());
4778 viewport.setViewStyle(vs);
4779 // TODO: allow custom charWidth/Heights to be restored by updating them
4780 // after setting font - which means set above to false
4781 viewport.setRenderGaps(safeBoolean(view.isRenderGaps()));
4782 viewport.setWrapAlignment(safeBoolean(view.isWrapAlignment()));
4783 viewport.setShowAnnotation(safeBoolean(view.isShowAnnotation()));
4785 viewport.setShowBoxes(safeBoolean(view.isShowBoxes()));
4787 viewport.setShowText(safeBoolean(view.isShowText()));
4789 viewport.setTextColour(new Color(safeInt(view.getTextCol1())));
4790 viewport.setTextColour2(new Color(safeInt(view.getTextCol2())));
4791 viewport.setThresholdTextColour(safeInt(view.getTextColThreshold()));
4792 viewport.setShowUnconserved(view.isShowUnconserved());
4793 viewport.getRanges().setStartRes(safeInt(view.getStartRes()));
4795 if (view.getViewName() != null)
4797 viewport.setViewName(view.getViewName());
4798 af.setInitialTabVisible();
4800 af.setBounds(safeInt(view.getXpos()), safeInt(view.getYpos()),
4801 safeInt(view.getWidth()), safeInt(view.getHeight()));
4802 // startSeq set in af.alignPanel.updateLayout below
4803 af.alignPanel.updateLayout();
4804 ColourSchemeI cs = null;
4805 // apply colourschemes
4806 if (view.getBgColour() != null)
4808 if (view.getBgColour().startsWith("ucs"))
4810 cs = getUserColourScheme(jm, view.getBgColour());
4812 else if (view.getBgColour().startsWith("Annotation"))
4814 AnnotationColourScheme viewAnnColour = view.getAnnotationColours();
4815 cs = constructAnnotationColour(viewAnnColour, af, al, jm, true);
4822 cs = ColourSchemeProperty.getColourScheme(af.getViewport(), al,
4823 view.getBgColour());
4828 * turn off 'alignment colour applies to all groups'
4829 * while restoring global colour scheme
4831 viewport.setColourAppliesToAllGroups(false);
4832 viewport.setGlobalColourScheme(cs);
4833 viewport.getResidueShading().setThreshold(pidThreshold,
4834 view.isIgnoreGapsinConsensus());
4835 viewport.getResidueShading()
4836 .setConsensus(viewport.getSequenceConsensusHash());
4837 if (safeBoolean(view.isConservationSelected()) && cs != null)
4839 viewport.getResidueShading()
4840 .setConservationInc(safeInt(view.getConsThreshold()));
4842 af.changeColour(cs);
4843 viewport.setColourAppliesToAllGroups(true);
4845 viewport.setShowSequenceFeatures(
4846 safeBoolean(view.isShowSequenceFeatures()));
4848 viewport.setCentreColumnLabels(view.isCentreColumnLabels());
4849 viewport.setIgnoreGapsConsensus(view.isIgnoreGapsinConsensus(), null);
4850 viewport.setFollowHighlight(view.isFollowHighlight());
4851 viewport.followSelection = view.isFollowSelection();
4852 viewport.setShowConsensusHistogram(view.isShowConsensusHistogram());
4853 viewport.setShowSequenceLogo(view.isShowSequenceLogo());
4854 viewport.setNormaliseSequenceLogo(view.isNormaliseSequenceLogo());
4855 viewport.setShowDBRefs(safeBoolean(view.isShowDbRefTooltip()));
4856 viewport.setShowNPFeats(safeBoolean(view.isShowNPfeatureTooltip()));
4857 viewport.setShowGroupConsensus(view.isShowGroupConsensus());
4858 viewport.setShowGroupConservation(view.isShowGroupConservation());
4859 viewport.setShowComplementFeatures(view.isShowComplementFeatures());
4860 viewport.setShowComplementFeaturesOnTop(
4861 view.isShowComplementFeaturesOnTop());
4863 // recover feature settings
4864 if (jm.getFeatureSettings() != null)
4866 FeatureRendererModel fr = af.alignPanel.getSeqPanel().seqCanvas
4867 .getFeatureRenderer();
4868 FeaturesDisplayed fdi;
4869 viewport.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
4870 String[] renderOrder = new String[jm.getFeatureSettings().getSetting()
4872 Map<String, FeatureColourI> featureColours = new Hashtable<>();
4873 Map<String, Float> featureOrder = new Hashtable<>();
4875 for (int fs = 0; fs < jm.getFeatureSettings().getSetting()
4878 Setting setting = jm.getFeatureSettings().getSetting().get(fs);
4879 String featureType = setting.getType();
4882 * restore feature filters (if any)
4884 jalview.xml.binding.jalview.FeatureMatcherSet filters = setting
4886 if (filters != null)
4888 FeatureMatcherSetI filter = Jalview2XML.parseFilter(featureType,
4890 if (!filter.isEmpty())
4892 fr.setFeatureFilter(featureType, filter);
4897 * restore feature colour scheme
4899 Color maxColour = new Color(setting.getColour());
4900 if (setting.getMincolour() != null)
4903 * minColour is always set unless a simple colour
4904 * (including for colour by label though it doesn't use it)
4906 Color minColour = new Color(setting.getMincolour().intValue());
4907 Color noValueColour = minColour;
4908 NoValueColour noColour = setting.getNoValueColour();
4909 if (noColour == NoValueColour.NONE)
4911 noValueColour = null;
4913 else if (noColour == NoValueColour.MAX)
4915 noValueColour = maxColour;
4917 float min = safeFloat(safeFloat(setting.getMin()));
4918 float max = setting.getMax() == null ? 1f
4919 : setting.getMax().floatValue();
4920 FeatureColourI gc = new FeatureColour(maxColour, minColour,
4921 maxColour, noValueColour, min, max);
4922 if (setting.getAttributeName().size() > 0)
4924 gc.setAttributeName(setting.getAttributeName().toArray(
4925 new String[setting.getAttributeName().size()]));
4927 if (setting.getThreshold() != null)
4929 gc.setThreshold(setting.getThreshold().floatValue());
4930 int threshstate = safeInt(setting.getThreshstate());
4931 // -1 = None, 0 = Below, 1 = Above threshold
4932 if (threshstate == 0)
4934 gc.setBelowThreshold(true);
4936 else if (threshstate == 1)
4938 gc.setAboveThreshold(true);
4941 gc.setAutoScaled(true); // default
4942 if (setting.isAutoScale() != null)
4944 gc.setAutoScaled(setting.isAutoScale());
4946 if (setting.isColourByLabel() != null)
4948 gc.setColourByLabel(setting.isColourByLabel());
4950 // and put in the feature colour table.
4951 featureColours.put(featureType, gc);
4955 featureColours.put(featureType, new FeatureColour(maxColour));
4957 renderOrder[fs] = featureType;
4958 if (setting.getOrder() != null)
4960 featureOrder.put(featureType, setting.getOrder().floatValue());
4964 featureOrder.put(featureType, Float.valueOf(
4965 fs / jm.getFeatureSettings().getSetting().size()));
4967 if (safeBoolean(setting.isDisplay()))
4969 fdi.setVisible(featureType);
4972 Map<String, Boolean> fgtable = new Hashtable<>();
4973 for (int gs = 0; gs < jm.getFeatureSettings().getGroup().size(); gs++)
4975 Group grp = jm.getFeatureSettings().getGroup().get(gs);
4976 fgtable.put(grp.getName(), Boolean.valueOf(grp.isDisplay()));
4978 // FeatureRendererSettings frs = new FeatureRendererSettings(renderOrder,
4979 // fgtable, featureColours, jms.getFeatureSettings().hasTransparency() ?
4980 // jms.getFeatureSettings().getTransparency() : 0.0, featureOrder);
4981 FeatureRendererSettings frs = new FeatureRendererSettings(renderOrder,
4982 fgtable, featureColours, 1.0f, featureOrder);
4983 fr.transferSettings(frs);
4986 if (view.getHiddenColumns().size() > 0)
4988 for (int c = 0; c < view.getHiddenColumns().size(); c++)
4990 final HiddenColumns hc = view.getHiddenColumns().get(c);
4991 viewport.hideColumns(safeInt(hc.getStart()),
4992 safeInt(hc.getEnd()) /* +1 */);
4995 if (view.getCalcIdParam() != null)
4997 for (CalcIdParam calcIdParam : view.getCalcIdParam())
4999 if (calcIdParam != null)
5001 if (recoverCalcIdParam(calcIdParam, viewport))
5006 Console.warn("Couldn't recover parameters for "
5007 + calcIdParam.getCalcId());
5012 af.setMenusFromViewport(viewport);
5013 af.setTitle(view.getTitle());
5014 // TODO: we don't need to do this if the viewport is aready visible.
5016 * Add the AlignFrame to the desktop (it may be 'gathered' later), unless it
5017 * has a 'cdna/protein complement' view, in which case save it in order to
5018 * populate a SplitFrame once all views have been read in.
5020 String complementaryViewId = view.getComplementId();
5021 if (complementaryViewId == null)
5023 Desktop.addInternalFrame(af, view.getTitle(),
5024 safeInt(view.getWidth()), safeInt(view.getHeight()));
5025 // recompute any autoannotation
5026 af.alignPanel.updateAnnotation(false, true);
5027 reorderAutoannotation(af, al, autoAlan);
5028 af.alignPanel.alignmentChanged();
5032 splitFrameCandidates.put(view, af);
5038 * Reads saved data to restore Colour by Annotation settings
5040 * @param viewAnnColour
5044 * @param checkGroupAnnColour
5047 private ColourSchemeI constructAnnotationColour(
5048 AnnotationColourScheme viewAnnColour, AlignFrame af,
5049 AlignmentI al, JalviewModel model, boolean checkGroupAnnColour)
5051 boolean propagateAnnColour = false;
5052 AlignmentI annAlignment = af != null ? af.getViewport().getAlignment()
5054 if (checkGroupAnnColour && al.getGroups() != null
5055 && al.getGroups().size() > 0)
5057 // pre 2.8.1 behaviour
5058 // check to see if we should transfer annotation colours
5059 propagateAnnColour = true;
5060 for (SequenceGroup sg : al.getGroups())
5062 if (sg.getColourScheme() instanceof AnnotationColourGradient)
5064 propagateAnnColour = false;
5070 * 2.10.2- : saved annotationId is AlignmentAnnotation.annotationId
5072 String annotationId = viewAnnColour.getAnnotation();
5073 AlignmentAnnotation matchedAnnotation = annotationIds.get(annotationId);
5076 * pre 2.10.2: saved annotationId is AlignmentAnnotation.label
5078 if (matchedAnnotation == null
5079 && annAlignment.getAlignmentAnnotation() != null)
5081 for (int i = 0; i < annAlignment.getAlignmentAnnotation().length; i++)
5084 .equals(annAlignment.getAlignmentAnnotation()[i].label))
5086 matchedAnnotation = annAlignment.getAlignmentAnnotation()[i];
5091 if (matchedAnnotation == null)
5093 System.err.println("Failed to match annotation colour scheme for "
5097 // belt-and-braces create a threshold line if the
5098 // colourscheme needs one but the matchedAnnotation doesn't have one
5099 if (safeInt(viewAnnColour.getAboveThreshold()) != 0
5100 && matchedAnnotation.getThreshold() == null)
5102 matchedAnnotation.setThreshold(
5103 new GraphLine(safeFloat(viewAnnColour.getThreshold()),
5104 "Threshold", Color.black));
5107 AnnotationColourGradient cs = null;
5108 if (viewAnnColour.getColourScheme().equals("None"))
5110 cs = new AnnotationColourGradient(matchedAnnotation,
5111 new Color(safeInt(viewAnnColour.getMinColour())),
5112 new Color(safeInt(viewAnnColour.getMaxColour())),
5113 safeInt(viewAnnColour.getAboveThreshold()));
5115 else if (viewAnnColour.getColourScheme().startsWith("ucs"))
5117 cs = new AnnotationColourGradient(matchedAnnotation,
5118 getUserColourScheme(model, viewAnnColour.getColourScheme()),
5119 safeInt(viewAnnColour.getAboveThreshold()));
5123 cs = new AnnotationColourGradient(matchedAnnotation,
5124 ColourSchemeProperty.getColourScheme(af.getViewport(), al,
5125 viewAnnColour.getColourScheme()),
5126 safeInt(viewAnnColour.getAboveThreshold()));
5129 boolean perSequenceOnly = safeBoolean(viewAnnColour.isPerSequence());
5130 boolean useOriginalColours = safeBoolean(
5131 viewAnnColour.isPredefinedColours());
5132 cs.setSeqAssociated(perSequenceOnly);
5133 cs.setPredefinedColours(useOriginalColours);
5135 if (propagateAnnColour && al.getGroups() != null)
5137 // Also use these settings for all the groups
5138 for (int g = 0; g < al.getGroups().size(); g++)
5140 SequenceGroup sg = al.getGroups().get(g);
5141 if (sg.getGroupColourScheme() == null)
5146 AnnotationColourGradient groupScheme = new AnnotationColourGradient(
5147 matchedAnnotation, sg.getColourScheme(),
5148 safeInt(viewAnnColour.getAboveThreshold()));
5149 sg.setColourScheme(groupScheme);
5150 groupScheme.setSeqAssociated(perSequenceOnly);
5151 groupScheme.setPredefinedColours(useOriginalColours);
5157 private void reorderAutoannotation(AlignFrame af, AlignmentI al,
5158 List<JvAnnotRow> autoAlan)
5160 // copy over visualization settings for autocalculated annotation in the
5162 if (al.getAlignmentAnnotation() != null)
5165 * Kludge for magic autoannotation names (see JAL-811)
5167 String[] magicNames = new String[] { "Consensus", "Quality",
5169 JvAnnotRow nullAnnot = new JvAnnotRow(-1, null);
5170 Hashtable<String, JvAnnotRow> visan = new Hashtable<>();
5171 for (String nm : magicNames)
5173 visan.put(nm, nullAnnot);
5175 for (JvAnnotRow auan : autoAlan)
5177 visan.put(auan.template.label
5178 + (auan.template.getCalcId() == null ? ""
5179 : "\t" + auan.template.getCalcId()),
5182 int hSize = al.getAlignmentAnnotation().length;
5183 List<JvAnnotRow> reorder = new ArrayList<>();
5184 // work through any autoCalculated annotation already on the view
5185 // removing it if it should be placed in a different location on the
5186 // annotation panel.
5187 List<String> remains = new ArrayList<>(visan.keySet());
5188 for (int h = 0; h < hSize; h++)
5190 jalview.datamodel.AlignmentAnnotation jalan = al
5191 .getAlignmentAnnotation()[h];
5192 if (jalan.autoCalculated)
5195 JvAnnotRow valan = visan.get(k = jalan.label);
5196 if (jalan.getCalcId() != null)
5198 valan = visan.get(k = jalan.label + "\t" + jalan.getCalcId());
5203 // delete the auto calculated row from the alignment
5204 al.deleteAnnotation(jalan, false);
5208 if (valan != nullAnnot)
5210 if (jalan != valan.template)
5212 // newly created autoannotation row instance
5213 // so keep a reference to the visible annotation row
5214 // and copy over all relevant attributes
5215 if (valan.template.graphHeight >= 0)
5218 jalan.graphHeight = valan.template.graphHeight;
5220 jalan.visible = valan.template.visible;
5222 reorder.add(new JvAnnotRow(valan.order, jalan));
5227 // Add any (possibly stale) autocalculated rows that were not appended to
5228 // the view during construction
5229 for (String other : remains)
5231 JvAnnotRow othera = visan.get(other);
5232 if (othera != nullAnnot && othera.template.getCalcId() != null
5233 && othera.template.getCalcId().length() > 0)
5235 reorder.add(othera);
5238 // now put the automatic annotation in its correct place
5239 int s = 0, srt[] = new int[reorder.size()];
5240 JvAnnotRow[] rws = new JvAnnotRow[reorder.size()];
5241 for (JvAnnotRow jvar : reorder)
5244 srt[s++] = jvar.order;
5247 jalview.util.QuickSort.sort(srt, rws);
5248 // and re-insert the annotation at its correct position
5249 for (JvAnnotRow jvar : rws)
5251 al.addAnnotation(jvar.template, jvar.order);
5253 af.alignPanel.adjustAnnotationHeight();
5257 Hashtable skipList = null;
5260 * TODO remove this method
5263 * @return AlignFrame bound to sequenceSetId from view, if one exists. private
5264 * AlignFrame getSkippedFrame(Viewport view) { if (skipList==null) {
5265 * throw new Error("Implementation Error. No skipList defined for this
5266 * Jalview2XML instance."); } return (AlignFrame)
5267 * skipList.get(view.getSequenceSetId()); }
5271 * Check if the Jalview view contained in object should be skipped or not.
5274 * @return true if view's sequenceSetId is a key in skipList
5276 private boolean skipViewport(JalviewModel object)
5278 if (skipList == null)
5282 String id = object.getViewport().get(0).getSequenceSetId();
5283 if (skipList.containsKey(id))
5285 Console.debug("Skipping seuqence set id " + id);
5291 public void addToSkipList(AlignFrame af)
5293 if (skipList == null)
5295 skipList = new Hashtable();
5297 skipList.put(af.getViewport().getSequenceSetId(), af);
5300 public void clearSkipList()
5302 if (skipList != null)
5309 private void recoverDatasetFor(SequenceSet vamsasSet, AlignmentI al,
5310 boolean ignoreUnrefed, String uniqueSeqSetId)
5312 jalview.datamodel.AlignmentI ds = getDatasetFor(
5313 vamsasSet.getDatasetId());
5314 AlignmentI xtant_ds = ds;
5315 if (xtant_ds == null)
5317 // good chance we are about to create a new dataset, but check if we've
5318 // seen some of the dataset sequence IDs before.
5319 // TODO: skip this check if we are working with project generated by
5320 // version 2.11 or later
5321 xtant_ds = checkIfHasDataset(vamsasSet.getSequence());
5322 if (xtant_ds != null)
5325 addDatasetRef(vamsasSet.getDatasetId(), ds);
5328 Vector<SequenceI> dseqs = null;
5331 // recovering an alignment View
5332 AlignmentI seqSetDS = getDatasetFor(UNIQSEQSETID + uniqueSeqSetId);
5333 if (seqSetDS != null)
5335 if (ds != null && ds != seqSetDS)
5338 "JAL-3171 regression: Overwriting a dataset reference for an alignment"
5339 + " - CDS/Protein crossreference data may be lost");
5340 if (xtant_ds != null)
5342 // This can only happen if the unique sequence set ID was bound to a
5343 // dataset that did not contain any of the sequences in the view
5344 // currently being restored.
5346 "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.");
5350 addDatasetRef(vamsasSet.getDatasetId(), ds);
5355 // try even harder to restore dataset
5356 AlignmentI xtantDS = checkIfHasDataset(vamsasSet.getSequence());
5357 // create a list of new dataset sequences
5358 dseqs = new Vector<>();
5360 for (int i = 0, iSize = vamsasSet.getSequence().size(); i < iSize; i++)
5362 Sequence vamsasSeq = vamsasSet.getSequence().get(i);
5363 ensureJalviewDatasetSequence(vamsasSeq, ds, dseqs, ignoreUnrefed, i);
5365 // create a new dataset
5368 SequenceI[] dsseqs = new SequenceI[dseqs.size()];
5369 dseqs.copyInto(dsseqs);
5370 ds = new jalview.datamodel.Alignment(dsseqs);
5371 Console.debug("Created new dataset " + vamsasSet.getDatasetId()
5372 + " for alignment " + System.identityHashCode(al));
5373 addDatasetRef(vamsasSet.getDatasetId(), ds);
5375 // set the dataset for the newly imported alignment.
5376 if (al.getDataset() == null && !ignoreUnrefed)
5379 // register dataset for the alignment's uniqueSeqSetId for legacy projects
5380 addDatasetRef(UNIQSEQSETID + uniqueSeqSetId, ds);
5382 updateSeqDatasetBinding(vamsasSet.getSequence(), ds);
5386 * XML dataset sequence ID to materialised dataset reference
5388 HashMap<String, AlignmentI> seqToDataset = new HashMap<>();
5391 * @return the first materialised dataset reference containing a dataset
5392 * sequence referenced in the given view
5394 * - sequences from the view
5396 AlignmentI checkIfHasDataset(List<Sequence> list)
5398 for (Sequence restoredSeq : list)
5400 AlignmentI datasetFor = seqToDataset.get(restoredSeq.getDsseqid());
5401 if (datasetFor != null)
5410 * Register ds as the containing dataset for the dataset sequences referenced
5411 * by sequences in list
5414 * - sequences in a view
5417 void updateSeqDatasetBinding(List<Sequence> list, AlignmentI ds)
5419 for (Sequence restoredSeq : list)
5421 AlignmentI prevDS = seqToDataset.put(restoredSeq.getDsseqid(), ds);
5422 if (prevDS != null && prevDS != ds)
5424 Console.warn("Dataset sequence appears in many datasets: "
5425 + restoredSeq.getDsseqid());
5426 // TODO: try to merge!
5434 * sequence definition to create/merge dataset sequence for
5438 * vector to add new dataset sequence to
5439 * @param ignoreUnrefed
5440 * - when true, don't create new sequences from vamsasSeq if it's id
5441 * doesn't already have an asssociated Jalview sequence.
5443 * - used to reorder the sequence in the alignment according to the
5444 * vamsasSeq array ordering, to preserve ordering of dataset
5446 private void ensureJalviewDatasetSequence(Sequence vamsasSeq,
5447 AlignmentI ds, Vector<SequenceI> dseqs, boolean ignoreUnrefed,
5450 // JBP TODO: Check this is called for AlCodonFrames to support recovery of
5452 SequenceI sq = seqRefIds.get(vamsasSeq.getId());
5453 boolean reorder = false;
5454 SequenceI dsq = null;
5455 if (sq != null && sq.getDatasetSequence() != null)
5457 dsq = sq.getDatasetSequence();
5463 if (sq == null && ignoreUnrefed)
5467 String sqid = vamsasSeq.getDsseqid();
5470 // need to create or add a new dataset sequence reference to this sequence
5473 dsq = seqRefIds.get(sqid);
5478 // make a new dataset sequence
5479 dsq = sq.createDatasetSequence();
5482 // make up a new dataset reference for this sequence
5483 sqid = seqHash(dsq);
5485 dsq.setVamsasId(uniqueSetSuffix + sqid);
5486 seqRefIds.put(sqid, dsq);
5491 dseqs.addElement(dsq);
5496 ds.addSequence(dsq);
5502 { // make this dataset sequence sq's dataset sequence
5503 sq.setDatasetSequence(dsq);
5504 // and update the current dataset alignment
5509 if (!dseqs.contains(dsq))
5516 if (ds.findIndex(dsq) < 0)
5518 ds.addSequence(dsq);
5525 // TODO: refactor this as a merge dataset sequence function
5526 // now check that sq (the dataset sequence) sequence really is the union of
5527 // all references to it
5528 // boolean pre = sq.getStart() < dsq.getStart();
5529 // boolean post = sq.getEnd() > dsq.getEnd();
5533 // StringBuffer sb = new StringBuffer();
5534 String newres = jalview.analysis.AlignSeq.extractGaps(
5535 jalview.util.Comparison.GapChars, sq.getSequenceAsString());
5536 if (!newres.equalsIgnoreCase(dsq.getSequenceAsString())
5537 && newres.length() > dsq.getLength())
5539 // Update with the longer sequence.
5543 * if (pre) { sb.insert(0, newres .substring(0, dsq.getStart() -
5544 * sq.getStart())); dsq.setStart(sq.getStart()); } if (post) {
5545 * sb.append(newres.substring(newres.length() - sq.getEnd() -
5546 * dsq.getEnd())); dsq.setEnd(sq.getEnd()); }
5548 dsq.setSequence(newres);
5550 // TODO: merges will never happen if we 'know' we have the real dataset
5551 // sequence - this should be detected when id==dssid
5553 "DEBUG Notice: Merged dataset sequence (if you see this often, post at http://issues.jalview.org/browse/JAL-1474)"); // ("
5554 // + (pre ? "prepended" : "") + " "
5555 // + (post ? "appended" : ""));
5560 // sequence refs are identical. We may need to update the existing dataset
5561 // alignment with this one, though.
5562 if (ds != null && dseqs == null)
5564 int opos = ds.findIndex(dsq);
5565 SequenceI tseq = null;
5566 if (opos != -1 && vseqpos != opos)
5568 // remove from old position
5569 ds.deleteSequence(dsq);
5571 if (vseqpos < ds.getHeight())
5573 if (vseqpos != opos)
5575 // save sequence at destination position
5576 tseq = ds.getSequenceAt(vseqpos);
5577 ds.replaceSequenceAt(vseqpos, dsq);
5578 ds.addSequence(tseq);
5583 ds.addSequence(dsq);
5590 * TODO use AlignmentI here and in related methods - needs
5591 * AlignmentI.getDataset() changed to return AlignmentI instead of Alignment
5593 Hashtable<String, AlignmentI> datasetIds = null;
5595 IdentityHashMap<AlignmentI, String> dataset2Ids = null;
5597 private AlignmentI getDatasetFor(String datasetId)
5599 if (datasetIds == null)
5601 datasetIds = new Hashtable<>();
5604 if (datasetIds.containsKey(datasetId))
5606 return datasetIds.get(datasetId);
5611 private void addDatasetRef(String datasetId, AlignmentI dataset)
5613 if (datasetIds == null)
5615 datasetIds = new Hashtable<>();
5617 datasetIds.put(datasetId, dataset);
5621 * make a new dataset ID for this jalview dataset alignment
5626 private String getDatasetIdRef(AlignmentI dataset)
5628 if (dataset.getDataset() != null)
5631 "Serious issue! Dataset Object passed to getDatasetIdRef is not a Jalview DATASET alignment...");
5633 String datasetId = makeHashCode(dataset, null);
5634 if (datasetId == null)
5636 // make a new datasetId and record it
5637 if (dataset2Ids == null)
5639 dataset2Ids = new IdentityHashMap<>();
5643 datasetId = dataset2Ids.get(dataset);
5645 if (datasetId == null)
5647 datasetId = "ds" + dataset2Ids.size() + 1;
5648 dataset2Ids.put(dataset, datasetId);
5655 * Add any saved DBRefEntry's to the sequence. An entry flagged as 'locus' is
5656 * constructed as a special subclass GeneLocus.
5658 * @param datasetSequence
5661 private void addDBRefs(SequenceI datasetSequence, Sequence sequence)
5663 for (int d = 0; d < sequence.getDBRef().size(); d++)
5665 DBRef dr = sequence.getDBRef().get(d);
5669 entry = new GeneLocus(dr.getSource(), dr.getVersion(),
5670 dr.getAccessionId());
5674 entry = new DBRefEntry(dr.getSource(), dr.getVersion(),
5675 dr.getAccessionId());
5677 if (dr.getMapping() != null)
5679 entry.setMap(addMapping(dr.getMapping()));
5681 entry.setCanonical(dr.isCanonical());
5682 datasetSequence.addDBRef(entry);
5686 private jalview.datamodel.Mapping addMapping(Mapping m)
5688 SequenceI dsto = null;
5689 // Mapping m = dr.getMapping();
5690 int fr[] = new int[m.getMapListFrom().size() * 2];
5691 Iterator<MapListFrom> from = m.getMapListFrom().iterator();// enumerateMapListFrom();
5692 for (int _i = 0; from.hasNext(); _i += 2)
5694 MapListFrom mf = from.next();
5695 fr[_i] = mf.getStart();
5696 fr[_i + 1] = mf.getEnd();
5698 int fto[] = new int[m.getMapListTo().size() * 2];
5699 Iterator<MapListTo> to = m.getMapListTo().iterator();// enumerateMapListTo();
5700 for (int _i = 0; to.hasNext(); _i += 2)
5702 MapListTo mf = to.next();
5703 fto[_i] = mf.getStart();
5704 fto[_i + 1] = mf.getEnd();
5706 jalview.datamodel.Mapping jmap = new jalview.datamodel.Mapping(dsto, fr,
5707 fto, m.getMapFromUnit().intValue(),
5708 m.getMapToUnit().intValue());
5711 * (optional) choice of dseqFor or Sequence
5713 if (m.getDseqFor() != null)
5715 String dsfor = m.getDseqFor();
5716 if (seqRefIds.containsKey(dsfor))
5721 jmap.setTo(seqRefIds.get(dsfor));
5725 frefedSequence.add(newMappingRef(dsfor, jmap));
5728 else if (m.getSequence() != null)
5731 * local sequence definition
5733 Sequence ms = m.getSequence();
5734 SequenceI djs = null;
5735 String sqid = ms.getDsseqid();
5736 if (sqid != null && sqid.length() > 0)
5739 * recover dataset sequence
5741 djs = seqRefIds.get(sqid);
5746 "Warning - making up dataset sequence id for DbRef sequence map reference");
5747 sqid = ((Object) ms).toString(); // make up a new hascode for
5748 // undefined dataset sequence hash
5749 // (unlikely to happen)
5755 * make a new dataset sequence and add it to refIds hash
5757 djs = new jalview.datamodel.Sequence(ms.getName(),
5759 djs.setStart(jmap.getMap().getToLowest());
5760 djs.setEnd(jmap.getMap().getToHighest());
5761 djs.setVamsasId(uniqueSetSuffix + sqid);
5763 incompleteSeqs.put(sqid, djs);
5764 seqRefIds.put(sqid, djs);
5767 Console.debug("about to recurse on addDBRefs.");
5776 * Provides a 'copy' of an alignment view (on action New View) by 'saving' the
5777 * view as XML (but not to file), and then reloading it
5782 public AlignmentPanel copyAlignPanel(AlignmentPanel ap)
5785 JalviewModel jm = saveState(ap, null, null, null);
5788 jm.getVamsasModel().getSequenceSet().get(0).getDatasetId(),
5789 ap.getAlignment().getDataset());
5791 uniqueSetSuffix = "";
5792 // jm.getJalviewModelSequence().getViewport(0).setId(null);
5793 jm.getViewport().get(0).setId(null);
5794 // we don't overwrite the view we just copied
5796 if (this.frefedSequence == null)
5798 frefedSequence = new Vector<>();
5801 viewportsAdded.clear();
5803 AlignFrame af = loadFromObject(jm, null, false, null);
5804 af.getAlignPanels().clear();
5805 af.closeMenuItem_actionPerformed(true);
5808 * if(ap.av.getAlignment().getAlignmentAnnotation()!=null) { for(int i=0;
5809 * i<ap.av.getAlignment().getAlignmentAnnotation().length; i++) {
5810 * if(!ap.av.getAlignment().getAlignmentAnnotation()[i].autoCalculated) {
5811 * af.alignPanel.av.getAlignment().getAlignmentAnnotation()[i] =
5812 * ap.av.getAlignment().getAlignmentAnnotation()[i]; } } }
5815 return af.alignPanel;
5818 private Hashtable jvids2vobj;
5821 * set the object to ID mapping tables used to write/recover objects and XML
5822 * ID strings for the jalview project. If external tables are provided then
5823 * finalize and clearSeqRefs will not clear the tables when the Jalview2XML
5824 * object goes out of scope. - also populates the datasetIds hashtable with
5825 * alignment objects containing dataset sequences
5828 * Map from ID strings to jalview datamodel
5830 * Map from jalview datamodel to ID strings
5834 public void setObjectMappingTables(Hashtable vobj2jv,
5835 IdentityHashMap jv2vobj)
5837 this.jv2vobj = jv2vobj;
5838 this.vobj2jv = vobj2jv;
5839 Iterator ds = jv2vobj.keySet().iterator();
5841 while (ds.hasNext())
5843 Object jvobj = ds.next();
5844 id = jv2vobj.get(jvobj).toString();
5845 if (jvobj instanceof jalview.datamodel.Alignment)
5847 if (((jalview.datamodel.Alignment) jvobj).getDataset() == null)
5849 addDatasetRef(id, (jalview.datamodel.Alignment) jvobj);
5852 else if (jvobj instanceof jalview.datamodel.Sequence)
5854 // register sequence object so the XML parser can recover it.
5855 if (seqRefIds == null)
5857 seqRefIds = new HashMap<>();
5859 if (seqsToIds == null)
5861 seqsToIds = new IdentityHashMap<>();
5863 seqRefIds.put(jv2vobj.get(jvobj).toString(), (SequenceI) jvobj);
5864 seqsToIds.put((SequenceI) jvobj, id);
5866 else if (jvobj instanceof jalview.datamodel.AlignmentAnnotation)
5869 AlignmentAnnotation jvann = (AlignmentAnnotation) jvobj;
5870 annotationIds.put(anid = jv2vobj.get(jvobj).toString(), jvann);
5871 if (jvann.annotationId == null)
5873 jvann.annotationId = anid;
5875 if (!jvann.annotationId.equals(anid))
5877 // TODO verify that this is the correct behaviour
5878 Console.warn("Overriding Annotation ID for " + anid
5879 + " from different id : " + jvann.annotationId);
5880 jvann.annotationId = anid;
5883 else if (jvobj instanceof String)
5885 if (jvids2vobj == null)
5887 jvids2vobj = new Hashtable();
5888 jvids2vobj.put(jvobj, jv2vobj.get(jvobj).toString());
5893 Console.debug("Ignoring " + jvobj.getClass() + " (ID = " + id);
5899 * set the uniqueSetSuffix used to prefix/suffix object IDs for jalview
5900 * objects created from the project archive. If string is null (default for
5901 * construction) then suffix will be set automatically.
5905 public void setUniqueSetSuffix(String string)
5907 uniqueSetSuffix = string;
5912 * uses skipList2 as the skipList for skipping views on sequence sets
5913 * associated with keys in the skipList
5917 public void setSkipList(Hashtable skipList2)
5919 skipList = skipList2;
5923 * Reads the jar entry of given name and returns its contents, or null if the
5924 * entry is not found.
5927 * @param jarEntryName
5930 protected String readJarEntry(jarInputStreamProvider jprovider,
5931 String jarEntryName)
5933 String result = null;
5934 BufferedReader in = null;
5939 * Reopen the jar input stream and traverse its entries to find a matching
5942 JarInputStream jin = jprovider.getJarInputStream();
5943 JarEntry entry = null;
5946 entry = jin.getNextJarEntry();
5947 } while (entry != null && !entry.getName().equals(jarEntryName));
5951 StringBuilder out = new StringBuilder(256);
5952 in = new BufferedReader(new InputStreamReader(jin, UTF_8));
5955 while ((data = in.readLine()) != null)
5959 result = out.toString();
5964 "Couldn't find entry in Jalview Jar for " + jarEntryName);
5966 } catch (Exception ex)
5968 ex.printStackTrace();
5976 } catch (IOException e)
5987 * Returns an incrementing counter (0, 1, 2...)
5991 private synchronized int nextCounter()
5997 * Loads any saved PCA viewers
6002 protected void loadPCAViewers(JalviewModel model, AlignmentPanel ap)
6006 List<PcaViewer> pcaviewers = model.getPcaViewer();
6007 for (PcaViewer viewer : pcaviewers)
6009 String modelName = viewer.getScoreModelName();
6010 SimilarityParamsI params = new SimilarityParams(
6011 viewer.isIncludeGappedColumns(), viewer.isMatchGaps(),
6012 viewer.isIncludeGaps(),
6013 viewer.isDenominateByShortestLength());
6016 * create the panel (without computing the PCA)
6018 PCAPanel panel = new PCAPanel(ap, modelName, params);
6020 panel.setTitle(viewer.getTitle());
6021 panel.setBounds(new Rectangle(viewer.getXpos(), viewer.getYpos(),
6022 viewer.getWidth(), viewer.getHeight()));
6024 boolean showLabels = viewer.isShowLabels();
6025 panel.setShowLabels(showLabels);
6026 panel.getRotatableCanvas().setShowLabels(showLabels);
6027 panel.getRotatableCanvas()
6028 .setBgColour(new Color(viewer.getBgColour()));
6029 panel.getRotatableCanvas()
6030 .setApplyToAllViews(viewer.isLinkToAllViews());
6033 * load PCA output data
6035 ScoreModelI scoreModel = ScoreModels.getInstance()
6036 .getScoreModel(modelName, ap);
6037 PCA pca = new PCA(null, scoreModel, params);
6038 PcaDataType pcaData = viewer.getPcaData();
6040 MatrixI pairwise = loadDoubleMatrix(pcaData.getPairwiseMatrix());
6041 pca.setPairwiseScores(pairwise);
6043 MatrixI triDiag = loadDoubleMatrix(pcaData.getTridiagonalMatrix());
6044 pca.setTridiagonal(triDiag);
6046 MatrixI result = loadDoubleMatrix(pcaData.getEigenMatrix());
6047 pca.setEigenmatrix(result);
6049 panel.getPcaModel().setPCA(pca);
6052 * we haven't saved the input data! (JAL-2647 to do)
6054 panel.setInputData(null);
6057 * add the sequence points for the PCA display
6059 List<jalview.datamodel.SequencePoint> seqPoints = new ArrayList<>();
6060 for (SequencePoint sp : viewer.getSequencePoint())
6062 String seqId = sp.getSequenceRef();
6063 SequenceI seq = seqRefIds.get(seqId);
6066 throw new IllegalStateException(
6067 "Unmatched seqref for PCA: " + seqId);
6069 Point pt = new Point(sp.getXPos(), sp.getYPos(), sp.getZPos());
6070 jalview.datamodel.SequencePoint seqPoint = new jalview.datamodel.SequencePoint(
6072 seqPoints.add(seqPoint);
6074 panel.getRotatableCanvas().setPoints(seqPoints, seqPoints.size());
6077 * set min-max ranges and scale after setPoints (which recomputes them)
6079 panel.getRotatableCanvas().setScaleFactor(viewer.getScaleFactor());
6080 SeqPointMin spMin = viewer.getSeqPointMin();
6081 float[] min = new float[] { spMin.getXPos(), spMin.getYPos(),
6083 SeqPointMax spMax = viewer.getSeqPointMax();
6084 float[] max = new float[] { spMax.getXPos(), spMax.getYPos(),
6086 panel.getRotatableCanvas().setSeqMinMax(min, max);
6088 // todo: hold points list in PCAModel only
6089 panel.getPcaModel().setSequencePoints(seqPoints);
6091 panel.setSelectedDimensionIndex(viewer.getXDim(), X);
6092 panel.setSelectedDimensionIndex(viewer.getYDim(), Y);
6093 panel.setSelectedDimensionIndex(viewer.getZDim(), Z);
6095 // is this duplication needed?
6096 panel.setTop(seqPoints.size() - 1);
6097 panel.getPcaModel().setTop(seqPoints.size() - 1);
6100 * add the axes' end points for the display
6102 for (int i = 0; i < 3; i++)
6104 Axis axis = viewer.getAxis().get(i);
6105 panel.getRotatableCanvas().getAxisEndPoints()[i] = new Point(
6106 axis.getXPos(), axis.getYPos(), axis.getZPos());
6109 Desktop.addInternalFrame(panel, MessageManager.formatMessage(
6110 "label.calc_title", "PCA", modelName), 475, 450);
6112 } catch (Exception ex)
6114 Console.error("Error loading PCA: " + ex.toString());
6119 * Creates a new structure viewer window
6126 protected void createStructureViewer(ViewerType viewerType,
6127 final Entry<String, StructureViewerModel> viewerData,
6128 AlignFrame af, jarInputStreamProvider jprovider)
6130 final StructureViewerModel viewerModel = viewerData.getValue();
6131 String sessionFilePath = null;
6133 if (viewerType == ViewerType.JMOL)
6135 sessionFilePath = rewriteJmolSession(viewerModel, jprovider);
6139 String viewerJarEntryName = getViewerJarEntryName(
6140 viewerModel.getViewId());
6141 sessionFilePath = copyJarEntry(jprovider, viewerJarEntryName,
6142 "viewerSession", ".tmp");
6144 final String sessionPath = sessionFilePath;
6145 final String sviewid = viewerData.getKey();
6148 SwingUtilities.invokeAndWait(new Runnable()
6153 JalviewStructureDisplayI sview = null;
6156 sview = StructureViewer.createView(viewerType, af.alignPanel,
6157 viewerModel, sessionPath, sviewid);
6158 addNewStructureViewer(sview);
6159 } catch (OutOfMemoryError ex)
6161 new OOMWarning("Restoring structure view for " + viewerType,
6162 (OutOfMemoryError) ex.getCause());
6163 if (sview != null && sview.isVisible())
6165 sview.closeViewer(false);
6166 sview.setVisible(false);
6172 } catch (InvocationTargetException | InterruptedException ex)
6174 Console.warn("Unexpected error when opening " + viewerType
6175 + " structure viewer", ex);
6180 * Rewrites a Jmol session script, saves it to a temporary file, and returns
6181 * the path of the file. "load file" commands are rewritten to change the
6182 * original PDB file names to those created as the Jalview project is loaded.
6188 private String rewriteJmolSession(StructureViewerModel svattrib,
6189 jarInputStreamProvider jprovider)
6191 String state = svattrib.getStateData(); // Jalview < 2.9
6192 if (state == null || state.isEmpty()) // Jalview >= 2.9
6194 String jarEntryName = getViewerJarEntryName(svattrib.getViewId());
6195 state = readJarEntry(jprovider, jarEntryName);
6197 // TODO or simpler? for each key in oldFiles,
6198 // replace key.getPath() in state with oldFiles.get(key).getFilePath()
6199 // (allowing for different path escapings)
6200 StringBuilder rewritten = new StringBuilder(state.length());
6201 int cp = 0, ncp, ecp;
6202 Map<File, StructureData> oldFiles = svattrib.getFileData();
6203 while ((ncp = state.indexOf("load ", cp)) > -1)
6207 // look for next filename in load statement
6208 rewritten.append(state.substring(cp,
6209 ncp = (state.indexOf("\"", ncp + 1) + 1)));
6210 String oldfilenam = state.substring(ncp,
6211 ecp = state.indexOf("\"", ncp));
6212 // recover the new mapping data for this old filename
6213 // have to normalize filename - since Jmol and jalview do
6214 // filename translation differently.
6215 StructureData filedat = oldFiles.get(new File(oldfilenam));
6216 if (filedat == null)
6218 String reformatedOldFilename = oldfilenam.replaceAll("/", "\\\\");
6219 filedat = oldFiles.get(new File(reformatedOldFilename));
6221 rewritten.append(Platform.escapeBackslashes(filedat.getFilePath()));
6222 rewritten.append("\"");
6223 cp = ecp + 1; // advance beyond last \" and set cursor so we can
6224 // look for next file statement.
6225 } while ((ncp = state.indexOf("/*file*/", cp)) > -1);
6229 // just append rest of state
6230 rewritten.append(state.substring(cp));
6234 System.err.print("Ignoring incomplete Jmol state for PDB ids: ");
6235 rewritten = new StringBuilder(state);
6236 rewritten.append("; load append ");
6237 for (File id : oldFiles.keySet())
6239 // add pdb files that should be present in the viewer
6240 StructureData filedat = oldFiles.get(id);
6241 rewritten.append(" \"").append(filedat.getFilePath()).append("\"");
6243 rewritten.append(";");
6246 if (rewritten.length() == 0)
6250 final String history = "history = ";
6251 int historyIndex = rewritten.indexOf(history);
6252 if (historyIndex > -1)
6255 * change "history = [true|false];" to "history = [1|0];"
6257 historyIndex += history.length();
6258 String val = rewritten.substring(historyIndex, historyIndex + 5);
6259 if (val.startsWith("true"))
6261 rewritten.replace(historyIndex, historyIndex + 4, "1");
6263 else if (val.startsWith("false"))
6265 rewritten.replace(historyIndex, historyIndex + 5, "0");
6271 File tmp = File.createTempFile("viewerSession", ".tmp");
6272 try (OutputStream os = new FileOutputStream(tmp))
6274 InputStream is = new ByteArrayInputStream(
6275 rewritten.toString().getBytes());
6277 return tmp.getAbsolutePath();
6279 } catch (IOException e)
6281 Console.error("Error restoring Jmol session: " + e.toString());
6287 * Populates an XML model of the feature colour scheme for one feature type
6289 * @param featureType
6293 public static Colour marshalColour(String featureType,
6294 FeatureColourI fcol)
6296 Colour col = new Colour();
6297 if (fcol.isSimpleColour())
6299 col.setRGB(Format.getHexString(fcol.getColour()));
6303 col.setRGB(Format.getHexString(fcol.getMaxColour()));
6304 col.setMin(fcol.getMin());
6305 col.setMax(fcol.getMax());
6306 col.setMinRGB(jalview.util.Format.getHexString(fcol.getMinColour()));
6307 col.setAutoScale(fcol.isAutoScaled());
6308 col.setThreshold(fcol.getThreshold());
6309 col.setColourByLabel(fcol.isColourByLabel());
6310 col.setThreshType(fcol.isAboveThreshold() ? ThresholdType.ABOVE
6311 : (fcol.isBelowThreshold() ? ThresholdType.BELOW
6312 : ThresholdType.NONE));
6313 if (fcol.isColourByAttribute())
6315 final String[] attName = fcol.getAttributeName();
6316 col.getAttributeName().add(attName[0]);
6317 if (attName.length > 1)
6319 col.getAttributeName().add(attName[1]);
6322 Color noColour = fcol.getNoColour();
6323 if (noColour == null)
6325 col.setNoValueColour(NoValueColour.NONE);
6327 else if (noColour == fcol.getMaxColour())
6329 col.setNoValueColour(NoValueColour.MAX);
6333 col.setNoValueColour(NoValueColour.MIN);
6336 col.setName(featureType);
6341 * Populates an XML model of the feature filter(s) for one feature type
6343 * @param firstMatcher
6344 * the first (or only) match condition)
6346 * remaining match conditions (if any)
6348 * if true, conditions are and-ed, else or-ed
6350 public static jalview.xml.binding.jalview.FeatureMatcherSet marshalFilter(
6351 FeatureMatcherI firstMatcher, Iterator<FeatureMatcherI> filters,
6354 jalview.xml.binding.jalview.FeatureMatcherSet result = new jalview.xml.binding.jalview.FeatureMatcherSet();
6356 if (filters.hasNext())
6361 CompoundMatcher compound = new CompoundMatcher();
6362 compound.setAnd(and);
6363 jalview.xml.binding.jalview.FeatureMatcherSet matcher1 = marshalFilter(
6364 firstMatcher, Collections.emptyIterator(), and);
6365 // compound.addMatcherSet(matcher1);
6366 compound.getMatcherSet().add(matcher1);
6367 FeatureMatcherI nextMatcher = filters.next();
6368 jalview.xml.binding.jalview.FeatureMatcherSet matcher2 = marshalFilter(
6369 nextMatcher, filters, and);
6370 // compound.addMatcherSet(matcher2);
6371 compound.getMatcherSet().add(matcher2);
6372 result.setCompoundMatcher(compound);
6377 * single condition matcher
6379 // MatchCondition matcherModel = new MatchCondition();
6380 jalview.xml.binding.jalview.FeatureMatcher matcherModel = new jalview.xml.binding.jalview.FeatureMatcher();
6381 matcherModel.setCondition(
6382 firstMatcher.getMatcher().getCondition().getStableName());
6383 matcherModel.setValue(firstMatcher.getMatcher().getPattern());
6384 if (firstMatcher.isByAttribute())
6386 matcherModel.setBy(FilterBy.BY_ATTRIBUTE);
6387 // matcherModel.setAttributeName(firstMatcher.getAttribute());
6388 String[] attName = firstMatcher.getAttribute();
6389 matcherModel.getAttributeName().add(attName[0]); // attribute
6390 if (attName.length > 1)
6392 matcherModel.getAttributeName().add(attName[1]); // sub-attribute
6395 else if (firstMatcher.isByLabel())
6397 matcherModel.setBy(FilterBy.BY_LABEL);
6399 else if (firstMatcher.isByScore())
6401 matcherModel.setBy(FilterBy.BY_SCORE);
6403 result.setMatchCondition(matcherModel);
6410 * Loads one XML model of a feature filter to a Jalview object
6412 * @param featureType
6413 * @param matcherSetModel
6416 public static FeatureMatcherSetI parseFilter(String featureType,
6417 jalview.xml.binding.jalview.FeatureMatcherSet matcherSetModel)
6419 FeatureMatcherSetI result = new FeatureMatcherSet();
6422 parseFilterConditions(result, matcherSetModel, true);
6423 } catch (IllegalStateException e)
6425 // mixing AND and OR conditions perhaps
6427 String.format("Error reading filter conditions for '%s': %s",
6428 featureType, e.getMessage()));
6429 // return as much as was parsed up to the error
6436 * Adds feature match conditions to matcherSet as unmarshalled from XML
6437 * (possibly recursively for compound conditions)
6440 * @param matcherSetModel
6442 * if true, multiple conditions are AND-ed, else they are OR-ed
6443 * @throws IllegalStateException
6444 * if AND and OR conditions are mixed
6446 protected static void parseFilterConditions(FeatureMatcherSetI matcherSet,
6447 jalview.xml.binding.jalview.FeatureMatcherSet matcherSetModel,
6450 jalview.xml.binding.jalview.FeatureMatcher mc = matcherSetModel
6451 .getMatchCondition();
6457 FilterBy filterBy = mc.getBy();
6458 Condition cond = Condition.fromString(mc.getCondition());
6459 String pattern = mc.getValue();
6460 FeatureMatcherI matchCondition = null;
6461 if (filterBy == FilterBy.BY_LABEL)
6463 matchCondition = FeatureMatcher.byLabel(cond, pattern);
6465 else if (filterBy == FilterBy.BY_SCORE)
6467 matchCondition = FeatureMatcher.byScore(cond, pattern);
6470 else if (filterBy == FilterBy.BY_ATTRIBUTE)
6472 final List<String> attributeName = mc.getAttributeName();
6473 String[] attNames = attributeName
6474 .toArray(new String[attributeName.size()]);
6475 matchCondition = FeatureMatcher.byAttribute(cond, pattern,
6480 * note this throws IllegalStateException if AND-ing to a
6481 * previously OR-ed compound condition, or vice versa
6485 matcherSet.and(matchCondition);
6489 matcherSet.or(matchCondition);
6495 * compound condition
6497 List<jalview.xml.binding.jalview.FeatureMatcherSet> matchers = matcherSetModel
6498 .getCompoundMatcher().getMatcherSet();
6499 boolean anded = matcherSetModel.getCompoundMatcher().isAnd();
6500 if (matchers.size() == 2)
6502 parseFilterConditions(matcherSet, matchers.get(0), anded);
6503 parseFilterConditions(matcherSet, matchers.get(1), anded);
6507 System.err.println("Malformed compound filter condition");
6513 * Loads one XML model of a feature colour to a Jalview object
6515 * @param colourModel
6518 public static FeatureColourI parseColour(Colour colourModel)
6520 FeatureColourI colour = null;
6522 if (colourModel.getMax() != null)
6524 Color mincol = null;
6525 Color maxcol = null;
6526 Color noValueColour = null;
6530 mincol = new Color(Integer.parseInt(colourModel.getMinRGB(), 16));
6531 maxcol = new Color(Integer.parseInt(colourModel.getRGB(), 16));
6532 } catch (Exception e)
6534 Console.warn("Couldn't parse out graduated feature color.", e);
6537 NoValueColour noCol = colourModel.getNoValueColour();
6538 if (noCol == NoValueColour.MIN)
6540 noValueColour = mincol;
6542 else if (noCol == NoValueColour.MAX)
6544 noValueColour = maxcol;
6547 colour = new FeatureColour(maxcol, mincol, maxcol, noValueColour,
6548 safeFloat(colourModel.getMin()),
6549 safeFloat(colourModel.getMax()));
6550 final List<String> attributeName = colourModel.getAttributeName();
6551 String[] attributes = attributeName
6552 .toArray(new String[attributeName.size()]);
6553 if (attributes != null && attributes.length > 0)
6555 colour.setAttributeName(attributes);
6557 if (colourModel.isAutoScale() != null)
6559 colour.setAutoScaled(colourModel.isAutoScale().booleanValue());
6561 if (colourModel.isColourByLabel() != null)
6563 colour.setColourByLabel(
6564 colourModel.isColourByLabel().booleanValue());
6566 if (colourModel.getThreshold() != null)
6568 colour.setThreshold(colourModel.getThreshold().floatValue());
6570 ThresholdType ttyp = colourModel.getThreshType();
6571 if (ttyp == ThresholdType.ABOVE)
6573 colour.setAboveThreshold(true);
6575 else if (ttyp == ThresholdType.BELOW)
6577 colour.setBelowThreshold(true);
6582 Color color = new Color(Integer.parseInt(colourModel.getRGB(), 16));
6583 colour = new FeatureColour(color);
6589 public static void setStateSavedUpToDate(boolean s)
6591 Console.debug("Setting overall stateSavedUpToDate to " + s);
6592 stateSavedUpToDate = s;
6595 public static boolean stateSavedUpToDate()
6597 Console.debug("Returning overall stateSavedUpToDate value: "
6598 + stateSavedUpToDate);
6599 return stateSavedUpToDate;
6602 public static boolean allSavedUpToDate()
6604 if (stateSavedUpToDate()) // nothing happened since last project save
6607 AlignFrame[] frames = Desktop.getAlignFrames();
6610 for (int i = 0; i < frames.length; i++)
6612 if (frames[i] == null)
6614 if (!frames[i].getViewport().savedUpToDate())
6615 return false; // at least one alignment is not individually saved
6621 // used for debugging and tests
6622 private static int debugDelaySave = 20;
6624 public static void setDebugDelaySave(int n)