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;
58 import java.util.Map.Entry;
60 import java.util.Vector;
61 import java.util.jar.JarEntry;
62 import java.util.jar.JarInputStream;
63 import java.util.jar.JarOutputStream;
65 import javax.swing.JInternalFrame;
66 import javax.swing.SwingUtilities;
67 import javax.xml.bind.JAXBContext;
68 import javax.xml.bind.JAXBElement;
69 import javax.xml.bind.Marshaller;
70 import javax.xml.datatype.DatatypeConfigurationException;
71 import javax.xml.datatype.DatatypeFactory;
72 import javax.xml.datatype.XMLGregorianCalendar;
73 import javax.xml.stream.XMLInputFactory;
74 import javax.xml.stream.XMLStreamReader;
76 import jalview.analysis.Conservation;
77 import jalview.analysis.PCA;
78 import jalview.analysis.scoremodels.ScoreModels;
79 import jalview.analysis.scoremodels.SimilarityParams;
80 import jalview.api.FeatureColourI;
81 import jalview.api.ViewStyleI;
82 import jalview.api.analysis.ScoreModelI;
83 import jalview.api.analysis.SimilarityParamsI;
84 import jalview.api.structures.JalviewStructureDisplayI;
85 import jalview.bin.Cache;
86 import jalview.datamodel.AlignedCodonFrame;
87 import jalview.datamodel.Alignment;
88 import jalview.datamodel.AlignmentAnnotation;
89 import jalview.datamodel.AlignmentI;
90 import jalview.datamodel.DBRefEntry;
91 import jalview.datamodel.GeneLocus;
92 import jalview.datamodel.GraphLine;
93 import jalview.datamodel.PDBEntry;
94 import jalview.datamodel.Point;
95 import jalview.datamodel.RnaViewerModel;
96 import jalview.datamodel.SequenceFeature;
97 import jalview.datamodel.SequenceGroup;
98 import jalview.datamodel.SequenceI;
99 import jalview.datamodel.StructureViewerModel;
100 import jalview.datamodel.StructureViewerModel.StructureData;
101 import jalview.datamodel.features.FeatureMatcher;
102 import jalview.datamodel.features.FeatureMatcherI;
103 import jalview.datamodel.features.FeatureMatcherSet;
104 import jalview.datamodel.features.FeatureMatcherSetI;
105 import jalview.ext.varna.RnaModel;
106 import jalview.gui.AlignFrame;
107 import jalview.gui.AlignViewport;
108 import jalview.gui.AlignmentPanel;
109 import jalview.gui.AppVarna;
110 import jalview.gui.Desktop;
111 import jalview.gui.JvOptionPane;
112 import jalview.gui.OOMWarning;
113 import jalview.gui.PCAPanel;
114 import jalview.gui.PaintRefresher;
115 import jalview.gui.SplitFrame;
116 import jalview.gui.StructureViewer;
117 import jalview.gui.StructureViewer.ViewerType;
118 import jalview.gui.StructureViewerBase;
119 import jalview.gui.TreePanel;
120 import jalview.io.BackupFiles;
121 import jalview.io.DataSourceType;
122 import jalview.io.FileFormat;
123 import jalview.io.NewickFile;
124 import jalview.math.Matrix;
125 import jalview.math.MatrixI;
126 import jalview.renderer.ResidueShaderI;
127 import jalview.schemes.AnnotationColourGradient;
128 import jalview.schemes.ColourSchemeI;
129 import jalview.schemes.ColourSchemeProperty;
130 import jalview.schemes.FeatureColour;
131 import jalview.schemes.ResidueProperties;
132 import jalview.schemes.UserColourScheme;
133 import jalview.structure.StructureSelectionManager;
134 import jalview.structures.models.AAStructureBindingModel;
135 import jalview.util.Format;
136 import jalview.util.MessageManager;
137 import jalview.util.Platform;
138 import jalview.util.StringUtils;
139 import jalview.util.jarInputStreamProvider;
140 import jalview.util.matcher.Condition;
141 import jalview.viewmodel.AlignmentViewport;
142 import jalview.viewmodel.PCAModel;
143 import jalview.viewmodel.ViewportRanges;
144 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
145 import jalview.viewmodel.seqfeatures.FeatureRendererSettings;
146 import jalview.viewmodel.seqfeatures.FeaturesDisplayed;
147 import jalview.ws.jws2.Jws2Discoverer;
148 import jalview.ws.jws2.dm.AAConSettings;
149 import jalview.ws.jws2.jabaws2.Jws2Instance;
150 import jalview.ws.params.ArgumentI;
151 import jalview.ws.params.AutoCalcSetting;
152 import jalview.ws.params.WsParamSetI;
153 import jalview.xml.binding.jalview.AlcodonFrame;
154 import jalview.xml.binding.jalview.AlcodonFrame.AlcodMap;
155 import jalview.xml.binding.jalview.Annotation;
156 import jalview.xml.binding.jalview.Annotation.ThresholdLine;
157 import jalview.xml.binding.jalview.AnnotationColourScheme;
158 import jalview.xml.binding.jalview.AnnotationElement;
159 import jalview.xml.binding.jalview.DoubleMatrix;
160 import jalview.xml.binding.jalview.DoubleVector;
161 import jalview.xml.binding.jalview.Feature;
162 import jalview.xml.binding.jalview.Feature.OtherData;
163 import jalview.xml.binding.jalview.FeatureMatcherSet.CompoundMatcher;
164 import jalview.xml.binding.jalview.FilterBy;
165 import jalview.xml.binding.jalview.JalviewModel;
166 import jalview.xml.binding.jalview.JalviewModel.FeatureSettings;
167 import jalview.xml.binding.jalview.JalviewModel.FeatureSettings.Group;
168 import jalview.xml.binding.jalview.JalviewModel.FeatureSettings.Setting;
169 import jalview.xml.binding.jalview.JalviewModel.JGroup;
170 import jalview.xml.binding.jalview.JalviewModel.JSeq;
171 import jalview.xml.binding.jalview.JalviewModel.JSeq.Pdbids;
172 import jalview.xml.binding.jalview.JalviewModel.JSeq.Pdbids.StructureState;
173 import jalview.xml.binding.jalview.JalviewModel.JSeq.RnaViewer;
174 import jalview.xml.binding.jalview.JalviewModel.JSeq.RnaViewer.SecondaryStructure;
175 import jalview.xml.binding.jalview.JalviewModel.PcaViewer;
176 import jalview.xml.binding.jalview.JalviewModel.PcaViewer.Axis;
177 import jalview.xml.binding.jalview.JalviewModel.PcaViewer.SeqPointMax;
178 import jalview.xml.binding.jalview.JalviewModel.PcaViewer.SeqPointMin;
179 import jalview.xml.binding.jalview.JalviewModel.PcaViewer.SequencePoint;
180 import jalview.xml.binding.jalview.JalviewModel.Tree;
181 import jalview.xml.binding.jalview.JalviewModel.UserColours;
182 import jalview.xml.binding.jalview.JalviewModel.Viewport;
183 import jalview.xml.binding.jalview.JalviewModel.Viewport.CalcIdParam;
184 import jalview.xml.binding.jalview.JalviewModel.Viewport.HiddenColumns;
185 import jalview.xml.binding.jalview.JalviewUserColours;
186 import jalview.xml.binding.jalview.JalviewUserColours.Colour;
187 import jalview.xml.binding.jalview.MapListType.MapListFrom;
188 import jalview.xml.binding.jalview.MapListType.MapListTo;
189 import jalview.xml.binding.jalview.Mapping;
190 import jalview.xml.binding.jalview.NoValueColour;
191 import jalview.xml.binding.jalview.ObjectFactory;
192 import jalview.xml.binding.jalview.PcaDataType;
193 import jalview.xml.binding.jalview.Pdbentry.Property;
194 import jalview.xml.binding.jalview.Sequence;
195 import jalview.xml.binding.jalview.Sequence.DBRef;
196 import jalview.xml.binding.jalview.SequenceSet;
197 import jalview.xml.binding.jalview.SequenceSet.SequenceSetProperties;
198 import jalview.xml.binding.jalview.ThresholdType;
199 import jalview.xml.binding.jalview.VAMSAS;
202 * Write out the current jalview desktop state as a Jalview XML stream.
204 * Note: the vamsas objects referred to here are primitive versions of the
205 * VAMSAS project schema elements - they are not the same and most likely never
209 * @version $Revision: 1.134 $
211 public class Jalview2XML
214 // BH 2018 we add the .jvp binary extension to J2S so that
215 // it will declare that binary when we do the file save from the browser
219 Platform.addJ2SBinaryType(".jvp?");
222 private static final String VIEWER_PREFIX = "viewer_";
224 private static final String RNA_PREFIX = "rna_";
226 private static final String UTF_8 = "UTF-8";
229 * prefix for recovering datasets for alignments with multiple views where
230 * non-existent dataset IDs were written for some views
232 private static final String UNIQSEQSETID = "uniqueSeqSetId.";
234 // use this with nextCounter() to make unique names for entities
235 private int counter = 0;
238 * SequenceI reference -> XML ID string in jalview XML. Populated as XML reps
239 * of sequence objects are created.
241 IdentityHashMap<SequenceI, String> seqsToIds = null;
244 * jalview XML Sequence ID to jalview sequence object reference (both dataset
245 * and alignment sequences. Populated as XML reps of sequence objects are
248 Map<String, SequenceI> seqRefIds = null;
250 Map<String, SequenceI> incompleteSeqs = null;
252 List<SeqFref> frefedSequence = null;
254 boolean raiseGUI = true; // whether errors are raised in dialog boxes or not
257 * Map of reconstructed AlignFrame objects that appear to have come from
258 * SplitFrame objects (have a dna/protein complement view).
260 private Map<Viewport, AlignFrame> splitFrameCandidates = new HashMap<>();
263 * Map from displayed rna structure models to their saved session state jar
266 private Map<RnaModel, String> rnaSessions = new HashMap<>();
269 * A helper method for safely using the value of an optional attribute that
270 * may be null if not present in the XML. Answers the boolean value, or false
276 public static boolean safeBoolean(Boolean b)
278 return b == null ? false : b.booleanValue();
282 * A helper method for safely using the value of an optional attribute that
283 * may be null if not present in the XML. Answers the integer value, or zero
289 public static int safeInt(Integer i)
291 return i == null ? 0 : i.intValue();
295 * A helper method for safely using the value of an optional attribute that
296 * may be null if not present in the XML. Answers the float value, or zero if
302 public static float safeFloat(Float f)
304 return f == null ? 0f : f.floatValue();
308 * create/return unique hash string for sq
311 * @return new or existing unique string for sq
313 String seqHash(SequenceI sq)
315 if (seqsToIds == null)
319 if (seqsToIds.containsKey(sq))
321 return seqsToIds.get(sq);
325 // create sequential key
326 String key = "sq" + (seqsToIds.size() + 1);
327 key = makeHashCode(sq, key); // check we don't have an external reference
329 seqsToIds.put(sq, key);
336 if (seqsToIds == null)
338 seqsToIds = new IdentityHashMap<>();
340 if (seqRefIds == null)
342 seqRefIds = new HashMap<>();
344 if (incompleteSeqs == null)
346 incompleteSeqs = new HashMap<>();
348 if (frefedSequence == null)
350 frefedSequence = new ArrayList<>();
358 public Jalview2XML(boolean raiseGUI)
360 this.raiseGUI = raiseGUI;
364 * base class for resolving forward references to sequences by their ID
369 abstract class SeqFref
375 public SeqFref(String _sref, String type)
381 public String getSref()
386 public SequenceI getSrefSeq()
388 return seqRefIds.get(sref);
391 public boolean isResolvable()
393 return seqRefIds.get(sref) != null;
396 public SequenceI getSrefDatasetSeq()
398 SequenceI sq = seqRefIds.get(sref);
401 while (sq.getDatasetSequence() != null)
403 sq = sq.getDatasetSequence();
410 * @return true if the forward reference was fully resolved
412 abstract boolean resolve();
415 public String toString()
417 return type + " reference to " + sref;
422 * create forward reference for a mapping
428 public SeqFref newMappingRef(final String sref,
429 final jalview.datamodel.Mapping _jmap)
431 SeqFref fref = new SeqFref(sref, "Mapping")
433 public jalview.datamodel.Mapping jmap = _jmap;
438 SequenceI seq = getSrefDatasetSeq();
450 public SeqFref newAlcodMapRef(final String sref,
451 final AlignedCodonFrame _cf,
452 final jalview.datamodel.Mapping _jmap)
455 SeqFref fref = new SeqFref(sref, "Codon Frame")
457 AlignedCodonFrame cf = _cf;
459 public jalview.datamodel.Mapping mp = _jmap;
462 public boolean isResolvable()
464 return super.isResolvable() && mp.getTo() != null;
470 SequenceI seq = getSrefDatasetSeq();
475 cf.addMap(seq, mp.getTo(), mp.getMap());
482 public void resolveFrefedSequences()
484 Iterator<SeqFref> nextFref = frefedSequence.iterator();
485 int toresolve = frefedSequence.size();
486 int unresolved = 0, failedtoresolve = 0;
487 while (nextFref.hasNext())
489 SeqFref ref = nextFref.next();
490 if (ref.isResolvable())
502 } catch (Exception x)
505 "IMPLEMENTATION ERROR: Failed to resolve forward reference for sequence "
518 System.err.println("Jalview Project Import: There were " + unresolved
519 + " forward references left unresolved on the stack.");
521 if (failedtoresolve > 0)
523 System.err.println("SERIOUS! " + failedtoresolve
524 + " resolvable forward references failed to resolve.");
526 if (incompleteSeqs != null && incompleteSeqs.size() > 0)
529 "Jalview Project Import: There are " + incompleteSeqs.size()
530 + " sequences which may have incomplete metadata.");
531 if (incompleteSeqs.size() < 10)
533 for (SequenceI s : incompleteSeqs.values())
535 System.err.println(s.toString());
541 "Too many to report. Skipping output of incomplete sequences.");
547 * This maintains a map of viewports, the key being the seqSetId. Important to
548 * set historyItem and redoList for multiple views
550 Map<String, AlignViewport> viewportsAdded = new HashMap<>();
552 Map<String, AlignmentAnnotation> annotationIds = new HashMap<>();
554 String uniqueSetSuffix = "";
557 * List of pdbfiles added to Jar
559 List<String> pdbfiles = null;
561 // SAVES SEVERAL ALIGNMENT WINDOWS TO SAME JARFILE
562 public void saveState(File statefile)
564 FileOutputStream fos = null;
569 fos = new FileOutputStream(statefile);
571 JarOutputStream jout = new JarOutputStream(fos);
575 } catch (Exception e)
577 Cache.log.error("Couln't write Jalview state to " + statefile, e);
578 // TODO: inform user of the problem - they need to know if their data was
580 if (errorMessage == null)
582 errorMessage = "Did't write Jalview Archive to output file '"
583 + statefile + "' - See console error log for details";
587 errorMessage += "(Didn't write Jalview Archive to output file '"
598 } catch (IOException e)
608 * Writes a jalview project archive to the given Jar output stream.
612 public void saveState(JarOutputStream jout)
614 AlignFrame[] frames = Desktop.getAlignFrames();
620 saveAllFrames(Arrays.asList(frames), jout);
624 * core method for storing state for a set of AlignFrames.
627 * - frames involving all data to be exported (including containing
630 * - project output stream
632 private void saveAllFrames(List<AlignFrame> frames, JarOutputStream jout)
634 Hashtable<String, AlignFrame> dsses = new Hashtable<>();
637 * ensure cached data is clear before starting
639 // todo tidy up seqRefIds, seqsToIds initialisation / reset
641 splitFrameCandidates.clear();
646 // NOTE UTF-8 MUST BE USED FOR WRITING UNICODE CHARS
647 // //////////////////////////////////////////////////
649 List<String> shortNames = new ArrayList<>();
650 List<String> viewIds = new ArrayList<>();
653 for (int i = frames.size() - 1; i > -1; i--)
655 AlignFrame af = frames.get(i);
657 if (skipList != null && skipList
658 .containsKey(af.getViewport().getSequenceSetId()))
663 String shortName = makeFilename(af, shortNames);
665 int apSize = af.getAlignPanels().size();
667 for (int ap = 0; ap < apSize; ap++)
669 AlignmentPanel apanel = (AlignmentPanel) af.getAlignPanels()
671 String fileName = apSize == 1 ? shortName : ap + shortName;
672 if (!fileName.endsWith(".xml"))
674 fileName = fileName + ".xml";
677 saveState(apanel, fileName, jout, viewIds);
679 String dssid = getDatasetIdRef(
680 af.getViewport().getAlignment().getDataset());
681 if (!dsses.containsKey(dssid))
683 dsses.put(dssid, af);
688 writeDatasetFor(dsses, "" + jout.hashCode() + " " + uniqueSetSuffix,
694 } catch (Exception foo)
698 } catch (Exception ex)
700 // TODO: inform user of the problem - they need to know if their data was
702 if (errorMessage == null)
704 errorMessage = "Couldn't write Jalview Archive - see error output for details";
706 ex.printStackTrace();
711 * Generates a distinct file name, based on the title of the AlignFrame, by
712 * appending _n for increasing n until an unused name is generated. The new
713 * name (without its extension) is added to the list.
717 * @return the generated name, with .xml extension
719 protected String makeFilename(AlignFrame af, List<String> namesUsed)
721 String shortName = af.getTitle();
723 if (shortName.indexOf(File.separatorChar) > -1)
725 shortName = shortName
726 .substring(shortName.lastIndexOf(File.separatorChar) + 1);
731 while (namesUsed.contains(shortName))
733 if (shortName.endsWith("_" + (count - 1)))
735 shortName = shortName.substring(0, shortName.lastIndexOf("_"));
738 shortName = shortName.concat("_" + count);
742 namesUsed.add(shortName);
744 if (!shortName.endsWith(".xml"))
746 shortName = shortName + ".xml";
751 // USE THIS METHOD TO SAVE A SINGLE ALIGNMENT WINDOW
752 public boolean saveAlignment(AlignFrame af, String jarFile,
757 // create backupfiles object and get new temp filename destination
758 boolean doBackup = BackupFiles.getEnabled();
759 BackupFiles backupfiles = doBackup ? new BackupFiles(jarFile) : null;
760 FileOutputStream fos = new FileOutputStream(doBackup ?
761 backupfiles.getTempFilePath() : jarFile);
763 JarOutputStream jout = new JarOutputStream(fos);
764 List<AlignFrame> frames = new ArrayList<>();
766 // resolve splitframes
767 if (af.getViewport().getCodingComplement() != null)
769 frames = ((SplitFrame) af.getSplitViewContainer()).getAlignFrames();
775 saveAllFrames(frames, jout);
779 } catch (Exception foo)
783 boolean success = true;
787 backupfiles.setWriteSuccess(success);
788 success = backupfiles.rollBackupsAndRenameTempFile();
792 } catch (Exception ex)
794 errorMessage = "Couldn't Write alignment view to Jalview Archive - see error output for details";
795 ex.printStackTrace();
800 private void writeDatasetFor(Hashtable<String, AlignFrame> dsses,
801 String fileName, JarOutputStream jout)
804 for (String dssids : dsses.keySet())
806 AlignFrame _af = dsses.get(dssids);
807 String jfileName = fileName + " Dataset for " + _af.getTitle();
808 if (!jfileName.endsWith(".xml"))
810 jfileName = jfileName + ".xml";
812 saveState(_af.alignPanel, jfileName, true, jout, null);
817 * create a JalviewModel from an alignment view and marshall it to a
821 * panel to create jalview model for
823 * name of alignment panel written to output stream
830 public JalviewModel saveState(AlignmentPanel ap, String fileName,
831 JarOutputStream jout, List<String> viewIds)
833 return saveState(ap, fileName, false, jout, viewIds);
837 * create a JalviewModel from an alignment view and marshall it to a
841 * panel to create jalview model for
843 * name of alignment panel written to output stream
845 * when true, only write the dataset for the alignment, not the data
846 * associated with the view.
852 public JalviewModel saveState(AlignmentPanel ap, String fileName,
853 boolean storeDS, JarOutputStream jout, List<String> viewIds)
857 viewIds = new ArrayList<>();
862 List<UserColourScheme> userColours = new ArrayList<>();
864 AlignViewport av = ap.av;
865 ViewportRanges vpRanges = av.getRanges();
867 final ObjectFactory objectFactory = new ObjectFactory();
868 JalviewModel object = objectFactory.createJalviewModel();
869 object.setVamsasModel(new VAMSAS());
871 // object.setCreationDate(new java.util.Date(System.currentTimeMillis()));
874 GregorianCalendar c = new GregorianCalendar();
875 DatatypeFactory datatypeFactory = DatatypeFactory.newInstance();
876 XMLGregorianCalendar now = datatypeFactory.newXMLGregorianCalendar(c);// gregorianCalendar);
877 object.setCreationDate(now);
878 } catch (DatatypeConfigurationException e)
880 System.err.println("error writing date: " + e.toString());
883 jalview.bin.Cache.getDefault("VERSION", "Development Build"));
886 * rjal is full height alignment, jal is actual alignment with full metadata
887 * but excludes hidden sequences.
889 jalview.datamodel.AlignmentI rjal = av.getAlignment(), jal = rjal;
891 if (av.hasHiddenRows())
893 rjal = jal.getHiddenSequences().getFullAlignment();
896 SequenceSet vamsasSet = new SequenceSet();
898 // JalviewModelSequence jms = new JalviewModelSequence();
900 vamsasSet.setGapChar(jal.getGapCharacter() + "");
902 if (jal.getDataset() != null)
904 // dataset id is the dataset's hashcode
905 vamsasSet.setDatasetId(getDatasetIdRef(jal.getDataset()));
908 // switch jal and the dataset
909 jal = jal.getDataset();
913 if (jal.getProperties() != null)
915 Enumeration en = jal.getProperties().keys();
916 while (en.hasMoreElements())
918 String key = en.nextElement().toString();
919 SequenceSetProperties ssp = new SequenceSetProperties();
921 ssp.setValue(jal.getProperties().get(key).toString());
922 // vamsasSet.addSequenceSetProperties(ssp);
923 vamsasSet.getSequenceSetProperties().add(ssp);
928 Set<String> calcIdSet = new HashSet<>();
929 // record the set of vamsas sequence XML POJO we create.
930 HashMap<String, Sequence> vamsasSetIds = new HashMap<>();
932 for (final SequenceI jds : rjal.getSequences())
934 final SequenceI jdatasq = jds.getDatasetSequence() == null ? jds
935 : jds.getDatasetSequence();
936 String id = seqHash(jds);
937 if (vamsasSetIds.get(id) == null)
939 if (seqRefIds.get(id) != null && !storeDS)
941 // This happens for two reasons: 1. multiple views are being
943 // 2. the hashCode has collided with another sequence's code. This
945 // HAPPEN! (PF00072.15.stk does this)
946 // JBPNote: Uncomment to debug writing out of files that do not read
947 // back in due to ArrayOutOfBoundExceptions.
948 // System.err.println("vamsasSeq backref: "+id+"");
949 // System.err.println(jds.getName()+"
950 // "+jds.getStart()+"-"+jds.getEnd()+" "+jds.getSequenceAsString());
951 // System.err.println("Hashcode: "+seqHash(jds));
952 // SequenceI rsq = (SequenceI) seqRefIds.get(id + "");
953 // System.err.println(rsq.getName()+"
954 // "+rsq.getStart()+"-"+rsq.getEnd()+" "+rsq.getSequenceAsString());
955 // System.err.println("Hashcode: "+seqHash(rsq));
959 vamsasSeq = createVamsasSequence(id, jds);
960 // vamsasSet.addSequence(vamsasSeq);
961 vamsasSet.getSequence().add(vamsasSeq);
962 vamsasSetIds.put(id, vamsasSeq);
963 seqRefIds.put(id, jds);
967 jseq.setStart(jds.getStart());
968 jseq.setEnd(jds.getEnd());
969 jseq.setColour(av.getSequenceColour(jds).getRGB());
971 jseq.setId(id); // jseq id should be a string not a number
974 // Store any sequences this sequence represents
975 if (av.hasHiddenRows())
977 // use rjal, contains the full height alignment
979 av.getAlignment().getHiddenSequences().isHidden(jds));
981 if (av.isHiddenRepSequence(jds))
983 jalview.datamodel.SequenceI[] reps = av
984 .getRepresentedSequences(jds).getSequencesInOrder(rjal);
986 for (int h = 0; h < reps.length; h++)
990 // jseq.addHiddenSequences(rjal.findIndex(reps[h]));
991 jseq.getHiddenSequences().add(rjal.findIndex(reps[h]));
996 // mark sequence as reference - if it is the reference for this view
999 jseq.setViewreference(jds == jal.getSeqrep());
1003 // TODO: omit sequence features from each alignment view's XML dump if we
1004 // are storing dataset
1005 List<SequenceFeature> sfs = jds.getSequenceFeatures();
1006 for (SequenceFeature sf : sfs)
1008 // Features features = new Features();
1009 Feature features = new Feature();
1011 features.setBegin(sf.getBegin());
1012 features.setEnd(sf.getEnd());
1013 features.setDescription(sf.getDescription());
1014 features.setType(sf.getType());
1015 features.setFeatureGroup(sf.getFeatureGroup());
1016 features.setScore(sf.getScore());
1017 if (sf.links != null)
1019 for (int l = 0; l < sf.links.size(); l++)
1021 OtherData keyValue = new OtherData();
1022 keyValue.setKey("LINK_" + l);
1023 keyValue.setValue(sf.links.elementAt(l).toString());
1024 // features.addOtherData(keyValue);
1025 features.getOtherData().add(keyValue);
1028 if (sf.otherDetails != null)
1031 * save feature attributes, which may be simple strings or
1032 * map valued (have sub-attributes)
1034 for (Entry<String, Object> entry : sf.otherDetails.entrySet())
1036 String key = entry.getKey();
1037 Object value = entry.getValue();
1038 if (value instanceof Map<?, ?>)
1040 for (Entry<String, Object> subAttribute : ((Map<String, Object>) value)
1043 OtherData otherData = new OtherData();
1044 otherData.setKey(key);
1045 otherData.setKey2(subAttribute.getKey());
1046 otherData.setValue(subAttribute.getValue().toString());
1047 // features.addOtherData(otherData);
1048 features.getOtherData().add(otherData);
1053 OtherData otherData = new OtherData();
1054 otherData.setKey(key);
1055 otherData.setValue(value.toString());
1056 // features.addOtherData(otherData);
1057 features.getOtherData().add(otherData);
1062 // jseq.addFeatures(features);
1063 jseq.getFeatures().add(features);
1066 if (jdatasq.getAllPDBEntries() != null)
1068 Enumeration<PDBEntry> en = jdatasq.getAllPDBEntries().elements();
1069 while (en.hasMoreElements())
1071 Pdbids pdb = new Pdbids();
1072 jalview.datamodel.PDBEntry entry = en.nextElement();
1074 String pdbId = entry.getId();
1076 pdb.setType(entry.getType());
1079 * Store any structure views associated with this sequence. This
1080 * section copes with duplicate entries in the project, so a dataset
1081 * only view *should* be coped with sensibly.
1083 // This must have been loaded, is it still visible?
1084 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
1085 String matchedFile = null;
1086 for (int f = frames.length - 1; f > -1; f--)
1088 if (frames[f] instanceof StructureViewerBase)
1090 StructureViewerBase viewFrame = (StructureViewerBase) frames[f];
1091 matchedFile = saveStructureViewer(ap, jds, pdb, entry, viewIds,
1092 matchedFile, viewFrame);
1094 * Only store each structure viewer's state once in the project
1095 * jar. First time through only (storeDS==false)
1097 String viewId = viewFrame.getViewId();
1098 String viewerType = viewFrame.getViewerType().toString();
1099 if (!storeDS && !viewIds.contains(viewId))
1101 viewIds.add(viewId);
1102 File viewerState = viewFrame.saveSession();
1103 if (viewerState != null)
1105 copyFileToJar(jout, viewerState.getPath(),
1106 getViewerJarEntryName(viewId), viewerType);
1110 Cache.log.error("Failed to save viewer state for "
1118 if (matchedFile != null || entry.getFile() != null)
1120 if (entry.getFile() != null)
1123 matchedFile = entry.getFile();
1125 pdb.setFile(matchedFile); // entry.getFile());
1126 if (pdbfiles == null)
1128 pdbfiles = new ArrayList<>();
1131 if (!pdbfiles.contains(pdbId))
1133 pdbfiles.add(pdbId);
1134 copyFileToJar(jout, matchedFile, pdbId, pdbId);
1138 Enumeration<String> props = entry.getProperties();
1139 if (props.hasMoreElements())
1141 // PdbentryItem item = new PdbentryItem();
1142 while (props.hasMoreElements())
1144 Property prop = new Property();
1145 String key = props.nextElement();
1147 prop.setValue(entry.getProperty(key).toString());
1148 // item.addProperty(prop);
1149 pdb.getProperty().add(prop);
1151 // pdb.addPdbentryItem(item);
1154 // jseq.addPdbids(pdb);
1155 jseq.getPdbids().add(pdb);
1159 saveRnaViewers(jout, jseq, jds, viewIds, ap, storeDS);
1161 // jms.addJSeq(jseq);
1162 object.getJSeq().add(jseq);
1165 if (!storeDS && av.hasHiddenRows())
1167 jal = av.getAlignment();
1171 if (storeDS && jal.getCodonFrames() != null)
1173 List<AlignedCodonFrame> jac = jal.getCodonFrames();
1174 for (AlignedCodonFrame acf : jac)
1176 AlcodonFrame alc = new AlcodonFrame();
1177 if (acf.getProtMappings() != null
1178 && acf.getProtMappings().length > 0)
1180 boolean hasMap = false;
1181 SequenceI[] dnas = acf.getdnaSeqs();
1182 jalview.datamodel.Mapping[] pmaps = acf.getProtMappings();
1183 for (int m = 0; m < pmaps.length; m++)
1185 AlcodMap alcmap = new AlcodMap();
1186 alcmap.setDnasq(seqHash(dnas[m]));
1188 createVamsasMapping(pmaps[m], dnas[m], null, false));
1189 // alc.addAlcodMap(alcmap);
1190 alc.getAlcodMap().add(alcmap);
1195 // vamsasSet.addAlcodonFrame(alc);
1196 vamsasSet.getAlcodonFrame().add(alc);
1199 // TODO: delete this ? dead code from 2.8.3->2.9 ?
1201 // AlcodonFrame alc = new AlcodonFrame();
1202 // vamsasSet.addAlcodonFrame(alc);
1203 // for (int p = 0; p < acf.aaWidth; p++)
1205 // Alcodon cmap = new Alcodon();
1206 // if (acf.codons[p] != null)
1208 // // Null codons indicate a gapped column in the translated peptide
1210 // cmap.setPos1(acf.codons[p][0]);
1211 // cmap.setPos2(acf.codons[p][1]);
1212 // cmap.setPos3(acf.codons[p][2]);
1214 // alc.addAlcodon(cmap);
1216 // if (acf.getProtMappings() != null
1217 // && acf.getProtMappings().length > 0)
1219 // SequenceI[] dnas = acf.getdnaSeqs();
1220 // jalview.datamodel.Mapping[] pmaps = acf.getProtMappings();
1221 // for (int m = 0; m < pmaps.length; m++)
1223 // AlcodMap alcmap = new AlcodMap();
1224 // alcmap.setDnasq(seqHash(dnas[m]));
1225 // alcmap.setMapping(createVamsasMapping(pmaps[m], dnas[m], null,
1227 // alc.addAlcodMap(alcmap);
1234 // /////////////////////////////////
1235 if (!storeDS && av.getCurrentTree() != null)
1237 // FIND ANY ASSOCIATED TREES
1238 // NOT IMPLEMENTED FOR HEADLESS STATE AT PRESENT
1239 if (Desktop.desktop != null)
1241 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
1243 for (int t = 0; t < frames.length; t++)
1245 if (frames[t] instanceof TreePanel)
1247 TreePanel tp = (TreePanel) frames[t];
1249 if (tp.getTreeCanvas().getViewport().getAlignment() == jal)
1251 JalviewModel.Tree tree = new JalviewModel.Tree();
1252 tree.setTitle(tp.getTitle());
1253 tree.setCurrentTree((av.getCurrentTree() == tp.getTree()));
1254 tree.setNewick(tp.getTree().print());
1255 tree.setThreshold(tp.getTreeCanvas().getThreshold());
1257 tree.setFitToWindow(tp.fitToWindow.getState());
1258 tree.setFontName(tp.getTreeFont().getName());
1259 tree.setFontSize(tp.getTreeFont().getSize());
1260 tree.setFontStyle(tp.getTreeFont().getStyle());
1261 tree.setMarkUnlinked(tp.placeholdersMenu.getState());
1263 tree.setShowBootstrap(tp.bootstrapMenu.getState());
1264 tree.setShowDistances(tp.distanceMenu.getState());
1266 tree.setHeight(tp.getHeight());
1267 tree.setWidth(tp.getWidth());
1268 tree.setXpos(tp.getX());
1269 tree.setYpos(tp.getY());
1270 tree.setId(makeHashCode(tp, null));
1271 tree.setLinkToAllViews(
1272 tp.getTreeCanvas().isApplyToAllViews());
1274 // jms.addTree(tree);
1275 object.getTree().add(tree);
1285 if (!storeDS && Desktop.desktop != null)
1287 for (JInternalFrame frame : Desktop.desktop.getAllFrames())
1289 if (frame instanceof PCAPanel)
1291 PCAPanel panel = (PCAPanel) frame;
1292 if (panel.getAlignViewport().getAlignment() == jal)
1294 savePCA(panel, object);
1302 * store forward refs from an annotationRow to any groups
1304 IdentityHashMap<SequenceGroup, String> groupRefs = new IdentityHashMap<>();
1307 for (SequenceI sq : jal.getSequences())
1309 // Store annotation on dataset sequences only
1310 AlignmentAnnotation[] aa = sq.getAnnotation();
1311 if (aa != null && aa.length > 0)
1313 storeAlignmentAnnotation(aa, groupRefs, av, calcIdSet, storeDS,
1320 if (jal.getAlignmentAnnotation() != null)
1322 // Store the annotation shown on the alignment.
1323 AlignmentAnnotation[] aa = jal.getAlignmentAnnotation();
1324 storeAlignmentAnnotation(aa, groupRefs, av, calcIdSet, storeDS,
1329 if (jal.getGroups() != null)
1331 JGroup[] groups = new JGroup[jal.getGroups().size()];
1333 for (jalview.datamodel.SequenceGroup sg : jal.getGroups())
1335 JGroup jGroup = new JGroup();
1336 groups[++i] = jGroup;
1338 jGroup.setStart(sg.getStartRes());
1339 jGroup.setEnd(sg.getEndRes());
1340 jGroup.setName(sg.getName());
1341 if (groupRefs.containsKey(sg))
1343 // group has references so set its ID field
1344 jGroup.setId(groupRefs.get(sg));
1346 ColourSchemeI colourScheme = sg.getColourScheme();
1347 if (colourScheme != null)
1349 ResidueShaderI groupColourScheme = sg.getGroupColourScheme();
1350 if (groupColourScheme.conservationApplied())
1352 jGroup.setConsThreshold(groupColourScheme.getConservationInc());
1354 if (colourScheme instanceof jalview.schemes.UserColourScheme)
1357 setUserColourScheme(colourScheme, userColours,
1362 jGroup.setColour(colourScheme.getSchemeName());
1365 else if (colourScheme instanceof jalview.schemes.AnnotationColourGradient)
1367 jGroup.setColour("AnnotationColourGradient");
1368 jGroup.setAnnotationColours(constructAnnotationColours(
1369 (jalview.schemes.AnnotationColourGradient) colourScheme,
1370 userColours, object));
1372 else if (colourScheme instanceof jalview.schemes.UserColourScheme)
1375 setUserColourScheme(colourScheme, userColours, object));
1379 jGroup.setColour(colourScheme.getSchemeName());
1382 jGroup.setPidThreshold(groupColourScheme.getThreshold());
1385 jGroup.setOutlineColour(sg.getOutlineColour().getRGB());
1386 jGroup.setDisplayBoxes(sg.getDisplayBoxes());
1387 jGroup.setDisplayText(sg.getDisplayText());
1388 jGroup.setColourText(sg.getColourText());
1389 jGroup.setTextCol1(sg.textColour.getRGB());
1390 jGroup.setTextCol2(sg.textColour2.getRGB());
1391 jGroup.setTextColThreshold(sg.thresholdTextColour);
1392 jGroup.setShowUnconserved(sg.getShowNonconserved());
1393 jGroup.setIgnoreGapsinConsensus(sg.getIgnoreGapsConsensus());
1394 jGroup.setShowConsensusHistogram(sg.isShowConsensusHistogram());
1395 jGroup.setShowSequenceLogo(sg.isShowSequenceLogo());
1396 jGroup.setNormaliseSequenceLogo(sg.isNormaliseSequenceLogo());
1397 for (SequenceI seq : sg.getSequences())
1399 // jGroup.addSeq(seqHash(seq));
1400 jGroup.getSeq().add(seqHash(seq));
1404 //jms.setJGroup(groups);
1406 for (JGroup grp : groups)
1408 object.getJGroup().add(grp);
1413 // /////////SAVE VIEWPORT
1414 Viewport view = new Viewport();
1415 view.setTitle(ap.alignFrame.getTitle());
1416 view.setSequenceSetId(
1417 makeHashCode(av.getSequenceSetId(), av.getSequenceSetId()));
1418 view.setId(av.getViewId());
1419 if (av.getCodingComplement() != null)
1421 view.setComplementId(av.getCodingComplement().getViewId());
1423 view.setViewName(av.getViewName());
1424 view.setGatheredViews(av.isGatherViewsHere());
1426 Rectangle size = ap.av.getExplodedGeometry();
1427 Rectangle position = size;
1430 size = ap.alignFrame.getBounds();
1431 if (av.getCodingComplement() != null)
1433 position = ((SplitFrame) ap.alignFrame.getSplitViewContainer())
1441 view.setXpos(position.x);
1442 view.setYpos(position.y);
1444 view.setWidth(size.width);
1445 view.setHeight(size.height);
1447 view.setStartRes(vpRanges.getStartRes());
1448 view.setStartSeq(vpRanges.getStartSeq());
1450 if (av.getGlobalColourScheme() instanceof jalview.schemes.UserColourScheme)
1452 view.setBgColour(setUserColourScheme(av.getGlobalColourScheme(),
1453 userColours, object));
1456 .getGlobalColourScheme() instanceof jalview.schemes.AnnotationColourGradient)
1458 AnnotationColourScheme ac = constructAnnotationColours(
1459 (jalview.schemes.AnnotationColourGradient) av
1460 .getGlobalColourScheme(),
1461 userColours, object);
1463 view.setAnnotationColours(ac);
1464 view.setBgColour("AnnotationColourGradient");
1468 view.setBgColour(ColourSchemeProperty
1469 .getColourName(av.getGlobalColourScheme()));
1472 ResidueShaderI vcs = av.getResidueShading();
1473 ColourSchemeI cs = av.getGlobalColourScheme();
1477 if (vcs.conservationApplied())
1479 view.setConsThreshold(vcs.getConservationInc());
1480 if (cs instanceof jalview.schemes.UserColourScheme)
1482 view.setBgColour(setUserColourScheme(cs, userColours, object));
1485 view.setPidThreshold(vcs.getThreshold());
1488 view.setConservationSelected(av.getConservationSelected());
1489 view.setPidSelected(av.getAbovePIDThreshold());
1490 final Font font = av.getFont();
1491 view.setFontName(font.getName());
1492 view.setFontSize(font.getSize());
1493 view.setFontStyle(font.getStyle());
1494 view.setScaleProteinAsCdna(av.getViewStyle().isScaleProteinAsCdna());
1495 view.setRenderGaps(av.isRenderGaps());
1496 view.setShowAnnotation(av.isShowAnnotation());
1497 view.setShowBoxes(av.getShowBoxes());
1498 view.setShowColourText(av.getColourText());
1499 view.setShowFullId(av.getShowJVSuffix());
1500 view.setRightAlignIds(av.isRightAlignIds());
1501 view.setShowSequenceFeatures(av.isShowSequenceFeatures());
1502 view.setShowText(av.getShowText());
1503 view.setShowUnconserved(av.getShowUnconserved());
1504 view.setWrapAlignment(av.getWrapAlignment());
1505 view.setTextCol1(av.getTextColour().getRGB());
1506 view.setTextCol2(av.getTextColour2().getRGB());
1507 view.setTextColThreshold(av.getThresholdTextColour());
1508 view.setShowConsensusHistogram(av.isShowConsensusHistogram());
1509 view.setShowSequenceLogo(av.isShowSequenceLogo());
1510 view.setNormaliseSequenceLogo(av.isNormaliseSequenceLogo());
1511 view.setShowGroupConsensus(av.isShowGroupConsensus());
1512 view.setShowGroupConservation(av.isShowGroupConservation());
1513 view.setShowNPfeatureTooltip(av.isShowNPFeats());
1514 view.setShowDbRefTooltip(av.isShowDBRefs());
1515 view.setFollowHighlight(av.isFollowHighlight());
1516 view.setFollowSelection(av.followSelection);
1517 view.setIgnoreGapsinConsensus(av.isIgnoreGapsConsensus());
1518 view.setShowComplementFeatures(av.isShowComplementFeatures());
1519 view.setShowComplementFeaturesOnTop(
1520 av.isShowComplementFeaturesOnTop());
1521 if (av.getFeaturesDisplayed() != null)
1523 FeatureSettings fs = new FeatureSettings();
1525 FeatureRendererModel fr = ap.getSeqPanel().seqCanvas
1526 .getFeatureRenderer();
1527 String[] renderOrder = fr.getRenderOrder().toArray(new String[0]);
1529 Vector<String> settingsAdded = new Vector<>();
1530 if (renderOrder != null)
1532 for (String featureType : renderOrder)
1534 FeatureSettings.Setting setting = new FeatureSettings.Setting();
1535 setting.setType(featureType);
1538 * save any filter for the feature type
1540 FeatureMatcherSetI filter = fr.getFeatureFilter(featureType);
1541 if (filter != null) {
1542 Iterator<FeatureMatcherI> filters = filter.getMatchers().iterator();
1543 FeatureMatcherI firstFilter = filters.next();
1544 setting.setMatcherSet(Jalview2XML.marshalFilter(
1545 firstFilter, filters, filter.isAnded()));
1549 * save colour scheme for the feature type
1551 FeatureColourI fcol = fr.getFeatureStyle(featureType);
1552 if (!fcol.isSimpleColour())
1554 setting.setColour(fcol.getMaxColour().getRGB());
1555 setting.setMincolour(fcol.getMinColour().getRGB());
1556 setting.setMin(fcol.getMin());
1557 setting.setMax(fcol.getMax());
1558 setting.setColourByLabel(fcol.isColourByLabel());
1559 if (fcol.isColourByAttribute())
1561 String[] attName = fcol.getAttributeName();
1562 setting.getAttributeName().add(attName[0]);
1563 if (attName.length > 1)
1565 setting.getAttributeName().add(attName[1]);
1568 setting.setAutoScale(fcol.isAutoScaled());
1569 setting.setThreshold(fcol.getThreshold());
1570 Color noColour = fcol.getNoColour();
1571 if (noColour == null)
1573 setting.setNoValueColour(NoValueColour.NONE);
1575 else if (noColour.equals(fcol.getMaxColour()))
1577 setting.setNoValueColour(NoValueColour.MAX);
1581 setting.setNoValueColour(NoValueColour.MIN);
1583 // -1 = No threshold, 0 = Below, 1 = Above
1584 setting.setThreshstate(fcol.isAboveThreshold() ? 1
1585 : (fcol.isBelowThreshold() ? 0 : -1));
1589 setting.setColour(fcol.getColour().getRGB());
1593 av.getFeaturesDisplayed().isVisible(featureType));
1595 .getOrder(featureType);
1598 setting.setOrder(rorder);
1600 /// fs.addSetting(setting);
1601 fs.getSetting().add(setting);
1602 settingsAdded.addElement(featureType);
1606 // is groups actually supposed to be a map here ?
1607 Iterator<String> en = fr.getFeatureGroups().iterator();
1608 Vector<String> groupsAdded = new Vector<>();
1609 while (en.hasNext())
1611 String grp = en.next();
1612 if (groupsAdded.contains(grp))
1616 Group g = new Group();
1618 g.setDisplay(((Boolean) fr.checkGroupVisibility(grp, false))
1621 fs.getGroup().add(g);
1622 groupsAdded.addElement(grp);
1624 // jms.setFeatureSettings(fs);
1625 object.setFeatureSettings(fs);
1628 if (av.hasHiddenColumns())
1630 jalview.datamodel.HiddenColumns hidden = av.getAlignment()
1631 .getHiddenColumns();
1634 warn("REPORT BUG: avoided null columnselection bug (DMAM reported). Please contact Jim about this.");
1638 Iterator<int[]> hiddenRegions = hidden.iterator();
1639 while (hiddenRegions.hasNext())
1641 int[] region = hiddenRegions.next();
1642 HiddenColumns hc = new HiddenColumns();
1643 hc.setStart(region[0]);
1644 hc.setEnd(region[1]);
1645 // view.addHiddenColumns(hc);
1646 view.getHiddenColumns().add(hc);
1650 if (calcIdSet.size() > 0)
1652 for (String calcId : calcIdSet)
1654 if (calcId.trim().length() > 0)
1656 CalcIdParam cidp = createCalcIdParam(calcId, av);
1657 // Some calcIds have no parameters.
1660 // view.addCalcIdParam(cidp);
1661 view.getCalcIdParam().add(cidp);
1667 // jms.addViewport(view);
1668 object.getViewport().add(view);
1670 // object.setJalviewModelSequence(jms);
1671 // object.getVamsasModel().addSequenceSet(vamsasSet);
1672 object.getVamsasModel().getSequenceSet().add(vamsasSet);
1674 if (jout != null && fileName != null)
1676 // We may not want to write the object to disk,
1677 // eg we can copy the alignViewport to a new view object
1678 // using save and then load
1681 fileName = fileName.replace('\\', '/');
1682 System.out.println("Writing jar entry " + fileName);
1683 JarEntry entry = new JarEntry(fileName);
1684 jout.putNextEntry(entry);
1685 PrintWriter pout = new PrintWriter(
1686 new OutputStreamWriter(jout, UTF_8));
1687 JAXBContext jaxbContext = JAXBContext
1688 .newInstance(JalviewModel.class);
1689 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
1691 // output pretty printed
1692 // jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
1693 jaxbMarshaller.marshal(
1694 new ObjectFactory().createJalviewModel(object), pout);
1696 // jaxbMarshaller.marshal(object, pout);
1697 // marshaller.marshal(object);
1700 } catch (Exception ex)
1702 // TODO: raise error in GUI if marshalling failed.
1703 System.err.println("Error writing Jalview project");
1704 ex.printStackTrace();
1711 * Writes PCA viewer attributes and computed values to an XML model object and
1712 * adds it to the JalviewModel. Any exceptions are reported by logging.
1714 protected void savePCA(PCAPanel panel, JalviewModel object)
1718 PcaViewer viewer = new PcaViewer();
1719 viewer.setHeight(panel.getHeight());
1720 viewer.setWidth(panel.getWidth());
1721 viewer.setXpos(panel.getX());
1722 viewer.setYpos(panel.getY());
1723 viewer.setTitle(panel.getTitle());
1724 PCAModel pcaModel = panel.getPcaModel();
1725 viewer.setScoreModelName(pcaModel.getScoreModelName());
1726 viewer.setXDim(panel.getSelectedDimensionIndex(X));
1727 viewer.setYDim(panel.getSelectedDimensionIndex(Y));
1728 viewer.setZDim(panel.getSelectedDimensionIndex(Z));
1730 panel.getRotatableCanvas().getBackgroundColour().getRGB());
1731 viewer.setScaleFactor(panel.getRotatableCanvas().getScaleFactor());
1732 float[] spMin = panel.getRotatableCanvas().getSeqMin();
1733 SeqPointMin spmin = new SeqPointMin();
1734 spmin.setXPos(spMin[0]);
1735 spmin.setYPos(spMin[1]);
1736 spmin.setZPos(spMin[2]);
1737 viewer.setSeqPointMin(spmin);
1738 float[] spMax = panel.getRotatableCanvas().getSeqMax();
1739 SeqPointMax spmax = new SeqPointMax();
1740 spmax.setXPos(spMax[0]);
1741 spmax.setYPos(spMax[1]);
1742 spmax.setZPos(spMax[2]);
1743 viewer.setSeqPointMax(spmax);
1744 viewer.setShowLabels(panel.getRotatableCanvas().isShowLabels());
1745 viewer.setLinkToAllViews(
1746 panel.getRotatableCanvas().isApplyToAllViews());
1747 SimilarityParamsI sp = pcaModel.getSimilarityParameters();
1748 viewer.setIncludeGaps(sp.includeGaps());
1749 viewer.setMatchGaps(sp.matchGaps());
1750 viewer.setIncludeGappedColumns(sp.includeGappedColumns());
1751 viewer.setDenominateByShortestLength(sp.denominateByShortestLength());
1754 * sequence points on display
1756 for (jalview.datamodel.SequencePoint spt : pcaModel
1757 .getSequencePoints())
1759 SequencePoint point = new SequencePoint();
1760 point.setSequenceRef(seqHash(spt.getSequence()));
1761 point.setXPos(spt.coord.x);
1762 point.setYPos(spt.coord.y);
1763 point.setZPos(spt.coord.z);
1764 viewer.getSequencePoint().add(point);
1768 * (end points of) axes on display
1770 for (Point p : panel.getRotatableCanvas().getAxisEndPoints())
1773 Axis axis = new Axis();
1777 viewer.getAxis().add(axis);
1781 * raw PCA data (note we are not restoring PCA inputs here -
1782 * alignment view, score model, similarity parameters)
1784 PcaDataType data = new PcaDataType();
1785 viewer.setPcaData(data);
1786 PCA pca = pcaModel.getPcaData();
1788 DoubleMatrix pm = new DoubleMatrix();
1789 saveDoubleMatrix(pca.getPairwiseScores(), pm);
1790 data.setPairwiseMatrix(pm);
1792 DoubleMatrix tm = new DoubleMatrix();
1793 saveDoubleMatrix(pca.getTridiagonal(), tm);
1794 data.setTridiagonalMatrix(tm);
1796 DoubleMatrix eigenMatrix = new DoubleMatrix();
1797 data.setEigenMatrix(eigenMatrix);
1798 saveDoubleMatrix(pca.getEigenmatrix(), eigenMatrix);
1800 object.getPcaViewer().add(viewer);
1801 } catch (Throwable t)
1803 Cache.log.error("Error saving PCA: " + t.getMessage());
1808 * Stores values from a matrix into an XML element, including (if present) the
1813 * @see #loadDoubleMatrix(DoubleMatrix)
1815 protected void saveDoubleMatrix(MatrixI m, DoubleMatrix xmlMatrix)
1817 xmlMatrix.setRows(m.height());
1818 xmlMatrix.setColumns(m.width());
1819 for (int i = 0; i < m.height(); i++)
1821 DoubleVector row = new DoubleVector();
1822 for (int j = 0; j < m.width(); j++)
1824 row.getV().add(m.getValue(i, j));
1826 xmlMatrix.getRow().add(row);
1828 if (m.getD() != null)
1830 DoubleVector dVector = new DoubleVector();
1831 for (double d : m.getD())
1833 dVector.getV().add(d);
1835 xmlMatrix.setD(dVector);
1837 if (m.getE() != null)
1839 DoubleVector eVector = new DoubleVector();
1840 for (double e : m.getE())
1842 eVector.getV().add(e);
1844 xmlMatrix.setE(eVector);
1849 * Loads XML matrix data into a new Matrix object, including the D and/or E
1850 * vectors (if present)
1854 * @see Jalview2XML#saveDoubleMatrix(MatrixI, DoubleMatrix)
1856 protected MatrixI loadDoubleMatrix(DoubleMatrix mData)
1858 int rows = mData.getRows();
1859 double[][] vals = new double[rows][];
1861 for (int i = 0; i < rows; i++)
1863 List<Double> dVector = mData.getRow().get(i).getV();
1864 vals[i] = new double[dVector.size()];
1866 for (Double d : dVector)
1872 MatrixI m = new Matrix(vals);
1874 if (mData.getD() != null)
1876 List<Double> dVector = mData.getD().getV();
1877 double[] vec = new double[dVector.size()];
1879 for (Double d : dVector)
1885 if (mData.getE() != null)
1887 List<Double> dVector = mData.getE().getV();
1888 double[] vec = new double[dVector.size()];
1890 for (Double d : dVector)
1901 * Save any Varna viewers linked to this sequence. Writes an rnaViewer element
1902 * for each viewer, with
1904 * <li>viewer geometry (position, size, split pane divider location)</li>
1905 * <li>index of the selected structure in the viewer (currently shows gapped
1907 * <li>the id of the annotation holding RNA secondary structure</li>
1908 * <li>(currently only one SS is shown per viewer, may be more in future)</li>
1910 * Varna viewer state is also written out (in native Varna XML) to separate
1911 * project jar entries. A separate entry is written for each RNA structure
1912 * displayed, with the naming convention
1914 * <li>rna_viewId_sequenceId_annotationId_[gapped|trimmed]</li>
1922 * @param storeDataset
1924 protected void saveRnaViewers(JarOutputStream jout, JSeq jseq,
1925 final SequenceI jds, List<String> viewIds, AlignmentPanel ap,
1926 boolean storeDataset)
1928 if (Desktop.desktop == null)
1932 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
1933 for (int f = frames.length - 1; f > -1; f--)
1935 if (frames[f] instanceof AppVarna)
1937 AppVarna varna = (AppVarna) frames[f];
1939 * link the sequence to every viewer that is showing it and is linked to
1940 * its alignment panel
1942 if (varna.isListeningFor(jds) && ap == varna.getAlignmentPanel())
1944 String viewId = varna.getViewId();
1945 RnaViewer rna = new RnaViewer();
1946 rna.setViewId(viewId);
1947 rna.setTitle(varna.getTitle());
1948 rna.setXpos(varna.getX());
1949 rna.setYpos(varna.getY());
1950 rna.setWidth(varna.getWidth());
1951 rna.setHeight(varna.getHeight());
1952 rna.setDividerLocation(varna.getDividerLocation());
1953 rna.setSelectedRna(varna.getSelectedIndex());
1954 // jseq.addRnaViewer(rna);
1955 jseq.getRnaViewer().add(rna);
1958 * Store each Varna panel's state once in the project per sequence.
1959 * First time through only (storeDataset==false)
1961 // boolean storeSessions = false;
1962 // String sequenceViewId = viewId + seqsToIds.get(jds);
1963 // if (!storeDataset && !viewIds.contains(sequenceViewId))
1965 // viewIds.add(sequenceViewId);
1966 // storeSessions = true;
1968 for (RnaModel model : varna.getModels())
1970 if (model.seq == jds)
1973 * VARNA saves each view (sequence or alignment secondary
1974 * structure, gapped or trimmed) as a separate XML file
1976 String jarEntryName = rnaSessions.get(model);
1977 if (jarEntryName == null)
1980 String varnaStateFile = varna.getStateInfo(model.rna);
1981 jarEntryName = RNA_PREFIX + viewId + "_" + nextCounter();
1982 copyFileToJar(jout, varnaStateFile, jarEntryName, "Varna");
1983 rnaSessions.put(model, jarEntryName);
1985 SecondaryStructure ss = new SecondaryStructure();
1986 String annotationId = varna.getAnnotation(jds).annotationId;
1987 ss.setAnnotationId(annotationId);
1988 ss.setViewerState(jarEntryName);
1989 ss.setGapped(model.gapped);
1990 ss.setTitle(model.title);
1991 // rna.addSecondaryStructure(ss);
1992 rna.getSecondaryStructure().add(ss);
2001 * Copy the contents of a file to a new entry added to the output jar
2005 * @param jarEntryName
2007 * additional identifying info to log to the console
2009 protected void copyFileToJar(JarOutputStream jout, String infilePath,
2010 String jarEntryName, String msg)
2012 try (InputStream is = new FileInputStream(infilePath))
2014 File file = new File(infilePath);
2015 if (file.exists() && jout != null)
2018 "Writing jar entry " + jarEntryName + " (" + msg + ")");
2019 jout.putNextEntry(new JarEntry(jarEntryName));
2022 // dis = new DataInputStream(new FileInputStream(file));
2023 // byte[] data = new byte[(int) file.length()];
2024 // dis.readFully(data);
2025 // writeJarEntry(jout, jarEntryName, data);
2027 } catch (Exception ex)
2029 ex.printStackTrace();
2034 * Copies input to output, in 4K buffers; handles any data (text or binary)
2038 * @throws IOException
2040 protected void copyAll(InputStream in, OutputStream out)
2043 byte[] buffer = new byte[4096];
2045 while ((bytesRead = in.read(buffer)) != -1)
2047 out.write(buffer, 0, bytesRead);
2052 * Save the state of a structure viewer
2057 * the archive XML element under which to save the state
2060 * @param matchedFile
2064 protected String saveStructureViewer(AlignmentPanel ap, SequenceI jds,
2065 Pdbids pdb, PDBEntry entry, List<String> viewIds,
2066 String matchedFile, StructureViewerBase viewFrame)
2068 final AAStructureBindingModel bindingModel = viewFrame.getBinding();
2071 * Look for any bindings for this viewer to the PDB file of interest
2072 * (including part matches excluding chain id)
2074 for (int peid = 0; peid < bindingModel.getPdbCount(); peid++)
2076 final PDBEntry pdbentry = bindingModel.getPdbEntry(peid);
2077 final String pdbId = pdbentry.getId();
2078 if (!pdbId.equals(entry.getId())
2079 && !(entry.getId().length() > 4 && entry.getId().toLowerCase()
2080 .startsWith(pdbId.toLowerCase())))
2083 * not interested in a binding to a different PDB entry here
2087 if (matchedFile == null)
2089 matchedFile = pdbentry.getFile();
2091 else if (!matchedFile.equals(pdbentry.getFile()))
2094 "Probably lost some PDB-Sequence mappings for this structure file (which apparently has same PDB Entry code): "
2095 + pdbentry.getFile());
2099 // can get at it if the ID
2100 // match is ambiguous (e.g.
2103 for (int smap = 0; smap < viewFrame.getBinding()
2104 .getSequence()[peid].length; smap++)
2106 // if (jal.findIndex(jmol.jmb.sequence[peid][smap]) > -1)
2107 if (jds == viewFrame.getBinding().getSequence()[peid][smap])
2109 StructureState state = new StructureState();
2110 state.setVisible(true);
2111 state.setXpos(viewFrame.getX());
2112 state.setYpos(viewFrame.getY());
2113 state.setWidth(viewFrame.getWidth());
2114 state.setHeight(viewFrame.getHeight());
2115 final String viewId = viewFrame.getViewId();
2116 state.setViewId(viewId);
2117 state.setAlignwithAlignPanel(viewFrame.isUsedforaligment(ap));
2118 state.setColourwithAlignPanel(viewFrame.isUsedForColourBy(ap));
2119 state.setColourByJmol(viewFrame.isColouredByViewer());
2120 state.setType(viewFrame.getViewerType().toString());
2121 // pdb.addStructureState(state);
2122 pdb.getStructureState().add(state);
2130 * Populates the AnnotationColourScheme xml for save. This captures the
2131 * settings of the options in the 'Colour by Annotation' dialog.
2134 * @param userColours
2138 private AnnotationColourScheme constructAnnotationColours(
2139 AnnotationColourGradient acg, List<UserColourScheme> userColours,
2142 AnnotationColourScheme ac = new AnnotationColourScheme();
2143 ac.setAboveThreshold(acg.getAboveThreshold());
2144 ac.setThreshold(acg.getAnnotationThreshold());
2145 // 2.10.2 save annotationId (unique) not annotation label
2146 ac.setAnnotation(acg.getAnnotation().annotationId);
2147 if (acg.getBaseColour() instanceof UserColourScheme)
2150 setUserColourScheme(acg.getBaseColour(), userColours, jm));
2155 ColourSchemeProperty.getColourName(acg.getBaseColour()));
2158 ac.setMaxColour(acg.getMaxColour().getRGB());
2159 ac.setMinColour(acg.getMinColour().getRGB());
2160 ac.setPerSequence(acg.isSeqAssociated());
2161 ac.setPredefinedColours(acg.isPredefinedColours());
2165 private void storeAlignmentAnnotation(AlignmentAnnotation[] aa,
2166 IdentityHashMap<SequenceGroup, String> groupRefs,
2167 AlignmentViewport av, Set<String> calcIdSet, boolean storeDS,
2168 SequenceSet vamsasSet)
2171 for (int i = 0; i < aa.length; i++)
2173 Annotation an = new Annotation();
2175 AlignmentAnnotation annotation = aa[i];
2176 if (annotation.annotationId != null)
2178 annotationIds.put(annotation.annotationId, annotation);
2181 an.setId(annotation.annotationId);
2183 an.setVisible(annotation.visible);
2185 an.setDescription(annotation.description);
2187 if (annotation.sequenceRef != null)
2189 // 2.9 JAL-1781 xref on sequence id rather than name
2190 an.setSequenceRef(seqsToIds.get(annotation.sequenceRef));
2192 if (annotation.groupRef != null)
2194 String groupIdr = groupRefs.get(annotation.groupRef);
2195 if (groupIdr == null)
2197 // make a locally unique String
2198 groupRefs.put(annotation.groupRef,
2199 groupIdr = ("" + System.currentTimeMillis()
2200 + annotation.groupRef.getName()
2201 + groupRefs.size()));
2203 an.setGroupRef(groupIdr.toString());
2206 // store all visualization attributes for annotation
2207 an.setGraphHeight(annotation.graphHeight);
2208 an.setCentreColLabels(annotation.centreColLabels);
2209 an.setScaleColLabels(annotation.scaleColLabel);
2210 an.setShowAllColLabels(annotation.showAllColLabels);
2211 an.setBelowAlignment(annotation.belowAlignment);
2213 if (annotation.graph > 0)
2216 an.setGraphType(annotation.graph);
2217 an.setGraphGroup(annotation.graphGroup);
2218 if (annotation.getThreshold() != null)
2220 ThresholdLine line = new ThresholdLine();
2221 line.setLabel(annotation.getThreshold().label);
2222 line.setValue(annotation.getThreshold().value);
2223 line.setColour(annotation.getThreshold().colour.getRGB());
2224 an.setThresholdLine(line);
2232 an.setLabel(annotation.label);
2234 if (annotation == av.getAlignmentQualityAnnot()
2235 || annotation == av.getAlignmentConservationAnnotation()
2236 || annotation == av.getAlignmentConsensusAnnotation()
2237 || annotation.autoCalculated)
2239 // new way of indicating autocalculated annotation -
2240 an.setAutoCalculated(annotation.autoCalculated);
2242 if (annotation.hasScore())
2244 an.setScore(annotation.getScore());
2247 if (annotation.getCalcId() != null)
2249 calcIdSet.add(annotation.getCalcId());
2250 an.setCalcId(annotation.getCalcId());
2252 if (annotation.hasProperties())
2254 for (String pr : annotation.getProperties())
2256 jalview.xml.binding.jalview.Annotation.Property prop = new jalview.xml.binding.jalview.Annotation.Property();
2258 prop.setValue(annotation.getProperty(pr));
2259 // an.addProperty(prop);
2260 an.getProperty().add(prop);
2264 AnnotationElement ae;
2265 if (annotation.annotations != null)
2267 an.setScoreOnly(false);
2268 for (int a = 0; a < annotation.annotations.length; a++)
2270 if ((annotation == null) || (annotation.annotations[a] == null))
2275 ae = new AnnotationElement();
2276 if (annotation.annotations[a].description != null)
2278 ae.setDescription(annotation.annotations[a].description);
2280 if (annotation.annotations[a].displayCharacter != null)
2282 ae.setDisplayCharacter(
2283 annotation.annotations[a].displayCharacter);
2286 if (!Float.isNaN(annotation.annotations[a].value))
2288 ae.setValue(annotation.annotations[a].value);
2292 if (annotation.annotations[a].secondaryStructure > ' ')
2294 ae.setSecondaryStructure(
2295 annotation.annotations[a].secondaryStructure + "");
2298 if (annotation.annotations[a].colour != null
2299 && annotation.annotations[a].colour != java.awt.Color.black)
2301 ae.setColour(annotation.annotations[a].colour.getRGB());
2304 // an.addAnnotationElement(ae);
2305 an.getAnnotationElement().add(ae);
2306 if (annotation.autoCalculated)
2308 // only write one non-null entry into the annotation row -
2309 // sufficient to get the visualization attributes necessary to
2317 an.setScoreOnly(true);
2319 if (!storeDS || (storeDS && !annotation.autoCalculated))
2321 // skip autocalculated annotation - these are only provided for
2323 // vamsasSet.addAnnotation(an);
2324 vamsasSet.getAnnotation().add(an);
2330 private CalcIdParam createCalcIdParam(String calcId, AlignViewport av)
2332 AutoCalcSetting settings = av.getCalcIdSettingsFor(calcId);
2333 if (settings != null)
2335 CalcIdParam vCalcIdParam = new CalcIdParam();
2336 vCalcIdParam.setCalcId(calcId);
2337 // vCalcIdParam.addServiceURL(settings.getServiceURI());
2338 vCalcIdParam.getServiceURL().add(settings.getServiceURI());
2339 // generic URI allowing a third party to resolve another instance of the
2340 // service used for this calculation
2341 for (String url : settings.getServiceURLs())
2343 // vCalcIdParam.addServiceURL(urls);
2344 vCalcIdParam.getServiceURL().add(url);
2346 vCalcIdParam.setVersion("1.0");
2347 if (settings.getPreset() != null)
2349 WsParamSetI setting = settings.getPreset();
2350 vCalcIdParam.setName(setting.getName());
2351 vCalcIdParam.setDescription(setting.getDescription());
2355 vCalcIdParam.setName("");
2356 vCalcIdParam.setDescription("Last used parameters");
2358 // need to be able to recover 1) settings 2) user-defined presets or
2359 // recreate settings from preset 3) predefined settings provided by
2360 // service - or settings that can be transferred (or discarded)
2361 vCalcIdParam.setParameters(
2362 settings.getWsParamFile().replace("\n", "|\\n|"));
2363 vCalcIdParam.setAutoUpdate(settings.isAutoUpdate());
2364 // todo - decide if updateImmediately is needed for any projects.
2366 return vCalcIdParam;
2371 private boolean recoverCalcIdParam(CalcIdParam calcIdParam,
2374 if (calcIdParam.getVersion().equals("1.0"))
2376 final String[] calcIds = calcIdParam.getServiceURL().toArray(new String[0]);
2377 Jws2Instance service = Jws2Discoverer.getDiscoverer()
2378 .getPreferredServiceFor(calcIds);
2379 if (service != null)
2381 WsParamSetI parmSet = null;
2384 parmSet = service.getParamStore().parseServiceParameterFile(
2385 calcIdParam.getName(), calcIdParam.getDescription(),
2387 calcIdParam.getParameters().replace("|\\n|", "\n"));
2388 } catch (IOException x)
2390 warn("Couldn't parse parameter data for "
2391 + calcIdParam.getCalcId(), x);
2394 List<ArgumentI> argList = null;
2395 if (calcIdParam.getName().length() > 0)
2397 parmSet = service.getParamStore()
2398 .getPreset(calcIdParam.getName());
2399 if (parmSet != null)
2401 // TODO : check we have a good match with settings in AACon -
2402 // otherwise we'll need to create a new preset
2407 argList = parmSet.getArguments();
2410 AAConSettings settings = new AAConSettings(
2411 calcIdParam.isAutoUpdate(), service, parmSet, argList);
2412 av.setCalcIdSettingsFor(calcIdParam.getCalcId(), settings,
2413 calcIdParam.isNeedsUpdate());
2418 warn("Cannot resolve a service for the parameters used in this project. Try configuring a JABAWS server.");
2422 throw new Error(MessageManager.formatMessage(
2423 "error.unsupported_version_calcIdparam", new Object[]
2424 { calcIdParam.toString() }));
2428 * External mapping between jalview objects and objects yielding a valid and
2429 * unique object ID string. This is null for normal Jalview project IO, but
2430 * non-null when a jalview project is being read or written as part of a
2433 IdentityHashMap jv2vobj = null;
2436 * Construct a unique ID for jvobj using either existing bindings or if none
2437 * exist, the result of the hashcode call for the object.
2440 * jalview data object
2441 * @return unique ID for referring to jvobj
2443 private String makeHashCode(Object jvobj, String altCode)
2445 if (jv2vobj != null)
2447 Object id = jv2vobj.get(jvobj);
2450 return id.toString();
2452 // check string ID mappings
2453 if (jvids2vobj != null && jvobj instanceof String)
2455 id = jvids2vobj.get(jvobj);
2459 return id.toString();
2461 // give up and warn that something has gone wrong
2462 warn("Cannot find ID for object in external mapping : " + jvobj);
2468 * return local jalview object mapped to ID, if it exists
2472 * @return null or object bound to idcode
2474 private Object retrieveExistingObj(String idcode)
2476 if (idcode != null && vobj2jv != null)
2478 return vobj2jv.get(idcode);
2484 * binding from ID strings from external mapping table to jalview data model
2487 private Hashtable vobj2jv;
2489 private Sequence createVamsasSequence(String id, SequenceI jds)
2491 return createVamsasSequence(true, id, jds, null);
2494 private Sequence createVamsasSequence(boolean recurse, String id,
2495 SequenceI jds, SequenceI parentseq)
2497 Sequence vamsasSeq = new Sequence();
2498 vamsasSeq.setId(id);
2499 vamsasSeq.setName(jds.getName());
2500 vamsasSeq.setSequence(jds.getSequenceAsString());
2501 vamsasSeq.setDescription(jds.getDescription());
2502 List<DBRefEntry> dbrefs = null;
2503 if (jds.getDatasetSequence() != null)
2505 vamsasSeq.setDsseqid(seqHash(jds.getDatasetSequence()));
2509 // seqId==dsseqid so we can tell which sequences really are
2510 // dataset sequences only
2511 vamsasSeq.setDsseqid(id);
2512 dbrefs = jds.getDBRefs();
2513 if (parentseq == null)
2520 * save any dbrefs; special subclass GeneLocus is flagged as 'locus'
2524 for (int d = 0, nd = dbrefs.size(); d < nd; d++)
2526 DBRef dbref = new DBRef();
2527 DBRefEntry ref = dbrefs.get(d);
2528 dbref.setSource(ref.getSource());
2529 dbref.setVersion(ref.getVersion());
2530 dbref.setAccessionId(ref.getAccessionId());
2531 if (ref instanceof GeneLocus)
2533 dbref.setLocus(true);
2537 Mapping mp = createVamsasMapping(ref.getMap(), parentseq,
2539 dbref.setMapping(mp);
2541 vamsasSeq.getDBRef().add(dbref);
2547 private Mapping createVamsasMapping(jalview.datamodel.Mapping jmp,
2548 SequenceI parentseq, SequenceI jds, boolean recurse)
2551 if (jmp.getMap() != null)
2555 jalview.util.MapList mlst = jmp.getMap();
2556 List<int[]> r = mlst.getFromRanges();
2557 for (int[] range : r)
2559 MapListFrom mfrom = new MapListFrom();
2560 mfrom.setStart(range[0]);
2561 mfrom.setEnd(range[1]);
2562 // mp.addMapListFrom(mfrom);
2563 mp.getMapListFrom().add(mfrom);
2565 r = mlst.getToRanges();
2566 for (int[] range : r)
2568 MapListTo mto = new MapListTo();
2569 mto.setStart(range[0]);
2570 mto.setEnd(range[1]);
2571 // mp.addMapListTo(mto);
2572 mp.getMapListTo().add(mto);
2574 mp.setMapFromUnit(BigInteger.valueOf(mlst.getFromRatio()));
2575 mp.setMapToUnit(BigInteger.valueOf(mlst.getToRatio()));
2576 if (jmp.getTo() != null)
2578 // MappingChoice mpc = new MappingChoice();
2580 // check/create ID for the sequence referenced by getTo()
2583 SequenceI ps = null;
2584 if (parentseq != jmp.getTo()
2585 && parentseq.getDatasetSequence() != jmp.getTo())
2587 // chaining dbref rather than a handshaking one
2588 jmpid = seqHash(ps = jmp.getTo());
2592 jmpid = seqHash(ps = parentseq);
2594 // mpc.setDseqFor(jmpid);
2595 mp.setDseqFor(jmpid);
2596 if (!seqRefIds.containsKey(jmpid))
2598 jalview.bin.Cache.log.debug("creatign new DseqFor ID");
2599 seqRefIds.put(jmpid, ps);
2603 jalview.bin.Cache.log.debug("reusing DseqFor ID");
2606 // mp.setMappingChoice(mpc);
2612 String setUserColourScheme(jalview.schemes.ColourSchemeI cs,
2613 List<UserColourScheme> userColours, JalviewModel jm)
2616 jalview.schemes.UserColourScheme ucs = (jalview.schemes.UserColourScheme) cs;
2617 boolean newucs = false;
2618 if (!userColours.contains(ucs))
2620 userColours.add(ucs);
2623 id = "ucs" + userColours.indexOf(ucs);
2626 // actually create the scheme's entry in the XML model
2627 java.awt.Color[] colours = ucs.getColours();
2628 UserColours uc = new UserColours();
2629 // UserColourScheme jbucs = new UserColourScheme();
2630 JalviewUserColours jbucs = new JalviewUserColours();
2632 for (int i = 0; i < colours.length; i++)
2634 Colour col = new Colour();
2635 col.setName(ResidueProperties.aa[i]);
2636 col.setRGB(jalview.util.Format.getHexString(colours[i]));
2637 // jbucs.addColour(col);
2638 jbucs.getColour().add(col);
2640 if (ucs.getLowerCaseColours() != null)
2642 colours = ucs.getLowerCaseColours();
2643 for (int i = 0; i < colours.length; i++)
2645 Colour col = new Colour();
2646 col.setName(ResidueProperties.aa[i].toLowerCase());
2647 col.setRGB(jalview.util.Format.getHexString(colours[i]));
2648 // jbucs.addColour(col);
2649 jbucs.getColour().add(col);
2654 uc.setUserColourScheme(jbucs);
2655 // jm.addUserColours(uc);
2656 jm.getUserColours().add(uc);
2662 jalview.schemes.UserColourScheme getUserColourScheme(
2663 JalviewModel jm, String id)
2665 List<UserColours> uc = jm.getUserColours();
2666 UserColours colours = null;
2668 for (int i = 0; i < uc.length; i++)
2670 if (uc[i].getId().equals(id))
2677 for (UserColours c : uc)
2679 if (c.getId().equals(id))
2686 java.awt.Color[] newColours = new java.awt.Color[24];
2688 for (int i = 0; i < 24; i++)
2690 newColours[i] = new java.awt.Color(Integer.parseInt(
2691 // colours.getUserColourScheme().getColour(i).getRGB(), 16));
2692 colours.getUserColourScheme().getColour().get(i).getRGB(),
2696 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(
2699 if (colours.getUserColourScheme().getColour().size()/*Count()*/ > 24)
2701 newColours = new java.awt.Color[23];
2702 for (int i = 0; i < 23; i++)
2704 newColours[i] = new java.awt.Color(Integer.parseInt(
2705 colours.getUserColourScheme().getColour().get(i + 24)
2709 ucs.setLowerCaseColours(newColours);
2716 * contains last error message (if any) encountered by XML loader.
2718 String errorMessage = null;
2721 * flag to control whether the Jalview2XML_V1 parser should be deferred to if
2722 * exceptions are raised during project XML parsing
2724 public boolean attemptversion1parse = false;
2727 * Load a jalview project archive from a jar file
2730 * - HTTP URL or filename
2732 public AlignFrame loadJalviewAlign(final Object file)
2735 jalview.gui.AlignFrame af = null;
2739 // create list to store references for any new Jmol viewers created
2740 newStructureViewers = new Vector<>();
2741 // UNMARSHALLER SEEMS TO CLOSE JARINPUTSTREAM, MOST ANNOYING
2742 // Workaround is to make sure caller implements the JarInputStreamProvider
2744 // so we can re-open the jar input stream for each entry.
2746 jarInputStreamProvider jprovider = createjarInputStreamProvider(file);
2747 af = loadJalviewAlign(jprovider);
2750 af.setMenusForViewport();
2752 } catch (MalformedURLException e)
2754 errorMessage = "Invalid URL format for '" + file + "'";
2760 SwingUtilities.invokeAndWait(new Runnable()
2765 setLoadingFinishedForNewStructureViewers();
2768 } catch (Exception x)
2770 System.err.println("Error loading alignment: " + x.getMessage());
2776 @SuppressWarnings("unused")
2777 private jarInputStreamProvider createjarInputStreamProvider(final Object ofile) throws MalformedURLException {
2779 // BH 2018 allow for bytes already attached to File object
2781 String file = (ofile instanceof File ? ((File) ofile).getCanonicalPath() : ofile.toString());
2782 byte[] bytes = Platform.isJS() ? Platform.getFileBytes((File) ofile)
2785 errorMessage = null;
2786 uniqueSetSuffix = null;
2788 viewportsAdded.clear();
2789 frefedSequence = null;
2791 if (file.startsWith("http://")) {
2792 url = new URL(file);
2794 final URL _url = url;
2795 return new jarInputStreamProvider() {
2798 public JarInputStream getJarInputStream() throws IOException {
2799 if (bytes != null) {
2800 // System.out.println("Jalview2XML: opening byte jarInputStream for bytes.length=" + bytes.length);
2801 return new JarInputStream(new ByteArrayInputStream(bytes));
2804 // System.out.println("Jalview2XML: opening url jarInputStream for " + _url);
2805 return new JarInputStream(_url.openStream());
2807 // System.out.println("Jalview2XML: opening file jarInputStream for " + file);
2808 return new JarInputStream(new FileInputStream(file));
2813 public String getFilename() {
2817 } catch (IOException e) {
2818 e.printStackTrace();
2824 * Recover jalview session from a jalview project archive. Caller may
2825 * initialise uniqueSetSuffix, seqRefIds, viewportsAdded and frefedSequence
2826 * themselves. Any null fields will be initialised with default values,
2827 * non-null fields are left alone.
2832 public AlignFrame loadJalviewAlign(final jarInputStreamProvider jprovider)
2834 errorMessage = null;
2835 if (uniqueSetSuffix == null)
2837 uniqueSetSuffix = System.currentTimeMillis() % 100000 + "";
2839 if (seqRefIds == null)
2843 AlignFrame af = null, _af = null;
2844 IdentityHashMap<AlignmentI, AlignmentI> importedDatasets = new IdentityHashMap<>();
2845 Map<String, AlignFrame> gatherToThisFrame = new HashMap<>();
2846 final String file = jprovider.getFilename();
2849 JarInputStream jin = null;
2850 JarEntry jarentry = null;
2855 jin = jprovider.getJarInputStream();
2856 for (int i = 0; i < entryCount; i++)
2858 jarentry = jin.getNextJarEntry();
2861 if (jarentry != null && jarentry.getName().endsWith(".xml"))
2863 JAXBContext jc = JAXBContext
2864 .newInstance("jalview.xml.binding.jalview");
2865 XMLStreamReader streamReader = XMLInputFactory.newInstance()
2866 .createXMLStreamReader(jin);
2867 javax.xml.bind.Unmarshaller um = jc.createUnmarshaller();
2868 JAXBElement<JalviewModel> jbe = um
2869 .unmarshal(streamReader, JalviewModel.class);
2870 JalviewModel object = jbe.getValue();
2872 if (true) // !skipViewport(object))
2874 _af = loadFromObject(object, file, true, jprovider);
2875 if (_af != null && object.getViewport().size() > 0)
2876 // getJalviewModelSequence().getViewportCount() > 0)
2880 // store a reference to the first view
2883 if (_af.getViewport().isGatherViewsHere())
2885 // if this is a gathered view, keep its reference since
2886 // after gathering views, only this frame will remain
2888 gatherToThisFrame.put(_af.getViewport().getSequenceSetId(),
2891 // Save dataset to register mappings once all resolved
2892 importedDatasets.put(
2893 af.getViewport().getAlignment().getDataset(),
2894 af.getViewport().getAlignment().getDataset());
2899 else if (jarentry != null)
2901 // Some other file here.
2904 } while (jarentry != null);
2905 resolveFrefedSequences();
2906 } catch (IOException ex)
2908 ex.printStackTrace();
2909 errorMessage = "Couldn't locate Jalview XML file : " + file;
2911 "Exception whilst loading jalview XML file : " + ex + "\n");
2912 } catch (Exception ex)
2914 System.err.println("Parsing as Jalview Version 2 file failed.");
2915 ex.printStackTrace(System.err);
2916 if (attemptversion1parse)
2918 // used to attempt to parse as V1 castor-generated xml
2920 if (Desktop.instance != null)
2922 Desktop.instance.stopLoading();
2926 System.out.println("Successfully loaded archive file");
2929 ex.printStackTrace();
2932 "Exception whilst loading jalview XML file : " + ex + "\n");
2933 } catch (OutOfMemoryError e)
2935 // Don't use the OOM Window here
2936 errorMessage = "Out of memory loading jalview XML file";
2937 System.err.println("Out of memory whilst loading jalview XML file");
2938 e.printStackTrace();
2942 * Regather multiple views (with the same sequence set id) to the frame (if
2943 * any) that is flagged as the one to gather to, i.e. convert them to tabbed
2944 * views instead of separate frames. Note this doesn't restore a state where
2945 * some expanded views in turn have tabbed views - the last "first tab" read
2946 * in will play the role of gatherer for all.
2948 for (AlignFrame fr : gatherToThisFrame.values())
2950 Desktop.instance.gatherViews(fr);
2953 restoreSplitFrames();
2954 for (AlignmentI ds : importedDatasets.keySet())
2956 if (ds.getCodonFrames() != null)
2958 StructureSelectionManager
2959 .getStructureSelectionManager(Desktop.instance)
2960 .registerMappings(ds.getCodonFrames());
2963 if (errorMessage != null)
2968 if (Desktop.instance != null)
2970 Desktop.instance.stopLoading();
2977 * Try to reconstruct and display SplitFrame windows, where each contains
2978 * complementary dna and protein alignments. Done by pairing up AlignFrame
2979 * objects (created earlier) which have complementary viewport ids associated.
2981 protected void restoreSplitFrames()
2983 List<SplitFrame> gatherTo = new ArrayList<>();
2984 List<AlignFrame> addedToSplitFrames = new ArrayList<>();
2985 Map<String, AlignFrame> dna = new HashMap<>();
2988 * Identify the DNA alignments
2990 for (Entry<Viewport, AlignFrame> candidate : splitFrameCandidates
2993 AlignFrame af = candidate.getValue();
2994 if (af.getViewport().getAlignment().isNucleotide())
2996 dna.put(candidate.getKey().getId(), af);
3001 * Try to match up the protein complements
3003 for (Entry<Viewport, AlignFrame> candidate : splitFrameCandidates
3006 AlignFrame af = candidate.getValue();
3007 if (!af.getViewport().getAlignment().isNucleotide())
3009 String complementId = candidate.getKey().getComplementId();
3010 // only non-null complements should be in the Map
3011 if (complementId != null && dna.containsKey(complementId))
3013 final AlignFrame dnaFrame = dna.get(complementId);
3014 SplitFrame sf = createSplitFrame(dnaFrame, af);
3015 addedToSplitFrames.add(dnaFrame);
3016 addedToSplitFrames.add(af);
3017 dnaFrame.setMenusForViewport();
3018 af.setMenusForViewport();
3019 if (af.getViewport().isGatherViewsHere())
3028 * Open any that we failed to pair up (which shouldn't happen!) as
3029 * standalone AlignFrame's.
3031 for (Entry<Viewport, AlignFrame> candidate : splitFrameCandidates
3034 AlignFrame af = candidate.getValue();
3035 if (!addedToSplitFrames.contains(af))
3037 Viewport view = candidate.getKey();
3038 Desktop.addInternalFrame(af, view.getTitle(),
3039 safeInt(view.getWidth()), safeInt(view.getHeight()));
3040 af.setMenusForViewport();
3041 System.err.println("Failed to restore view " + view.getTitle()
3042 + " to split frame");
3047 * Gather back into tabbed views as flagged.
3049 for (SplitFrame sf : gatherTo)
3051 Desktop.instance.gatherViews(sf);
3054 splitFrameCandidates.clear();
3058 * Construct and display one SplitFrame holding DNA and protein alignments.
3061 * @param proteinFrame
3064 protected SplitFrame createSplitFrame(AlignFrame dnaFrame,
3065 AlignFrame proteinFrame)
3067 SplitFrame splitFrame = new SplitFrame(dnaFrame, proteinFrame);
3068 String title = MessageManager.getString("label.linked_view_title");
3069 int width = (int) dnaFrame.getBounds().getWidth();
3070 int height = (int) (dnaFrame.getBounds().getHeight()
3071 + proteinFrame.getBounds().getHeight() + 50);
3074 * SplitFrame location is saved to both enclosed frames
3076 splitFrame.setLocation(dnaFrame.getX(), dnaFrame.getY());
3077 Desktop.addInternalFrame(splitFrame, title, width, height);
3080 * And compute cDNA consensus (couldn't do earlier with consensus as
3081 * mappings were not yet present)
3083 proteinFrame.getViewport().alignmentChanged(proteinFrame.alignPanel);
3089 * check errorMessage for a valid error message and raise an error box in the
3090 * GUI or write the current errorMessage to stderr and then clear the error
3093 protected void reportErrors()
3095 reportErrors(false);
3098 protected void reportErrors(final boolean saving)
3100 if (errorMessage != null)
3102 final String finalErrorMessage = errorMessage;
3105 javax.swing.SwingUtilities.invokeLater(new Runnable()
3110 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
3112 "Error " + (saving ? "saving" : "loading")
3114 JvOptionPane.WARNING_MESSAGE);
3120 System.err.println("Problem loading Jalview file: " + errorMessage);
3123 errorMessage = null;
3126 Map<String, String> alreadyLoadedPDB = new HashMap<>();
3129 * when set, local views will be updated from view stored in JalviewXML
3130 * Currently (28th Sep 2008) things will go horribly wrong in vamsas document
3131 * sync if this is set to true.
3133 private final boolean updateLocalViews = false;
3136 * Returns the path to a temporary file holding the PDB file for the given PDB
3137 * id. The first time of asking, searches for a file of that name in the
3138 * Jalview project jar, and copies it to a new temporary file. Any repeat
3139 * requests just return the path to the file previously created.
3145 String loadPDBFile(jarInputStreamProvider jprovider, String pdbId,
3148 if (alreadyLoadedPDB.containsKey(pdbId))
3150 return alreadyLoadedPDB.get(pdbId).toString();
3153 String tempFile = copyJarEntry(jprovider, pdbId, "jalview_pdb",
3155 if (tempFile != null)
3157 alreadyLoadedPDB.put(pdbId, tempFile);
3163 * Copies the jar entry of given name to a new temporary file and returns the
3164 * path to the file, or null if the entry is not found.
3167 * @param jarEntryName
3169 * a prefix for the temporary file name, must be at least three
3171 * @param suffixModel
3172 * null or original file - so new file can be given the same suffix
3176 protected String copyJarEntry(jarInputStreamProvider jprovider,
3177 String jarEntryName, String prefix, String suffixModel)
3179 String suffix = ".tmp";
3180 if (suffixModel == null)
3182 suffixModel = jarEntryName;
3184 int sfpos = suffixModel.lastIndexOf(".");
3185 if (sfpos > -1 && sfpos < (suffixModel.length() - 1))
3187 suffix = "." + suffixModel.substring(sfpos + 1);
3190 try (JarInputStream jin = jprovider.getJarInputStream())
3192 JarEntry entry = null;
3195 entry = jin.getNextJarEntry();
3196 } while (entry != null && !entry.getName().equals(jarEntryName));
3200 // in = new BufferedReader(new InputStreamReader(jin, UTF_8));
3201 File outFile = File.createTempFile(prefix, suffix);
3202 outFile.deleteOnExit();
3203 try (OutputStream os = new FileOutputStream(outFile))
3207 String t = outFile.getAbsolutePath();
3212 warn("Couldn't find entry in Jalview Jar for " + jarEntryName);
3214 } catch (Exception ex)
3216 ex.printStackTrace();
3222 private class JvAnnotRow
3224 public JvAnnotRow(int i, AlignmentAnnotation jaa)
3231 * persisted version of annotation row from which to take vis properties
3233 public jalview.datamodel.AlignmentAnnotation template;
3236 * original position of the annotation row in the alignment
3242 * Load alignment frame from jalview XML DOM object
3244 * @param jalviewModel
3247 * filename source string
3248 * @param loadTreesAndStructures
3249 * when false only create Viewport
3251 * data source provider
3252 * @return alignment frame created from view stored in DOM
3254 AlignFrame loadFromObject(JalviewModel jalviewModel, String file,
3255 boolean loadTreesAndStructures, jarInputStreamProvider jprovider)
3257 SequenceSet vamsasSet = jalviewModel.getVamsasModel().getSequenceSet().get(0);
3258 List<Sequence> vamsasSeqs = vamsasSet.getSequence();
3260 // JalviewModelSequence jms = object.getJalviewModelSequence();
3262 // Viewport view = (jms.getViewportCount() > 0) ? jms.getViewport(0)
3264 Viewport view = (jalviewModel.getViewport().size() > 0)
3265 ? jalviewModel.getViewport().get(0)
3268 // ////////////////////////////////
3269 // INITIALISE ALIGNMENT SEQUENCESETID AND VIEWID
3272 // If we just load in the same jar file again, the sequenceSetId
3273 // will be the same, and we end up with multiple references
3274 // to the same sequenceSet. We must modify this id on load
3275 // so that each load of the file gives a unique id
3278 * used to resolve correct alignment dataset for alignments with multiple
3281 String uniqueSeqSetId = null;
3282 String viewId = null;
3285 uniqueSeqSetId = view.getSequenceSetId() + uniqueSetSuffix;
3286 viewId = (view.getId() == null ? null
3287 : view.getId() + uniqueSetSuffix);
3290 // ////////////////////////////////
3293 List<SequenceI> hiddenSeqs = null;
3295 List<SequenceI> tmpseqs = new ArrayList<>();
3297 boolean multipleView = false;
3298 SequenceI referenceseqForView = null;
3299 // JSeq[] jseqs = object.getJalviewModelSequence().getJSeq();
3300 List<JSeq> jseqs = jalviewModel.getJSeq();
3301 int vi = 0; // counter in vamsasSeq array
3302 for (int i = 0; i < jseqs.size(); i++)
3304 JSeq jseq = jseqs.get(i);
3305 String seqId = jseq.getId();
3307 SequenceI tmpSeq = seqRefIds.get(seqId);
3310 if (!incompleteSeqs.containsKey(seqId))
3312 // may not need this check, but keep it for at least 2.9,1 release
3313 if (tmpSeq.getStart() != jseq.getStart()
3314 || tmpSeq.getEnd() != jseq.getEnd())
3317 String.format("Warning JAL-2154 regression: updating start/end for sequence %s from %d/%d to %d/%d",
3318 tmpSeq.getName(), tmpSeq.getStart(),
3319 tmpSeq.getEnd(), jseq.getStart(),
3325 incompleteSeqs.remove(seqId);
3327 if (vamsasSeqs.size() > vi
3328 && vamsasSeqs.get(vi).getId().equals(seqId))
3330 // most likely we are reading a dataset XML document so
3331 // update from vamsasSeq section of XML for this sequence
3332 tmpSeq.setName(vamsasSeqs.get(vi).getName());
3333 tmpSeq.setDescription(vamsasSeqs.get(vi).getDescription());
3334 tmpSeq.setSequence(vamsasSeqs.get(vi).getSequence());
3339 // reading multiple views, so vamsasSeq set is a subset of JSeq
3340 multipleView = true;
3342 tmpSeq.setStart(jseq.getStart());
3343 tmpSeq.setEnd(jseq.getEnd());
3344 tmpseqs.add(tmpSeq);
3348 Sequence vamsasSeq = vamsasSeqs.get(vi);
3349 tmpSeq = new jalview.datamodel.Sequence(vamsasSeq.getName(),
3350 vamsasSeq.getSequence());
3351 tmpSeq.setDescription(vamsasSeq.getDescription());
3352 tmpSeq.setStart(jseq.getStart());
3353 tmpSeq.setEnd(jseq.getEnd());
3354 tmpSeq.setVamsasId(uniqueSetSuffix + seqId);
3355 seqRefIds.put(vamsasSeq.getId(), tmpSeq);
3356 tmpseqs.add(tmpSeq);
3360 if (safeBoolean(jseq.isViewreference()))
3362 referenceseqForView = tmpseqs.get(tmpseqs.size() - 1);
3365 if (jseq.isHidden() != null && jseq.isHidden().booleanValue())
3367 if (hiddenSeqs == null)
3369 hiddenSeqs = new ArrayList<>();
3372 hiddenSeqs.add(tmpSeq);
3377 // Create the alignment object from the sequence set
3378 // ///////////////////////////////
3379 SequenceI[] orderedSeqs = tmpseqs
3380 .toArray(new SequenceI[tmpseqs.size()]);
3382 AlignmentI al = null;
3383 // so we must create or recover the dataset alignment before going further
3384 // ///////////////////////////////
3385 if (vamsasSet.getDatasetId() == null || vamsasSet.getDatasetId() == "")
3387 // older jalview projects do not have a dataset - so creat alignment and
3389 al = new Alignment(orderedSeqs);
3390 al.setDataset(null);
3394 boolean isdsal = jalviewModel.getViewport().isEmpty();
3397 // we are importing a dataset record, so
3398 // recover reference to an alignment already materialsed as dataset
3399 al = getDatasetFor(vamsasSet.getDatasetId());
3403 // materialse the alignment
3404 al = new Alignment(orderedSeqs);
3408 addDatasetRef(vamsasSet.getDatasetId(), al);
3411 // finally, verify all data in vamsasSet is actually present in al
3412 // passing on flag indicating if it is actually a stored dataset
3413 recoverDatasetFor(vamsasSet, al, isdsal, uniqueSeqSetId);
3416 if (referenceseqForView != null)
3418 al.setSeqrep(referenceseqForView);
3420 // / Add the alignment properties
3421 for (int i = 0; i < vamsasSet.getSequenceSetProperties().size(); i++)
3423 SequenceSetProperties ssp = vamsasSet.getSequenceSetProperties()
3425 al.setProperty(ssp.getKey(), ssp.getValue());
3428 // ///////////////////////////////
3430 Hashtable pdbloaded = new Hashtable(); // TODO nothing writes to this??
3433 // load sequence features, database references and any associated PDB
3434 // structures for the alignment
3436 // prior to 2.10, this part would only be executed the first time a
3437 // sequence was encountered, but not afterwards.
3438 // now, for 2.10 projects, this is also done if the xml doc includes
3439 // dataset sequences not actually present in any particular view.
3441 for (int i = 0; i < vamsasSeqs.size(); i++)
3443 JSeq jseq = jseqs.get(i);
3444 if (jseq.getFeatures().size() > 0)
3446 List<Feature> features = jseq.getFeatures();
3447 for (int f = 0; f < features.size(); f++)
3449 Feature feat = features.get(f);
3450 SequenceFeature sf = new SequenceFeature(feat.getType(),
3451 feat.getDescription(), feat.getBegin(), feat.getEnd(),
3452 safeFloat(feat.getScore()), feat.getFeatureGroup());
3453 sf.setStatus(feat.getStatus());
3456 * load any feature attributes - include map-valued attributes
3458 Map<String, Map<String, String>> mapAttributes = new HashMap<>();
3459 for (int od = 0; od < feat.getOtherData().size(); od++)
3461 OtherData keyValue = feat.getOtherData().get(od);
3462 String attributeName = keyValue.getKey();
3463 String attributeValue = keyValue.getValue();
3464 if (attributeName.startsWith("LINK"))
3466 sf.addLink(attributeValue);
3470 String subAttribute = keyValue.getKey2();
3471 if (subAttribute == null)
3473 // simple string-valued attribute
3474 sf.setValue(attributeName, attributeValue);
3478 // attribute 'key' has sub-attribute 'key2'
3479 if (!mapAttributes.containsKey(attributeName))
3481 mapAttributes.put(attributeName, new HashMap<>());
3483 mapAttributes.get(attributeName).put(subAttribute,
3488 for (Entry<String, Map<String, String>> mapAttribute : mapAttributes
3491 sf.setValue(mapAttribute.getKey(), mapAttribute.getValue());
3494 // adds feature to datasequence's feature set (since Jalview 2.10)
3495 al.getSequenceAt(i).addSequenceFeature(sf);
3498 if (vamsasSeqs.get(i).getDBRef().size() > 0)
3500 // adds dbrefs to datasequence's set (since Jalview 2.10)
3502 al.getSequenceAt(i).getDatasetSequence() == null
3503 ? al.getSequenceAt(i)
3504 : al.getSequenceAt(i).getDatasetSequence(),
3507 if (jseq.getPdbids().size() > 0)
3509 List<Pdbids> ids = jseq.getPdbids();
3510 for (int p = 0; p < ids.size(); p++)
3512 Pdbids pdbid = ids.get(p);
3513 jalview.datamodel.PDBEntry entry = new jalview.datamodel.PDBEntry();
3514 entry.setId(pdbid.getId());
3515 if (pdbid.getType() != null)
3517 if (PDBEntry.Type.getType(pdbid.getType()) != null)
3519 entry.setType(PDBEntry.Type.getType(pdbid.getType()));
3523 entry.setType(PDBEntry.Type.FILE);
3526 // jprovider is null when executing 'New View'
3527 if (pdbid.getFile() != null && jprovider != null)
3529 if (!pdbloaded.containsKey(pdbid.getFile()))
3531 entry.setFile(loadPDBFile(jprovider, pdbid.getId(),
3536 entry.setFile(pdbloaded.get(pdbid.getId()).toString());
3540 if (pdbid.getPdbentryItem() != null)
3542 for (PdbentryItem item : pdbid.getPdbentryItem())
3544 for (Property pr : item.getProperty())
3546 entry.setProperty(pr.getName(), pr.getValue());
3551 for (Property prop : pdbid.getProperty())
3553 entry.setProperty(prop.getName(), prop.getValue());
3555 StructureSelectionManager
3556 .getStructureSelectionManager(Desktop.instance)
3557 .registerPDBEntry(entry);
3558 // adds PDBEntry to datasequence's set (since Jalview 2.10)
3559 if (al.getSequenceAt(i).getDatasetSequence() != null)
3561 al.getSequenceAt(i).getDatasetSequence().addPDBId(entry);
3565 al.getSequenceAt(i).addPDBId(entry);
3570 } // end !multipleview
3572 // ///////////////////////////////
3573 // LOAD SEQUENCE MAPPINGS
3575 if (vamsasSet.getAlcodonFrame().size() > 0)
3577 // TODO Potentially this should only be done once for all views of an
3579 List<AlcodonFrame> alc = vamsasSet.getAlcodonFrame();
3580 for (int i = 0; i < alc.size(); i++)
3582 AlignedCodonFrame cf = new AlignedCodonFrame();
3583 if (alc.get(i).getAlcodMap().size() > 0)
3585 List<AlcodMap> maps = alc.get(i).getAlcodMap();
3586 for (int m = 0; m < maps.size(); m++)
3588 AlcodMap map = maps.get(m);
3589 SequenceI dnaseq = seqRefIds.get(map.getDnasq());
3591 jalview.datamodel.Mapping mapping = null;
3592 // attach to dna sequence reference.
3593 if (map.getMapping() != null)
3595 mapping = addMapping(map.getMapping());
3596 if (dnaseq != null && mapping.getTo() != null)
3598 cf.addMap(dnaseq, mapping.getTo(), mapping.getMap());
3604 newAlcodMapRef(map.getDnasq(), cf, mapping));
3608 al.addCodonFrame(cf);
3613 // ////////////////////////////////
3615 List<JvAnnotRow> autoAlan = new ArrayList<>();
3618 * store any annotations which forward reference a group's ID
3620 Map<String, List<AlignmentAnnotation>> groupAnnotRefs = new Hashtable<>();
3622 if (vamsasSet.getAnnotation().size()/*Count()*/ > 0)
3624 List<Annotation> an = vamsasSet.getAnnotation();
3626 for (int i = 0; i < an.size(); i++)
3628 Annotation annotation = an.get(i);
3631 * test if annotation is automatically calculated for this view only
3633 boolean autoForView = false;
3634 if (annotation.getLabel().equals("Quality")
3635 || annotation.getLabel().equals("Conservation")
3636 || annotation.getLabel().equals("Consensus"))
3638 // Kludge for pre 2.5 projects which lacked the autocalculated flag
3640 // JAXB has no has() test; schema defaults value to false
3641 // if (!annotation.hasAutoCalculated())
3643 // annotation.setAutoCalculated(true);
3646 if (autoForView || annotation.isAutoCalculated())
3648 // remove ID - we don't recover annotation from other views for
3649 // view-specific annotation
3650 annotation.setId(null);
3653 // set visibility for other annotation in this view
3654 String annotationId = annotation.getId();
3655 if (annotationId != null && annotationIds.containsKey(annotationId))
3657 AlignmentAnnotation jda = annotationIds.get(annotationId);
3658 // in principle Visible should always be true for annotation displayed
3659 // in multiple views
3660 if (annotation.isVisible() != null)
3662 jda.visible = annotation.isVisible();
3665 al.addAnnotation(jda);
3669 // Construct new annotation from model.
3670 List<AnnotationElement> ae = annotation.getAnnotationElement();
3671 jalview.datamodel.Annotation[] anot = null;
3672 java.awt.Color firstColour = null;
3674 if (!annotation.isScoreOnly())
3676 anot = new jalview.datamodel.Annotation[al.getWidth()];
3677 for (int aa = 0; aa < ae.size() && aa < anot.length; aa++)
3679 AnnotationElement annElement = ae.get(aa);
3680 anpos = annElement.getPosition();
3682 if (anpos >= anot.length)
3687 float value = safeFloat(annElement.getValue());
3688 anot[anpos] = new jalview.datamodel.Annotation(
3689 annElement.getDisplayCharacter(),
3690 annElement.getDescription(),
3691 (annElement.getSecondaryStructure() == null
3692 || annElement.getSecondaryStructure()
3696 .getSecondaryStructure()
3699 anot[anpos].colour = new Color(safeInt(annElement.getColour()));
3700 if (firstColour == null)
3702 firstColour = anot[anpos].colour;
3706 jalview.datamodel.AlignmentAnnotation jaa = null;
3708 if (annotation.isGraph())
3710 float llim = 0, hlim = 0;
3711 // if (autoForView || an[i].isAutoCalculated()) {
3714 jaa = new jalview.datamodel.AlignmentAnnotation(
3715 annotation.getLabel(), annotation.getDescription(), anot,
3716 llim, hlim, safeInt(annotation.getGraphType()));
3718 jaa.graphGroup = safeInt(annotation.getGraphGroup());
3719 jaa._linecolour = firstColour;
3720 if (annotation.getThresholdLine() != null)
3722 jaa.setThreshold(new jalview.datamodel.GraphLine(
3723 safeFloat(annotation.getThresholdLine().getValue()),
3724 annotation.getThresholdLine().getLabel(),
3725 new java.awt.Color(safeInt(
3726 annotation.getThresholdLine().getColour()))));
3728 if (autoForView || annotation.isAutoCalculated())
3730 // Hardwire the symbol display line to ensure that labels for
3731 // histograms are displayed
3737 jaa = new jalview.datamodel.AlignmentAnnotation(
3738 annotation.getLabel(), annotation.getDescription(), anot);
3739 jaa._linecolour = firstColour;
3741 // register new annotation
3742 if (annotation.getId() != null)
3744 annotationIds.put(annotation.getId(), jaa);
3745 jaa.annotationId = annotation.getId();
3747 // recover sequence association
3748 String sequenceRef = annotation.getSequenceRef();
3749 if (sequenceRef != null)
3751 // from 2.9 sequenceRef is to sequence id (JAL-1781)
3752 SequenceI sequence = seqRefIds.get(sequenceRef);
3753 if (sequence == null)
3755 // in pre-2.9 projects sequence ref is to sequence name
3756 sequence = al.findName(sequenceRef);
3758 if (sequence != null)
3760 jaa.createSequenceMapping(sequence, 1, true);
3761 sequence.addAlignmentAnnotation(jaa);
3764 // and make a note of any group association
3765 if (annotation.getGroupRef() != null
3766 && annotation.getGroupRef().length() > 0)
3768 List<jalview.datamodel.AlignmentAnnotation> aal = groupAnnotRefs
3769 .get(annotation.getGroupRef());
3772 aal = new ArrayList<>();
3773 groupAnnotRefs.put(annotation.getGroupRef(), aal);
3778 if (annotation.getScore() != null)
3780 jaa.setScore(annotation.getScore().doubleValue());
3782 if (annotation.isVisible() != null)
3784 jaa.visible = annotation.isVisible().booleanValue();
3787 if (annotation.isCentreColLabels() != null)
3789 jaa.centreColLabels = annotation.isCentreColLabels()
3793 if (annotation.isScaleColLabels() != null)
3795 jaa.scaleColLabel = annotation.isScaleColLabels().booleanValue();
3797 if (annotation.isAutoCalculated())
3799 // newer files have an 'autoCalculated' flag and store calculation
3800 // state in viewport properties
3801 jaa.autoCalculated = true; // means annotation will be marked for
3802 // update at end of load.
3804 if (annotation.getGraphHeight() != null)
3806 jaa.graphHeight = annotation.getGraphHeight().intValue();
3808 jaa.belowAlignment = annotation.isBelowAlignment();
3809 jaa.setCalcId(annotation.getCalcId());
3810 if (annotation.getProperty().size() > 0)
3812 for (Annotation.Property prop : annotation
3815 jaa.setProperty(prop.getName(), prop.getValue());
3818 if (jaa.autoCalculated)
3820 autoAlan.add(new JvAnnotRow(i, jaa));
3823 // if (!autoForView)
3825 // add autocalculated group annotation and any user created annotation
3827 al.addAnnotation(jaa);
3831 // ///////////////////////
3833 // Create alignment markup and styles for this view
3834 if (jalviewModel.getJGroup().size() > 0)
3836 List<JGroup> groups = jalviewModel.getJGroup();
3837 boolean addAnnotSchemeGroup = false;
3838 for (int i = 0; i < groups.size(); i++)
3840 JGroup jGroup = groups.get(i);
3841 ColourSchemeI cs = null;
3842 if (jGroup.getColour() != null)
3844 if (jGroup.getColour().startsWith("ucs"))
3846 cs = getUserColourScheme(jalviewModel, jGroup.getColour());
3848 else if (jGroup.getColour().equals("AnnotationColourGradient")
3849 && jGroup.getAnnotationColours() != null)
3851 addAnnotSchemeGroup = true;
3855 cs = ColourSchemeProperty.getColourScheme(null, al,
3856 jGroup.getColour());
3859 int pidThreshold = safeInt(jGroup.getPidThreshold());
3861 Vector<SequenceI> seqs = new Vector<>();
3863 for (int s = 0; s < jGroup.getSeq().size(); s++)
3865 String seqId = jGroup.getSeq().get(s);
3866 SequenceI ts = seqRefIds.get(seqId);
3870 seqs.addElement(ts);
3874 if (seqs.size() < 1)
3879 SequenceGroup sg = new SequenceGroup(seqs, jGroup.getName(), cs,
3880 safeBoolean(jGroup.isDisplayBoxes()),
3881 safeBoolean(jGroup.isDisplayText()),
3882 safeBoolean(jGroup.isColourText()),
3883 safeInt(jGroup.getStart()), safeInt(jGroup.getEnd()));
3884 sg.getGroupColourScheme().setThreshold(pidThreshold, true);
3885 sg.getGroupColourScheme()
3886 .setConservationInc(safeInt(jGroup.getConsThreshold()));
3887 sg.setOutlineColour(new Color(safeInt(jGroup.getOutlineColour())));
3889 sg.textColour = new Color(safeInt(jGroup.getTextCol1()));
3890 sg.textColour2 = new Color(safeInt(jGroup.getTextCol2()));
3891 sg.setShowNonconserved(safeBoolean(jGroup.isShowUnconserved()));
3892 sg.thresholdTextColour = safeInt(jGroup.getTextColThreshold());
3893 // attributes with a default in the schema are never null
3894 sg.setShowConsensusHistogram(jGroup.isShowConsensusHistogram());
3895 sg.setshowSequenceLogo(jGroup.isShowSequenceLogo());
3896 sg.setNormaliseSequenceLogo(jGroup.isNormaliseSequenceLogo());
3897 sg.setIgnoreGapsConsensus(jGroup.isIgnoreGapsinConsensus());
3898 if (jGroup.getConsThreshold() != null
3899 && jGroup.getConsThreshold().intValue() != 0)
3901 Conservation c = new Conservation("All", sg.getSequences(null), 0,
3904 c.verdict(false, 25);
3905 sg.cs.setConservation(c);
3908 if (jGroup.getId() != null && groupAnnotRefs.size() > 0)
3910 // re-instate unique group/annotation row reference
3911 List<AlignmentAnnotation> jaal = groupAnnotRefs
3912 .get(jGroup.getId());
3915 for (AlignmentAnnotation jaa : jaal)
3918 if (jaa.autoCalculated)
3920 // match up and try to set group autocalc alignment row for this
3922 if (jaa.label.startsWith("Consensus for "))
3924 sg.setConsensus(jaa);
3926 // match up and try to set group autocalc alignment row for this
3928 if (jaa.label.startsWith("Conservation for "))
3930 sg.setConservationRow(jaa);
3937 if (addAnnotSchemeGroup)
3939 // reconstruct the annotation colourscheme
3940 sg.setColourScheme(constructAnnotationColour(
3941 jGroup.getAnnotationColours(), null, al, jalviewModel, false));
3947 // only dataset in this model, so just return.
3950 // ///////////////////////////////
3953 AlignFrame af = null;
3954 AlignViewport av = null;
3955 // now check to see if we really need to create a new viewport.
3956 if (multipleView && viewportsAdded.size() == 0)
3958 // We recovered an alignment for which a viewport already exists.
3959 // TODO: fix up any settings necessary for overlaying stored state onto
3960 // state recovered from another document. (may not be necessary).
3961 // we may need a binding from a viewport in memory to one recovered from
3963 // and then recover its containing af to allow the settings to be applied.
3964 // TODO: fix for vamsas demo
3966 "About to recover a viewport for existing alignment: Sequence set ID is "
3968 Object seqsetobj = retrieveExistingObj(uniqueSeqSetId);
3969 if (seqsetobj != null)
3971 if (seqsetobj instanceof String)
3973 uniqueSeqSetId = (String) seqsetobj;
3975 "Recovered extant sequence set ID mapping for ID : New Sequence set ID is "
3981 "Warning : Collision between sequence set ID string and existing jalview object mapping.");
3987 * indicate that annotation colours are applied across all groups (pre
3988 * Jalview 2.8.1 behaviour)
3990 boolean doGroupAnnColour = Jalview2XML.isVersionStringLaterThan("2.8.1",
3991 jalviewModel.getVersion());
3993 AlignmentPanel ap = null;
3994 boolean isnewview = true;
3997 // Check to see if this alignment already has a view id == viewId
3998 jalview.gui.AlignmentPanel views[] = Desktop
3999 .getAlignmentPanels(uniqueSeqSetId);
4000 if (views != null && views.length > 0)
4002 for (int v = 0; v < views.length; v++)
4004 if (views[v].av.getViewId().equalsIgnoreCase(viewId))
4006 // recover the existing alignpanel, alignframe, viewport
4007 af = views[v].alignFrame;
4010 // TODO: could even skip resetting view settings if we don't want to
4011 // change the local settings from other jalview processes
4020 af = loadViewport(file, jseqs, hiddenSeqs, al, jalviewModel, view,
4021 uniqueSeqSetId, viewId, autoAlan);
4022 av = af.getViewport();
4027 * Load any trees, PDB structures and viewers
4029 * Not done if flag is false (when this method is used for New View)
4031 if (loadTreesAndStructures)
4033 loadTrees(jalviewModel, view, af, av, ap);
4034 loadPCAViewers(jalviewModel, ap);
4035 loadPDBStructures(jprovider, jseqs, af, ap);
4036 loadRnaViewers(jprovider, jseqs, ap);
4038 // and finally return.
4043 * Instantiate and link any saved RNA (Varna) viewers. The state of the Varna
4044 * panel is restored from separate jar entries, two (gapped and trimmed) per
4045 * sequence and secondary structure.
4047 * Currently each viewer shows just one sequence and structure (gapped and
4048 * trimmed), however this method is designed to support multiple sequences or
4049 * structures in viewers if wanted in future.
4055 private void loadRnaViewers(jarInputStreamProvider jprovider,
4056 List<JSeq> jseqs, AlignmentPanel ap)
4059 * scan the sequences for references to viewers; create each one the first
4060 * time it is referenced, add Rna models to existing viewers
4062 for (JSeq jseq : jseqs)
4064 for (int i = 0; i < jseq.getRnaViewer().size(); i++)
4066 RnaViewer viewer = jseq.getRnaViewer().get(i);
4067 AppVarna appVarna = findOrCreateVarnaViewer(viewer, uniqueSetSuffix,
4070 for (int j = 0; j < viewer.getSecondaryStructure().size(); j++)
4072 SecondaryStructure ss = viewer.getSecondaryStructure().get(j);
4073 SequenceI seq = seqRefIds.get(jseq.getId());
4074 AlignmentAnnotation ann = this.annotationIds
4075 .get(ss.getAnnotationId());
4078 * add the structure to the Varna display (with session state copied
4079 * from the jar to a temporary file)
4081 boolean gapped = safeBoolean(ss.isGapped());
4082 String rnaTitle = ss.getTitle();
4083 String sessionState = ss.getViewerState();
4084 String tempStateFile = copyJarEntry(jprovider, sessionState,
4086 RnaModel rna = new RnaModel(rnaTitle, ann, seq, null, gapped);
4087 appVarna.addModelSession(rna, rnaTitle, tempStateFile);
4089 appVarna.setInitialSelection(safeInt(viewer.getSelectedRna()));
4095 * Locate and return an already instantiated matching AppVarna, or create one
4099 * @param viewIdSuffix
4103 protected AppVarna findOrCreateVarnaViewer(RnaViewer viewer,
4104 String viewIdSuffix, AlignmentPanel ap)
4107 * on each load a suffix is appended to the saved viewId, to avoid conflicts
4108 * if load is repeated
4110 String postLoadId = viewer.getViewId() + viewIdSuffix;
4111 for (JInternalFrame frame : getAllFrames())
4113 if (frame instanceof AppVarna)
4115 AppVarna varna = (AppVarna) frame;
4116 if (postLoadId.equals(varna.getViewId()))
4118 // this viewer is already instantiated
4119 // could in future here add ap as another 'parent' of the
4120 // AppVarna window; currently just 1-to-many
4127 * viewer not found - make it
4129 RnaViewerModel model = new RnaViewerModel(postLoadId, viewer.getTitle(),
4130 safeInt(viewer.getXpos()), safeInt(viewer.getYpos()),
4131 safeInt(viewer.getWidth()), safeInt(viewer.getHeight()),
4132 safeInt(viewer.getDividerLocation()));
4133 AppVarna varna = new AppVarna(model, ap);
4139 * Load any saved trees
4147 protected void loadTrees(JalviewModel jm, Viewport view,
4148 AlignFrame af, AlignViewport av, AlignmentPanel ap)
4150 // TODO result of automated refactoring - are all these parameters needed?
4153 for (int t = 0; t < jm.getTree().size(); t++)
4156 Tree tree = jm.getTree().get(t);
4158 TreePanel tp = (TreePanel) retrieveExistingObj(tree.getId());
4161 tp = af.showNewickTree(new NewickFile(tree.getNewick()),
4162 tree.getTitle(), safeInt(tree.getWidth()),
4163 safeInt(tree.getHeight()), safeInt(tree.getXpos()),
4164 safeInt(tree.getYpos()));
4165 if (tree.getId() != null)
4167 // perhaps bind the tree id to something ?
4172 // update local tree attributes ?
4173 // TODO: should check if tp has been manipulated by user - if so its
4174 // settings shouldn't be modified
4175 tp.setTitle(tree.getTitle());
4176 tp.setBounds(new Rectangle(safeInt(tree.getXpos()),
4177 safeInt(tree.getYpos()), safeInt(tree.getWidth()),
4178 safeInt(tree.getHeight())));
4179 tp.setViewport(av); // af.viewport;
4180 // TODO: verify 'associate with all views' works still
4181 tp.getTreeCanvas().setViewport(av); // af.viewport;
4182 tp.getTreeCanvas().setAssociatedPanel(ap); // af.alignPanel;
4184 tp.getTreeCanvas().setApplyToAllViews(tree.isLinkToAllViews());
4187 warn("There was a problem recovering stored Newick tree: \n"
4188 + tree.getNewick());
4192 tp.fitToWindow.setState(safeBoolean(tree.isFitToWindow()));
4193 tp.fitToWindow_actionPerformed(null);
4195 if (tree.getFontName() != null)
4198 new Font(tree.getFontName(), safeInt(tree.getFontStyle()),
4199 safeInt(tree.getFontSize())));
4204 new Font(view.getFontName(), safeInt(view.getFontStyle()),
4205 safeInt(view.getFontSize())));
4208 tp.showPlaceholders(safeBoolean(tree.isMarkUnlinked()));
4209 tp.showBootstrap(safeBoolean(tree.isShowBootstrap()));
4210 tp.showDistances(safeBoolean(tree.isShowDistances()));
4212 tp.getTreeCanvas().setThreshold(safeFloat(tree.getThreshold()));
4214 if (safeBoolean(tree.isCurrentTree()))
4216 af.getViewport().setCurrentTree(tp.getTree());
4220 } catch (Exception ex)
4222 ex.printStackTrace();
4227 * Load and link any saved structure viewers.
4234 protected void loadPDBStructures(jarInputStreamProvider jprovider,
4235 List<JSeq> jseqs, AlignFrame af, AlignmentPanel ap)
4238 * Run through all PDB ids on the alignment, and collect mappings between
4239 * distinct view ids and all sequences referring to that view.
4241 Map<String, StructureViewerModel> structureViewers = new LinkedHashMap<>();
4243 for (int i = 0; i < jseqs.size(); i++)
4245 JSeq jseq = jseqs.get(i);
4246 if (jseq.getPdbids().size() > 0)
4248 List<Pdbids> ids = jseq.getPdbids();
4249 for (int p = 0; p < ids.size(); p++)
4251 Pdbids pdbid = ids.get(p);
4252 final int structureStateCount = pdbid.getStructureState().size();
4253 for (int s = 0; s < structureStateCount; s++)
4255 // check to see if we haven't already created this structure view
4256 final StructureState structureState = pdbid
4257 .getStructureState().get(s);
4258 String sviewid = (structureState.getViewId() == null) ? null
4259 : structureState.getViewId() + uniqueSetSuffix;
4260 jalview.datamodel.PDBEntry jpdb = new jalview.datamodel.PDBEntry();
4261 // Originally : pdbid.getFile()
4262 // : TODO: verify external PDB file recovery still works in normal
4263 // jalview project load
4265 loadPDBFile(jprovider, pdbid.getId(), pdbid.getFile()));
4266 jpdb.setId(pdbid.getId());
4268 int x = safeInt(structureState.getXpos());
4269 int y = safeInt(structureState.getYpos());
4270 int width = safeInt(structureState.getWidth());
4271 int height = safeInt(structureState.getHeight());
4273 // Probably don't need to do this anymore...
4274 // Desktop.desktop.getComponentAt(x, y);
4275 // TODO: NOW: check that this recovers the PDB file correctly.
4276 String pdbFile = loadPDBFile(jprovider, pdbid.getId(),
4278 jalview.datamodel.SequenceI seq = seqRefIds
4279 .get(jseq.getId() + "");
4280 if (sviewid == null)
4282 sviewid = "_jalview_pre2_4_" + x + "," + y + "," + width + ","
4285 if (!structureViewers.containsKey(sviewid))
4287 String viewerType = structureState.getType();
4288 if (viewerType == null) // pre Jalview 2.9
4290 viewerType = ViewerType.JMOL.toString();
4292 structureViewers.put(sviewid,
4293 new StructureViewerModel(x, y, width, height, false,
4294 false, true, structureState.getViewId(),
4296 // Legacy pre-2.7 conversion JAL-823 :
4297 // do not assume any view has to be linked for colour by
4301 // assemble String[] { pdb files }, String[] { id for each
4302 // file }, orig_fileloc, SequenceI[][] {{ seqs_file 1 }, {
4303 // seqs_file 2}, boolean[] {
4304 // linkAlignPanel,superposeWithAlignpanel}} from hash
4305 StructureViewerModel jmoldat = structureViewers.get(sviewid);
4306 jmoldat.setAlignWithPanel(jmoldat.isAlignWithPanel()
4307 || structureState.isAlignwithAlignPanel());
4310 * Default colour by linked panel to false if not specified (e.g.
4311 * for pre-2.7 projects)
4313 boolean colourWithAlignPanel = jmoldat.isColourWithAlignPanel();
4314 colourWithAlignPanel |= structureState.isColourwithAlignPanel();
4315 jmoldat.setColourWithAlignPanel(colourWithAlignPanel);
4318 * Default colour by viewer to true if not specified (e.g. for
4321 boolean colourByViewer = jmoldat.isColourByViewer();
4322 colourByViewer &= structureState.isColourByJmol();
4323 jmoldat.setColourByViewer(colourByViewer);
4325 if (jmoldat.getStateData().length() < structureState
4326 .getValue()/*Content()*/.length())
4328 jmoldat.setStateData(structureState.getValue());// Content());
4330 if (pdbid.getFile() != null)
4332 File mapkey = new File(pdbid.getFile());
4333 StructureData seqstrmaps = jmoldat.getFileData().get(mapkey);
4334 if (seqstrmaps == null)
4336 jmoldat.getFileData().put(mapkey,
4337 seqstrmaps = jmoldat.new StructureData(pdbFile,
4340 if (!seqstrmaps.getSeqList().contains(seq))
4342 seqstrmaps.getSeqList().add(seq);
4348 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");
4355 // Instantiate the associated structure views
4356 for (Entry<String, StructureViewerModel> entry : structureViewers
4361 createOrLinkStructureViewer(entry, af, ap, jprovider);
4362 } catch (Exception e)
4365 "Error loading structure viewer: " + e.getMessage());
4366 // failed - try the next one
4378 protected void createOrLinkStructureViewer(
4379 Entry<String, StructureViewerModel> viewerData, AlignFrame af,
4380 AlignmentPanel ap, jarInputStreamProvider jprovider)
4382 final StructureViewerModel stateData = viewerData.getValue();
4385 * Search for any viewer windows already open from other alignment views
4386 * that exactly match the stored structure state
4388 StructureViewerBase comp = findMatchingViewer(viewerData);
4392 linkStructureViewer(ap, comp, stateData);
4396 String type = stateData.getType();
4399 ViewerType viewerType = ViewerType.valueOf(type);
4400 createStructureViewer(viewerType, viewerData, af, jprovider);
4401 } catch (IllegalArgumentException | NullPointerException e)
4403 // TODO JAL-3619 show error dialog / offer an alternative viewer
4405 "Invalid structure viewer type: " + type);
4410 * Generates a name for the entry in the project jar file to hold state
4411 * information for a structure viewer
4416 protected String getViewerJarEntryName(String viewId)
4418 return VIEWER_PREFIX + viewId;
4422 * Returns any open frame that matches given structure viewer data. The match
4423 * is based on the unique viewId, or (for older project versions) the frame's
4429 protected StructureViewerBase findMatchingViewer(
4430 Entry<String, StructureViewerModel> viewerData)
4432 final String sviewid = viewerData.getKey();
4433 final StructureViewerModel svattrib = viewerData.getValue();
4434 StructureViewerBase comp = null;
4435 JInternalFrame[] frames = getAllFrames();
4436 for (JInternalFrame frame : frames)
4438 if (frame instanceof StructureViewerBase)
4441 * Post jalview 2.4 schema includes structure view id
4443 if (sviewid != null && ((StructureViewerBase) frame).getViewId()
4446 comp = (StructureViewerBase) frame;
4447 break; // break added in 2.9
4450 * Otherwise test for matching position and size of viewer frame
4452 else if (frame.getX() == svattrib.getX()
4453 && frame.getY() == svattrib.getY()
4454 && frame.getHeight() == svattrib.getHeight()
4455 && frame.getWidth() == svattrib.getWidth())
4457 comp = (StructureViewerBase) frame;
4458 // no break in faint hope of an exact match on viewId
4466 * Link an AlignmentPanel to an existing structure viewer.
4471 * @param useinViewerSuperpos
4472 * @param usetoColourbyseq
4473 * @param viewerColouring
4475 protected void linkStructureViewer(AlignmentPanel ap,
4476 StructureViewerBase viewer, StructureViewerModel stateData)
4478 // NOTE: if the jalview project is part of a shared session then
4479 // view synchronization should/could be done here.
4481 final boolean useinViewerSuperpos = stateData.isAlignWithPanel();
4482 final boolean usetoColourbyseq = stateData.isColourWithAlignPanel();
4483 final boolean viewerColouring = stateData.isColourByViewer();
4484 Map<File, StructureData> oldFiles = stateData.getFileData();
4487 * Add mapping for sequences in this view to an already open viewer
4489 final AAStructureBindingModel binding = viewer.getBinding();
4490 for (File id : oldFiles.keySet())
4492 // add this and any other pdb files that should be present in the
4494 StructureData filedat = oldFiles.get(id);
4495 String pdbFile = filedat.getFilePath();
4496 SequenceI[] seq = filedat.getSeqList().toArray(new SequenceI[0]);
4497 binding.getSsm().setMapping(seq, null, pdbFile, DataSourceType.FILE,
4499 binding.addSequenceForStructFile(pdbFile, seq);
4501 // and add the AlignmentPanel's reference to the view panel
4502 viewer.addAlignmentPanel(ap);
4503 if (useinViewerSuperpos)
4505 viewer.useAlignmentPanelForSuperposition(ap);
4509 viewer.excludeAlignmentPanelForSuperposition(ap);
4511 if (usetoColourbyseq)
4513 viewer.useAlignmentPanelForColourbyseq(ap, !viewerColouring);
4517 viewer.excludeAlignmentPanelForColourbyseq(ap);
4522 * Get all frames within the Desktop.
4526 protected JInternalFrame[] getAllFrames()
4528 JInternalFrame[] frames = null;
4529 // TODO is this necessary - is it safe - risk of hanging?
4534 frames = Desktop.desktop.getAllFrames();
4535 } catch (ArrayIndexOutOfBoundsException e)
4537 // occasional No such child exceptions are thrown here...
4541 } catch (InterruptedException f)
4545 } while (frames == null);
4550 * Answers true if 'version' is equal to or later than 'supported', where each
4551 * is formatted as major/minor versions like "2.8.3" or "2.3.4b1" for bugfix
4552 * changes. Development and test values for 'version' are leniently treated
4556 * - minimum version we are comparing against
4558 * - version of data being processsed
4561 public static boolean isVersionStringLaterThan(String supported,
4564 if (supported == null || version == null
4565 || version.equalsIgnoreCase("DEVELOPMENT BUILD")
4566 || version.equalsIgnoreCase("Test")
4567 || version.equalsIgnoreCase("AUTOMATED BUILD"))
4569 System.err.println("Assuming project file with "
4570 + (version == null ? "null" : version)
4571 + " is compatible with Jalview version " + supported);
4576 return StringUtils.compareVersions(version, supported, "b") >= 0;
4580 Vector<JalviewStructureDisplayI> newStructureViewers = null;
4582 protected void addNewStructureViewer(JalviewStructureDisplayI sview)
4584 if (newStructureViewers != null)
4586 sview.getBinding().setFinishedLoadingFromArchive(false);
4587 newStructureViewers.add(sview);
4591 protected void setLoadingFinishedForNewStructureViewers()
4593 if (newStructureViewers != null)
4595 for (JalviewStructureDisplayI sview : newStructureViewers)
4597 sview.getBinding().setFinishedLoadingFromArchive(true);
4599 newStructureViewers.clear();
4600 newStructureViewers = null;
4604 AlignFrame loadViewport(String file, List<JSeq> JSEQ,
4605 List<SequenceI> hiddenSeqs, AlignmentI al,
4606 JalviewModel jm, Viewport view, String uniqueSeqSetId,
4607 String viewId, List<JvAnnotRow> autoAlan)
4609 AlignFrame af = null;
4610 af = new AlignFrame(al, safeInt(view.getWidth()),
4611 safeInt(view.getHeight()), uniqueSeqSetId, viewId)
4615 // protected void processKeyEvent(java.awt.event.KeyEvent e) {
4616 // System.out.println("Jalview2XML AF " + e);
4617 // super.processKeyEvent(e);
4624 af.setFileName(file, FileFormat.Jalview);
4626 final AlignViewport viewport = af.getViewport();
4627 for (int i = 0; i < JSEQ.size(); i++)
4629 int colour = safeInt(JSEQ.get(i).getColour());
4630 viewport.setSequenceColour(viewport.getAlignment().getSequenceAt(i),
4636 viewport.setColourByReferenceSeq(true);
4637 viewport.setDisplayReferenceSeq(true);
4640 viewport.setGatherViewsHere(safeBoolean(view.isGatheredViews()));
4642 if (view.getSequenceSetId() != null)
4644 AlignmentViewport av = viewportsAdded.get(uniqueSeqSetId);
4646 viewport.setSequenceSetId(uniqueSeqSetId);
4649 // propagate shared settings to this new view
4650 viewport.setHistoryList(av.getHistoryList());
4651 viewport.setRedoList(av.getRedoList());
4655 viewportsAdded.put(uniqueSeqSetId, viewport);
4657 // TODO: check if this method can be called repeatedly without
4658 // side-effects if alignpanel already registered.
4659 PaintRefresher.Register(af.alignPanel, uniqueSeqSetId);
4661 // apply Hidden regions to view.
4662 if (hiddenSeqs != null)
4664 for (int s = 0; s < JSEQ.size(); s++)
4666 SequenceGroup hidden = new SequenceGroup();
4667 boolean isRepresentative = false;
4668 for (int r = 0; r < JSEQ.get(s).getHiddenSequences().size(); r++)
4670 isRepresentative = true;
4671 SequenceI sequenceToHide = al
4672 .getSequenceAt(JSEQ.get(s).getHiddenSequences().get(r));
4673 hidden.addSequence(sequenceToHide, false);
4674 // remove from hiddenSeqs list so we don't try to hide it twice
4675 hiddenSeqs.remove(sequenceToHide);
4677 if (isRepresentative)
4679 SequenceI representativeSequence = al.getSequenceAt(s);
4680 hidden.addSequence(representativeSequence, false);
4681 viewport.hideRepSequences(representativeSequence, hidden);
4685 SequenceI[] hseqs = hiddenSeqs
4686 .toArray(new SequenceI[hiddenSeqs.size()]);
4687 viewport.hideSequence(hseqs);
4690 // recover view properties and display parameters
4692 viewport.setShowAnnotation(safeBoolean(view.isShowAnnotation()));
4693 viewport.setAbovePIDThreshold(safeBoolean(view.isPidSelected()));
4694 final int pidThreshold = safeInt(view.getPidThreshold());
4695 viewport.setThreshold(pidThreshold);
4697 viewport.setColourText(safeBoolean(view.isShowColourText()));
4700 .setConservationSelected(
4701 safeBoolean(view.isConservationSelected()));
4702 viewport.setIncrement(safeInt(view.getConsThreshold()));
4703 viewport.setShowJVSuffix(safeBoolean(view.isShowFullId()));
4704 viewport.setRightAlignIds(safeBoolean(view.isRightAlignIds()));
4705 viewport.setFont(new Font(view.getFontName(),
4706 safeInt(view.getFontStyle()), safeInt(view.getFontSize())),
4708 ViewStyleI vs = viewport.getViewStyle();
4709 vs.setScaleProteinAsCdna(view.isScaleProteinAsCdna());
4710 viewport.setViewStyle(vs);
4711 // TODO: allow custom charWidth/Heights to be restored by updating them
4712 // after setting font - which means set above to false
4713 viewport.setRenderGaps(safeBoolean(view.isRenderGaps()));
4714 viewport.setWrapAlignment(safeBoolean(view.isWrapAlignment()));
4715 viewport.setShowAnnotation(safeBoolean(view.isShowAnnotation()));
4717 viewport.setShowBoxes(safeBoolean(view.isShowBoxes()));
4719 viewport.setShowText(safeBoolean(view.isShowText()));
4721 viewport.setTextColour(new Color(safeInt(view.getTextCol1())));
4722 viewport.setTextColour2(new Color(safeInt(view.getTextCol2())));
4723 viewport.setThresholdTextColour(safeInt(view.getTextColThreshold()));
4724 viewport.setShowUnconserved(view.isShowUnconserved());
4725 viewport.getRanges().setStartRes(safeInt(view.getStartRes()));
4727 if (view.getViewName() != null)
4729 viewport.setViewName(view.getViewName());
4730 af.setInitialTabVisible();
4732 af.setBounds(safeInt(view.getXpos()), safeInt(view.getYpos()),
4733 safeInt(view.getWidth()), safeInt(view.getHeight()));
4734 // startSeq set in af.alignPanel.updateLayout below
4735 af.alignPanel.updateLayout();
4736 ColourSchemeI cs = null;
4737 // apply colourschemes
4738 if (view.getBgColour() != null)
4740 if (view.getBgColour().startsWith("ucs"))
4742 cs = getUserColourScheme(jm, view.getBgColour());
4744 else if (view.getBgColour().startsWith("Annotation"))
4746 AnnotationColourScheme viewAnnColour = view.getAnnotationColours();
4747 cs = constructAnnotationColour(viewAnnColour, af, al, jm, true);
4754 cs = ColourSchemeProperty.getColourScheme(af.getViewport(), al,
4755 view.getBgColour());
4760 * turn off 'alignment colour applies to all groups'
4761 * while restoring global colour scheme
4763 viewport.setColourAppliesToAllGroups(false);
4764 viewport.setGlobalColourScheme(cs);
4765 viewport.getResidueShading().setThreshold(pidThreshold,
4766 view.isIgnoreGapsinConsensus());
4767 viewport.getResidueShading()
4768 .setConsensus(viewport.getSequenceConsensusHash());
4769 if (safeBoolean(view.isConservationSelected()) && cs != null)
4771 viewport.getResidueShading()
4772 .setConservationInc(safeInt(view.getConsThreshold()));
4774 af.changeColour(cs);
4775 viewport.setColourAppliesToAllGroups(true);
4778 .setShowSequenceFeatures(
4779 safeBoolean(view.isShowSequenceFeatures()));
4781 viewport.setCentreColumnLabels(view.isCentreColumnLabels());
4782 viewport.setIgnoreGapsConsensus(view.isIgnoreGapsinConsensus(), null);
4783 viewport.setFollowHighlight(view.isFollowHighlight());
4784 viewport.followSelection = view.isFollowSelection();
4785 viewport.setShowConsensusHistogram(view.isShowConsensusHistogram());
4786 viewport.setShowSequenceLogo(view.isShowSequenceLogo());
4787 viewport.setNormaliseSequenceLogo(view.isNormaliseSequenceLogo());
4788 viewport.setShowDBRefs(safeBoolean(view.isShowDbRefTooltip()));
4789 viewport.setShowNPFeats(safeBoolean(view.isShowNPfeatureTooltip()));
4790 viewport.setShowGroupConsensus(view.isShowGroupConsensus());
4791 viewport.setShowGroupConservation(view.isShowGroupConservation());
4792 viewport.setShowComplementFeatures(view.isShowComplementFeatures());
4793 viewport.setShowComplementFeaturesOnTop(
4794 view.isShowComplementFeaturesOnTop());
4796 // recover feature settings
4797 if (jm.getFeatureSettings() != null)
4799 FeatureRendererModel fr = af.alignPanel.getSeqPanel().seqCanvas
4800 .getFeatureRenderer();
4801 FeaturesDisplayed fdi;
4802 viewport.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
4803 String[] renderOrder = new String[jm.getFeatureSettings()
4804 .getSetting().size()];
4805 Map<String, FeatureColourI> featureColours = new Hashtable<>();
4806 Map<String, Float> featureOrder = new Hashtable<>();
4808 for (int fs = 0; fs < jm.getFeatureSettings()
4809 .getSetting().size(); fs++)
4811 Setting setting = jm.getFeatureSettings().getSetting().get(fs);
4812 String featureType = setting.getType();
4815 * restore feature filters (if any)
4817 jalview.xml.binding.jalview.FeatureMatcherSet filters = setting
4819 if (filters != null)
4821 FeatureMatcherSetI filter = Jalview2XML
4822 .parseFilter(featureType, filters);
4823 if (!filter.isEmpty())
4825 fr.setFeatureFilter(featureType, filter);
4830 * restore feature colour scheme
4832 Color maxColour = new Color(setting.getColour());
4833 if (setting.getMincolour() != null)
4836 * minColour is always set unless a simple colour
4837 * (including for colour by label though it doesn't use it)
4839 Color minColour = new Color(setting.getMincolour().intValue());
4840 Color noValueColour = minColour;
4841 NoValueColour noColour = setting.getNoValueColour();
4842 if (noColour == NoValueColour.NONE)
4844 noValueColour = null;
4846 else if (noColour == NoValueColour.MAX)
4848 noValueColour = maxColour;
4850 float min = safeFloat(safeFloat(setting.getMin()));
4851 float max = setting.getMax() == null ? 1f
4852 : setting.getMax().floatValue();
4853 FeatureColourI gc = new FeatureColour(maxColour, minColour,
4855 noValueColour, min, max);
4856 if (setting.getAttributeName().size() > 0)
4858 gc.setAttributeName(setting.getAttributeName().toArray(
4859 new String[setting.getAttributeName().size()]));
4861 if (setting.getThreshold() != null)
4863 gc.setThreshold(setting.getThreshold().floatValue());
4864 int threshstate = safeInt(setting.getThreshstate());
4865 // -1 = None, 0 = Below, 1 = Above threshold
4866 if (threshstate == 0)
4868 gc.setBelowThreshold(true);
4870 else if (threshstate == 1)
4872 gc.setAboveThreshold(true);
4875 gc.setAutoScaled(true); // default
4876 if (setting.isAutoScale() != null)
4878 gc.setAutoScaled(setting.isAutoScale());
4880 if (setting.isColourByLabel() != null)
4882 gc.setColourByLabel(setting.isColourByLabel());
4884 // and put in the feature colour table.
4885 featureColours.put(featureType, gc);
4889 featureColours.put(featureType,
4890 new FeatureColour(maxColour));
4892 renderOrder[fs] = featureType;
4893 if (setting.getOrder() != null)
4895 featureOrder.put(featureType, setting.getOrder().floatValue());
4899 featureOrder.put(featureType, Float.valueOf(
4900 fs / jm.getFeatureSettings().getSetting().size()));
4902 if (safeBoolean(setting.isDisplay()))
4904 fdi.setVisible(featureType);
4907 Map<String, Boolean> fgtable = new Hashtable<>();
4908 for (int gs = 0; gs < jm.getFeatureSettings().getGroup().size(); gs++)
4910 Group grp = jm.getFeatureSettings().getGroup().get(gs);
4911 fgtable.put(grp.getName(), Boolean.valueOf(grp.isDisplay()));
4913 // FeatureRendererSettings frs = new FeatureRendererSettings(renderOrder,
4914 // fgtable, featureColours, jms.getFeatureSettings().hasTransparency() ?
4915 // jms.getFeatureSettings().getTransparency() : 0.0, featureOrder);
4916 FeatureRendererSettings frs = new FeatureRendererSettings(renderOrder,
4917 fgtable, featureColours, 1.0f, featureOrder);
4918 fr.transferSettings(frs);
4921 if (view.getHiddenColumns().size() > 0)
4923 for (int c = 0; c < view.getHiddenColumns().size(); c++)
4925 final HiddenColumns hc = view.getHiddenColumns().get(c);
4926 viewport.hideColumns(safeInt(hc.getStart()),
4927 safeInt(hc.getEnd()) /* +1 */);
4930 if (view.getCalcIdParam() != null)
4932 for (CalcIdParam calcIdParam : view.getCalcIdParam())
4934 if (calcIdParam != null)
4936 if (recoverCalcIdParam(calcIdParam, viewport))
4941 warn("Couldn't recover parameters for "
4942 + calcIdParam.getCalcId());
4947 af.setMenusFromViewport(viewport);
4948 af.setTitle(view.getTitle());
4949 // TODO: we don't need to do this if the viewport is aready visible.
4951 * Add the AlignFrame to the desktop (it may be 'gathered' later), unless it
4952 * has a 'cdna/protein complement' view, in which case save it in order to
4953 * populate a SplitFrame once all views have been read in.
4955 String complementaryViewId = view.getComplementId();
4956 if (complementaryViewId == null)
4958 Desktop.addInternalFrame(af, view.getTitle(),
4959 safeInt(view.getWidth()), safeInt(view.getHeight()));
4960 // recompute any autoannotation
4961 af.alignPanel.updateAnnotation(false, true);
4962 reorderAutoannotation(af, al, autoAlan);
4963 af.alignPanel.alignmentChanged();
4967 splitFrameCandidates.put(view, af);
4973 * Reads saved data to restore Colour by Annotation settings
4975 * @param viewAnnColour
4979 * @param checkGroupAnnColour
4982 private ColourSchemeI constructAnnotationColour(
4983 AnnotationColourScheme viewAnnColour, AlignFrame af,
4984 AlignmentI al, JalviewModel model, boolean checkGroupAnnColour)
4986 boolean propagateAnnColour = false;
4987 AlignmentI annAlignment = af != null ? af.getViewport().getAlignment()
4989 if (checkGroupAnnColour && al.getGroups() != null
4990 && al.getGroups().size() > 0)
4992 // pre 2.8.1 behaviour
4993 // check to see if we should transfer annotation colours
4994 propagateAnnColour = true;
4995 for (SequenceGroup sg : al.getGroups())
4997 if (sg.getColourScheme() instanceof AnnotationColourGradient)
4999 propagateAnnColour = false;
5005 * 2.10.2- : saved annotationId is AlignmentAnnotation.annotationId
5007 String annotationId = viewAnnColour.getAnnotation();
5008 AlignmentAnnotation matchedAnnotation = annotationIds.get(annotationId);
5011 * pre 2.10.2: saved annotationId is AlignmentAnnotation.label
5013 if (matchedAnnotation == null
5014 && annAlignment.getAlignmentAnnotation() != null)
5016 for (int i = 0; i < annAlignment.getAlignmentAnnotation().length; i++)
5019 .equals(annAlignment.getAlignmentAnnotation()[i].label))
5021 matchedAnnotation = annAlignment.getAlignmentAnnotation()[i];
5026 if (matchedAnnotation == null)
5028 System.err.println("Failed to match annotation colour scheme for "
5032 if (matchedAnnotation.getThreshold() == null)
5034 matchedAnnotation.setThreshold(
5035 new GraphLine(safeFloat(viewAnnColour.getThreshold()),
5036 "Threshold", Color.black));
5039 AnnotationColourGradient cs = null;
5040 if (viewAnnColour.getColourScheme().equals("None"))
5042 cs = new AnnotationColourGradient(matchedAnnotation,
5043 new Color(safeInt(viewAnnColour.getMinColour())),
5044 new Color(safeInt(viewAnnColour.getMaxColour())),
5045 safeInt(viewAnnColour.getAboveThreshold()));
5047 else if (viewAnnColour.getColourScheme().startsWith("ucs"))
5049 cs = new AnnotationColourGradient(matchedAnnotation,
5050 getUserColourScheme(model, viewAnnColour.getColourScheme()),
5051 safeInt(viewAnnColour.getAboveThreshold()));
5055 cs = new AnnotationColourGradient(matchedAnnotation,
5056 ColourSchemeProperty.getColourScheme(af.getViewport(), al,
5057 viewAnnColour.getColourScheme()),
5058 safeInt(viewAnnColour.getAboveThreshold()));
5061 boolean perSequenceOnly = safeBoolean(viewAnnColour.isPerSequence());
5062 boolean useOriginalColours = safeBoolean(
5063 viewAnnColour.isPredefinedColours());
5064 cs.setSeqAssociated(perSequenceOnly);
5065 cs.setPredefinedColours(useOriginalColours);
5067 if (propagateAnnColour && al.getGroups() != null)
5069 // Also use these settings for all the groups
5070 for (int g = 0; g < al.getGroups().size(); g++)
5072 SequenceGroup sg = al.getGroups().get(g);
5073 if (sg.getGroupColourScheme() == null)
5078 AnnotationColourGradient groupScheme = new AnnotationColourGradient(
5079 matchedAnnotation, sg.getColourScheme(),
5080 safeInt(viewAnnColour.getAboveThreshold()));
5081 sg.setColourScheme(groupScheme);
5082 groupScheme.setSeqAssociated(perSequenceOnly);
5083 groupScheme.setPredefinedColours(useOriginalColours);
5089 private void reorderAutoannotation(AlignFrame af, AlignmentI al,
5090 List<JvAnnotRow> autoAlan)
5092 // copy over visualization settings for autocalculated annotation in the
5094 if (al.getAlignmentAnnotation() != null)
5097 * Kludge for magic autoannotation names (see JAL-811)
5099 String[] magicNames = new String[] { "Consensus", "Quality",
5101 JvAnnotRow nullAnnot = new JvAnnotRow(-1, null);
5102 Hashtable<String, JvAnnotRow> visan = new Hashtable<>();
5103 for (String nm : magicNames)
5105 visan.put(nm, nullAnnot);
5107 for (JvAnnotRow auan : autoAlan)
5109 visan.put(auan.template.label
5110 + (auan.template.getCalcId() == null ? ""
5111 : "\t" + auan.template.getCalcId()),
5114 int hSize = al.getAlignmentAnnotation().length;
5115 List<JvAnnotRow> reorder = new ArrayList<>();
5116 // work through any autoCalculated annotation already on the view
5117 // removing it if it should be placed in a different location on the
5118 // annotation panel.
5119 List<String> remains = new ArrayList<>(visan.keySet());
5120 for (int h = 0; h < hSize; h++)
5122 jalview.datamodel.AlignmentAnnotation jalan = al
5123 .getAlignmentAnnotation()[h];
5124 if (jalan.autoCalculated)
5127 JvAnnotRow valan = visan.get(k = jalan.label);
5128 if (jalan.getCalcId() != null)
5130 valan = visan.get(k = jalan.label + "\t" + jalan.getCalcId());
5135 // delete the auto calculated row from the alignment
5136 al.deleteAnnotation(jalan, false);
5140 if (valan != nullAnnot)
5142 if (jalan != valan.template)
5144 // newly created autoannotation row instance
5145 // so keep a reference to the visible annotation row
5146 // and copy over all relevant attributes
5147 if (valan.template.graphHeight >= 0)
5150 jalan.graphHeight = valan.template.graphHeight;
5152 jalan.visible = valan.template.visible;
5154 reorder.add(new JvAnnotRow(valan.order, jalan));
5159 // Add any (possibly stale) autocalculated rows that were not appended to
5160 // the view during construction
5161 for (String other : remains)
5163 JvAnnotRow othera = visan.get(other);
5164 if (othera != nullAnnot && othera.template.getCalcId() != null
5165 && othera.template.getCalcId().length() > 0)
5167 reorder.add(othera);
5170 // now put the automatic annotation in its correct place
5171 int s = 0, srt[] = new int[reorder.size()];
5172 JvAnnotRow[] rws = new JvAnnotRow[reorder.size()];
5173 for (JvAnnotRow jvar : reorder)
5176 srt[s++] = jvar.order;
5179 jalview.util.QuickSort.sort(srt, rws);
5180 // and re-insert the annotation at its correct position
5181 for (JvAnnotRow jvar : rws)
5183 al.addAnnotation(jvar.template, jvar.order);
5185 af.alignPanel.adjustAnnotationHeight();
5189 Hashtable skipList = null;
5192 * TODO remove this method
5195 * @return AlignFrame bound to sequenceSetId from view, if one exists. private
5196 * AlignFrame getSkippedFrame(Viewport view) { if (skipList==null) {
5197 * throw new Error("Implementation Error. No skipList defined for this
5198 * Jalview2XML instance."); } return (AlignFrame)
5199 * skipList.get(view.getSequenceSetId()); }
5203 * Check if the Jalview view contained in object should be skipped or not.
5206 * @return true if view's sequenceSetId is a key in skipList
5208 private boolean skipViewport(JalviewModel object)
5210 if (skipList == null)
5214 String id = object.getViewport().get(0).getSequenceSetId();
5215 if (skipList.containsKey(id))
5217 if (Cache.log != null && Cache.log.isDebugEnabled())
5219 Cache.log.debug("Skipping seuqence set id " + id);
5226 public void addToSkipList(AlignFrame af)
5228 if (skipList == null)
5230 skipList = new Hashtable();
5232 skipList.put(af.getViewport().getSequenceSetId(), af);
5235 public void clearSkipList()
5237 if (skipList != null)
5244 private void recoverDatasetFor(SequenceSet vamsasSet, AlignmentI al,
5245 boolean ignoreUnrefed, String uniqueSeqSetId)
5247 jalview.datamodel.AlignmentI ds = getDatasetFor(
5248 vamsasSet.getDatasetId());
5249 AlignmentI xtant_ds = ds;
5250 if (xtant_ds == null)
5252 // good chance we are about to create a new dataset, but check if we've
5253 // seen some of the dataset sequence IDs before.
5254 // TODO: skip this check if we are working with project generated by
5255 // version 2.11 or later
5256 xtant_ds = checkIfHasDataset(vamsasSet.getSequence());
5257 if (xtant_ds != null)
5260 addDatasetRef(vamsasSet.getDatasetId(), ds);
5263 Vector<SequenceI> dseqs = null;
5266 // recovering an alignment View
5267 AlignmentI seqSetDS = getDatasetFor(UNIQSEQSETID + uniqueSeqSetId);
5268 if (seqSetDS != null)
5270 if (ds != null && ds != seqSetDS)
5272 warn("JAL-3171 regression: Overwriting a dataset reference for an alignment"
5273 + " - CDS/Protein crossreference data may be lost");
5274 if (xtant_ds != null)
5276 // This can only happen if the unique sequence set ID was bound to a
5277 // dataset that did not contain any of the sequences in the view
5278 // currently being restored.
5279 warn("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.");
5283 addDatasetRef(vamsasSet.getDatasetId(), ds);
5288 // try even harder to restore dataset
5289 AlignmentI xtantDS = checkIfHasDataset(vamsasSet.getSequence());
5290 // create a list of new dataset sequences
5291 dseqs = new Vector<>();
5293 for (int i = 0, iSize = vamsasSet.getSequence().size(); i < iSize; i++)
5295 Sequence vamsasSeq = vamsasSet.getSequence().get(i);
5296 ensureJalviewDatasetSequence(vamsasSeq, ds, dseqs, ignoreUnrefed, i);
5298 // create a new dataset
5301 SequenceI[] dsseqs = new SequenceI[dseqs.size()];
5302 dseqs.copyInto(dsseqs);
5303 ds = new jalview.datamodel.Alignment(dsseqs);
5304 debug("Created new dataset " + vamsasSet.getDatasetId()
5305 + " for alignment " + System.identityHashCode(al));
5306 addDatasetRef(vamsasSet.getDatasetId(), ds);
5308 // set the dataset for the newly imported alignment.
5309 if (al.getDataset() == null && !ignoreUnrefed)
5312 // register dataset for the alignment's uniqueSeqSetId for legacy projects
5313 addDatasetRef(UNIQSEQSETID + uniqueSeqSetId, ds);
5315 updateSeqDatasetBinding(vamsasSet.getSequence(), ds);
5319 * XML dataset sequence ID to materialised dataset reference
5321 HashMap<String, AlignmentI> seqToDataset = new HashMap<>();
5324 * @return the first materialised dataset reference containing a dataset
5325 * sequence referenced in the given view
5327 * - sequences from the view
5329 AlignmentI checkIfHasDataset(List<Sequence> list)
5331 for (Sequence restoredSeq : list)
5333 AlignmentI datasetFor = seqToDataset.get(restoredSeq.getDsseqid());
5334 if (datasetFor != null)
5343 * Register ds as the containing dataset for the dataset sequences referenced
5344 * by sequences in list
5347 * - sequences in a view
5350 void updateSeqDatasetBinding(List<Sequence> list, AlignmentI ds)
5352 for (Sequence restoredSeq : list)
5354 AlignmentI prevDS = seqToDataset.put(restoredSeq.getDsseqid(), ds);
5355 if (prevDS != null && prevDS != ds)
5357 warn("Dataset sequence appears in many datasets: "
5358 + restoredSeq.getDsseqid());
5359 // TODO: try to merge!
5366 * sequence definition to create/merge dataset sequence for
5370 * vector to add new dataset sequence to
5371 * @param ignoreUnrefed
5372 * - when true, don't create new sequences from vamsasSeq if it's id
5373 * doesn't already have an asssociated Jalview sequence.
5375 * - used to reorder the sequence in the alignment according to the
5376 * vamsasSeq array ordering, to preserve ordering of dataset
5378 private void ensureJalviewDatasetSequence(Sequence vamsasSeq,
5379 AlignmentI ds, Vector<SequenceI> dseqs, boolean ignoreUnrefed,
5382 // JBP TODO: Check this is called for AlCodonFrames to support recovery of
5384 SequenceI sq = seqRefIds.get(vamsasSeq.getId());
5385 boolean reorder = false;
5386 SequenceI dsq = null;
5387 if (sq != null && sq.getDatasetSequence() != null)
5389 dsq = sq.getDatasetSequence();
5395 if (sq == null && ignoreUnrefed)
5399 String sqid = vamsasSeq.getDsseqid();
5402 // need to create or add a new dataset sequence reference to this sequence
5405 dsq = seqRefIds.get(sqid);
5410 // make a new dataset sequence
5411 dsq = sq.createDatasetSequence();
5414 // make up a new dataset reference for this sequence
5415 sqid = seqHash(dsq);
5417 dsq.setVamsasId(uniqueSetSuffix + sqid);
5418 seqRefIds.put(sqid, dsq);
5423 dseqs.addElement(dsq);
5428 ds.addSequence(dsq);
5434 { // make this dataset sequence sq's dataset sequence
5435 sq.setDatasetSequence(dsq);
5436 // and update the current dataset alignment
5441 if (!dseqs.contains(dsq))
5448 if (ds.findIndex(dsq) < 0)
5450 ds.addSequence(dsq);
5457 // TODO: refactor this as a merge dataset sequence function
5458 // now check that sq (the dataset sequence) sequence really is the union of
5459 // all references to it
5460 // boolean pre = sq.getStart() < dsq.getStart();
5461 // boolean post = sq.getEnd() > dsq.getEnd();
5465 // StringBuffer sb = new StringBuffer();
5466 String newres = jalview.analysis.AlignSeq.extractGaps(
5467 jalview.util.Comparison.GapChars, sq.getSequenceAsString());
5468 if (!newres.equalsIgnoreCase(dsq.getSequenceAsString())
5469 && newres.length() > dsq.getLength())
5471 // Update with the longer sequence.
5475 * if (pre) { sb.insert(0, newres .substring(0, dsq.getStart() -
5476 * sq.getStart())); dsq.setStart(sq.getStart()); } if (post) {
5477 * sb.append(newres.substring(newres.length() - sq.getEnd() -
5478 * dsq.getEnd())); dsq.setEnd(sq.getEnd()); }
5480 dsq.setSequence(newres);
5482 // TODO: merges will never happen if we 'know' we have the real dataset
5483 // sequence - this should be detected when id==dssid
5485 "DEBUG Notice: Merged dataset sequence (if you see this often, post at http://issues.jalview.org/browse/JAL-1474)"); // ("
5486 // + (pre ? "prepended" : "") + " "
5487 // + (post ? "appended" : ""));
5492 // sequence refs are identical. We may need to update the existing dataset
5493 // alignment with this one, though.
5494 if (ds != null && dseqs == null)
5496 int opos = ds.findIndex(dsq);
5497 SequenceI tseq = null;
5498 if (opos != -1 && vseqpos != opos)
5500 // remove from old position
5501 ds.deleteSequence(dsq);
5503 if (vseqpos < ds.getHeight())
5505 if (vseqpos != opos)
5507 // save sequence at destination position
5508 tseq = ds.getSequenceAt(vseqpos);
5509 ds.replaceSequenceAt(vseqpos, dsq);
5510 ds.addSequence(tseq);
5515 ds.addSequence(dsq);
5522 * TODO use AlignmentI here and in related methods - needs
5523 * AlignmentI.getDataset() changed to return AlignmentI instead of Alignment
5525 Hashtable<String, AlignmentI> datasetIds = null;
5527 IdentityHashMap<AlignmentI, String> dataset2Ids = null;
5529 private AlignmentI getDatasetFor(String datasetId)
5531 if (datasetIds == null)
5533 datasetIds = new Hashtable<>();
5536 if (datasetIds.containsKey(datasetId))
5538 return datasetIds.get(datasetId);
5543 private void addDatasetRef(String datasetId, AlignmentI dataset)
5545 if (datasetIds == null)
5547 datasetIds = new Hashtable<>();
5549 datasetIds.put(datasetId, dataset);
5553 * make a new dataset ID for this jalview dataset alignment
5558 private String getDatasetIdRef(AlignmentI dataset)
5560 if (dataset.getDataset() != null)
5562 warn("Serious issue! Dataset Object passed to getDatasetIdRef is not a Jalview DATASET alignment...");
5564 String datasetId = makeHashCode(dataset, null);
5565 if (datasetId == null)
5567 // make a new datasetId and record it
5568 if (dataset2Ids == null)
5570 dataset2Ids = new IdentityHashMap<>();
5574 datasetId = dataset2Ids.get(dataset);
5576 if (datasetId == null)
5578 datasetId = "ds" + dataset2Ids.size() + 1;
5579 dataset2Ids.put(dataset, datasetId);
5586 * Add any saved DBRefEntry's to the sequence. An entry flagged as 'locus' is
5587 * constructed as a special subclass GeneLocus.
5589 * @param datasetSequence
5592 private void addDBRefs(SequenceI datasetSequence, Sequence sequence)
5594 for (int d = 0; d < sequence.getDBRef().size(); d++)
5596 DBRef dr = sequence.getDBRef().get(d);
5600 entry = new GeneLocus(dr.getSource(), dr.getVersion(),
5601 dr.getAccessionId());
5605 entry = new DBRefEntry(dr.getSource(), dr.getVersion(),
5606 dr.getAccessionId());
5608 if (dr.getMapping() != null)
5610 entry.setMap(addMapping(dr.getMapping()));
5612 datasetSequence.addDBRef(entry);
5616 private jalview.datamodel.Mapping addMapping(Mapping m)
5618 SequenceI dsto = null;
5619 // Mapping m = dr.getMapping();
5620 int fr[] = new int[m.getMapListFrom().size() * 2];
5621 Iterator<MapListFrom> from = m.getMapListFrom().iterator();// enumerateMapListFrom();
5622 for (int _i = 0; from.hasNext(); _i += 2)
5624 MapListFrom mf = from.next();
5625 fr[_i] = mf.getStart();
5626 fr[_i + 1] = mf.getEnd();
5628 int fto[] = new int[m.getMapListTo().size() * 2];
5629 Iterator<MapListTo> to = m.getMapListTo().iterator();// enumerateMapListTo();
5630 for (int _i = 0; to.hasNext(); _i += 2)
5632 MapListTo mf = to.next();
5633 fto[_i] = mf.getStart();
5634 fto[_i + 1] = mf.getEnd();
5636 jalview.datamodel.Mapping jmap = new jalview.datamodel.Mapping(dsto, fr,
5637 fto, m.getMapFromUnit().intValue(),
5638 m.getMapToUnit().intValue());
5641 * (optional) choice of dseqFor or Sequence
5643 if (m.getDseqFor() != null)
5645 String dsfor = m.getDseqFor();
5646 if (seqRefIds.containsKey(dsfor))
5651 jmap.setTo(seqRefIds.get(dsfor));
5655 frefedSequence.add(newMappingRef(dsfor, jmap));
5658 else if (m.getSequence() != null)
5661 * local sequence definition
5663 Sequence ms = m.getSequence();
5664 SequenceI djs = null;
5665 String sqid = ms.getDsseqid();
5666 if (sqid != null && sqid.length() > 0)
5669 * recover dataset sequence
5671 djs = seqRefIds.get(sqid);
5676 "Warning - making up dataset sequence id for DbRef sequence map reference");
5677 sqid = ((Object) ms).toString(); // make up a new hascode for
5678 // undefined dataset sequence hash
5679 // (unlikely to happen)
5685 * make a new dataset sequence and add it to refIds hash
5687 djs = new jalview.datamodel.Sequence(ms.getName(),
5689 djs.setStart(jmap.getMap().getToLowest());
5690 djs.setEnd(jmap.getMap().getToHighest());
5691 djs.setVamsasId(uniqueSetSuffix + sqid);
5693 incompleteSeqs.put(sqid, djs);
5694 seqRefIds.put(sqid, djs);
5697 jalview.bin.Cache.log.debug("about to recurse on addDBRefs.");
5706 * Provides a 'copy' of an alignment view (on action New View) by 'saving' the
5707 * view as XML (but not to file), and then reloading it
5712 public AlignmentPanel copyAlignPanel(AlignmentPanel ap)
5715 JalviewModel jm = saveState(ap, null, null, null);
5718 jm.getVamsasModel().getSequenceSet().get(0).getDatasetId(),
5719 ap.getAlignment().getDataset());
5721 uniqueSetSuffix = "";
5722 // jm.getJalviewModelSequence().getViewport(0).setId(null);
5723 jm.getViewport().get(0).setId(null);
5724 // we don't overwrite the view we just copied
5726 if (this.frefedSequence == null)
5728 frefedSequence = new Vector<>();
5731 viewportsAdded.clear();
5733 AlignFrame af = loadFromObject(jm, null, false, null);
5734 af.getAlignPanels().clear();
5735 af.closeMenuItem_actionPerformed(true);
5738 * if(ap.av.getAlignment().getAlignmentAnnotation()!=null) { for(int i=0;
5739 * i<ap.av.getAlignment().getAlignmentAnnotation().length; i++) {
5740 * if(!ap.av.getAlignment().getAlignmentAnnotation()[i].autoCalculated) {
5741 * af.alignPanel.av.getAlignment().getAlignmentAnnotation()[i] =
5742 * ap.av.getAlignment().getAlignmentAnnotation()[i]; } } }
5745 return af.alignPanel;
5748 private Hashtable jvids2vobj;
5750 private void warn(String msg)
5755 private void warn(String msg, Exception e)
5757 if (Cache.log != null)
5761 Cache.log.warn(msg, e);
5765 Cache.log.warn(msg);
5770 System.err.println("Warning: " + msg);
5773 e.printStackTrace();
5778 private void debug(String string)
5780 debug(string, null);
5783 private void debug(String msg, Exception e)
5785 if (Cache.log != null)
5789 Cache.log.debug(msg, e);
5793 Cache.log.debug(msg);
5798 System.err.println("Warning: " + msg);
5801 e.printStackTrace();
5807 * set the object to ID mapping tables used to write/recover objects and XML
5808 * ID strings for the jalview project. If external tables are provided then
5809 * finalize and clearSeqRefs will not clear the tables when the Jalview2XML
5810 * object goes out of scope. - also populates the datasetIds hashtable with
5811 * alignment objects containing dataset sequences
5814 * Map from ID strings to jalview datamodel
5816 * Map from jalview datamodel to ID strings
5820 public void setObjectMappingTables(Hashtable vobj2jv,
5821 IdentityHashMap jv2vobj)
5823 this.jv2vobj = jv2vobj;
5824 this.vobj2jv = vobj2jv;
5825 Iterator ds = jv2vobj.keySet().iterator();
5827 while (ds.hasNext())
5829 Object jvobj = ds.next();
5830 id = jv2vobj.get(jvobj).toString();
5831 if (jvobj instanceof jalview.datamodel.Alignment)
5833 if (((jalview.datamodel.Alignment) jvobj).getDataset() == null)
5835 addDatasetRef(id, (jalview.datamodel.Alignment) jvobj);
5838 else if (jvobj instanceof jalview.datamodel.Sequence)
5840 // register sequence object so the XML parser can recover it.
5841 if (seqRefIds == null)
5843 seqRefIds = new HashMap<>();
5845 if (seqsToIds == null)
5847 seqsToIds = new IdentityHashMap<>();
5849 seqRefIds.put(jv2vobj.get(jvobj).toString(), (SequenceI) jvobj);
5850 seqsToIds.put((SequenceI) jvobj, id);
5852 else if (jvobj instanceof jalview.datamodel.AlignmentAnnotation)
5855 AlignmentAnnotation jvann = (AlignmentAnnotation) jvobj;
5856 annotationIds.put(anid = jv2vobj.get(jvobj).toString(), jvann);
5857 if (jvann.annotationId == null)
5859 jvann.annotationId = anid;
5861 if (!jvann.annotationId.equals(anid))
5863 // TODO verify that this is the correct behaviour
5864 this.warn("Overriding Annotation ID for " + anid
5865 + " from different id : " + jvann.annotationId);
5866 jvann.annotationId = anid;
5869 else if (jvobj instanceof String)
5871 if (jvids2vobj == null)
5873 jvids2vobj = new Hashtable();
5874 jvids2vobj.put(jvobj, jv2vobj.get(jvobj).toString());
5879 Cache.log.debug("Ignoring " + jvobj.getClass() + " (ID = " + id);
5885 * set the uniqueSetSuffix used to prefix/suffix object IDs for jalview
5886 * objects created from the project archive. If string is null (default for
5887 * construction) then suffix will be set automatically.
5891 public void setUniqueSetSuffix(String string)
5893 uniqueSetSuffix = string;
5898 * uses skipList2 as the skipList for skipping views on sequence sets
5899 * associated with keys in the skipList
5903 public void setSkipList(Hashtable skipList2)
5905 skipList = skipList2;
5909 * Reads the jar entry of given name and returns its contents, or null if the
5910 * entry is not found.
5913 * @param jarEntryName
5916 protected String readJarEntry(jarInputStreamProvider jprovider,
5917 String jarEntryName)
5919 String result = null;
5920 BufferedReader in = null;
5925 * Reopen the jar input stream and traverse its entries to find a matching
5928 JarInputStream jin = jprovider.getJarInputStream();
5929 JarEntry entry = null;
5932 entry = jin.getNextJarEntry();
5933 } while (entry != null && !entry.getName().equals(jarEntryName));
5937 StringBuilder out = new StringBuilder(256);
5938 in = new BufferedReader(new InputStreamReader(jin, UTF_8));
5941 while ((data = in.readLine()) != null)
5945 result = out.toString();
5949 warn("Couldn't find entry in Jalview Jar for " + jarEntryName);
5951 } catch (Exception ex)
5953 ex.printStackTrace();
5961 } catch (IOException e)
5972 * Returns an incrementing counter (0, 1, 2...)
5976 private synchronized int nextCounter()
5982 * Loads any saved PCA viewers
5987 protected void loadPCAViewers(JalviewModel model, AlignmentPanel ap)
5991 List<PcaViewer> pcaviewers = model.getPcaViewer();
5992 for (PcaViewer viewer : pcaviewers)
5994 String modelName = viewer.getScoreModelName();
5995 SimilarityParamsI params = new SimilarityParams(
5996 viewer.isIncludeGappedColumns(), viewer.isMatchGaps(),
5997 viewer.isIncludeGaps(),
5998 viewer.isDenominateByShortestLength());
6001 * create the panel (without computing the PCA)
6003 PCAPanel panel = new PCAPanel(ap, modelName, params);
6005 panel.setTitle(viewer.getTitle());
6006 panel.setBounds(new Rectangle(viewer.getXpos(), viewer.getYpos(),
6007 viewer.getWidth(), viewer.getHeight()));
6009 boolean showLabels = viewer.isShowLabels();
6010 panel.setShowLabels(showLabels);
6011 panel.getRotatableCanvas().setShowLabels(showLabels);
6012 panel.getRotatableCanvas()
6013 .setBgColour(new Color(viewer.getBgColour()));
6014 panel.getRotatableCanvas()
6015 .setApplyToAllViews(viewer.isLinkToAllViews());
6018 * load PCA output data
6020 ScoreModelI scoreModel = ScoreModels.getInstance()
6021 .getScoreModel(modelName, ap);
6022 PCA pca = new PCA(null, scoreModel, params);
6023 PcaDataType pcaData = viewer.getPcaData();
6025 MatrixI pairwise = loadDoubleMatrix(pcaData.getPairwiseMatrix());
6026 pca.setPairwiseScores(pairwise);
6028 MatrixI triDiag = loadDoubleMatrix(pcaData.getTridiagonalMatrix());
6029 pca.setTridiagonal(triDiag);
6031 MatrixI result = loadDoubleMatrix(pcaData.getEigenMatrix());
6032 pca.setEigenmatrix(result);
6034 panel.getPcaModel().setPCA(pca);
6037 * we haven't saved the input data! (JAL-2647 to do)
6039 panel.setInputData(null);
6042 * add the sequence points for the PCA display
6044 List<jalview.datamodel.SequencePoint> seqPoints = new ArrayList<>();
6045 for (SequencePoint sp : viewer.getSequencePoint())
6047 String seqId = sp.getSequenceRef();
6048 SequenceI seq = seqRefIds.get(seqId);
6051 throw new IllegalStateException(
6052 "Unmatched seqref for PCA: " + seqId);
6054 Point pt = new Point(sp.getXPos(), sp.getYPos(), sp.getZPos());
6055 jalview.datamodel.SequencePoint seqPoint = new jalview.datamodel.SequencePoint(
6057 seqPoints.add(seqPoint);
6059 panel.getRotatableCanvas().setPoints(seqPoints, seqPoints.size());
6062 * set min-max ranges and scale after setPoints (which recomputes them)
6064 panel.getRotatableCanvas().setScaleFactor(viewer.getScaleFactor());
6065 SeqPointMin spMin = viewer.getSeqPointMin();
6066 float[] min = new float[] { spMin.getXPos(), spMin.getYPos(),
6068 SeqPointMax spMax = viewer.getSeqPointMax();
6069 float[] max = new float[] { spMax.getXPos(), spMax.getYPos(),
6071 panel.getRotatableCanvas().setSeqMinMax(min, max);
6073 // todo: hold points list in PCAModel only
6074 panel.getPcaModel().setSequencePoints(seqPoints);
6076 panel.setSelectedDimensionIndex(viewer.getXDim(), X);
6077 panel.setSelectedDimensionIndex(viewer.getYDim(), Y);
6078 panel.setSelectedDimensionIndex(viewer.getZDim(), Z);
6080 // is this duplication needed?
6081 panel.setTop(seqPoints.size() - 1);
6082 panel.getPcaModel().setTop(seqPoints.size() - 1);
6085 * add the axes' end points for the display
6087 for (int i = 0; i < 3; i++)
6089 Axis axis = viewer.getAxis().get(i);
6090 panel.getRotatableCanvas().getAxisEndPoints()[i] = new Point(
6091 axis.getXPos(), axis.getYPos(), axis.getZPos());
6094 Desktop.addInternalFrame(panel, MessageManager.formatMessage(
6095 "label.calc_title", "PCA", modelName), 475, 450);
6097 } catch (Exception ex)
6099 Cache.log.error("Error loading PCA: " + ex.toString());
6104 * Creates a new structure viewer window
6111 protected void createStructureViewer(
6112 ViewerType viewerType, final Entry<String, StructureViewerModel> viewerData,
6113 AlignFrame af, jarInputStreamProvider jprovider)
6115 final StructureViewerModel viewerModel = viewerData.getValue();
6116 String sessionFilePath = null;
6118 if (viewerType == ViewerType.JMOL)
6120 sessionFilePath = rewriteJmolSession(viewerModel, jprovider);
6124 String viewerJarEntryName = getViewerJarEntryName(
6125 viewerModel.getViewId());
6126 sessionFilePath = copyJarEntry(jprovider,
6128 "viewerSession", ".tmp");
6130 final String sessionPath = sessionFilePath;
6131 final String sviewid = viewerData.getKey();
6134 SwingUtilities.invokeAndWait(new Runnable()
6139 JalviewStructureDisplayI sview = null;
6142 sview = StructureViewer.createView(viewerType, af.alignPanel,
6143 viewerModel, sessionPath, sviewid);
6144 addNewStructureViewer(sview);
6145 } catch (OutOfMemoryError ex)
6147 new OOMWarning("Restoring structure view for "
6149 (OutOfMemoryError) ex.getCause());
6150 if (sview != null && sview.isVisible())
6152 sview.closeViewer(false);
6153 sview.setVisible(false);
6159 } catch (InvocationTargetException | InterruptedException ex)
6161 warn("Unexpected error when opening " + viewerType
6162 + " structure viewer", ex);
6167 * Rewrites a Jmol session script, saves it to a temporary file, and returns
6168 * the path of the file. "load file" commands are rewritten to change the
6169 * original PDB file names to those created as the Jalview project is loaded.
6175 private String rewriteJmolSession(StructureViewerModel svattrib,
6176 jarInputStreamProvider jprovider)
6178 String state = svattrib.getStateData(); // Jalview < 2.9
6179 if (state == null || state.isEmpty()) // Jalview >= 2.9
6181 String jarEntryName = getViewerJarEntryName(svattrib.getViewId());
6182 state = readJarEntry(jprovider, jarEntryName);
6184 // TODO or simpler? for each key in oldFiles,
6185 // replace key.getPath() in state with oldFiles.get(key).getFilePath()
6186 // (allowing for different path escapings)
6187 StringBuilder rewritten = new StringBuilder(state.length());
6188 int cp = 0, ncp, ecp;
6189 Map<File, StructureData> oldFiles = svattrib.getFileData();
6190 while ((ncp = state.indexOf("load ", cp)) > -1)
6194 // look for next filename in load statement
6195 rewritten.append(state.substring(cp,
6196 ncp = (state.indexOf("\"", ncp + 1) + 1)));
6197 String oldfilenam = state.substring(ncp,
6198 ecp = state.indexOf("\"", ncp));
6199 // recover the new mapping data for this old filename
6200 // have to normalize filename - since Jmol and jalview do
6201 // filename translation differently.
6202 StructureData filedat = oldFiles.get(new File(oldfilenam));
6203 if (filedat == null)
6205 String reformatedOldFilename = oldfilenam.replaceAll("/", "\\\\");
6206 filedat = oldFiles.get(new File(reformatedOldFilename));
6209 .append(Platform.escapeBackslashes(filedat.getFilePath()));
6210 rewritten.append("\"");
6211 cp = ecp + 1; // advance beyond last \" and set cursor so we can
6212 // look for next file statement.
6213 } while ((ncp = state.indexOf("/*file*/", cp)) > -1);
6217 // just append rest of state
6218 rewritten.append(state.substring(cp));
6222 System.err.print("Ignoring incomplete Jmol state for PDB ids: ");
6223 rewritten = new StringBuilder(state);
6224 rewritten.append("; load append ");
6225 for (File id : oldFiles.keySet())
6227 // add pdb files that should be present in the viewer
6228 StructureData filedat = oldFiles.get(id);
6229 rewritten.append(" \"").append(filedat.getFilePath()).append("\"");
6231 rewritten.append(";");
6234 if (rewritten.length() == 0)
6238 final String history = "history = ";
6239 int historyIndex = rewritten.indexOf(history);
6240 if (historyIndex > -1)
6243 * change "history = [true|false];" to "history = [1|0];"
6245 historyIndex += history.length();
6246 String val = rewritten.substring(historyIndex, historyIndex + 5);
6247 if (val.startsWith("true"))
6249 rewritten.replace(historyIndex, historyIndex + 4, "1");
6251 else if (val.startsWith("false"))
6253 rewritten.replace(historyIndex, historyIndex + 5, "0");
6259 File tmp = File.createTempFile("viewerSession", ".tmp");
6260 try (OutputStream os = new FileOutputStream(tmp))
6262 InputStream is = new ByteArrayInputStream(
6263 rewritten.toString().getBytes());
6265 return tmp.getAbsolutePath();
6267 } catch (IOException e)
6269 Cache.log.error("Error restoring Jmol session: " + e.toString());
6275 * Populates an XML model of the feature colour scheme for one feature type
6277 * @param featureType
6281 public static Colour marshalColour(
6282 String featureType, FeatureColourI fcol)
6284 Colour col = new Colour();
6285 if (fcol.isSimpleColour())
6287 col.setRGB(Format.getHexString(fcol.getColour()));
6291 col.setRGB(Format.getHexString(fcol.getMaxColour()));
6292 col.setMin(fcol.getMin());
6293 col.setMax(fcol.getMax());
6294 col.setMinRGB(jalview.util.Format.getHexString(fcol.getMinColour()));
6295 col.setAutoScale(fcol.isAutoScaled());
6296 col.setThreshold(fcol.getThreshold());
6297 col.setColourByLabel(fcol.isColourByLabel());
6298 col.setThreshType(fcol.isAboveThreshold() ? ThresholdType.ABOVE
6299 : (fcol.isBelowThreshold() ? ThresholdType.BELOW
6300 : ThresholdType.NONE));
6301 if (fcol.isColourByAttribute())
6303 final String[] attName = fcol.getAttributeName();
6304 col.getAttributeName().add(attName[0]);
6305 if (attName.length > 1)
6307 col.getAttributeName().add(attName[1]);
6310 Color noColour = fcol.getNoColour();
6311 if (noColour == null)
6313 col.setNoValueColour(NoValueColour.NONE);
6315 else if (noColour == fcol.getMaxColour())
6317 col.setNoValueColour(NoValueColour.MAX);
6321 col.setNoValueColour(NoValueColour.MIN);
6324 col.setName(featureType);
6329 * Populates an XML model of the feature filter(s) for one feature type
6331 * @param firstMatcher
6332 * the first (or only) match condition)
6334 * remaining match conditions (if any)
6336 * if true, conditions are and-ed, else or-ed
6338 public static jalview.xml.binding.jalview.FeatureMatcherSet marshalFilter(
6339 FeatureMatcherI firstMatcher, Iterator<FeatureMatcherI> filters,
6342 jalview.xml.binding.jalview.FeatureMatcherSet result = new jalview.xml.binding.jalview.FeatureMatcherSet();
6344 if (filters.hasNext())
6349 CompoundMatcher compound = new CompoundMatcher();
6350 compound.setAnd(and);
6351 jalview.xml.binding.jalview.FeatureMatcherSet matcher1 = marshalFilter(
6352 firstMatcher, Collections.emptyIterator(), and);
6353 // compound.addMatcherSet(matcher1);
6354 compound.getMatcherSet().add(matcher1);
6355 FeatureMatcherI nextMatcher = filters.next();
6356 jalview.xml.binding.jalview.FeatureMatcherSet matcher2 = marshalFilter(
6357 nextMatcher, filters, and);
6358 // compound.addMatcherSet(matcher2);
6359 compound.getMatcherSet().add(matcher2);
6360 result.setCompoundMatcher(compound);
6365 * single condition matcher
6367 // MatchCondition matcherModel = new MatchCondition();
6368 jalview.xml.binding.jalview.FeatureMatcher matcherModel = new jalview.xml.binding.jalview.FeatureMatcher();
6369 matcherModel.setCondition(
6370 firstMatcher.getMatcher().getCondition().getStableName());
6371 matcherModel.setValue(firstMatcher.getMatcher().getPattern());
6372 if (firstMatcher.isByAttribute())
6374 matcherModel.setBy(FilterBy.BY_ATTRIBUTE);
6375 // matcherModel.setAttributeName(firstMatcher.getAttribute());
6376 String[] attName = firstMatcher.getAttribute();
6377 matcherModel.getAttributeName().add(attName[0]); // attribute
6378 if (attName.length > 1)
6380 matcherModel.getAttributeName().add(attName[1]); // sub-attribute
6383 else if (firstMatcher.isByLabel())
6385 matcherModel.setBy(FilterBy.BY_LABEL);
6387 else if (firstMatcher.isByScore())
6389 matcherModel.setBy(FilterBy.BY_SCORE);
6391 result.setMatchCondition(matcherModel);
6398 * Loads one XML model of a feature filter to a Jalview object
6400 * @param featureType
6401 * @param matcherSetModel
6404 public static FeatureMatcherSetI parseFilter(
6406 jalview.xml.binding.jalview.FeatureMatcherSet matcherSetModel)
6408 FeatureMatcherSetI result = new FeatureMatcherSet();
6411 parseFilterConditions(result, matcherSetModel, true);
6412 } catch (IllegalStateException e)
6414 // mixing AND and OR conditions perhaps
6416 String.format("Error reading filter conditions for '%s': %s",
6417 featureType, e.getMessage()));
6418 // return as much as was parsed up to the error
6425 * Adds feature match conditions to matcherSet as unmarshalled from XML
6426 * (possibly recursively for compound conditions)
6429 * @param matcherSetModel
6431 * if true, multiple conditions are AND-ed, else they are OR-ed
6432 * @throws IllegalStateException
6433 * if AND and OR conditions are mixed
6435 protected static void parseFilterConditions(
6436 FeatureMatcherSetI matcherSet,
6437 jalview.xml.binding.jalview.FeatureMatcherSet matcherSetModel,
6440 jalview.xml.binding.jalview.FeatureMatcher mc = matcherSetModel
6441 .getMatchCondition();
6447 FilterBy filterBy = mc.getBy();
6448 Condition cond = Condition.fromString(mc.getCondition());
6449 String pattern = mc.getValue();
6450 FeatureMatcherI matchCondition = null;
6451 if (filterBy == FilterBy.BY_LABEL)
6453 matchCondition = FeatureMatcher.byLabel(cond, pattern);
6455 else if (filterBy == FilterBy.BY_SCORE)
6457 matchCondition = FeatureMatcher.byScore(cond, pattern);
6460 else if (filterBy == FilterBy.BY_ATTRIBUTE)
6462 final List<String> attributeName = mc.getAttributeName();
6463 String[] attNames = attributeName
6464 .toArray(new String[attributeName.size()]);
6465 matchCondition = FeatureMatcher.byAttribute(cond, pattern,
6470 * note this throws IllegalStateException if AND-ing to a
6471 * previously OR-ed compound condition, or vice versa
6475 matcherSet.and(matchCondition);
6479 matcherSet.or(matchCondition);
6485 * compound condition
6487 List<jalview.xml.binding.jalview.FeatureMatcherSet> matchers = matcherSetModel
6488 .getCompoundMatcher().getMatcherSet();
6489 boolean anded = matcherSetModel.getCompoundMatcher().isAnd();
6490 if (matchers.size() == 2)
6492 parseFilterConditions(matcherSet, matchers.get(0), anded);
6493 parseFilterConditions(matcherSet, matchers.get(1), anded);
6497 System.err.println("Malformed compound filter condition");
6503 * Loads one XML model of a feature colour to a Jalview object
6505 * @param colourModel
6508 public static FeatureColourI parseColour(Colour colourModel)
6510 FeatureColourI colour = null;
6512 if (colourModel.getMax() != null)
6514 Color mincol = null;
6515 Color maxcol = null;
6516 Color noValueColour = null;
6520 mincol = new Color(Integer.parseInt(colourModel.getMinRGB(), 16));
6521 maxcol = new Color(Integer.parseInt(colourModel.getRGB(), 16));
6522 } catch (Exception e)
6524 Cache.log.warn("Couldn't parse out graduated feature color.", e);
6527 NoValueColour noCol = colourModel.getNoValueColour();
6528 if (noCol == NoValueColour.MIN)
6530 noValueColour = mincol;
6532 else if (noCol == NoValueColour.MAX)
6534 noValueColour = maxcol;
6537 colour = new FeatureColour(maxcol, mincol, maxcol, noValueColour,
6538 safeFloat(colourModel.getMin()),
6539 safeFloat(colourModel.getMax()));
6540 final List<String> attributeName = colourModel.getAttributeName();
6541 String[] attributes = attributeName
6542 .toArray(new String[attributeName.size()]);
6543 if (attributes != null && attributes.length > 0)
6545 colour.setAttributeName(attributes);
6547 if (colourModel.isAutoScale() != null)
6549 colour.setAutoScaled(colourModel.isAutoScale().booleanValue());
6551 if (colourModel.isColourByLabel() != null)
6553 colour.setColourByLabel(
6554 colourModel.isColourByLabel().booleanValue());
6556 if (colourModel.getThreshold() != null)
6558 colour.setThreshold(colourModel.getThreshold().floatValue());
6560 ThresholdType ttyp = colourModel.getThreshType();
6561 if (ttyp == ThresholdType.ABOVE)
6563 colour.setAboveThreshold(true);
6565 else if (ttyp == ThresholdType.BELOW)
6567 colour.setBelowThreshold(true);
6572 Color color = new Color(Integer.parseInt(colourModel.getRGB(), 16));
6573 colour = new FeatureColour(color);