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 jalview.analysis.Conservation;
28 import jalview.analysis.PCA;
29 import jalview.analysis.scoremodels.ScoreModels;
30 import jalview.analysis.scoremodels.SimilarityParams;
31 import jalview.api.FeatureColourI;
32 import jalview.api.ViewStyleI;
33 import jalview.api.analysis.ScoreModelI;
34 import jalview.api.analysis.SimilarityParamsI;
35 import jalview.api.structures.JalviewStructureDisplayI;
36 import jalview.bin.Cache;
37 import jalview.datamodel.AlignedCodonFrame;
38 import jalview.datamodel.Alignment;
39 import jalview.datamodel.AlignmentAnnotation;
40 import jalview.datamodel.AlignmentI;
41 import jalview.datamodel.DBRefEntry;
42 import jalview.datamodel.GraphLine;
43 import jalview.datamodel.PDBEntry;
44 import jalview.datamodel.Point;
45 import jalview.datamodel.RnaViewerModel;
46 import jalview.datamodel.SequenceFeature;
47 import jalview.datamodel.SequenceGroup;
48 import jalview.datamodel.SequenceI;
49 import jalview.datamodel.StructureViewerModel;
50 import jalview.datamodel.StructureViewerModel.StructureData;
51 import jalview.datamodel.features.FeatureMatcher;
52 import jalview.datamodel.features.FeatureMatcherI;
53 import jalview.datamodel.features.FeatureMatcherSet;
54 import jalview.datamodel.features.FeatureMatcherSetI;
55 import jalview.ext.varna.RnaModel;
56 import jalview.gui.AlignFrame;
57 import jalview.gui.AlignViewport;
58 import jalview.gui.AlignmentPanel;
59 import jalview.gui.AppVarna;
60 import jalview.gui.ChimeraViewFrame;
61 import jalview.gui.Desktop;
62 import jalview.gui.FeatureRenderer;
63 import jalview.gui.JvOptionPane;
64 import jalview.gui.OOMWarning;
65 import jalview.gui.PCAPanel;
66 import jalview.gui.PaintRefresher;
67 import jalview.gui.SplitFrame;
68 import jalview.gui.StructureViewer;
69 import jalview.gui.StructureViewer.ViewerType;
70 import jalview.gui.StructureViewerBase;
71 import jalview.gui.TreePanel;
72 import jalview.io.BackupFiles;
73 import jalview.io.DataSourceType;
74 import jalview.io.FileFormat;
75 import jalview.io.NewickFile;
76 import jalview.math.Matrix;
77 import jalview.math.MatrixI;
78 import jalview.renderer.ResidueShaderI;
79 import jalview.schemes.AnnotationColourGradient;
80 import jalview.schemes.ColourSchemeI;
81 import jalview.schemes.ColourSchemeProperty;
82 import jalview.schemes.FeatureColour;
83 import jalview.schemes.ResidueProperties;
84 import jalview.schemes.UserColourScheme;
85 import jalview.structure.StructureSelectionManager;
86 import jalview.structures.models.AAStructureBindingModel;
87 import jalview.util.Format;
88 import jalview.util.MessageManager;
89 import jalview.util.Platform;
90 import jalview.util.StringUtils;
91 import jalview.util.jarInputStreamProvider;
92 import jalview.util.matcher.Condition;
93 import jalview.viewmodel.AlignmentViewport;
94 import jalview.viewmodel.PCAModel;
95 import jalview.viewmodel.ViewportRanges;
96 import jalview.viewmodel.seqfeatures.FeatureRendererSettings;
97 import jalview.viewmodel.seqfeatures.FeaturesDisplayed;
98 import jalview.ws.jws2.Jws2Discoverer;
99 import jalview.ws.jws2.dm.AAConSettings;
100 import jalview.ws.jws2.jabaws2.Jws2Instance;
101 import jalview.ws.params.ArgumentI;
102 import jalview.ws.params.AutoCalcSetting;
103 import jalview.ws.params.WsParamSetI;
104 import jalview.xml.binding.jalview.AlcodonFrame;
105 import jalview.xml.binding.jalview.AlcodonFrame.AlcodMap;
106 import jalview.xml.binding.jalview.Annotation;
107 import jalview.xml.binding.jalview.Annotation.ThresholdLine;
108 import jalview.xml.binding.jalview.AnnotationColourScheme;
109 import jalview.xml.binding.jalview.AnnotationElement;
110 import jalview.xml.binding.jalview.DoubleMatrix;
111 import jalview.xml.binding.jalview.DoubleVector;
112 import jalview.xml.binding.jalview.Feature;
113 import jalview.xml.binding.jalview.Feature.OtherData;
114 import jalview.xml.binding.jalview.FeatureMatcherSet.CompoundMatcher;
115 import jalview.xml.binding.jalview.FilterBy;
116 import jalview.xml.binding.jalview.JalviewModel;
117 import jalview.xml.binding.jalview.JalviewModel.FeatureSettings;
118 import jalview.xml.binding.jalview.JalviewModel.FeatureSettings.Group;
119 import jalview.xml.binding.jalview.JalviewModel.FeatureSettings.Setting;
120 import jalview.xml.binding.jalview.JalviewModel.JGroup;
121 import jalview.xml.binding.jalview.JalviewModel.JSeq;
122 import jalview.xml.binding.jalview.JalviewModel.JSeq.Pdbids;
123 import jalview.xml.binding.jalview.JalviewModel.JSeq.Pdbids.StructureState;
124 import jalview.xml.binding.jalview.JalviewModel.JSeq.RnaViewer;
125 import jalview.xml.binding.jalview.JalviewModel.JSeq.RnaViewer.SecondaryStructure;
126 import jalview.xml.binding.jalview.JalviewModel.PcaViewer;
127 import jalview.xml.binding.jalview.JalviewModel.PcaViewer.Axis;
128 import jalview.xml.binding.jalview.JalviewModel.PcaViewer.SeqPointMax;
129 import jalview.xml.binding.jalview.JalviewModel.PcaViewer.SeqPointMin;
130 import jalview.xml.binding.jalview.JalviewModel.PcaViewer.SequencePoint;
131 import jalview.xml.binding.jalview.JalviewModel.Tree;
132 import jalview.xml.binding.jalview.JalviewModel.UserColours;
133 import jalview.xml.binding.jalview.JalviewModel.Viewport;
134 import jalview.xml.binding.jalview.JalviewModel.Viewport.CalcIdParam;
135 import jalview.xml.binding.jalview.JalviewModel.Viewport.HiddenColumns;
136 import jalview.xml.binding.jalview.JalviewUserColours;
137 import jalview.xml.binding.jalview.JalviewUserColours.Colour;
138 import jalview.xml.binding.jalview.MapListType.MapListFrom;
139 import jalview.xml.binding.jalview.MapListType.MapListTo;
140 import jalview.xml.binding.jalview.Mapping;
141 import jalview.xml.binding.jalview.NoValueColour;
142 import jalview.xml.binding.jalview.ObjectFactory;
143 import jalview.xml.binding.jalview.PcaDataType;
144 import jalview.xml.binding.jalview.Pdbentry.Property;
145 import jalview.xml.binding.jalview.Sequence;
146 import jalview.xml.binding.jalview.Sequence.DBRef;
147 import jalview.xml.binding.jalview.SequenceSet;
148 import jalview.xml.binding.jalview.SequenceSet.SequenceSetProperties;
149 import jalview.xml.binding.jalview.ThresholdType;
150 import jalview.xml.binding.jalview.VAMSAS;
152 import java.awt.Color;
153 import java.awt.Font;
154 import java.awt.Rectangle;
155 import java.io.BufferedReader;
156 import java.io.ByteArrayInputStream;
157 import java.io.DataInputStream;
158 import java.io.DataOutputStream;
160 import java.io.FileInputStream;
161 import java.io.FileOutputStream;
162 import java.io.IOException;
163 import java.io.InputStreamReader;
164 import java.io.OutputStreamWriter;
165 import java.io.PrintWriter;
166 import java.lang.reflect.InvocationTargetException;
167 import java.math.BigInteger;
168 import java.net.MalformedURLException;
170 import java.util.ArrayList;
171 import java.util.Arrays;
172 import java.util.Collections;
173 import java.util.Enumeration;
174 import java.util.GregorianCalendar;
175 import java.util.HashMap;
176 import java.util.HashSet;
177 import java.util.Hashtable;
178 import java.util.IdentityHashMap;
179 import java.util.Iterator;
180 import java.util.LinkedHashMap;
181 import java.util.List;
182 import java.util.Map;
183 import java.util.Map.Entry;
184 import java.util.Set;
185 import java.util.Vector;
186 import java.util.jar.JarEntry;
187 import java.util.jar.JarInputStream;
188 import java.util.jar.JarOutputStream;
190 import javax.swing.JInternalFrame;
191 import javax.swing.SwingUtilities;
192 import javax.xml.bind.JAXBContext;
193 import javax.xml.bind.JAXBElement;
194 import javax.xml.bind.Marshaller;
195 import javax.xml.datatype.DatatypeConfigurationException;
196 import javax.xml.datatype.DatatypeFactory;
197 import javax.xml.datatype.XMLGregorianCalendar;
198 import javax.xml.stream.XMLInputFactory;
199 import javax.xml.stream.XMLStreamReader;
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
217 private static void addJ2SBinaryType(String ext)
219 ext = "." + ext + "?";
224 * J2S._binaryTypes.push(ext);
231 addJ2SBinaryType(".jvp?");
234 private static final String VIEWER_PREFIX = "viewer_";
236 private static final String RNA_PREFIX = "rna_";
238 private static final String UTF_8 = "UTF-8";
241 * prefix for recovering datasets for alignments with multiple views where
242 * non-existent dataset IDs were written for some views
244 private static final String UNIQSEQSETID = "uniqueSeqSetId.";
246 // use this with nextCounter() to make unique names for entities
247 private int counter = 0;
250 * SequenceI reference -> XML ID string in jalview XML. Populated as XML reps
251 * of sequence objects are created.
253 IdentityHashMap<SequenceI, String> seqsToIds = null;
256 * jalview XML Sequence ID to jalview sequence object reference (both dataset
257 * and alignment sequences. Populated as XML reps of sequence objects are
260 Map<String, SequenceI> seqRefIds = null;
262 Map<String, SequenceI> incompleteSeqs = null;
264 List<SeqFref> frefedSequence = null;
266 boolean raiseGUI = true; // whether errors are raised in dialog boxes or not
269 * Map of reconstructed AlignFrame objects that appear to have come from
270 * SplitFrame objects (have a dna/protein complement view).
272 private Map<Viewport, AlignFrame> splitFrameCandidates = new HashMap<>();
275 * Map from displayed rna structure models to their saved session state jar
278 private Map<RnaModel, String> rnaSessions = new HashMap<>();
281 * A helper method for safely using the value of an optional attribute that
282 * may be null if not present in the XML. Answers the boolean value, or false
288 public static boolean safeBoolean(Boolean b)
290 return b == null ? false : b.booleanValue();
294 * A helper method for safely using the value of an optional attribute that
295 * may be null if not present in the XML. Answers the integer value, or zero
301 public static int safeInt(Integer i)
303 return i == null ? 0 : i.intValue();
307 * A helper method for safely using the value of an optional attribute that
308 * may be null if not present in the XML. Answers the float value, or zero if
314 public static float safeFloat(Float f)
316 return f == null ? 0f : f.floatValue();
320 * create/return unique hash string for sq
323 * @return new or existing unique string for sq
325 String seqHash(SequenceI sq)
327 if (seqsToIds == null)
331 if (seqsToIds.containsKey(sq))
333 return seqsToIds.get(sq);
337 // create sequential key
338 String key = "sq" + (seqsToIds.size() + 1);
339 key = makeHashCode(sq, key); // check we don't have an external reference
341 seqsToIds.put(sq, key);
348 if (seqsToIds == null)
350 seqsToIds = new IdentityHashMap<>();
352 if (seqRefIds == null)
354 seqRefIds = new HashMap<>();
356 if (incompleteSeqs == null)
358 incompleteSeqs = new HashMap<>();
360 if (frefedSequence == null)
362 frefedSequence = new ArrayList<>();
370 public Jalview2XML(boolean raiseGUI)
372 this.raiseGUI = raiseGUI;
376 * base class for resolving forward references to sequences by their ID
381 abstract class SeqFref
387 public SeqFref(String _sref, String type)
393 public String getSref()
398 public SequenceI getSrefSeq()
400 return seqRefIds.get(sref);
403 public boolean isResolvable()
405 return seqRefIds.get(sref) != null;
408 public SequenceI getSrefDatasetSeq()
410 SequenceI sq = seqRefIds.get(sref);
413 while (sq.getDatasetSequence() != null)
415 sq = sq.getDatasetSequence();
422 * @return true if the forward reference was fully resolved
424 abstract boolean resolve();
427 public String toString()
429 return type + " reference to " + sref;
434 * create forward reference for a mapping
440 public SeqFref newMappingRef(final String sref,
441 final jalview.datamodel.Mapping _jmap)
443 SeqFref fref = new SeqFref(sref, "Mapping")
445 public jalview.datamodel.Mapping jmap = _jmap;
450 SequenceI seq = getSrefDatasetSeq();
462 public SeqFref newAlcodMapRef(final String sref,
463 final AlignedCodonFrame _cf,
464 final jalview.datamodel.Mapping _jmap)
467 SeqFref fref = new SeqFref(sref, "Codon Frame")
469 AlignedCodonFrame cf = _cf;
471 public jalview.datamodel.Mapping mp = _jmap;
474 public boolean isResolvable()
476 return super.isResolvable() && mp.getTo() != null;
482 SequenceI seq = getSrefDatasetSeq();
487 cf.addMap(seq, mp.getTo(), mp.getMap());
494 public void resolveFrefedSequences()
496 Iterator<SeqFref> nextFref = frefedSequence.iterator();
497 int toresolve = frefedSequence.size();
498 int unresolved = 0, failedtoresolve = 0;
499 while (nextFref.hasNext())
501 SeqFref ref = nextFref.next();
502 if (ref.isResolvable())
514 } catch (Exception x)
517 "IMPLEMENTATION ERROR: Failed to resolve forward reference for sequence "
530 System.err.println("Jalview Project Import: There were " + unresolved
531 + " forward references left unresolved on the stack.");
533 if (failedtoresolve > 0)
535 System.err.println("SERIOUS! " + failedtoresolve
536 + " resolvable forward references failed to resolve.");
538 if (incompleteSeqs != null && incompleteSeqs.size() > 0)
541 "Jalview Project Import: There are " + incompleteSeqs.size()
542 + " sequences which may have incomplete metadata.");
543 if (incompleteSeqs.size() < 10)
545 for (SequenceI s : incompleteSeqs.values())
547 System.err.println(s.toString());
553 "Too many to report. Skipping output of incomplete sequences.");
559 * This maintains a map of viewports, the key being the seqSetId. Important to
560 * set historyItem and redoList for multiple views
562 Map<String, AlignViewport> viewportsAdded = new HashMap<>();
564 Map<String, AlignmentAnnotation> annotationIds = new HashMap<>();
566 String uniqueSetSuffix = "";
569 * List of pdbfiles added to Jar
571 List<String> pdbfiles = null;
573 // SAVES SEVERAL ALIGNMENT WINDOWS TO SAME JARFILE
574 public void saveState(File statefile)
576 FileOutputStream fos = null;
581 fos = new FileOutputStream(statefile);
583 JarOutputStream jout = new JarOutputStream(fos);
587 } catch (Exception e)
589 Cache.log.error("Couln't write Jalview state to " + statefile, e);
590 // TODO: inform user of the problem - they need to know if their data was
592 if (errorMessage == null)
594 errorMessage = "Did't write Jalview Archive to output file '"
595 + statefile + "' - See console error log for details";
599 errorMessage += "(Didn't write Jalview Archive to output file '"
610 } catch (IOException e)
620 * Writes a jalview project archive to the given Jar output stream.
624 public void saveState(JarOutputStream jout)
626 AlignFrame[] frames = Desktop.getAlignFrames();
632 saveAllFrames(Arrays.asList(frames), jout);
636 * core method for storing state for a set of AlignFrames.
639 * - frames involving all data to be exported (including containing
642 * - project output stream
644 private void saveAllFrames(List<AlignFrame> frames, JarOutputStream jout)
646 Hashtable<String, AlignFrame> dsses = new Hashtable<>();
649 * ensure cached data is clear before starting
651 // todo tidy up seqRefIds, seqsToIds initialisation / reset
653 splitFrameCandidates.clear();
658 // NOTE UTF-8 MUST BE USED FOR WRITING UNICODE CHARS
659 // //////////////////////////////////////////////////
661 List<String> shortNames = new ArrayList<>();
662 List<String> viewIds = new ArrayList<>();
665 for (int i = frames.size() - 1; i > -1; i--)
667 AlignFrame af = frames.get(i);
669 if (skipList != null && skipList
670 .containsKey(af.getViewport().getSequenceSetId()))
675 String shortName = makeFilename(af, shortNames);
677 int apSize = af.getAlignPanels().size();
679 for (int ap = 0; ap < apSize; ap++)
681 AlignmentPanel apanel = (AlignmentPanel) af.getAlignPanels()
683 String fileName = apSize == 1 ? shortName : ap + shortName;
684 if (!fileName.endsWith(".xml"))
686 fileName = fileName + ".xml";
689 saveState(apanel, fileName, jout, viewIds);
691 String dssid = getDatasetIdRef(
692 af.getViewport().getAlignment().getDataset());
693 if (!dsses.containsKey(dssid))
695 dsses.put(dssid, af);
700 writeDatasetFor(dsses, "" + jout.hashCode() + " " + uniqueSetSuffix,
706 } catch (Exception foo)
711 } catch (Exception ex)
713 // TODO: inform user of the problem - they need to know if their data was
715 if (errorMessage == null)
717 errorMessage = "Couldn't write Jalview Archive - see error output for details";
719 ex.printStackTrace();
724 * Generates a distinct file name, based on the title of the AlignFrame, by
725 * appending _n for increasing n until an unused name is generated. The new
726 * name (without its extension) is added to the list.
730 * @return the generated name, with .xml extension
732 protected String makeFilename(AlignFrame af, List<String> namesUsed)
734 String shortName = af.getTitle();
736 if (shortName.indexOf(File.separatorChar) > -1)
738 shortName = shortName
739 .substring(shortName.lastIndexOf(File.separatorChar) + 1);
744 while (namesUsed.contains(shortName))
746 if (shortName.endsWith("_" + (count - 1)))
748 shortName = shortName.substring(0, shortName.lastIndexOf("_"));
751 shortName = shortName.concat("_" + count);
755 namesUsed.add(shortName);
757 if (!shortName.endsWith(".xml"))
759 shortName = shortName + ".xml";
764 // USE THIS METHOD TO SAVE A SINGLE ALIGNMENT WINDOW
765 public boolean saveAlignment(AlignFrame af, String jarFile,
770 // create backupfiles object and get new temp filename destination
771 BackupFiles backupfiles = new BackupFiles(jarFile);
772 FileOutputStream fos = new FileOutputStream(
773 backupfiles.getTempFilePath());
775 JarOutputStream jout = new JarOutputStream(fos);
776 List<AlignFrame> frames = new ArrayList<>();
778 // resolve splitframes
779 if (af.getViewport().getCodingComplement() != null)
781 frames = ((SplitFrame) af.getSplitViewContainer()).getAlignFrames();
787 saveAllFrames(frames, jout);
791 } catch (Exception foo)
796 boolean success = true;
798 backupfiles.setWriteSuccess(success);
799 success = backupfiles.rollBackupsAndRenameTempFile();
802 } catch (Exception ex)
804 errorMessage = "Couldn't Write alignment view to Jalview Archive - see error output for details";
805 ex.printStackTrace();
810 private void writeDatasetFor(Hashtable<String, AlignFrame> dsses,
811 String fileName, JarOutputStream jout)
814 for (String dssids : dsses.keySet())
816 AlignFrame _af = dsses.get(dssids);
817 String jfileName = fileName + " Dataset for " + _af.getTitle();
818 if (!jfileName.endsWith(".xml"))
820 jfileName = jfileName + ".xml";
822 saveState(_af.alignPanel, jfileName, true, jout, null);
827 * create a JalviewModel from an alignment view and marshall it to a
831 * panel to create jalview model for
833 * name of alignment panel written to output stream
840 public JalviewModel saveState(AlignmentPanel ap, String fileName,
841 JarOutputStream jout, List<String> viewIds)
843 return saveState(ap, fileName, false, jout, viewIds);
847 * create a JalviewModel from an alignment view and marshall it to a
851 * panel to create jalview model for
853 * name of alignment panel written to output stream
855 * when true, only write the dataset for the alignment, not the data
856 * associated with the view.
862 public JalviewModel saveState(AlignmentPanel ap, String fileName,
863 boolean storeDS, JarOutputStream jout, List<String> viewIds)
867 viewIds = new ArrayList<>();
872 List<UserColourScheme> userColours = new ArrayList<>();
874 AlignViewport av = ap.av;
875 ViewportRanges vpRanges = av.getRanges();
877 final ObjectFactory objectFactory = new ObjectFactory();
878 JalviewModel object = objectFactory.createJalviewModel();
879 object.setVamsasModel(new VAMSAS());
881 // object.setCreationDate(new java.util.Date(System.currentTimeMillis()));
884 GregorianCalendar c = new GregorianCalendar();
885 DatatypeFactory datatypeFactory = DatatypeFactory.newInstance();
886 XMLGregorianCalendar now = datatypeFactory.newXMLGregorianCalendar(c);// gregorianCalendar);
887 object.setCreationDate(now);
888 } catch (DatatypeConfigurationException e)
890 System.err.println("error writing date: " + e.toString());
893 jalview.bin.Cache.getDefault("VERSION", "Development Build"));
896 * rjal is full height alignment, jal is actual alignment with full metadata
897 * but excludes hidden sequences.
899 jalview.datamodel.AlignmentI rjal = av.getAlignment(), jal = rjal;
901 if (av.hasHiddenRows())
903 rjal = jal.getHiddenSequences().getFullAlignment();
906 SequenceSet vamsasSet = new SequenceSet();
908 // JalviewModelSequence jms = new JalviewModelSequence();
910 vamsasSet.setGapChar(jal.getGapCharacter() + "");
912 if (jal.getDataset() != null)
914 // dataset id is the dataset's hashcode
915 vamsasSet.setDatasetId(getDatasetIdRef(jal.getDataset()));
918 // switch jal and the dataset
919 jal = jal.getDataset();
923 if (jal.getProperties() != null)
925 Enumeration en = jal.getProperties().keys();
926 while (en.hasMoreElements())
928 String key = en.nextElement().toString();
929 SequenceSetProperties ssp = new SequenceSetProperties();
931 ssp.setValue(jal.getProperties().get(key).toString());
932 // vamsasSet.addSequenceSetProperties(ssp);
933 vamsasSet.getSequenceSetProperties().add(ssp);
938 Set<String> calcIdSet = new HashSet<>();
939 // record the set of vamsas sequence XML POJO we create.
940 HashMap<String, Sequence> vamsasSetIds = new HashMap<>();
942 for (final SequenceI jds : rjal.getSequences())
944 final SequenceI jdatasq = jds.getDatasetSequence() == null ? jds
945 : jds.getDatasetSequence();
946 String id = seqHash(jds);
947 if (vamsasSetIds.get(id) == null)
949 if (seqRefIds.get(id) != null && !storeDS)
951 // This happens for two reasons: 1. multiple views are being
953 // 2. the hashCode has collided with another sequence's code. This
955 // HAPPEN! (PF00072.15.stk does this)
956 // JBPNote: Uncomment to debug writing out of files that do not read
957 // back in due to ArrayOutOfBoundExceptions.
958 // System.err.println("vamsasSeq backref: "+id+"");
959 // System.err.println(jds.getName()+"
960 // "+jds.getStart()+"-"+jds.getEnd()+" "+jds.getSequenceAsString());
961 // System.err.println("Hashcode: "+seqHash(jds));
962 // SequenceI rsq = (SequenceI) seqRefIds.get(id + "");
963 // System.err.println(rsq.getName()+"
964 // "+rsq.getStart()+"-"+rsq.getEnd()+" "+rsq.getSequenceAsString());
965 // System.err.println("Hashcode: "+seqHash(rsq));
969 vamsasSeq = createVamsasSequence(id, jds);
970 // vamsasSet.addSequence(vamsasSeq);
971 vamsasSet.getSequence().add(vamsasSeq);
972 vamsasSetIds.put(id, vamsasSeq);
973 seqRefIds.put(id, jds);
977 jseq.setStart(jds.getStart());
978 jseq.setEnd(jds.getEnd());
979 jseq.setColour(av.getSequenceColour(jds).getRGB());
981 jseq.setId(id); // jseq id should be a string not a number
984 // Store any sequences this sequence represents
985 if (av.hasHiddenRows())
987 // use rjal, contains the full height alignment
989 av.getAlignment().getHiddenSequences().isHidden(jds));
991 if (av.isHiddenRepSequence(jds))
993 jalview.datamodel.SequenceI[] reps = av
994 .getRepresentedSequences(jds).getSequencesInOrder(rjal);
996 for (int h = 0; h < reps.length; h++)
1000 // jseq.addHiddenSequences(rjal.findIndex(reps[h]));
1001 jseq.getHiddenSequences().add(rjal.findIndex(reps[h]));
1006 // mark sequence as reference - if it is the reference for this view
1007 if (jal.hasSeqrep())
1009 jseq.setViewreference(jds == jal.getSeqrep());
1013 // TODO: omit sequence features from each alignment view's XML dump if we
1014 // are storing dataset
1015 List<SequenceFeature> sfs = jds.getSequenceFeatures();
1016 for (SequenceFeature sf : sfs)
1018 // Features features = new Features();
1019 Feature features = new Feature();
1021 features.setBegin(sf.getBegin());
1022 features.setEnd(sf.getEnd());
1023 features.setDescription(sf.getDescription());
1024 features.setType(sf.getType());
1025 features.setFeatureGroup(sf.getFeatureGroup());
1026 features.setScore(sf.getScore());
1027 if (sf.links != null)
1029 for (int l = 0; l < sf.links.size(); l++)
1031 OtherData keyValue = new OtherData();
1032 keyValue.setKey("LINK_" + l);
1033 keyValue.setValue(sf.links.elementAt(l).toString());
1034 // features.addOtherData(keyValue);
1035 features.getOtherData().add(keyValue);
1038 if (sf.otherDetails != null)
1041 * save feature attributes, which may be simple strings or
1042 * map valued (have sub-attributes)
1044 for (Entry<String, Object> entry : sf.otherDetails.entrySet())
1046 String key = entry.getKey();
1047 Object value = entry.getValue();
1048 if (value instanceof Map<?, ?>)
1050 for (Entry<String, Object> subAttribute : ((Map<String, Object>) value)
1053 OtherData otherData = new OtherData();
1054 otherData.setKey(key);
1055 otherData.setKey2(subAttribute.getKey());
1056 otherData.setValue(subAttribute.getValue().toString());
1057 // features.addOtherData(otherData);
1058 features.getOtherData().add(otherData);
1063 OtherData otherData = new OtherData();
1064 otherData.setKey(key);
1065 otherData.setValue(value.toString());
1066 // features.addOtherData(otherData);
1067 features.getOtherData().add(otherData);
1072 // jseq.addFeatures(features);
1073 jseq.getFeatures().add(features);
1076 if (jdatasq.getAllPDBEntries() != null)
1078 Enumeration<PDBEntry> en = jdatasq.getAllPDBEntries().elements();
1079 while (en.hasMoreElements())
1081 Pdbids pdb = new Pdbids();
1082 jalview.datamodel.PDBEntry entry = en.nextElement();
1084 String pdbId = entry.getId();
1086 pdb.setType(entry.getType());
1089 * Store any structure views associated with this sequence. This
1090 * section copes with duplicate entries in the project, so a dataset
1091 * only view *should* be coped with sensibly.
1093 // This must have been loaded, is it still visible?
1094 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
1095 String matchedFile = null;
1096 for (int f = frames.length - 1; f > -1; f--)
1098 if (frames[f] instanceof StructureViewerBase)
1100 StructureViewerBase viewFrame = (StructureViewerBase) frames[f];
1101 matchedFile = saveStructureState(ap, jds, pdb, entry, viewIds,
1102 matchedFile, viewFrame);
1104 * Only store each structure viewer's state once in the project
1105 * jar. First time through only (storeDS==false)
1107 String viewId = viewFrame.getViewId();
1108 if (!storeDS && !viewIds.contains(viewId))
1110 viewIds.add(viewId);
1113 String viewerState = viewFrame.getStateInfo();
1114 writeJarEntry(jout, getViewerJarEntryName(viewId),
1115 viewerState.getBytes());
1116 } catch (IOException e)
1119 "Error saving viewer state: " + e.getMessage());
1125 if (matchedFile != null || entry.getFile() != null)
1127 if (entry.getFile() != null)
1130 matchedFile = entry.getFile();
1132 pdb.setFile(matchedFile); // entry.getFile());
1133 if (pdbfiles == null)
1135 pdbfiles = new ArrayList<>();
1138 if (!pdbfiles.contains(pdbId))
1140 pdbfiles.add(pdbId);
1141 copyFileToJar(jout, matchedFile, pdbId);
1145 Enumeration<String> props = entry.getProperties();
1146 if (props.hasMoreElements())
1148 // PdbentryItem item = new PdbentryItem();
1149 while (props.hasMoreElements())
1151 Property prop = new Property();
1152 String key = props.nextElement();
1154 prop.setValue(entry.getProperty(key).toString());
1155 // item.addProperty(prop);
1156 pdb.getProperty().add(prop);
1158 // pdb.addPdbentryItem(item);
1161 // jseq.addPdbids(pdb);
1162 jseq.getPdbids().add(pdb);
1166 saveRnaViewers(jout, jseq, jds, viewIds, ap, storeDS);
1168 // jms.addJSeq(jseq);
1169 object.getJSeq().add(jseq);
1172 if (!storeDS && av.hasHiddenRows())
1174 jal = av.getAlignment();
1178 if (storeDS && jal.getCodonFrames() != null)
1180 List<AlignedCodonFrame> jac = jal.getCodonFrames();
1181 for (AlignedCodonFrame acf : jac)
1183 AlcodonFrame alc = new AlcodonFrame();
1184 if (acf.getProtMappings() != null
1185 && acf.getProtMappings().length > 0)
1187 boolean hasMap = false;
1188 SequenceI[] dnas = acf.getdnaSeqs();
1189 jalview.datamodel.Mapping[] pmaps = acf.getProtMappings();
1190 for (int m = 0; m < pmaps.length; m++)
1192 AlcodMap alcmap = new AlcodMap();
1193 alcmap.setDnasq(seqHash(dnas[m]));
1195 createVamsasMapping(pmaps[m], dnas[m], null, false));
1196 // alc.addAlcodMap(alcmap);
1197 alc.getAlcodMap().add(alcmap);
1202 // vamsasSet.addAlcodonFrame(alc);
1203 vamsasSet.getAlcodonFrame().add(alc);
1206 // TODO: delete this ? dead code from 2.8.3->2.9 ?
1208 // AlcodonFrame alc = new AlcodonFrame();
1209 // vamsasSet.addAlcodonFrame(alc);
1210 // for (int p = 0; p < acf.aaWidth; p++)
1212 // Alcodon cmap = new Alcodon();
1213 // if (acf.codons[p] != null)
1215 // // Null codons indicate a gapped column in the translated peptide
1217 // cmap.setPos1(acf.codons[p][0]);
1218 // cmap.setPos2(acf.codons[p][1]);
1219 // cmap.setPos3(acf.codons[p][2]);
1221 // alc.addAlcodon(cmap);
1223 // if (acf.getProtMappings() != null
1224 // && acf.getProtMappings().length > 0)
1226 // SequenceI[] dnas = acf.getdnaSeqs();
1227 // jalview.datamodel.Mapping[] pmaps = acf.getProtMappings();
1228 // for (int m = 0; m < pmaps.length; m++)
1230 // AlcodMap alcmap = new AlcodMap();
1231 // alcmap.setDnasq(seqHash(dnas[m]));
1232 // alcmap.setMapping(createVamsasMapping(pmaps[m], dnas[m], null,
1234 // alc.addAlcodMap(alcmap);
1241 // /////////////////////////////////
1242 if (!storeDS && av.getCurrentTree() != null)
1244 // FIND ANY ASSOCIATED TREES
1245 // NOT IMPLEMENTED FOR HEADLESS STATE AT PRESENT
1246 if (Desktop.desktop != null)
1248 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
1250 for (int t = 0; t < frames.length; t++)
1252 if (frames[t] instanceof TreePanel)
1254 TreePanel tp = (TreePanel) frames[t];
1256 if (tp.getTreeCanvas().getViewport().getAlignment() == jal)
1258 JalviewModel.Tree tree = new JalviewModel.Tree();
1259 tree.setTitle(tp.getTitle());
1260 tree.setCurrentTree((av.getCurrentTree() == tp.getTree()));
1261 tree.setNewick(tp.getTree().print());
1262 tree.setThreshold(tp.getTreeCanvas().getThreshold());
1264 tree.setFitToWindow(tp.fitToWindow.getState());
1265 tree.setFontName(tp.getTreeFont().getName());
1266 tree.setFontSize(tp.getTreeFont().getSize());
1267 tree.setFontStyle(tp.getTreeFont().getStyle());
1268 tree.setMarkUnlinked(tp.placeholdersMenu.getState());
1270 tree.setShowBootstrap(tp.bootstrapMenu.getState());
1271 tree.setShowDistances(tp.distanceMenu.getState());
1273 tree.setHeight(tp.getHeight());
1274 tree.setWidth(tp.getWidth());
1275 tree.setXpos(tp.getX());
1276 tree.setYpos(tp.getY());
1277 tree.setId(makeHashCode(tp, null));
1278 tree.setLinkToAllViews(
1279 tp.getTreeCanvas().isApplyToAllViews());
1281 // jms.addTree(tree);
1282 object.getTree().add(tree);
1292 if (!storeDS && Desktop.desktop != null)
1294 for (JInternalFrame frame : Desktop.desktop.getAllFrames())
1296 if (frame instanceof PCAPanel)
1298 PCAPanel panel = (PCAPanel) frame;
1299 if (panel.getAlignViewport().getAlignment() == jal)
1301 savePCA(panel, object);
1309 * store forward refs from an annotationRow to any groups
1311 IdentityHashMap<SequenceGroup, String> groupRefs = new IdentityHashMap<>();
1314 for (SequenceI sq : jal.getSequences())
1316 // Store annotation on dataset sequences only
1317 AlignmentAnnotation[] aa = sq.getAnnotation();
1318 if (aa != null && aa.length > 0)
1320 storeAlignmentAnnotation(aa, groupRefs, av, calcIdSet, storeDS,
1327 if (jal.getAlignmentAnnotation() != null)
1329 // Store the annotation shown on the alignment.
1330 AlignmentAnnotation[] aa = jal.getAlignmentAnnotation();
1331 storeAlignmentAnnotation(aa, groupRefs, av, calcIdSet, storeDS,
1336 if (jal.getGroups() != null)
1338 JGroup[] groups = new JGroup[jal.getGroups().size()];
1340 for (jalview.datamodel.SequenceGroup sg : jal.getGroups())
1342 JGroup jGroup = new JGroup();
1343 groups[++i] = jGroup;
1345 jGroup.setStart(sg.getStartRes());
1346 jGroup.setEnd(sg.getEndRes());
1347 jGroup.setName(sg.getName());
1348 if (groupRefs.containsKey(sg))
1350 // group has references so set its ID field
1351 jGroup.setId(groupRefs.get(sg));
1353 ColourSchemeI colourScheme = sg.getColourScheme();
1354 if (colourScheme != null)
1356 ResidueShaderI groupColourScheme = sg.getGroupColourScheme();
1357 if (groupColourScheme.conservationApplied())
1359 jGroup.setConsThreshold(groupColourScheme.getConservationInc());
1361 if (colourScheme instanceof jalview.schemes.UserColourScheme)
1364 setUserColourScheme(colourScheme, userColours,
1369 jGroup.setColour(colourScheme.getSchemeName());
1372 else if (colourScheme instanceof jalview.schemes.AnnotationColourGradient)
1374 jGroup.setColour("AnnotationColourGradient");
1375 jGroup.setAnnotationColours(constructAnnotationColours(
1376 (jalview.schemes.AnnotationColourGradient) colourScheme,
1377 userColours, object));
1379 else if (colourScheme instanceof jalview.schemes.UserColourScheme)
1382 setUserColourScheme(colourScheme, userColours, object));
1386 jGroup.setColour(colourScheme.getSchemeName());
1389 jGroup.setPidThreshold(groupColourScheme.getThreshold());
1392 jGroup.setOutlineColour(sg.getOutlineColour().getRGB());
1393 jGroup.setDisplayBoxes(sg.getDisplayBoxes());
1394 jGroup.setDisplayText(sg.getDisplayText());
1395 jGroup.setColourText(sg.getColourText());
1396 jGroup.setTextCol1(sg.textColour.getRGB());
1397 jGroup.setTextCol2(sg.textColour2.getRGB());
1398 jGroup.setTextColThreshold(sg.thresholdTextColour);
1399 jGroup.setShowUnconserved(sg.getShowNonconserved());
1400 jGroup.setIgnoreGapsinConsensus(sg.getIgnoreGapsConsensus());
1401 jGroup.setShowConsensusHistogram(sg.isShowConsensusHistogram());
1402 jGroup.setShowSequenceLogo(sg.isShowSequenceLogo());
1403 jGroup.setNormaliseSequenceLogo(sg.isNormaliseSequenceLogo());
1404 for (SequenceI seq : sg.getSequences())
1406 // jGroup.addSeq(seqHash(seq));
1407 jGroup.getSeq().add(seqHash(seq));
1411 //jms.setJGroup(groups);
1413 for (JGroup grp : groups)
1415 object.getJGroup().add(grp);
1420 // /////////SAVE VIEWPORT
1421 Viewport view = new Viewport();
1422 view.setTitle(ap.alignFrame.getTitle());
1423 view.setSequenceSetId(
1424 makeHashCode(av.getSequenceSetId(), av.getSequenceSetId()));
1425 view.setId(av.getViewId());
1426 if (av.getCodingComplement() != null)
1428 view.setComplementId(av.getCodingComplement().getViewId());
1430 view.setViewName(av.getViewName());
1431 view.setGatheredViews(av.isGatherViewsHere());
1433 Rectangle size = ap.av.getExplodedGeometry();
1434 Rectangle position = size;
1437 size = ap.alignFrame.getBounds();
1438 if (av.getCodingComplement() != null)
1440 position = ((SplitFrame) ap.alignFrame.getSplitViewContainer())
1448 view.setXpos(position.x);
1449 view.setYpos(position.y);
1451 view.setWidth(size.width);
1452 view.setHeight(size.height);
1454 view.setStartRes(vpRanges.getStartRes());
1455 view.setStartSeq(vpRanges.getStartSeq());
1457 if (av.getGlobalColourScheme() instanceof jalview.schemes.UserColourScheme)
1459 view.setBgColour(setUserColourScheme(av.getGlobalColourScheme(),
1460 userColours, object));
1463 .getGlobalColourScheme() instanceof jalview.schemes.AnnotationColourGradient)
1465 AnnotationColourScheme ac = constructAnnotationColours(
1466 (jalview.schemes.AnnotationColourGradient) av
1467 .getGlobalColourScheme(),
1468 userColours, object);
1470 view.setAnnotationColours(ac);
1471 view.setBgColour("AnnotationColourGradient");
1475 view.setBgColour(ColourSchemeProperty
1476 .getColourName(av.getGlobalColourScheme()));
1479 ResidueShaderI vcs = av.getResidueShading();
1480 ColourSchemeI cs = av.getGlobalColourScheme();
1484 if (vcs.conservationApplied())
1486 view.setConsThreshold(vcs.getConservationInc());
1487 if (cs instanceof jalview.schemes.UserColourScheme)
1489 view.setBgColour(setUserColourScheme(cs, userColours, object));
1492 view.setPidThreshold(vcs.getThreshold());
1495 view.setConservationSelected(av.getConservationSelected());
1496 view.setPidSelected(av.getAbovePIDThreshold());
1497 final Font font = av.getFont();
1498 view.setFontName(font.getName());
1499 view.setFontSize(font.getSize());
1500 view.setFontStyle(font.getStyle());
1501 view.setScaleProteinAsCdna(av.getViewStyle().isScaleProteinAsCdna());
1502 view.setRenderGaps(av.isRenderGaps());
1503 view.setShowAnnotation(av.isShowAnnotation());
1504 view.setShowBoxes(av.getShowBoxes());
1505 view.setShowColourText(av.getColourText());
1506 view.setShowFullId(av.getShowJVSuffix());
1507 view.setRightAlignIds(av.isRightAlignIds());
1508 view.setShowSequenceFeatures(av.isShowSequenceFeatures());
1509 view.setShowText(av.getShowText());
1510 view.setShowUnconserved(av.getShowUnconserved());
1511 view.setWrapAlignment(av.getWrapAlignment());
1512 view.setTextCol1(av.getTextColour().getRGB());
1513 view.setTextCol2(av.getTextColour2().getRGB());
1514 view.setTextColThreshold(av.getThresholdTextColour());
1515 view.setShowConsensusHistogram(av.isShowConsensusHistogram());
1516 view.setShowSequenceLogo(av.isShowSequenceLogo());
1517 view.setNormaliseSequenceLogo(av.isNormaliseSequenceLogo());
1518 view.setShowGroupConsensus(av.isShowGroupConsensus());
1519 view.setShowGroupConservation(av.isShowGroupConservation());
1520 view.setShowNPfeatureTooltip(av.isShowNPFeats());
1521 view.setShowDbRefTooltip(av.isShowDBRefs());
1522 view.setFollowHighlight(av.isFollowHighlight());
1523 view.setFollowSelection(av.followSelection);
1524 view.setIgnoreGapsinConsensus(av.isIgnoreGapsConsensus());
1525 if (av.getFeaturesDisplayed() != null)
1527 FeatureSettings fs = new FeatureSettings();
1529 FeatureRenderer fr = ap.getSeqPanel().seqCanvas
1530 .getFeatureRenderer();
1531 String[] renderOrder = fr.getRenderOrder().toArray(new String[0]);
1533 Vector<String> settingsAdded = new Vector<>();
1534 if (renderOrder != null)
1536 for (String featureType : renderOrder)
1538 FeatureSettings.Setting setting = new FeatureSettings.Setting();
1539 setting.setType(featureType);
1542 * save any filter for the feature type
1544 FeatureMatcherSetI filter = fr.getFeatureFilter(featureType);
1545 if (filter != null) {
1546 Iterator<FeatureMatcherI> filters = filter.getMatchers().iterator();
1547 FeatureMatcherI firstFilter = filters.next();
1548 setting.setMatcherSet(Jalview2XML.marshalFilter(
1549 firstFilter, filters, filter.isAnded()));
1553 * save colour scheme for the feature type
1555 FeatureColourI fcol = fr.getFeatureStyle(featureType);
1556 if (!fcol.isSimpleColour())
1558 setting.setColour(fcol.getMaxColour().getRGB());
1559 setting.setMincolour(fcol.getMinColour().getRGB());
1560 setting.setMin(fcol.getMin());
1561 setting.setMax(fcol.getMax());
1562 setting.setColourByLabel(fcol.isColourByLabel());
1563 if (fcol.isColourByAttribute())
1565 String[] attName = fcol.getAttributeName();
1566 setting.getAttributeName().add(attName[0]);
1567 if (attName.length > 1)
1569 setting.getAttributeName().add(attName[1]);
1572 setting.setAutoScale(fcol.isAutoScaled());
1573 setting.setThreshold(fcol.getThreshold());
1574 Color noColour = fcol.getNoColour();
1575 if (noColour == null)
1577 setting.setNoValueColour(NoValueColour.NONE);
1579 else if (noColour.equals(fcol.getMaxColour()))
1581 setting.setNoValueColour(NoValueColour.MAX);
1585 setting.setNoValueColour(NoValueColour.MIN);
1587 // -1 = No threshold, 0 = Below, 1 = Above
1588 setting.setThreshstate(fcol.isAboveThreshold() ? 1
1589 : (fcol.isBelowThreshold() ? 0 : -1));
1593 setting.setColour(fcol.getColour().getRGB());
1597 av.getFeaturesDisplayed().isVisible(featureType));
1599 .getOrder(featureType);
1602 setting.setOrder(rorder);
1604 /// fs.addSetting(setting);
1605 fs.getSetting().add(setting);
1606 settingsAdded.addElement(featureType);
1610 // is groups actually supposed to be a map here ?
1611 Iterator<String> en = fr.getFeatureGroups().iterator();
1612 Vector<String> groupsAdded = new Vector<>();
1613 while (en.hasNext())
1615 String grp = en.next();
1616 if (groupsAdded.contains(grp))
1620 Group g = new Group();
1622 g.setDisplay(((Boolean) fr.checkGroupVisibility(grp, false))
1625 fs.getGroup().add(g);
1626 groupsAdded.addElement(grp);
1628 // jms.setFeatureSettings(fs);
1629 object.setFeatureSettings(fs);
1632 if (av.hasHiddenColumns())
1634 jalview.datamodel.HiddenColumns hidden = av.getAlignment()
1635 .getHiddenColumns();
1638 warn("REPORT BUG: avoided null columnselection bug (DMAM reported). Please contact Jim about this.");
1642 Iterator<int[]> hiddenRegions = hidden.iterator();
1643 while (hiddenRegions.hasNext())
1645 int[] region = hiddenRegions.next();
1646 HiddenColumns hc = new HiddenColumns();
1647 hc.setStart(region[0]);
1648 hc.setEnd(region[1]);
1649 // view.addHiddenColumns(hc);
1650 view.getHiddenColumns().add(hc);
1654 if (calcIdSet.size() > 0)
1656 for (String calcId : calcIdSet)
1658 if (calcId.trim().length() > 0)
1660 CalcIdParam cidp = createCalcIdParam(calcId, av);
1661 // Some calcIds have no parameters.
1664 // view.addCalcIdParam(cidp);
1665 view.getCalcIdParam().add(cidp);
1671 // jms.addViewport(view);
1672 object.getViewport().add(view);
1674 // object.setJalviewModelSequence(jms);
1675 // object.getVamsasModel().addSequenceSet(vamsasSet);
1676 object.getVamsasModel().getSequenceSet().add(vamsasSet);
1678 if (jout != null && fileName != null)
1680 // We may not want to write the object to disk,
1681 // eg we can copy the alignViewport to a new view object
1682 // using save and then load
1685 fileName = fileName.replace('\\', '/');
1686 System.out.println("Writing jar entry " + fileName);
1687 JarEntry entry = new JarEntry(fileName);
1688 jout.putNextEntry(entry);
1689 PrintWriter pout = new PrintWriter(
1690 new OutputStreamWriter(jout, UTF_8));
1691 JAXBContext jaxbContext = JAXBContext
1692 .newInstance(JalviewModel.class);
1693 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
1695 // output pretty printed
1696 // jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
1697 jaxbMarshaller.marshal(
1698 new ObjectFactory().createJalviewModel(object), pout);
1700 // jaxbMarshaller.marshal(object, pout);
1701 // marshaller.marshal(object);
1704 } catch (Exception ex)
1706 // TODO: raise error in GUI if marshalling failed.
1707 System.err.println("Error writing Jalview project");
1708 ex.printStackTrace();
1715 * Writes PCA viewer attributes and computed values to an XML model object and
1716 * adds it to the JalviewModel. Any exceptions are reported by logging.
1718 protected void savePCA(PCAPanel panel, JalviewModel object)
1722 PcaViewer viewer = new PcaViewer();
1723 viewer.setHeight(panel.getHeight());
1724 viewer.setWidth(panel.getWidth());
1725 viewer.setXpos(panel.getX());
1726 viewer.setYpos(panel.getY());
1727 viewer.setTitle(panel.getTitle());
1728 PCAModel pcaModel = panel.getPcaModel();
1729 viewer.setScoreModelName(pcaModel.getScoreModelName());
1730 viewer.setXDim(panel.getSelectedDimensionIndex(X));
1731 viewer.setYDim(panel.getSelectedDimensionIndex(Y));
1732 viewer.setZDim(panel.getSelectedDimensionIndex(Z));
1734 panel.getRotatableCanvas().getBackgroundColour().getRGB());
1735 viewer.setScaleFactor(panel.getRotatableCanvas().getScaleFactor());
1736 float[] spMin = panel.getRotatableCanvas().getSeqMin();
1737 SeqPointMin spmin = new SeqPointMin();
1738 spmin.setXPos(spMin[0]);
1739 spmin.setYPos(spMin[1]);
1740 spmin.setZPos(spMin[2]);
1741 viewer.setSeqPointMin(spmin);
1742 float[] spMax = panel.getRotatableCanvas().getSeqMax();
1743 SeqPointMax spmax = new SeqPointMax();
1744 spmax.setXPos(spMax[0]);
1745 spmax.setYPos(spMax[1]);
1746 spmax.setZPos(spMax[2]);
1747 viewer.setSeqPointMax(spmax);
1748 viewer.setShowLabels(panel.getRotatableCanvas().isShowLabels());
1749 viewer.setLinkToAllViews(
1750 panel.getRotatableCanvas().isApplyToAllViews());
1751 SimilarityParamsI sp = pcaModel.getSimilarityParameters();
1752 viewer.setIncludeGaps(sp.includeGaps());
1753 viewer.setMatchGaps(sp.matchGaps());
1754 viewer.setIncludeGappedColumns(sp.includeGappedColumns());
1755 viewer.setDenominateByShortestLength(sp.denominateByShortestLength());
1758 * sequence points on display
1760 for (jalview.datamodel.SequencePoint spt : pcaModel
1761 .getSequencePoints())
1763 SequencePoint point = new SequencePoint();
1764 point.setSequenceRef(seqHash(spt.getSequence()));
1765 point.setXPos(spt.coord.x);
1766 point.setYPos(spt.coord.y);
1767 point.setZPos(spt.coord.z);
1768 viewer.getSequencePoint().add(point);
1772 * (end points of) axes on display
1774 for (Point p : panel.getRotatableCanvas().getAxisEndPoints())
1777 Axis axis = new Axis();
1781 viewer.getAxis().add(axis);
1785 * raw PCA data (note we are not restoring PCA inputs here -
1786 * alignment view, score model, similarity parameters)
1788 PcaDataType data = new PcaDataType();
1789 viewer.setPcaData(data);
1790 PCA pca = pcaModel.getPcaData();
1792 DoubleMatrix pm = new DoubleMatrix();
1793 saveDoubleMatrix(pca.getPairwiseScores(), pm);
1794 data.setPairwiseMatrix(pm);
1796 DoubleMatrix tm = new DoubleMatrix();
1797 saveDoubleMatrix(pca.getTridiagonal(), tm);
1798 data.setTridiagonalMatrix(tm);
1800 DoubleMatrix eigenMatrix = new DoubleMatrix();
1801 data.setEigenMatrix(eigenMatrix);
1802 saveDoubleMatrix(pca.getEigenmatrix(), eigenMatrix);
1804 object.getPcaViewer().add(viewer);
1805 } catch (Throwable t)
1807 Cache.log.error("Error saving PCA: " + t.getMessage());
1812 * Stores values from a matrix into an XML element, including (if present) the
1817 * @see #loadDoubleMatrix(DoubleMatrix)
1819 protected void saveDoubleMatrix(MatrixI m, DoubleMatrix xmlMatrix)
1821 xmlMatrix.setRows(m.height());
1822 xmlMatrix.setColumns(m.width());
1823 for (int i = 0; i < m.height(); i++)
1825 DoubleVector row = new DoubleVector();
1826 for (int j = 0; j < m.width(); j++)
1828 row.getV().add(m.getValue(i, j));
1830 xmlMatrix.getRow().add(row);
1832 if (m.getD() != null)
1834 DoubleVector dVector = new DoubleVector();
1835 for (double d : m.getD())
1837 dVector.getV().add(d);
1839 xmlMatrix.setD(dVector);
1841 if (m.getE() != null)
1843 DoubleVector eVector = new DoubleVector();
1844 for (double e : m.getE())
1846 eVector.getV().add(e);
1848 xmlMatrix.setE(eVector);
1853 * Loads XML matrix data into a new Matrix object, including the D and/or E
1854 * vectors (if present)
1858 * @see Jalview2XML#saveDoubleMatrix(MatrixI, DoubleMatrix)
1860 protected MatrixI loadDoubleMatrix(DoubleMatrix mData)
1862 int rows = mData.getRows();
1863 double[][] vals = new double[rows][];
1865 for (int i = 0; i < rows; i++)
1867 List<Double> dVector = mData.getRow().get(i).getV();
1868 vals[i] = new double[dVector.size()];
1870 for (Double d : dVector)
1876 MatrixI m = new Matrix(vals);
1878 if (mData.getD() != null)
1880 List<Double> dVector = mData.getD().getV();
1881 double[] vec = new double[dVector.size()];
1883 for (Double d : dVector)
1889 if (mData.getE() != null)
1891 List<Double> dVector = mData.getE().getV();
1892 double[] vec = new double[dVector.size()];
1894 for (Double d : dVector)
1905 * Save any Varna viewers linked to this sequence. Writes an rnaViewer element
1906 * for each viewer, with
1908 * <li>viewer geometry (position, size, split pane divider location)</li>
1909 * <li>index of the selected structure in the viewer (currently shows gapped
1911 * <li>the id of the annotation holding RNA secondary structure</li>
1912 * <li>(currently only one SS is shown per viewer, may be more in future)</li>
1914 * Varna viewer state is also written out (in native Varna XML) to separate
1915 * project jar entries. A separate entry is written for each RNA structure
1916 * displayed, with the naming convention
1918 * <li>rna_viewId_sequenceId_annotationId_[gapped|trimmed]</li>
1926 * @param storeDataset
1928 protected void saveRnaViewers(JarOutputStream jout, JSeq jseq,
1929 final SequenceI jds, List<String> viewIds, AlignmentPanel ap,
1930 boolean storeDataset)
1932 if (Desktop.desktop == null)
1936 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
1937 for (int f = frames.length - 1; f > -1; f--)
1939 if (frames[f] instanceof AppVarna)
1941 AppVarna varna = (AppVarna) frames[f];
1943 * link the sequence to every viewer that is showing it and is linked to
1944 * its alignment panel
1946 if (varna.isListeningFor(jds) && ap == varna.getAlignmentPanel())
1948 String viewId = varna.getViewId();
1949 RnaViewer rna = new RnaViewer();
1950 rna.setViewId(viewId);
1951 rna.setTitle(varna.getTitle());
1952 rna.setXpos(varna.getX());
1953 rna.setYpos(varna.getY());
1954 rna.setWidth(varna.getWidth());
1955 rna.setHeight(varna.getHeight());
1956 rna.setDividerLocation(varna.getDividerLocation());
1957 rna.setSelectedRna(varna.getSelectedIndex());
1958 // jseq.addRnaViewer(rna);
1959 jseq.getRnaViewer().add(rna);
1962 * Store each Varna panel's state once in the project per sequence.
1963 * First time through only (storeDataset==false)
1965 // boolean storeSessions = false;
1966 // String sequenceViewId = viewId + seqsToIds.get(jds);
1967 // if (!storeDataset && !viewIds.contains(sequenceViewId))
1969 // viewIds.add(sequenceViewId);
1970 // storeSessions = true;
1972 for (RnaModel model : varna.getModels())
1974 if (model.seq == jds)
1977 * VARNA saves each view (sequence or alignment secondary
1978 * structure, gapped or trimmed) as a separate XML file
1980 String jarEntryName = rnaSessions.get(model);
1981 if (jarEntryName == null)
1984 String varnaStateFile = varna.getStateInfo(model.rna);
1985 jarEntryName = RNA_PREFIX + viewId + "_" + nextCounter();
1986 copyFileToJar(jout, varnaStateFile, jarEntryName);
1987 rnaSessions.put(model, jarEntryName);
1989 SecondaryStructure ss = new SecondaryStructure();
1990 String annotationId = varna.getAnnotation(jds).annotationId;
1991 ss.setAnnotationId(annotationId);
1992 ss.setViewerState(jarEntryName);
1993 ss.setGapped(model.gapped);
1994 ss.setTitle(model.title);
1995 // rna.addSecondaryStructure(ss);
1996 rna.getSecondaryStructure().add(ss);
2005 * Copy the contents of a file to a new entry added to the output jar
2009 * @param jarEntryName
2011 protected void copyFileToJar(JarOutputStream jout, String infilePath,
2012 String jarEntryName)
2014 DataInputStream dis = null;
2017 File file = new File(infilePath);
2018 if (file.exists() && jout != null)
2020 dis = new DataInputStream(new FileInputStream(file));
2021 byte[] data = new byte[(int) file.length()];
2022 dis.readFully(data);
2023 writeJarEntry(jout, jarEntryName, data);
2025 } catch (Exception ex)
2027 ex.printStackTrace();
2035 } catch (IOException e)
2044 * Write the data to a new entry of given name in the output jar file
2047 * @param jarEntryName
2049 * @throws IOException
2051 protected void writeJarEntry(JarOutputStream jout, String jarEntryName,
2052 byte[] data) throws IOException
2056 jarEntryName = jarEntryName.replace('\\','/');
2057 System.out.println("Writing jar entry " + jarEntryName);
2058 jout.putNextEntry(new JarEntry(jarEntryName));
2059 DataOutputStream dout = new DataOutputStream(jout);
2060 dout.write(data, 0, data.length);
2067 * Save the state of a structure viewer
2072 * the archive XML element under which to save the state
2075 * @param matchedFile
2079 protected String saveStructureState(AlignmentPanel ap, SequenceI jds,
2080 Pdbids pdb, PDBEntry entry, List<String> viewIds,
2081 String matchedFile, StructureViewerBase viewFrame)
2083 final AAStructureBindingModel bindingModel = viewFrame.getBinding();
2086 * Look for any bindings for this viewer to the PDB file of interest
2087 * (including part matches excluding chain id)
2089 for (int peid = 0; peid < bindingModel.getPdbCount(); peid++)
2091 final PDBEntry pdbentry = bindingModel.getPdbEntry(peid);
2092 final String pdbId = pdbentry.getId();
2093 if (!pdbId.equals(entry.getId())
2094 && !(entry.getId().length() > 4 && entry.getId().toLowerCase()
2095 .startsWith(pdbId.toLowerCase())))
2098 * not interested in a binding to a different PDB entry here
2102 if (matchedFile == null)
2104 matchedFile = pdbentry.getFile();
2106 else if (!matchedFile.equals(pdbentry.getFile()))
2109 "Probably lost some PDB-Sequence mappings for this structure file (which apparently has same PDB Entry code): "
2110 + pdbentry.getFile());
2114 // can get at it if the ID
2115 // match is ambiguous (e.g.
2118 for (int smap = 0; smap < viewFrame.getBinding()
2119 .getSequence()[peid].length; smap++)
2121 // if (jal.findIndex(jmol.jmb.sequence[peid][smap]) > -1)
2122 if (jds == viewFrame.getBinding().getSequence()[peid][smap])
2124 StructureState state = new StructureState();
2125 state.setVisible(true);
2126 state.setXpos(viewFrame.getX());
2127 state.setYpos(viewFrame.getY());
2128 state.setWidth(viewFrame.getWidth());
2129 state.setHeight(viewFrame.getHeight());
2130 final String viewId = viewFrame.getViewId();
2131 state.setViewId(viewId);
2132 state.setAlignwithAlignPanel(viewFrame.isUsedforaligment(ap));
2133 state.setColourwithAlignPanel(viewFrame.isUsedforcolourby(ap));
2134 state.setColourByJmol(viewFrame.isColouredByViewer());
2135 state.setType(viewFrame.getViewerType().toString());
2136 // pdb.addStructureState(state);
2137 pdb.getStructureState().add(state);
2145 * Populates the AnnotationColourScheme xml for save. This captures the
2146 * settings of the options in the 'Colour by Annotation' dialog.
2149 * @param userColours
2153 private AnnotationColourScheme constructAnnotationColours(
2154 AnnotationColourGradient acg, List<UserColourScheme> userColours,
2157 AnnotationColourScheme ac = new AnnotationColourScheme();
2158 ac.setAboveThreshold(acg.getAboveThreshold());
2159 ac.setThreshold(acg.getAnnotationThreshold());
2160 // 2.10.2 save annotationId (unique) not annotation label
2161 ac.setAnnotation(acg.getAnnotation().annotationId);
2162 if (acg.getBaseColour() instanceof UserColourScheme)
2165 setUserColourScheme(acg.getBaseColour(), userColours, jm));
2170 ColourSchemeProperty.getColourName(acg.getBaseColour()));
2173 ac.setMaxColour(acg.getMaxColour().getRGB());
2174 ac.setMinColour(acg.getMinColour().getRGB());
2175 ac.setPerSequence(acg.isSeqAssociated());
2176 ac.setPredefinedColours(acg.isPredefinedColours());
2180 private void storeAlignmentAnnotation(AlignmentAnnotation[] aa,
2181 IdentityHashMap<SequenceGroup, String> groupRefs,
2182 AlignmentViewport av, Set<String> calcIdSet, boolean storeDS,
2183 SequenceSet vamsasSet)
2186 for (int i = 0; i < aa.length; i++)
2188 Annotation an = new Annotation();
2190 AlignmentAnnotation annotation = aa[i];
2191 if (annotation.annotationId != null)
2193 annotationIds.put(annotation.annotationId, annotation);
2196 an.setId(annotation.annotationId);
2198 an.setVisible(annotation.visible);
2200 an.setDescription(annotation.description);
2202 if (annotation.sequenceRef != null)
2204 // 2.9 JAL-1781 xref on sequence id rather than name
2205 an.setSequenceRef(seqsToIds.get(annotation.sequenceRef));
2207 if (annotation.groupRef != null)
2209 String groupIdr = groupRefs.get(annotation.groupRef);
2210 if (groupIdr == null)
2212 // make a locally unique String
2213 groupRefs.put(annotation.groupRef,
2214 groupIdr = ("" + System.currentTimeMillis()
2215 + annotation.groupRef.getName()
2216 + groupRefs.size()));
2218 an.setGroupRef(groupIdr.toString());
2221 // store all visualization attributes for annotation
2222 an.setGraphHeight(annotation.graphHeight);
2223 an.setCentreColLabels(annotation.centreColLabels);
2224 an.setScaleColLabels(annotation.scaleColLabel);
2225 an.setShowAllColLabels(annotation.showAllColLabels);
2226 an.setBelowAlignment(annotation.belowAlignment);
2228 if (annotation.graph > 0)
2231 an.setGraphType(annotation.graph);
2232 an.setGraphGroup(annotation.graphGroup);
2233 if (annotation.getThreshold() != null)
2235 ThresholdLine line = new ThresholdLine();
2236 line.setLabel(annotation.getThreshold().label);
2237 line.setValue(annotation.getThreshold().value);
2238 line.setColour(annotation.getThreshold().colour.getRGB());
2239 an.setThresholdLine(line);
2247 an.setLabel(annotation.label);
2249 if (annotation == av.getAlignmentQualityAnnot()
2250 || annotation == av.getAlignmentConservationAnnotation()
2251 || annotation == av.getAlignmentConsensusAnnotation()
2252 || annotation.autoCalculated)
2254 // new way of indicating autocalculated annotation -
2255 an.setAutoCalculated(annotation.autoCalculated);
2257 if (annotation.hasScore())
2259 an.setScore(annotation.getScore());
2262 if (annotation.getCalcId() != null)
2264 calcIdSet.add(annotation.getCalcId());
2265 an.setCalcId(annotation.getCalcId());
2267 if (annotation.hasProperties())
2269 for (String pr : annotation.getProperties())
2271 jalview.xml.binding.jalview.Annotation.Property prop = new jalview.xml.binding.jalview.Annotation.Property();
2273 prop.setValue(annotation.getProperty(pr));
2274 // an.addProperty(prop);
2275 an.getProperty().add(prop);
2279 AnnotationElement ae;
2280 if (annotation.annotations != null)
2282 an.setScoreOnly(false);
2283 for (int a = 0; a < annotation.annotations.length; a++)
2285 if ((annotation == null) || (annotation.annotations[a] == null))
2290 ae = new AnnotationElement();
2291 if (annotation.annotations[a].description != null)
2293 ae.setDescription(annotation.annotations[a].description);
2295 if (annotation.annotations[a].displayCharacter != null)
2297 ae.setDisplayCharacter(
2298 annotation.annotations[a].displayCharacter);
2301 if (!Float.isNaN(annotation.annotations[a].value))
2303 ae.setValue(annotation.annotations[a].value);
2307 if (annotation.annotations[a].secondaryStructure > ' ')
2309 ae.setSecondaryStructure(
2310 annotation.annotations[a].secondaryStructure + "");
2313 if (annotation.annotations[a].colour != null
2314 && annotation.annotations[a].colour != java.awt.Color.black)
2316 ae.setColour(annotation.annotations[a].colour.getRGB());
2319 // an.addAnnotationElement(ae);
2320 an.getAnnotationElement().add(ae);
2321 if (annotation.autoCalculated)
2323 // only write one non-null entry into the annotation row -
2324 // sufficient to get the visualization attributes necessary to
2332 an.setScoreOnly(true);
2334 if (!storeDS || (storeDS && !annotation.autoCalculated))
2336 // skip autocalculated annotation - these are only provided for
2338 // vamsasSet.addAnnotation(an);
2339 vamsasSet.getAnnotation().add(an);
2345 private CalcIdParam createCalcIdParam(String calcId, AlignViewport av)
2347 AutoCalcSetting settings = av.getCalcIdSettingsFor(calcId);
2348 if (settings != null)
2350 CalcIdParam vCalcIdParam = new CalcIdParam();
2351 vCalcIdParam.setCalcId(calcId);
2352 // vCalcIdParam.addServiceURL(settings.getServiceURI());
2353 vCalcIdParam.getServiceURL().add(settings.getServiceURI());
2354 // generic URI allowing a third party to resolve another instance of the
2355 // service used for this calculation
2356 for (String url : settings.getServiceURLs())
2358 // vCalcIdParam.addServiceURL(urls);
2359 vCalcIdParam.getServiceURL().add(url);
2361 vCalcIdParam.setVersion("1.0");
2362 if (settings.getPreset() != null)
2364 WsParamSetI setting = settings.getPreset();
2365 vCalcIdParam.setName(setting.getName());
2366 vCalcIdParam.setDescription(setting.getDescription());
2370 vCalcIdParam.setName("");
2371 vCalcIdParam.setDescription("Last used parameters");
2373 // need to be able to recover 1) settings 2) user-defined presets or
2374 // recreate settings from preset 3) predefined settings provided by
2375 // service - or settings that can be transferred (or discarded)
2376 vCalcIdParam.setParameters(
2377 settings.getWsParamFile().replace("\n", "|\\n|"));
2378 vCalcIdParam.setAutoUpdate(settings.isAutoUpdate());
2379 // todo - decide if updateImmediately is needed for any projects.
2381 return vCalcIdParam;
2386 private boolean recoverCalcIdParam(CalcIdParam calcIdParam,
2389 if (calcIdParam.getVersion().equals("1.0"))
2391 final String[] calcIds = calcIdParam.getServiceURL().toArray(new String[0]);
2392 Jws2Instance service = Jws2Discoverer.getDiscoverer()
2393 .getPreferredServiceFor(calcIds);
2394 if (service != null)
2396 WsParamSetI parmSet = null;
2399 parmSet = service.getParamStore().parseServiceParameterFile(
2400 calcIdParam.getName(), calcIdParam.getDescription(),
2402 calcIdParam.getParameters().replace("|\\n|", "\n"));
2403 } catch (IOException x)
2405 warn("Couldn't parse parameter data for "
2406 + calcIdParam.getCalcId(), x);
2409 List<ArgumentI> argList = null;
2410 if (calcIdParam.getName().length() > 0)
2412 parmSet = service.getParamStore()
2413 .getPreset(calcIdParam.getName());
2414 if (parmSet != null)
2416 // TODO : check we have a good match with settings in AACon -
2417 // otherwise we'll need to create a new preset
2422 argList = parmSet.getArguments();
2425 AAConSettings settings = new AAConSettings(
2426 calcIdParam.isAutoUpdate(), service, parmSet, argList);
2427 av.setCalcIdSettingsFor(calcIdParam.getCalcId(), settings,
2428 calcIdParam.isNeedsUpdate());
2433 warn("Cannot resolve a service for the parameters used in this project. Try configuring a JABAWS server.");
2437 throw new Error(MessageManager.formatMessage(
2438 "error.unsupported_version_calcIdparam", new Object[]
2439 { calcIdParam.toString() }));
2443 * External mapping between jalview objects and objects yielding a valid and
2444 * unique object ID string. This is null for normal Jalview project IO, but
2445 * non-null when a jalview project is being read or written as part of a
2448 IdentityHashMap jv2vobj = null;
2451 * Construct a unique ID for jvobj using either existing bindings or if none
2452 * exist, the result of the hashcode call for the object.
2455 * jalview data object
2456 * @return unique ID for referring to jvobj
2458 private String makeHashCode(Object jvobj, String altCode)
2460 if (jv2vobj != null)
2462 Object id = jv2vobj.get(jvobj);
2465 return id.toString();
2467 // check string ID mappings
2468 if (jvids2vobj != null && jvobj instanceof String)
2470 id = jvids2vobj.get(jvobj);
2474 return id.toString();
2476 // give up and warn that something has gone wrong
2477 warn("Cannot find ID for object in external mapping : " + jvobj);
2483 * return local jalview object mapped to ID, if it exists
2487 * @return null or object bound to idcode
2489 private Object retrieveExistingObj(String idcode)
2491 if (idcode != null && vobj2jv != null)
2493 return vobj2jv.get(idcode);
2499 * binding from ID strings from external mapping table to jalview data model
2502 private Hashtable vobj2jv;
2504 private Sequence createVamsasSequence(String id, SequenceI jds)
2506 return createVamsasSequence(true, id, jds, null);
2509 private Sequence createVamsasSequence(boolean recurse, String id,
2510 SequenceI jds, SequenceI parentseq)
2512 Sequence vamsasSeq = new Sequence();
2513 vamsasSeq.setId(id);
2514 vamsasSeq.setName(jds.getName());
2515 vamsasSeq.setSequence(jds.getSequenceAsString());
2516 vamsasSeq.setDescription(jds.getDescription());
2517 List<DBRefEntry> dbrefs = null;
2518 if (jds.getDatasetSequence() != null)
2520 vamsasSeq.setDsseqid(seqHash(jds.getDatasetSequence()));
2524 // seqId==dsseqid so we can tell which sequences really are
2525 // dataset sequences only
2526 vamsasSeq.setDsseqid(id);
2527 dbrefs = jds.getDBRefs();
2528 if (parentseq == null)
2535 for (int d = 0, nd = dbrefs.size(); d < nd; d++)
2537 DBRef dbref = new DBRef();
2538 DBRefEntry ref = dbrefs.get(d);
2539 dbref.setSource(ref.getSource());
2540 dbref.setVersion(ref.getVersion());
2541 dbref.setAccessionId(ref.getAccessionId());
2544 Mapping mp = createVamsasMapping(ref.getMap(), parentseq,
2546 dbref.setMapping(mp);
2548 // vamsasSeq.addDBRef(dbref);
2549 vamsasSeq.getDBRef().add(dbref);
2555 private Mapping createVamsasMapping(jalview.datamodel.Mapping jmp,
2556 SequenceI parentseq, SequenceI jds, boolean recurse)
2559 if (jmp.getMap() != null)
2563 jalview.util.MapList mlst = jmp.getMap();
2564 List<int[]> r = mlst.getFromRanges();
2565 for (int[] range : r)
2567 MapListFrom mfrom = new MapListFrom();
2568 mfrom.setStart(range[0]);
2569 mfrom.setEnd(range[1]);
2570 // mp.addMapListFrom(mfrom);
2571 mp.getMapListFrom().add(mfrom);
2573 r = mlst.getToRanges();
2574 for (int[] range : r)
2576 MapListTo mto = new MapListTo();
2577 mto.setStart(range[0]);
2578 mto.setEnd(range[1]);
2579 // mp.addMapListTo(mto);
2580 mp.getMapListTo().add(mto);
2582 mp.setMapFromUnit(BigInteger.valueOf(mlst.getFromRatio()));
2583 mp.setMapToUnit(BigInteger.valueOf(mlst.getToRatio()));
2584 if (jmp.getTo() != null)
2586 // MappingChoice mpc = new MappingChoice();
2588 // check/create ID for the sequence referenced by getTo()
2591 SequenceI ps = null;
2592 if (parentseq != jmp.getTo()
2593 && parentseq.getDatasetSequence() != jmp.getTo())
2595 // chaining dbref rather than a handshaking one
2596 jmpid = seqHash(ps = jmp.getTo());
2600 jmpid = seqHash(ps = parentseq);
2602 // mpc.setDseqFor(jmpid);
2603 mp.setDseqFor(jmpid);
2604 if (!seqRefIds.containsKey(jmpid))
2606 jalview.bin.Cache.log.debug("creatign new DseqFor ID");
2607 seqRefIds.put(jmpid, ps);
2611 jalview.bin.Cache.log.debug("reusing DseqFor ID");
2614 // mp.setMappingChoice(mpc);
2620 String setUserColourScheme(jalview.schemes.ColourSchemeI cs,
2621 List<UserColourScheme> userColours, JalviewModel jm)
2624 jalview.schemes.UserColourScheme ucs = (jalview.schemes.UserColourScheme) cs;
2625 boolean newucs = false;
2626 if (!userColours.contains(ucs))
2628 userColours.add(ucs);
2631 id = "ucs" + userColours.indexOf(ucs);
2634 // actually create the scheme's entry in the XML model
2635 java.awt.Color[] colours = ucs.getColours();
2636 UserColours uc = new UserColours();
2637 // UserColourScheme jbucs = new UserColourScheme();
2638 JalviewUserColours jbucs = new JalviewUserColours();
2640 for (int i = 0; i < colours.length; i++)
2642 Colour col = new Colour();
2643 col.setName(ResidueProperties.aa[i]);
2644 col.setRGB(jalview.util.Format.getHexString(colours[i]));
2645 // jbucs.addColour(col);
2646 jbucs.getColour().add(col);
2648 if (ucs.getLowerCaseColours() != null)
2650 colours = ucs.getLowerCaseColours();
2651 for (int i = 0; i < colours.length; i++)
2653 Colour col = new Colour();
2654 col.setName(ResidueProperties.aa[i].toLowerCase());
2655 col.setRGB(jalview.util.Format.getHexString(colours[i]));
2656 // jbucs.addColour(col);
2657 jbucs.getColour().add(col);
2662 uc.setUserColourScheme(jbucs);
2663 // jm.addUserColours(uc);
2664 jm.getUserColours().add(uc);
2670 jalview.schemes.UserColourScheme getUserColourScheme(
2671 JalviewModel jm, String id)
2673 List<UserColours> uc = jm.getUserColours();
2674 UserColours colours = null;
2676 for (int i = 0; i < uc.length; i++)
2678 if (uc[i].getId().equals(id))
2685 for (UserColours c : uc)
2687 if (c.getId().equals(id))
2694 java.awt.Color[] newColours = new java.awt.Color[24];
2696 for (int i = 0; i < 24; i++)
2698 newColours[i] = new java.awt.Color(Integer.parseInt(
2699 // colours.getUserColourScheme().getColour(i).getRGB(), 16));
2700 colours.getUserColourScheme().getColour().get(i).getRGB(),
2704 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(
2707 if (colours.getUserColourScheme().getColour().size()/*Count()*/ > 24)
2709 newColours = new java.awt.Color[23];
2710 for (int i = 0; i < 23; i++)
2712 newColours[i] = new java.awt.Color(Integer.parseInt(
2713 colours.getUserColourScheme().getColour().get(i + 24)
2717 ucs.setLowerCaseColours(newColours);
2724 * contains last error message (if any) encountered by XML loader.
2726 String errorMessage = null;
2729 * flag to control whether the Jalview2XML_V1 parser should be deferred to if
2730 * exceptions are raised during project XML parsing
2732 public boolean attemptversion1parse = false;
2735 * Load a jalview project archive from a jar file
2738 * - HTTP URL or filename
2740 public AlignFrame loadJalviewAlign(final Object file)
2743 jalview.gui.AlignFrame af = null;
2747 // create list to store references for any new Jmol viewers created
2748 newStructureViewers = new Vector<>();
2749 // UNMARSHALLER SEEMS TO CLOSE JARINPUTSTREAM, MOST ANNOYING
2750 // Workaround is to make sure caller implements the JarInputStreamProvider
2752 // so we can re-open the jar input stream for each entry.
2754 jarInputStreamProvider jprovider = createjarInputStreamProvider(file);
2755 af = loadJalviewAlign(jprovider);
2758 af.setMenusForViewport();
2760 } catch (MalformedURLException e)
2762 errorMessage = "Invalid URL format for '" + file + "'";
2768 SwingUtilities.invokeAndWait(new Runnable()
2773 setLoadingFinishedForNewStructureViewers();
2776 } catch (Exception x)
2778 System.err.println("Error loading alignment: " + x.getMessage());
2784 @SuppressWarnings("unused")
2785 private jarInputStreamProvider createjarInputStreamProvider(final Object ofile) throws MalformedURLException {
2787 // BH 2018 allow for bytes already attached to File object
2789 String file = (ofile instanceof File ? ((File) ofile).getCanonicalPath() : ofile.toString());
2790 byte[] bytes = /** @j2sNative ofile._bytes || */
2793 errorMessage = null;
2794 uniqueSetSuffix = null;
2796 viewportsAdded.clear();
2797 frefedSequence = null;
2799 if (file.startsWith("http://")) {
2800 url = new URL(file);
2802 final URL _url = url;
2803 return new jarInputStreamProvider() {
2806 public JarInputStream getJarInputStream() throws IOException {
2807 if (bytes != null) {
2808 // System.out.println("Jalview2XML: opening byte jarInputStream for bytes.length=" + bytes.length);
2809 return new JarInputStream(new ByteArrayInputStream(bytes));
2812 // System.out.println("Jalview2XML: opening url jarInputStream for " + _url);
2813 return new JarInputStream(_url.openStream());
2815 // System.out.println("Jalview2XML: opening file jarInputStream for " + file);
2816 return new JarInputStream(new FileInputStream(file));
2821 public String getFilename() {
2825 } catch (IOException e) {
2826 e.printStackTrace();
2832 * Recover jalview session from a jalview project archive. Caller may
2833 * initialise uniqueSetSuffix, seqRefIds, viewportsAdded and frefedSequence
2834 * themselves. Any null fields will be initialised with default values,
2835 * non-null fields are left alone.
2840 public AlignFrame loadJalviewAlign(final jarInputStreamProvider jprovider)
2842 errorMessage = null;
2843 if (uniqueSetSuffix == null)
2845 uniqueSetSuffix = System.currentTimeMillis() % 100000 + "";
2847 if (seqRefIds == null)
2851 AlignFrame af = null, _af = null;
2852 IdentityHashMap<AlignmentI, AlignmentI> importedDatasets = new IdentityHashMap<>();
2853 Map<String, AlignFrame> gatherToThisFrame = new HashMap<>();
2854 final String file = jprovider.getFilename();
2857 JarInputStream jin = null;
2858 JarEntry jarentry = null;
2863 jin = jprovider.getJarInputStream();
2864 for (int i = 0; i < entryCount; i++)
2866 jarentry = jin.getNextJarEntry();
2869 if (jarentry != null && jarentry.getName().endsWith(".xml"))
2871 JAXBContext jc = JAXBContext
2872 .newInstance("jalview.xml.binding.jalview");
2873 XMLStreamReader streamReader = XMLInputFactory.newInstance()
2874 .createXMLStreamReader(jin);
2875 javax.xml.bind.Unmarshaller um = jc.createUnmarshaller();
2876 JAXBElement<JalviewModel> jbe = um
2877 .unmarshal(streamReader, JalviewModel.class);
2878 JalviewModel object = jbe.getValue();
2880 if (true) // !skipViewport(object))
2882 _af = loadFromObject(object, file, true, jprovider);
2883 if (_af != null && object.getViewport().size() > 0)
2884 // getJalviewModelSequence().getViewportCount() > 0)
2888 // store a reference to the first view
2891 if (_af.getViewport().isGatherViewsHere())
2893 // if this is a gathered view, keep its reference since
2894 // after gathering views, only this frame will remain
2896 gatherToThisFrame.put(_af.getViewport().getSequenceSetId(),
2899 // Save dataset to register mappings once all resolved
2900 importedDatasets.put(
2901 af.getViewport().getAlignment().getDataset(),
2902 af.getViewport().getAlignment().getDataset());
2907 else if (jarentry != null)
2909 // Some other file here.
2912 } while (jarentry != null);
2913 resolveFrefedSequences();
2914 } catch (IOException ex)
2916 ex.printStackTrace();
2917 errorMessage = "Couldn't locate Jalview XML file : " + file;
2919 "Exception whilst loading jalview XML file : " + ex + "\n");
2920 } catch (Exception ex)
2922 System.err.println("Parsing as Jalview Version 2 file failed.");
2923 ex.printStackTrace(System.err);
2924 if (attemptversion1parse)
2926 // used to attempt to parse as V1 castor-generated xml
2928 if (Desktop.instance != null)
2930 Desktop.instance.stopLoading();
2934 System.out.println("Successfully loaded archive file");
2937 ex.printStackTrace();
2940 "Exception whilst loading jalview XML file : " + ex + "\n");
2941 } catch (OutOfMemoryError e)
2943 // Don't use the OOM Window here
2944 errorMessage = "Out of memory loading jalview XML file";
2945 System.err.println("Out of memory whilst loading jalview XML file");
2946 e.printStackTrace();
2950 * Regather multiple views (with the same sequence set id) to the frame (if
2951 * any) that is flagged as the one to gather to, i.e. convert them to tabbed
2952 * views instead of separate frames. Note this doesn't restore a state where
2953 * some expanded views in turn have tabbed views - the last "first tab" read
2954 * in will play the role of gatherer for all.
2956 for (AlignFrame fr : gatherToThisFrame.values())
2958 Desktop.instance.gatherViews(fr);
2961 restoreSplitFrames();
2962 for (AlignmentI ds : importedDatasets.keySet())
2964 if (ds.getCodonFrames() != null)
2966 StructureSelectionManager
2967 .getStructureSelectionManager(Desktop.instance)
2968 .registerMappings(ds.getCodonFrames());
2971 if (errorMessage != null)
2976 if (Desktop.instance != null)
2978 Desktop.instance.stopLoading();
2985 * Try to reconstruct and display SplitFrame windows, where each contains
2986 * complementary dna and protein alignments. Done by pairing up AlignFrame
2987 * objects (created earlier) which have complementary viewport ids associated.
2989 protected void restoreSplitFrames()
2991 List<SplitFrame> gatherTo = new ArrayList<>();
2992 List<AlignFrame> addedToSplitFrames = new ArrayList<>();
2993 Map<String, AlignFrame> dna = new HashMap<>();
2996 * Identify the DNA alignments
2998 for (Entry<Viewport, AlignFrame> candidate : splitFrameCandidates
3001 AlignFrame af = candidate.getValue();
3002 if (af.getViewport().getAlignment().isNucleotide())
3004 dna.put(candidate.getKey().getId(), af);
3009 * Try to match up the protein complements
3011 for (Entry<Viewport, AlignFrame> candidate : splitFrameCandidates
3014 AlignFrame af = candidate.getValue();
3015 if (!af.getViewport().getAlignment().isNucleotide())
3017 String complementId = candidate.getKey().getComplementId();
3018 // only non-null complements should be in the Map
3019 if (complementId != null && dna.containsKey(complementId))
3021 final AlignFrame dnaFrame = dna.get(complementId);
3022 SplitFrame sf = createSplitFrame(dnaFrame, af);
3023 addedToSplitFrames.add(dnaFrame);
3024 addedToSplitFrames.add(af);
3025 dnaFrame.setMenusForViewport();
3026 af.setMenusForViewport();
3027 if (af.getViewport().isGatherViewsHere())
3036 * Open any that we failed to pair up (which shouldn't happen!) as
3037 * standalone AlignFrame's.
3039 for (Entry<Viewport, AlignFrame> candidate : splitFrameCandidates
3042 AlignFrame af = candidate.getValue();
3043 if (!addedToSplitFrames.contains(af))
3045 Viewport view = candidate.getKey();
3046 Desktop.addInternalFrame(af, view.getTitle(),
3047 safeInt(view.getWidth()), safeInt(view.getHeight()));
3048 af.setMenusForViewport();
3049 System.err.println("Failed to restore view " + view.getTitle()
3050 + " to split frame");
3055 * Gather back into tabbed views as flagged.
3057 for (SplitFrame sf : gatherTo)
3059 Desktop.instance.gatherViews(sf);
3062 splitFrameCandidates.clear();
3066 * Construct and display one SplitFrame holding DNA and protein alignments.
3069 * @param proteinFrame
3072 protected SplitFrame createSplitFrame(AlignFrame dnaFrame,
3073 AlignFrame proteinFrame)
3075 SplitFrame splitFrame = new SplitFrame(dnaFrame, proteinFrame);
3076 String title = MessageManager.getString("label.linked_view_title");
3077 int width = (int) dnaFrame.getBounds().getWidth();
3078 int height = (int) (dnaFrame.getBounds().getHeight()
3079 + proteinFrame.getBounds().getHeight() + 50);
3082 * SplitFrame location is saved to both enclosed frames
3084 splitFrame.setLocation(dnaFrame.getX(), dnaFrame.getY());
3085 Desktop.addInternalFrame(splitFrame, title, width, height);
3088 * And compute cDNA consensus (couldn't do earlier with consensus as
3089 * mappings were not yet present)
3091 proteinFrame.getViewport().alignmentChanged(proteinFrame.alignPanel);
3097 * check errorMessage for a valid error message and raise an error box in the
3098 * GUI or write the current errorMessage to stderr and then clear the error
3101 protected void reportErrors()
3103 reportErrors(false);
3106 protected void reportErrors(final boolean saving)
3108 if (errorMessage != null)
3110 final String finalErrorMessage = errorMessage;
3113 javax.swing.SwingUtilities.invokeLater(new Runnable()
3118 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
3120 "Error " + (saving ? "saving" : "loading")
3122 JvOptionPane.WARNING_MESSAGE);
3128 System.err.println("Problem loading Jalview file: " + errorMessage);
3131 errorMessage = null;
3134 Map<String, String> alreadyLoadedPDB = new HashMap<>();
3137 * when set, local views will be updated from view stored in JalviewXML
3138 * Currently (28th Sep 2008) things will go horribly wrong in vamsas document
3139 * sync if this is set to true.
3141 private final boolean updateLocalViews = false;
3144 * Returns the path to a temporary file holding the PDB file for the given PDB
3145 * id. The first time of asking, searches for a file of that name in the
3146 * Jalview project jar, and copies it to a new temporary file. Any repeat
3147 * requests just return the path to the file previously created.
3153 String loadPDBFile(jarInputStreamProvider jprovider, String pdbId,
3156 if (alreadyLoadedPDB.containsKey(pdbId))
3158 return alreadyLoadedPDB.get(pdbId).toString();
3161 String tempFile = copyJarEntry(jprovider, pdbId, "jalview_pdb",
3163 if (tempFile != null)
3165 alreadyLoadedPDB.put(pdbId, tempFile);
3171 * Copies the jar entry of given name to a new temporary file and returns the
3172 * path to the file, or null if the entry is not found.
3175 * @param jarEntryName
3177 * a prefix for the temporary file name, must be at least three
3180 * null or original file - so new file can be given the same suffix
3184 protected String copyJarEntry(jarInputStreamProvider jprovider,
3185 String jarEntryName, String prefix, String origFile)
3187 BufferedReader in = null;
3188 PrintWriter out = null;
3189 String suffix = ".tmp";
3190 if (origFile == null)
3192 origFile = jarEntryName;
3194 int sfpos = origFile.lastIndexOf(".");
3195 if (sfpos > -1 && sfpos < (origFile.length() - 3))
3197 suffix = "." + origFile.substring(sfpos + 1);
3201 JarInputStream jin = jprovider.getJarInputStream();
3203 * if (jprovider.startsWith("http://")) { jin = new JarInputStream(new
3204 * URL(jprovider).openStream()); } else { jin = new JarInputStream(new
3205 * FileInputStream(jprovider)); }
3208 JarEntry entry = null;
3211 entry = jin.getNextJarEntry();
3212 } while (entry != null && !entry.getName().equals(jarEntryName));
3215 in = new BufferedReader(new InputStreamReader(jin, UTF_8));
3216 File outFile = File.createTempFile(prefix, suffix);
3217 outFile.deleteOnExit();
3218 out = new PrintWriter(new FileOutputStream(outFile));
3221 while ((data = in.readLine()) != null)
3226 String t = outFile.getAbsolutePath();
3231 warn("Couldn't find entry in Jalview Jar for " + jarEntryName);
3233 } catch (Exception ex)
3235 ex.printStackTrace();
3243 } catch (IOException e)
3257 private class JvAnnotRow
3259 public JvAnnotRow(int i, AlignmentAnnotation jaa)
3266 * persisted version of annotation row from which to take vis properties
3268 public jalview.datamodel.AlignmentAnnotation template;
3271 * original position of the annotation row in the alignment
3277 * Load alignment frame from jalview XML DOM object
3279 * @param jalviewModel
3282 * filename source string
3283 * @param loadTreesAndStructures
3284 * when false only create Viewport
3286 * data source provider
3287 * @return alignment frame created from view stored in DOM
3289 AlignFrame loadFromObject(JalviewModel jalviewModel, String file,
3290 boolean loadTreesAndStructures, jarInputStreamProvider jprovider)
3292 SequenceSet vamsasSet = jalviewModel.getVamsasModel().getSequenceSet().get(0);
3293 List<Sequence> vamsasSeqs = vamsasSet.getSequence();
3295 // JalviewModelSequence jms = object.getJalviewModelSequence();
3297 // Viewport view = (jms.getViewportCount() > 0) ? jms.getViewport(0)
3299 Viewport view = (jalviewModel.getViewport().size() > 0)
3300 ? jalviewModel.getViewport().get(0)
3303 // ////////////////////////////////
3304 // INITIALISE ALIGNMENT SEQUENCESETID AND VIEWID
3307 // If we just load in the same jar file again, the sequenceSetId
3308 // will be the same, and we end up with multiple references
3309 // to the same sequenceSet. We must modify this id on load
3310 // so that each load of the file gives a unique id
3313 * used to resolve correct alignment dataset for alignments with multiple
3316 String uniqueSeqSetId = null;
3317 String viewId = null;
3320 uniqueSeqSetId = view.getSequenceSetId() + uniqueSetSuffix;
3321 viewId = (view.getId() == null ? null
3322 : view.getId() + uniqueSetSuffix);
3325 // ////////////////////////////////
3328 List<SequenceI> hiddenSeqs = null;
3330 List<SequenceI> tmpseqs = new ArrayList<>();
3332 boolean multipleView = false;
3333 SequenceI referenceseqForView = null;
3334 // JSeq[] jseqs = object.getJalviewModelSequence().getJSeq();
3335 List<JSeq> jseqs = jalviewModel.getJSeq();
3336 int vi = 0; // counter in vamsasSeq array
3337 for (int i = 0; i < jseqs.size(); i++)
3339 JSeq jseq = jseqs.get(i);
3340 String seqId = jseq.getId();
3342 SequenceI tmpSeq = seqRefIds.get(seqId);
3345 if (!incompleteSeqs.containsKey(seqId))
3347 // may not need this check, but keep it for at least 2.9,1 release
3348 if (tmpSeq.getStart() != jseq.getStart()
3349 || tmpSeq.getEnd() != jseq.getEnd())
3352 "Warning JAL-2154 regression: updating start/end for sequence "
3353 + tmpSeq.toString() + " to " + jseq);
3358 incompleteSeqs.remove(seqId);
3360 if (vamsasSeqs.size() > vi
3361 && vamsasSeqs.get(vi).getId().equals(seqId))
3363 // most likely we are reading a dataset XML document so
3364 // update from vamsasSeq section of XML for this sequence
3365 tmpSeq.setName(vamsasSeqs.get(vi).getName());
3366 tmpSeq.setDescription(vamsasSeqs.get(vi).getDescription());
3367 tmpSeq.setSequence(vamsasSeqs.get(vi).getSequence());
3372 // reading multiple views, so vamsasSeq set is a subset of JSeq
3373 multipleView = true;
3375 tmpSeq.setStart(jseq.getStart());
3376 tmpSeq.setEnd(jseq.getEnd());
3377 tmpseqs.add(tmpSeq);
3381 Sequence vamsasSeq = vamsasSeqs.get(vi);
3382 tmpSeq = new jalview.datamodel.Sequence(vamsasSeq.getName(),
3383 vamsasSeq.getSequence());
3384 tmpSeq.setDescription(vamsasSeq.getDescription());
3385 tmpSeq.setStart(jseq.getStart());
3386 tmpSeq.setEnd(jseq.getEnd());
3387 tmpSeq.setVamsasId(uniqueSetSuffix + seqId);
3388 seqRefIds.put(vamsasSeq.getId(), tmpSeq);
3389 tmpseqs.add(tmpSeq);
3393 if (safeBoolean(jseq.isViewreference()))
3395 referenceseqForView = tmpseqs.get(tmpseqs.size() - 1);
3398 if (jseq.isHidden() != null && jseq.isHidden().booleanValue())
3400 if (hiddenSeqs == null)
3402 hiddenSeqs = new ArrayList<>();
3405 hiddenSeqs.add(tmpSeq);
3410 // Create the alignment object from the sequence set
3411 // ///////////////////////////////
3412 SequenceI[] orderedSeqs = tmpseqs
3413 .toArray(new SequenceI[tmpseqs.size()]);
3415 AlignmentI al = null;
3416 // so we must create or recover the dataset alignment before going further
3417 // ///////////////////////////////
3418 if (vamsasSet.getDatasetId() == null || vamsasSet.getDatasetId() == "")
3420 // older jalview projects do not have a dataset - so creat alignment and
3422 al = new Alignment(orderedSeqs);
3423 al.setDataset(null);
3427 boolean isdsal = jalviewModel.getViewport().isEmpty();
3430 // we are importing a dataset record, so
3431 // recover reference to an alignment already materialsed as dataset
3432 al = getDatasetFor(vamsasSet.getDatasetId());
3436 // materialse the alignment
3437 al = new Alignment(orderedSeqs);
3441 addDatasetRef(vamsasSet.getDatasetId(), al);
3444 // finally, verify all data in vamsasSet is actually present in al
3445 // passing on flag indicating if it is actually a stored dataset
3446 recoverDatasetFor(vamsasSet, al, isdsal, uniqueSeqSetId);
3449 if (referenceseqForView != null)
3451 al.setSeqrep(referenceseqForView);
3453 // / Add the alignment properties
3454 for (int i = 0; i < vamsasSet.getSequenceSetProperties().size(); i++)
3456 SequenceSetProperties ssp = vamsasSet.getSequenceSetProperties()
3458 al.setProperty(ssp.getKey(), ssp.getValue());
3461 // ///////////////////////////////
3463 Hashtable pdbloaded = new Hashtable(); // TODO nothing writes to this??
3466 // load sequence features, database references and any associated PDB
3467 // structures for the alignment
3469 // prior to 2.10, this part would only be executed the first time a
3470 // sequence was encountered, but not afterwards.
3471 // now, for 2.10 projects, this is also done if the xml doc includes
3472 // dataset sequences not actually present in any particular view.
3474 for (int i = 0; i < vamsasSeqs.size(); i++)
3476 JSeq jseq = jseqs.get(i);
3477 if (jseq.getFeatures().size() > 0)
3479 List<Feature> features = jseq.getFeatures();
3480 for (int f = 0; f < features.size(); f++)
3482 Feature feat = features.get(f);
3483 SequenceFeature sf = new SequenceFeature(feat.getType(),
3484 feat.getDescription(), feat.getBegin(), feat.getEnd(),
3485 safeFloat(feat.getScore()), feat.getFeatureGroup());
3486 sf.setStatus(feat.getStatus());
3489 * load any feature attributes - include map-valued attributes
3491 Map<String, Map<String, String>> mapAttributes = new HashMap<>();
3492 for (int od = 0; od < feat.getOtherData().size(); od++)
3494 OtherData keyValue = feat.getOtherData().get(od);
3495 String attributeName = keyValue.getKey();
3496 String attributeValue = keyValue.getValue();
3497 if (attributeName.startsWith("LINK"))
3499 sf.addLink(attributeValue);
3503 String subAttribute = keyValue.getKey2();
3504 if (subAttribute == null)
3506 // simple string-valued attribute
3507 sf.setValue(attributeName, attributeValue);
3511 // attribute 'key' has sub-attribute 'key2'
3512 if (!mapAttributes.containsKey(attributeName))
3514 mapAttributes.put(attributeName, new HashMap<>());
3516 mapAttributes.get(attributeName).put(subAttribute,
3521 for (Entry<String, Map<String, String>> mapAttribute : mapAttributes
3524 sf.setValue(mapAttribute.getKey(), mapAttribute.getValue());
3527 // adds feature to datasequence's feature set (since Jalview 2.10)
3528 al.getSequenceAt(i).addSequenceFeature(sf);
3531 if (vamsasSeqs.get(i).getDBRef().size() > 0)
3533 // adds dbrefs to datasequence's set (since Jalview 2.10)
3535 al.getSequenceAt(i).getDatasetSequence() == null
3536 ? al.getSequenceAt(i)
3537 : al.getSequenceAt(i).getDatasetSequence(),
3540 if (jseq.getPdbids().size() > 0)
3542 List<Pdbids> ids = jseq.getPdbids();
3543 for (int p = 0; p < ids.size(); p++)
3545 Pdbids pdbid = ids.get(p);
3546 jalview.datamodel.PDBEntry entry = new jalview.datamodel.PDBEntry();
3547 entry.setId(pdbid.getId());
3548 if (pdbid.getType() != null)
3550 if (PDBEntry.Type.getType(pdbid.getType()) != null)
3552 entry.setType(PDBEntry.Type.getType(pdbid.getType()));
3556 entry.setType(PDBEntry.Type.FILE);
3559 // jprovider is null when executing 'New View'
3560 if (pdbid.getFile() != null && jprovider != null)
3562 if (!pdbloaded.containsKey(pdbid.getFile()))
3564 entry.setFile(loadPDBFile(jprovider, pdbid.getId(),
3569 entry.setFile(pdbloaded.get(pdbid.getId()).toString());
3573 if (pdbid.getPdbentryItem() != null)
3575 for (PdbentryItem item : pdbid.getPdbentryItem())
3577 for (Property pr : item.getProperty())
3579 entry.setProperty(pr.getName(), pr.getValue());
3584 for (Property prop : pdbid.getProperty())
3586 entry.setProperty(prop.getName(), prop.getValue());
3588 StructureSelectionManager
3589 .getStructureSelectionManager(Desktop.instance)
3590 .registerPDBEntry(entry);
3591 // adds PDBEntry to datasequence's set (since Jalview 2.10)
3592 if (al.getSequenceAt(i).getDatasetSequence() != null)
3594 al.getSequenceAt(i).getDatasetSequence().addPDBId(entry);
3598 al.getSequenceAt(i).addPDBId(entry);
3603 } // end !multipleview
3605 // ///////////////////////////////
3606 // LOAD SEQUENCE MAPPINGS
3608 if (vamsasSet.getAlcodonFrame().size() > 0)
3610 // TODO Potentially this should only be done once for all views of an
3612 List<AlcodonFrame> alc = vamsasSet.getAlcodonFrame();
3613 for (int i = 0; i < alc.size(); i++)
3615 AlignedCodonFrame cf = new AlignedCodonFrame();
3616 if (alc.get(i).getAlcodMap().size() > 0)
3618 List<AlcodMap> maps = alc.get(i).getAlcodMap();
3619 for (int m = 0; m < maps.size(); m++)
3621 AlcodMap map = maps.get(m);
3622 SequenceI dnaseq = seqRefIds.get(map.getDnasq());
3624 jalview.datamodel.Mapping mapping = null;
3625 // attach to dna sequence reference.
3626 if (map.getMapping() != null)
3628 mapping = addMapping(map.getMapping());
3629 if (dnaseq != null && mapping.getTo() != null)
3631 cf.addMap(dnaseq, mapping.getTo(), mapping.getMap());
3637 newAlcodMapRef(map.getDnasq(), cf, mapping));
3641 al.addCodonFrame(cf);
3646 // ////////////////////////////////
3648 List<JvAnnotRow> autoAlan = new ArrayList<>();
3651 * store any annotations which forward reference a group's ID
3653 Map<String, List<AlignmentAnnotation>> groupAnnotRefs = new Hashtable<>();
3655 if (vamsasSet.getAnnotation().size()/*Count()*/ > 0)
3657 List<Annotation> an = vamsasSet.getAnnotation();
3659 for (int i = 0; i < an.size(); i++)
3661 Annotation annotation = an.get(i);
3664 * test if annotation is automatically calculated for this view only
3666 boolean autoForView = false;
3667 if (annotation.getLabel().equals("Quality")
3668 || annotation.getLabel().equals("Conservation")
3669 || annotation.getLabel().equals("Consensus"))
3671 // Kludge for pre 2.5 projects which lacked the autocalculated flag
3673 // JAXB has no has() test; schema defaults value to false
3674 // if (!annotation.hasAutoCalculated())
3676 // annotation.setAutoCalculated(true);
3679 if (autoForView || annotation.isAutoCalculated())
3681 // remove ID - we don't recover annotation from other views for
3682 // view-specific annotation
3683 annotation.setId(null);
3686 // set visibility for other annotation in this view
3687 String annotationId = annotation.getId();
3688 if (annotationId != null && annotationIds.containsKey(annotationId))
3690 AlignmentAnnotation jda = annotationIds.get(annotationId);
3691 // in principle Visible should always be true for annotation displayed
3692 // in multiple views
3693 if (annotation.isVisible() != null)
3695 jda.visible = annotation.isVisible();
3698 al.addAnnotation(jda);
3702 // Construct new annotation from model.
3703 List<AnnotationElement> ae = annotation.getAnnotationElement();
3704 jalview.datamodel.Annotation[] anot = null;
3705 java.awt.Color firstColour = null;
3707 if (!annotation.isScoreOnly())
3709 anot = new jalview.datamodel.Annotation[al.getWidth()];
3710 for (int aa = 0; aa < ae.size() && aa < anot.length; aa++)
3712 AnnotationElement annElement = ae.get(aa);
3713 anpos = annElement.getPosition();
3715 if (anpos >= anot.length)
3720 float value = safeFloat(annElement.getValue());
3721 anot[anpos] = new jalview.datamodel.Annotation(
3722 annElement.getDisplayCharacter(),
3723 annElement.getDescription(),
3724 (annElement.getSecondaryStructure() == null
3725 || annElement.getSecondaryStructure()
3729 .getSecondaryStructure()
3732 anot[anpos].colour = new Color(safeInt(annElement.getColour()));
3733 if (firstColour == null)
3735 firstColour = anot[anpos].colour;
3739 jalview.datamodel.AlignmentAnnotation jaa = null;
3741 if (annotation.isGraph())
3743 float llim = 0, hlim = 0;
3744 // if (autoForView || an[i].isAutoCalculated()) {
3747 jaa = new jalview.datamodel.AlignmentAnnotation(
3748 annotation.getLabel(), annotation.getDescription(), anot,
3749 llim, hlim, safeInt(annotation.getGraphType()));
3751 jaa.graphGroup = safeInt(annotation.getGraphGroup());
3752 jaa._linecolour = firstColour;
3753 if (annotation.getThresholdLine() != null)
3755 jaa.setThreshold(new jalview.datamodel.GraphLine(
3756 safeFloat(annotation.getThresholdLine().getValue()),
3757 annotation.getThresholdLine().getLabel(),
3758 new java.awt.Color(safeInt(
3759 annotation.getThresholdLine().getColour()))));
3761 if (autoForView || annotation.isAutoCalculated())
3763 // Hardwire the symbol display line to ensure that labels for
3764 // histograms are displayed
3770 jaa = new jalview.datamodel.AlignmentAnnotation(
3771 annotation.getLabel(), annotation.getDescription(), anot);
3772 jaa._linecolour = firstColour;
3774 // register new annotation
3775 if (annotation.getId() != null)
3777 annotationIds.put(annotation.getId(), jaa);
3778 jaa.annotationId = annotation.getId();
3780 // recover sequence association
3781 String sequenceRef = annotation.getSequenceRef();
3782 if (sequenceRef != null)
3784 // from 2.9 sequenceRef is to sequence id (JAL-1781)
3785 SequenceI sequence = seqRefIds.get(sequenceRef);
3786 if (sequence == null)
3788 // in pre-2.9 projects sequence ref is to sequence name
3789 sequence = al.findName(sequenceRef);
3791 if (sequence != null)
3793 jaa.createSequenceMapping(sequence, 1, true);
3794 sequence.addAlignmentAnnotation(jaa);
3797 // and make a note of any group association
3798 if (annotation.getGroupRef() != null
3799 && annotation.getGroupRef().length() > 0)
3801 List<jalview.datamodel.AlignmentAnnotation> aal = groupAnnotRefs
3802 .get(annotation.getGroupRef());
3805 aal = new ArrayList<>();
3806 groupAnnotRefs.put(annotation.getGroupRef(), aal);
3811 if (annotation.getScore() != null)
3813 jaa.setScore(annotation.getScore().doubleValue());
3815 if (annotation.isVisible() != null)
3817 jaa.visible = annotation.isVisible().booleanValue();
3820 if (annotation.isCentreColLabels() != null)
3822 jaa.centreColLabels = annotation.isCentreColLabels()
3826 if (annotation.isScaleColLabels() != null)
3828 jaa.scaleColLabel = annotation.isScaleColLabels().booleanValue();
3830 if (annotation.isAutoCalculated())
3832 // newer files have an 'autoCalculated' flag and store calculation
3833 // state in viewport properties
3834 jaa.autoCalculated = true; // means annotation will be marked for
3835 // update at end of load.
3837 if (annotation.getGraphHeight() != null)
3839 jaa.graphHeight = annotation.getGraphHeight().intValue();
3841 jaa.belowAlignment = annotation.isBelowAlignment();
3842 jaa.setCalcId(annotation.getCalcId());
3843 if (annotation.getProperty().size() > 0)
3845 for (Annotation.Property prop : annotation
3848 jaa.setProperty(prop.getName(), prop.getValue());
3851 if (jaa.autoCalculated)
3853 autoAlan.add(new JvAnnotRow(i, jaa));
3856 // if (!autoForView)
3858 // add autocalculated group annotation and any user created annotation
3860 al.addAnnotation(jaa);
3864 // ///////////////////////
3866 // Create alignment markup and styles for this view
3867 if (jalviewModel.getJGroup().size() > 0)
3869 List<JGroup> groups = jalviewModel.getJGroup();
3870 boolean addAnnotSchemeGroup = false;
3871 for (int i = 0; i < groups.size(); i++)
3873 JGroup jGroup = groups.get(i);
3874 ColourSchemeI cs = null;
3875 if (jGroup.getColour() != null)
3877 if (jGroup.getColour().startsWith("ucs"))
3879 cs = getUserColourScheme(jalviewModel, jGroup.getColour());
3881 else if (jGroup.getColour().equals("AnnotationColourGradient")
3882 && jGroup.getAnnotationColours() != null)
3884 addAnnotSchemeGroup = true;
3888 cs = ColourSchemeProperty.getColourScheme(null, al,
3889 jGroup.getColour());
3892 int pidThreshold = safeInt(jGroup.getPidThreshold());
3894 Vector<SequenceI> seqs = new Vector<>();
3896 for (int s = 0; s < jGroup.getSeq().size(); s++)
3898 String seqId = jGroup.getSeq().get(s);
3899 SequenceI ts = seqRefIds.get(seqId);
3903 seqs.addElement(ts);
3907 if (seqs.size() < 1)
3912 SequenceGroup sg = new SequenceGroup(seqs, jGroup.getName(), cs,
3913 safeBoolean(jGroup.isDisplayBoxes()),
3914 safeBoolean(jGroup.isDisplayText()),
3915 safeBoolean(jGroup.isColourText()),
3916 safeInt(jGroup.getStart()), safeInt(jGroup.getEnd()));
3917 sg.getGroupColourScheme().setThreshold(pidThreshold, true);
3918 sg.getGroupColourScheme()
3919 .setConservationInc(safeInt(jGroup.getConsThreshold()));
3920 sg.setOutlineColour(new Color(safeInt(jGroup.getOutlineColour())));
3922 sg.textColour = new Color(safeInt(jGroup.getTextCol1()));
3923 sg.textColour2 = new Color(safeInt(jGroup.getTextCol2()));
3924 sg.setShowNonconserved(safeBoolean(jGroup.isShowUnconserved()));
3925 sg.thresholdTextColour = safeInt(jGroup.getTextColThreshold());
3926 // attributes with a default in the schema are never null
3927 sg.setShowConsensusHistogram(jGroup.isShowConsensusHistogram());
3928 sg.setshowSequenceLogo(jGroup.isShowSequenceLogo());
3929 sg.setNormaliseSequenceLogo(jGroup.isNormaliseSequenceLogo());
3930 sg.setIgnoreGapsConsensus(jGroup.isIgnoreGapsinConsensus());
3931 if (jGroup.getConsThreshold() != null
3932 && jGroup.getConsThreshold().intValue() != 0)
3934 Conservation c = new Conservation("All", sg.getSequences(null), 0,
3937 c.verdict(false, 25);
3938 sg.cs.setConservation(c);
3941 if (jGroup.getId() != null && groupAnnotRefs.size() > 0)
3943 // re-instate unique group/annotation row reference
3944 List<AlignmentAnnotation> jaal = groupAnnotRefs
3945 .get(jGroup.getId());
3948 for (AlignmentAnnotation jaa : jaal)
3951 if (jaa.autoCalculated)
3953 // match up and try to set group autocalc alignment row for this
3955 if (jaa.label.startsWith("Consensus for "))
3957 sg.setConsensus(jaa);
3959 // match up and try to set group autocalc alignment row for this
3961 if (jaa.label.startsWith("Conservation for "))
3963 sg.setConservationRow(jaa);
3970 if (addAnnotSchemeGroup)
3972 // reconstruct the annotation colourscheme
3973 sg.setColourScheme(constructAnnotationColour(
3974 jGroup.getAnnotationColours(), null, al, jalviewModel, false));
3980 // only dataset in this model, so just return.
3983 // ///////////////////////////////
3986 AlignFrame af = null;
3987 AlignViewport av = null;
3988 // now check to see if we really need to create a new viewport.
3989 if (multipleView && viewportsAdded.size() == 0)
3991 // We recovered an alignment for which a viewport already exists.
3992 // TODO: fix up any settings necessary for overlaying stored state onto
3993 // state recovered from another document. (may not be necessary).
3994 // we may need a binding from a viewport in memory to one recovered from
3996 // and then recover its containing af to allow the settings to be applied.
3997 // TODO: fix for vamsas demo
3999 "About to recover a viewport for existing alignment: Sequence set ID is "
4001 Object seqsetobj = retrieveExistingObj(uniqueSeqSetId);
4002 if (seqsetobj != null)
4004 if (seqsetobj instanceof String)
4006 uniqueSeqSetId = (String) seqsetobj;
4008 "Recovered extant sequence set ID mapping for ID : New Sequence set ID is "
4014 "Warning : Collision between sequence set ID string and existing jalview object mapping.");
4020 * indicate that annotation colours are applied across all groups (pre
4021 * Jalview 2.8.1 behaviour)
4023 boolean doGroupAnnColour = Jalview2XML.isVersionStringLaterThan("2.8.1",
4024 jalviewModel.getVersion());
4026 AlignmentPanel ap = null;
4027 boolean isnewview = true;
4030 // Check to see if this alignment already has a view id == viewId
4031 jalview.gui.AlignmentPanel views[] = Desktop
4032 .getAlignmentPanels(uniqueSeqSetId);
4033 if (views != null && views.length > 0)
4035 for (int v = 0; v < views.length; v++)
4037 if (views[v].av.getViewId().equalsIgnoreCase(viewId))
4039 // recover the existing alignpanel, alignframe, viewport
4040 af = views[v].alignFrame;
4043 // TODO: could even skip resetting view settings if we don't want to
4044 // change the local settings from other jalview processes
4053 af = loadViewport(file, jseqs, hiddenSeqs, al, jalviewModel, view,
4054 uniqueSeqSetId, viewId, autoAlan);
4055 av = af.getViewport();
4060 * Load any trees, PDB structures and viewers
4062 * Not done if flag is false (when this method is used for New View)
4064 if (loadTreesAndStructures)
4066 loadTrees(jalviewModel, view, af, av, ap);
4067 loadPCAViewers(jalviewModel, ap);
4068 loadPDBStructures(jprovider, jseqs, af, ap);
4069 loadRnaViewers(jprovider, jseqs, ap);
4071 // and finally return.
4076 * Instantiate and link any saved RNA (Varna) viewers. The state of the Varna
4077 * panel is restored from separate jar entries, two (gapped and trimmed) per
4078 * sequence and secondary structure.
4080 * Currently each viewer shows just one sequence and structure (gapped and
4081 * trimmed), however this method is designed to support multiple sequences or
4082 * structures in viewers if wanted in future.
4088 private void loadRnaViewers(jarInputStreamProvider jprovider,
4089 List<JSeq> jseqs, AlignmentPanel ap)
4092 * scan the sequences for references to viewers; create each one the first
4093 * time it is referenced, add Rna models to existing viewers
4095 for (JSeq jseq : jseqs)
4097 for (int i = 0; i < jseq.getRnaViewer().size(); i++)
4099 RnaViewer viewer = jseq.getRnaViewer().get(i);
4100 AppVarna appVarna = findOrCreateVarnaViewer(viewer, uniqueSetSuffix,
4103 for (int j = 0; j < viewer.getSecondaryStructure().size(); j++)
4105 SecondaryStructure ss = viewer.getSecondaryStructure().get(j);
4106 SequenceI seq = seqRefIds.get(jseq.getId());
4107 AlignmentAnnotation ann = this.annotationIds
4108 .get(ss.getAnnotationId());
4111 * add the structure to the Varna display (with session state copied
4112 * from the jar to a temporary file)
4114 boolean gapped = safeBoolean(ss.isGapped());
4115 String rnaTitle = ss.getTitle();
4116 String sessionState = ss.getViewerState();
4117 String tempStateFile = copyJarEntry(jprovider, sessionState,
4119 RnaModel rna = new RnaModel(rnaTitle, ann, seq, null, gapped);
4120 appVarna.addModelSession(rna, rnaTitle, tempStateFile);
4122 appVarna.setInitialSelection(safeInt(viewer.getSelectedRna()));
4128 * Locate and return an already instantiated matching AppVarna, or create one
4132 * @param viewIdSuffix
4136 protected AppVarna findOrCreateVarnaViewer(RnaViewer viewer,
4137 String viewIdSuffix, AlignmentPanel ap)
4140 * on each load a suffix is appended to the saved viewId, to avoid conflicts
4141 * if load is repeated
4143 String postLoadId = viewer.getViewId() + viewIdSuffix;
4144 for (JInternalFrame frame : getAllFrames())
4146 if (frame instanceof AppVarna)
4148 AppVarna varna = (AppVarna) frame;
4149 if (postLoadId.equals(varna.getViewId()))
4151 // this viewer is already instantiated
4152 // could in future here add ap as another 'parent' of the
4153 // AppVarna window; currently just 1-to-many
4160 * viewer not found - make it
4162 RnaViewerModel model = new RnaViewerModel(postLoadId, viewer.getTitle(),
4163 safeInt(viewer.getXpos()), safeInt(viewer.getYpos()),
4164 safeInt(viewer.getWidth()), safeInt(viewer.getHeight()),
4165 safeInt(viewer.getDividerLocation()));
4166 AppVarna varna = new AppVarna(model, ap);
4172 * Load any saved trees
4180 protected void loadTrees(JalviewModel jm, Viewport view,
4181 AlignFrame af, AlignViewport av, AlignmentPanel ap)
4183 // TODO result of automated refactoring - are all these parameters needed?
4186 for (int t = 0; t < jm.getTree().size(); t++)
4189 Tree tree = jm.getTree().get(t);
4191 TreePanel tp = (TreePanel) retrieveExistingObj(tree.getId());
4194 tp = af.showNewickTree(new NewickFile(tree.getNewick()),
4195 tree.getTitle(), safeInt(tree.getWidth()),
4196 safeInt(tree.getHeight()), safeInt(tree.getXpos()),
4197 safeInt(tree.getYpos()));
4198 if (tree.getId() != null)
4200 // perhaps bind the tree id to something ?
4205 // update local tree attributes ?
4206 // TODO: should check if tp has been manipulated by user - if so its
4207 // settings shouldn't be modified
4208 tp.setTitle(tree.getTitle());
4209 tp.setBounds(new Rectangle(safeInt(tree.getXpos()),
4210 safeInt(tree.getYpos()), safeInt(tree.getWidth()),
4211 safeInt(tree.getHeight())));
4212 tp.setViewport(av); // af.viewport;
4213 // TODO: verify 'associate with all views' works still
4214 tp.getTreeCanvas().setViewport(av); // af.viewport;
4215 tp.getTreeCanvas().setAssociatedPanel(ap); // af.alignPanel;
4216 // FIXME: should we use safeBoolean here ?
4217 tp.getTreeCanvas().setApplyToAllViews(tree.isLinkToAllViews());
4222 warn("There was a problem recovering stored Newick tree: \n"
4223 + tree.getNewick());
4227 tp.fitToWindow.setState(safeBoolean(tree.isFitToWindow()));
4228 tp.fitToWindow_actionPerformed(null);
4230 if (tree.getFontName() != null)
4233 new Font(tree.getFontName(), safeInt(tree.getFontStyle()),
4234 safeInt(tree.getFontSize())));
4239 new Font(view.getFontName(), safeInt(view.getFontStyle()),
4240 safeInt(view.getFontSize())));
4243 tp.showPlaceholders(safeBoolean(tree.isMarkUnlinked()));
4244 tp.showBootstrap(safeBoolean(tree.isShowBootstrap()));
4245 tp.showDistances(safeBoolean(tree.isShowDistances()));
4247 tp.getTreeCanvas().setThreshold(safeFloat(tree.getThreshold()));
4249 if (safeBoolean(tree.isCurrentTree()))
4251 af.getViewport().setCurrentTree(tp.getTree());
4255 } catch (Exception ex)
4257 ex.printStackTrace();
4262 * Load and link any saved structure viewers.
4269 protected void loadPDBStructures(jarInputStreamProvider jprovider,
4270 List<JSeq> jseqs, AlignFrame af, AlignmentPanel ap)
4273 * Run through all PDB ids on the alignment, and collect mappings between
4274 * distinct view ids and all sequences referring to that view.
4276 Map<String, StructureViewerModel> structureViewers = new LinkedHashMap<>();
4278 for (int i = 0; i < jseqs.size(); i++)
4280 JSeq jseq = jseqs.get(i);
4281 if (jseq.getPdbids().size() > 0)
4283 List<Pdbids> ids = jseq.getPdbids();
4284 for (int p = 0; p < ids.size(); p++)
4286 Pdbids pdbid = ids.get(p);
4287 final int structureStateCount = pdbid.getStructureState().size();
4288 for (int s = 0; s < structureStateCount; s++)
4290 // check to see if we haven't already created this structure view
4291 final StructureState structureState = pdbid
4292 .getStructureState().get(s);
4293 String sviewid = (structureState.getViewId() == null) ? null
4294 : structureState.getViewId() + uniqueSetSuffix;
4295 jalview.datamodel.PDBEntry jpdb = new jalview.datamodel.PDBEntry();
4296 // Originally : pdbid.getFile()
4297 // : TODO: verify external PDB file recovery still works in normal
4298 // jalview project load
4300 loadPDBFile(jprovider, pdbid.getId(), pdbid.getFile()));
4301 jpdb.setId(pdbid.getId());
4303 int x = safeInt(structureState.getXpos());
4304 int y = safeInt(structureState.getYpos());
4305 int width = safeInt(structureState.getWidth());
4306 int height = safeInt(structureState.getHeight());
4308 // Probably don't need to do this anymore...
4309 // Desktop.desktop.getComponentAt(x, y);
4310 // TODO: NOW: check that this recovers the PDB file correctly.
4311 String pdbFile = loadPDBFile(jprovider, pdbid.getId(),
4313 jalview.datamodel.SequenceI seq = seqRefIds
4314 .get(jseq.getId() + "");
4315 if (sviewid == null)
4317 sviewid = "_jalview_pre2_4_" + x + "," + y + "," + width + ","
4320 if (!structureViewers.containsKey(sviewid))
4322 structureViewers.put(sviewid,
4323 new StructureViewerModel(x, y, width, height, false,
4324 false, true, structureState.getViewId(),
4325 structureState.getType()));
4326 // Legacy pre-2.7 conversion JAL-823 :
4327 // do not assume any view has to be linked for colour by
4331 // assemble String[] { pdb files }, String[] { id for each
4332 // file }, orig_fileloc, SequenceI[][] {{ seqs_file 1 }, {
4333 // seqs_file 2}, boolean[] {
4334 // linkAlignPanel,superposeWithAlignpanel}} from hash
4335 StructureViewerModel jmoldat = structureViewers.get(sviewid);
4336 jmoldat.setAlignWithPanel(jmoldat.isAlignWithPanel()
4337 || structureState.isAlignwithAlignPanel());
4340 * Default colour by linked panel to false if not specified (e.g.
4341 * for pre-2.7 projects)
4343 boolean colourWithAlignPanel = jmoldat.isColourWithAlignPanel();
4344 colourWithAlignPanel |= structureState.isColourwithAlignPanel();
4345 jmoldat.setColourWithAlignPanel(colourWithAlignPanel);
4348 * Default colour by viewer to true if not specified (e.g. for
4351 boolean colourByViewer = jmoldat.isColourByViewer();
4352 colourByViewer &= structureState.isColourByJmol();
4353 jmoldat.setColourByViewer(colourByViewer);
4355 if (jmoldat.getStateData().length() < structureState
4356 .getValue()/*Content()*/.length())
4358 jmoldat.setStateData(structureState.getValue());// Content());
4360 if (pdbid.getFile() != null)
4362 File mapkey = new File(pdbid.getFile());
4363 StructureData seqstrmaps = jmoldat.getFileData().get(mapkey);
4364 if (seqstrmaps == null)
4366 jmoldat.getFileData().put(mapkey,
4367 seqstrmaps = jmoldat.new StructureData(pdbFile,
4370 if (!seqstrmaps.getSeqList().contains(seq))
4372 seqstrmaps.getSeqList().add(seq);
4378 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");
4385 // Instantiate the associated structure views
4386 for (Entry<String, StructureViewerModel> entry : structureViewers
4391 createOrLinkStructureViewer(entry, af, ap, jprovider);
4392 } catch (Exception e)
4395 "Error loading structure viewer: " + e.getMessage());
4396 // failed - try the next one
4408 protected void createOrLinkStructureViewer(
4409 Entry<String, StructureViewerModel> viewerData, AlignFrame af,
4410 AlignmentPanel ap, jarInputStreamProvider jprovider)
4412 final StructureViewerModel stateData = viewerData.getValue();
4415 * Search for any viewer windows already open from other alignment views
4416 * that exactly match the stored structure state
4418 StructureViewerBase comp = findMatchingViewer(viewerData);
4422 linkStructureViewer(ap, comp, stateData);
4427 * From 2.9: stateData.type contains JMOL or CHIMERA, data is in jar entry
4428 * "viewer_"+stateData.viewId
4430 if (ViewerType.CHIMERA.toString().equals(stateData.getType()))
4432 createChimeraViewer(viewerData, af, jprovider);
4437 * else Jmol (if pre-2.9, stateData contains JMOL state string)
4439 createJmolViewer(viewerData, af, jprovider);
4444 * Create a new Chimera viewer.
4450 protected void createChimeraViewer(
4451 Entry<String, StructureViewerModel> viewerData, AlignFrame af,
4452 jarInputStreamProvider jprovider)
4454 StructureViewerModel data = viewerData.getValue();
4455 String chimeraSessionFile = data.getStateData();
4458 * Copy Chimera session from jar entry "viewer_"+viewId to a temporary file
4460 * NB this is the 'saved' viewId as in the project file XML, _not_ the
4461 * 'uniquified' sviewid used to reconstruct the viewer here
4463 String viewerJarEntryName = getViewerJarEntryName(data.getViewId());
4464 chimeraSessionFile = copyJarEntry(jprovider, viewerJarEntryName,
4467 Set<Entry<File, StructureData>> fileData = data.getFileData()
4469 List<PDBEntry> pdbs = new ArrayList<>();
4470 List<SequenceI[]> allseqs = new ArrayList<>();
4471 for (Entry<File, StructureData> pdb : fileData)
4473 String filePath = pdb.getValue().getFilePath();
4474 String pdbId = pdb.getValue().getPdbId();
4475 // pdbs.add(new PDBEntry(filePath, pdbId));
4476 pdbs.add(new PDBEntry(pdbId, null, PDBEntry.Type.PDB, filePath));
4477 final List<SequenceI> seqList = pdb.getValue().getSeqList();
4478 SequenceI[] seqs = seqList.toArray(new SequenceI[seqList.size()]);
4482 boolean colourByChimera = data.isColourByViewer();
4483 boolean colourBySequence = data.isColourWithAlignPanel();
4485 // TODO use StructureViewer as a factory here, see JAL-1761
4486 final PDBEntry[] pdbArray = pdbs.toArray(new PDBEntry[pdbs.size()]);
4487 final SequenceI[][] seqsArray = allseqs
4488 .toArray(new SequenceI[allseqs.size()][]);
4489 String newViewId = viewerData.getKey();
4491 ChimeraViewFrame cvf = new ChimeraViewFrame(chimeraSessionFile,
4492 af.alignPanel, pdbArray, seqsArray, colourByChimera,
4493 colourBySequence, newViewId);
4494 cvf.setSize(data.getWidth(), data.getHeight());
4495 cvf.setLocation(data.getX(), data.getY());
4499 * Create a new Jmol window. First parse the Jmol state to translate filenames
4500 * loaded into the view, and record the order in which files are shown in the
4501 * Jmol view, so we can add the sequence mappings in same order.
4507 protected void createJmolViewer(
4508 final Entry<String, StructureViewerModel> viewerData,
4509 AlignFrame af, jarInputStreamProvider jprovider)
4511 final StructureViewerModel svattrib = viewerData.getValue();
4512 String state = svattrib.getStateData();
4515 * Pre-2.9: state element value is the Jmol state string
4517 * 2.9+: @type is "JMOL", state data is in a Jar file member named "viewer_"
4520 if (ViewerType.JMOL.toString().equals(svattrib.getType()))
4522 state = readJarEntry(jprovider,
4523 getViewerJarEntryName(svattrib.getViewId()));
4526 List<String> pdbfilenames = new ArrayList<>();
4527 List<SequenceI[]> seqmaps = new ArrayList<>();
4528 List<String> pdbids = new ArrayList<>();
4529 StringBuilder newFileLoc = new StringBuilder(64);
4530 int cp = 0, ncp, ecp;
4531 Map<File, StructureData> oldFiles = svattrib.getFileData();
4532 while ((ncp = state.indexOf("load ", cp)) > -1)
4536 // look for next filename in load statement
4537 newFileLoc.append(state.substring(cp,
4538 ncp = (state.indexOf("\"", ncp + 1) + 1)));
4539 String oldfilenam = state.substring(ncp,
4540 ecp = state.indexOf("\"", ncp));
4541 // recover the new mapping data for this old filename
4542 // have to normalize filename - since Jmol and jalview do
4544 // translation differently.
4545 StructureData filedat = oldFiles.get(new File(oldfilenam));
4546 if (filedat == null)
4548 String reformatedOldFilename = oldfilenam.replaceAll("/", "\\\\");
4549 filedat = oldFiles.get(new File(reformatedOldFilename));
4551 newFileLoc.append(Platform.escapeString(filedat.getFilePath()));
4552 pdbfilenames.add(filedat.getFilePath());
4553 pdbids.add(filedat.getPdbId());
4554 seqmaps.add(filedat.getSeqList().toArray(new SequenceI[0]));
4555 newFileLoc.append("\"");
4556 cp = ecp + 1; // advance beyond last \" and set cursor so we can
4557 // look for next file statement.
4558 } while ((ncp = state.indexOf("/*file*/", cp)) > -1);
4562 // just append rest of state
4563 newFileLoc.append(state.substring(cp));
4567 System.err.print("Ignoring incomplete Jmol state for PDB ids: ");
4568 newFileLoc = new StringBuilder(state);
4569 newFileLoc.append("; load append ");
4570 for (File id : oldFiles.keySet())
4572 // add this and any other pdb files that should be present in
4574 StructureData filedat = oldFiles.get(id);
4575 newFileLoc.append(filedat.getFilePath());
4576 pdbfilenames.add(filedat.getFilePath());
4577 pdbids.add(filedat.getPdbId());
4578 seqmaps.add(filedat.getSeqList().toArray(new SequenceI[0]));
4579 newFileLoc.append(" \"");
4580 newFileLoc.append(filedat.getFilePath());
4581 newFileLoc.append("\"");
4584 newFileLoc.append(";");
4587 if (newFileLoc.length() == 0)
4591 int histbug = newFileLoc.indexOf("history = ");
4595 * change "history = [true|false];" to "history = [1|0];"
4598 int diff = histbug == -1 ? -1 : newFileLoc.indexOf(";", histbug);
4599 String val = (diff == -1) ? null
4600 : newFileLoc.substring(histbug, diff);
4601 if (val != null && val.length() >= 4)
4603 if (val.contains("e")) // eh? what can it be?
4605 if (val.trim().equals("true"))
4613 newFileLoc.replace(histbug, diff, val);
4618 final String[] pdbf = pdbfilenames
4619 .toArray(new String[pdbfilenames.size()]);
4620 final String[] id = pdbids.toArray(new String[pdbids.size()]);
4621 final SequenceI[][] sq = seqmaps
4622 .toArray(new SequenceI[seqmaps.size()][]);
4623 final String fileloc = newFileLoc.toString();
4624 final String sviewid = viewerData.getKey();
4625 final AlignFrame alf = af;
4626 final Rectangle rect = new Rectangle(svattrib.getX(), svattrib.getY(),
4627 svattrib.getWidth(), svattrib.getHeight());
4630 javax.swing.SwingUtilities.invokeAndWait(new Runnable()
4635 JalviewStructureDisplayI sview = null;
4638 sview = new StructureViewer(
4639 alf.alignPanel.getStructureSelectionManager())
4640 .createView(StructureViewer.ViewerType.JMOL,
4641 pdbf, id, sq, alf.alignPanel, svattrib,
4642 fileloc, rect, sviewid);
4643 addNewStructureViewer(sview);
4644 } catch (OutOfMemoryError ex)
4646 new OOMWarning("restoring structure view for PDB id " + id,
4647 (OutOfMemoryError) ex.getCause());
4648 if (sview != null && sview.isVisible())
4650 sview.closeViewer(false);
4651 sview.setVisible(false);
4657 } catch (InvocationTargetException ex)
4659 warn("Unexpected error when opening Jmol view.", ex);
4661 } catch (InterruptedException e)
4663 // e.printStackTrace();
4669 * Generates a name for the entry in the project jar file to hold state
4670 * information for a structure viewer
4675 protected String getViewerJarEntryName(String viewId)
4677 return VIEWER_PREFIX + viewId;
4681 * Returns any open frame that matches given structure viewer data. The match
4682 * is based on the unique viewId, or (for older project versions) the frame's
4688 protected StructureViewerBase findMatchingViewer(
4689 Entry<String, StructureViewerModel> viewerData)
4691 final String sviewid = viewerData.getKey();
4692 final StructureViewerModel svattrib = viewerData.getValue();
4693 StructureViewerBase comp = null;
4694 JInternalFrame[] frames = getAllFrames();
4695 for (JInternalFrame frame : frames)
4697 if (frame instanceof StructureViewerBase)
4700 * Post jalview 2.4 schema includes structure view id
4702 if (sviewid != null && ((StructureViewerBase) frame).getViewId()
4705 comp = (StructureViewerBase) frame;
4706 break; // break added in 2.9
4709 * Otherwise test for matching position and size of viewer frame
4711 else if (frame.getX() == svattrib.getX()
4712 && frame.getY() == svattrib.getY()
4713 && frame.getHeight() == svattrib.getHeight()
4714 && frame.getWidth() == svattrib.getWidth())
4716 comp = (StructureViewerBase) frame;
4717 // no break in faint hope of an exact match on viewId
4725 * Link an AlignmentPanel to an existing structure viewer.
4730 * @param useinViewerSuperpos
4731 * @param usetoColourbyseq
4732 * @param viewerColouring
4734 protected void linkStructureViewer(AlignmentPanel ap,
4735 StructureViewerBase viewer, StructureViewerModel stateData)
4737 // NOTE: if the jalview project is part of a shared session then
4738 // view synchronization should/could be done here.
4740 final boolean useinViewerSuperpos = stateData.isAlignWithPanel();
4741 final boolean usetoColourbyseq = stateData.isColourWithAlignPanel();
4742 final boolean viewerColouring = stateData.isColourByViewer();
4743 Map<File, StructureData> oldFiles = stateData.getFileData();
4746 * Add mapping for sequences in this view to an already open viewer
4748 final AAStructureBindingModel binding = viewer.getBinding();
4749 for (File id : oldFiles.keySet())
4751 // add this and any other pdb files that should be present in the
4753 StructureData filedat = oldFiles.get(id);
4754 String pdbFile = filedat.getFilePath();
4755 SequenceI[] seq = filedat.getSeqList().toArray(new SequenceI[0]);
4756 binding.getSsm().setMapping(seq, null, pdbFile, DataSourceType.FILE,
4758 binding.addSequenceForStructFile(pdbFile, seq);
4760 // and add the AlignmentPanel's reference to the view panel
4761 viewer.addAlignmentPanel(ap);
4762 if (useinViewerSuperpos)
4764 viewer.useAlignmentPanelForSuperposition(ap);
4768 viewer.excludeAlignmentPanelForSuperposition(ap);
4770 if (usetoColourbyseq)
4772 viewer.useAlignmentPanelForColourbyseq(ap, !viewerColouring);
4776 viewer.excludeAlignmentPanelForColourbyseq(ap);
4781 * Get all frames within the Desktop.
4785 protected JInternalFrame[] getAllFrames()
4787 JInternalFrame[] frames = null;
4788 // TODO is this necessary - is it safe - risk of hanging?
4793 frames = Desktop.desktop.getAllFrames();
4794 } catch (ArrayIndexOutOfBoundsException e)
4796 // occasional No such child exceptions are thrown here...
4800 } catch (InterruptedException f)
4804 } while (frames == null);
4809 * Answers true if 'version' is equal to or later than 'supported', where each
4810 * is formatted as major/minor versions like "2.8.3" or "2.3.4b1" for bugfix
4811 * changes. Development and test values for 'version' are leniently treated
4815 * - minimum version we are comparing against
4817 * - version of data being processsed
4820 public static boolean isVersionStringLaterThan(String supported,
4823 if (supported == null || version == null
4824 || version.equalsIgnoreCase("DEVELOPMENT BUILD")
4825 || version.equalsIgnoreCase("Test")
4826 || version.equalsIgnoreCase("AUTOMATED BUILD"))
4828 System.err.println("Assuming project file with "
4829 + (version == null ? "null" : version)
4830 + " is compatible with Jalview version " + supported);
4835 return StringUtils.compareVersions(version, supported, "b") >= 0;
4839 Vector<JalviewStructureDisplayI> newStructureViewers = null;
4841 protected void addNewStructureViewer(JalviewStructureDisplayI sview)
4843 if (newStructureViewers != null)
4845 sview.getBinding().setFinishedLoadingFromArchive(false);
4846 newStructureViewers.add(sview);
4850 protected void setLoadingFinishedForNewStructureViewers()
4852 if (newStructureViewers != null)
4854 for (JalviewStructureDisplayI sview : newStructureViewers)
4856 sview.getBinding().setFinishedLoadingFromArchive(true);
4858 newStructureViewers.clear();
4859 newStructureViewers = null;
4863 AlignFrame loadViewport(String file, List<JSeq> JSEQ,
4864 List<SequenceI> hiddenSeqs, AlignmentI al,
4865 JalviewModel jm, Viewport view, String uniqueSeqSetId,
4866 String viewId, List<JvAnnotRow> autoAlan)
4868 AlignFrame af = null;
4869 af = new AlignFrame(al, safeInt(view.getWidth()),
4870 safeInt(view.getHeight()), uniqueSeqSetId, viewId)
4874 // protected void processKeyEvent(java.awt.event.KeyEvent e) {
4875 // System.out.println("Jalview2XML AF " + e);
4876 // super.processKeyEvent(e);
4883 af.setFileName(file, FileFormat.Jalview);
4885 final AlignViewport viewport = af.getViewport();
4886 for (int i = 0; i < JSEQ.size(); i++)
4888 int colour = safeInt(JSEQ.get(i).getColour());
4889 viewport.setSequenceColour(viewport.getAlignment().getSequenceAt(i),
4895 viewport.setColourByReferenceSeq(true);
4896 viewport.setDisplayReferenceSeq(true);
4899 viewport.setGatherViewsHere(safeBoolean(view.isGatheredViews()));
4901 if (view.getSequenceSetId() != null)
4903 AlignmentViewport av = viewportsAdded.get(uniqueSeqSetId);
4905 viewport.setSequenceSetId(uniqueSeqSetId);
4908 // propagate shared settings to this new view
4909 viewport.setHistoryList(av.getHistoryList());
4910 viewport.setRedoList(av.getRedoList());
4914 viewportsAdded.put(uniqueSeqSetId, viewport);
4916 // TODO: check if this method can be called repeatedly without
4917 // side-effects if alignpanel already registered.
4918 PaintRefresher.Register(af.alignPanel, uniqueSeqSetId);
4920 // apply Hidden regions to view.
4921 if (hiddenSeqs != null)
4923 for (int s = 0; s < JSEQ.size(); s++)
4925 SequenceGroup hidden = new SequenceGroup();
4926 boolean isRepresentative = false;
4927 for (int r = 0; r < JSEQ.get(s).getHiddenSequences().size(); r++)
4929 isRepresentative = true;
4930 SequenceI sequenceToHide = al
4931 .getSequenceAt(JSEQ.get(s).getHiddenSequences().get(r));
4932 hidden.addSequence(sequenceToHide, false);
4933 // remove from hiddenSeqs list so we don't try to hide it twice
4934 hiddenSeqs.remove(sequenceToHide);
4936 if (isRepresentative)
4938 SequenceI representativeSequence = al.getSequenceAt(s);
4939 hidden.addSequence(representativeSequence, false);
4940 viewport.hideRepSequences(representativeSequence, hidden);
4944 SequenceI[] hseqs = hiddenSeqs
4945 .toArray(new SequenceI[hiddenSeqs.size()]);
4946 viewport.hideSequence(hseqs);
4949 // recover view properties and display parameters
4951 viewport.setShowAnnotation(safeBoolean(view.isShowAnnotation()));
4952 viewport.setAbovePIDThreshold(safeBoolean(view.isPidSelected()));
4953 final int pidThreshold = safeInt(view.getPidThreshold());
4954 viewport.setThreshold(pidThreshold);
4956 viewport.setColourText(safeBoolean(view.isShowColourText()));
4959 .setConservationSelected(
4960 safeBoolean(view.isConservationSelected()));
4961 viewport.setIncrement(safeInt(view.getConsThreshold()));
4962 viewport.setShowJVSuffix(safeBoolean(view.isShowFullId()));
4963 viewport.setRightAlignIds(safeBoolean(view.isRightAlignIds()));
4964 viewport.setFont(new Font(view.getFontName(),
4965 safeInt(view.getFontStyle()), safeInt(view.getFontSize())),
4967 ViewStyleI vs = viewport.getViewStyle();
4968 vs.setScaleProteinAsCdna(view.isScaleProteinAsCdna());
4969 viewport.setViewStyle(vs);
4970 // TODO: allow custom charWidth/Heights to be restored by updating them
4971 // after setting font - which means set above to false
4972 viewport.setRenderGaps(safeBoolean(view.isRenderGaps()));
4973 viewport.setWrapAlignment(safeBoolean(view.isWrapAlignment()));
4974 viewport.setShowAnnotation(safeBoolean(view.isShowAnnotation()));
4976 viewport.setShowBoxes(safeBoolean(view.isShowBoxes()));
4978 viewport.setShowText(safeBoolean(view.isShowText()));
4980 viewport.setTextColour(new Color(safeInt(view.getTextCol1())));
4981 viewport.setTextColour2(new Color(safeInt(view.getTextCol2())));
4982 viewport.setThresholdTextColour(safeInt(view.getTextColThreshold()));
4983 viewport.setShowUnconserved(view.isShowUnconserved());
4984 viewport.getRanges().setStartRes(safeInt(view.getStartRes()));
4986 if (view.getViewName() != null)
4988 viewport.setViewName(view.getViewName());
4989 af.setInitialTabVisible();
4991 af.setBounds(safeInt(view.getXpos()), safeInt(view.getYpos()),
4992 safeInt(view.getWidth()), safeInt(view.getHeight()));
4993 // startSeq set in af.alignPanel.updateLayout below
4994 af.alignPanel.updateLayout();
4995 ColourSchemeI cs = null;
4996 // apply colourschemes
4997 if (view.getBgColour() != null)
4999 if (view.getBgColour().startsWith("ucs"))
5001 cs = getUserColourScheme(jm, view.getBgColour());
5003 else if (view.getBgColour().startsWith("Annotation"))
5005 AnnotationColourScheme viewAnnColour = view.getAnnotationColours();
5006 cs = constructAnnotationColour(viewAnnColour, af, al, jm, true);
5013 cs = ColourSchemeProperty.getColourScheme(af.getViewport(), al,
5014 view.getBgColour());
5019 * turn off 'alignment colour applies to all groups'
5020 * while restoring global colour scheme
5022 viewport.setColourAppliesToAllGroups(false);
5023 viewport.setGlobalColourScheme(cs);
5024 viewport.getResidueShading().setThreshold(pidThreshold,
5025 view.isIgnoreGapsinConsensus());
5026 viewport.getResidueShading()
5027 .setConsensus(viewport.getSequenceConsensusHash());
5028 if (safeBoolean(view.isConservationSelected()) && cs != null)
5030 viewport.getResidueShading()
5031 .setConservationInc(safeInt(view.getConsThreshold()));
5033 af.changeColour(cs);
5034 viewport.setColourAppliesToAllGroups(true);
5037 .setShowSequenceFeatures(
5038 safeBoolean(view.isShowSequenceFeatures()));
5040 viewport.setCentreColumnLabels(view.isCentreColumnLabels());
5041 viewport.setIgnoreGapsConsensus(view.isIgnoreGapsinConsensus(), null);
5042 viewport.setFollowHighlight(view.isFollowHighlight());
5043 viewport.followSelection = view.isFollowSelection();
5044 viewport.setShowConsensusHistogram(view.isShowConsensusHistogram());
5045 viewport.setShowSequenceLogo(view.isShowSequenceLogo());
5046 viewport.setNormaliseSequenceLogo(view.isNormaliseSequenceLogo());
5047 viewport.setShowDBRefs(safeBoolean(view.isShowDbRefTooltip()));
5048 viewport.setShowNPFeats(safeBoolean(view.isShowNPfeatureTooltip()));
5049 viewport.setShowGroupConsensus(view.isShowGroupConsensus());
5050 viewport.setShowGroupConservation(view.isShowGroupConservation());
5052 // recover feature settings
5053 if (jm.getFeatureSettings() != null)
5055 FeatureRenderer fr = af.alignPanel.getSeqPanel().seqCanvas
5056 .getFeatureRenderer();
5057 FeaturesDisplayed fdi;
5058 viewport.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
5059 String[] renderOrder = new String[jm.getFeatureSettings()
5060 .getSetting().size()];
5061 Map<String, FeatureColourI> featureColours = new Hashtable<>();
5062 Map<String, Float> featureOrder = new Hashtable<>();
5064 for (int fs = 0; fs < jm.getFeatureSettings()
5065 .getSetting().size(); fs++)
5067 Setting setting = jm.getFeatureSettings().getSetting().get(fs);
5068 String featureType = setting.getType();
5071 * restore feature filters (if any)
5073 jalview.xml.binding.jalview.FeatureMatcherSet filters = setting
5075 if (filters != null)
5077 FeatureMatcherSetI filter = Jalview2XML
5078 .parseFilter(featureType, filters);
5079 if (!filter.isEmpty())
5081 fr.setFeatureFilter(featureType, filter);
5086 * restore feature colour scheme
5088 Color maxColour = new Color(setting.getColour());
5089 if (setting.getMincolour() != null)
5092 * minColour is always set unless a simple colour
5093 * (including for colour by label though it doesn't use it)
5095 Color minColour = new Color(setting.getMincolour().intValue());
5096 Color noValueColour = minColour;
5097 NoValueColour noColour = setting.getNoValueColour();
5098 if (noColour == NoValueColour.NONE)
5100 noValueColour = null;
5102 else if (noColour == NoValueColour.MAX)
5104 noValueColour = maxColour;
5106 float min = safeFloat(safeFloat(setting.getMin()));
5107 float max = setting.getMax() == null ? 1f
5108 : setting.getMax().floatValue();
5109 FeatureColourI gc = new FeatureColour(maxColour, minColour,
5111 noValueColour, min, max);
5112 if (setting.getAttributeName().size() > 0)
5114 gc.setAttributeName(setting.getAttributeName().toArray(
5115 new String[setting.getAttributeName().size()]));
5117 if (setting.getThreshold() != null)
5119 gc.setThreshold(setting.getThreshold().floatValue());
5120 int threshstate = safeInt(setting.getThreshstate());
5121 // -1 = None, 0 = Below, 1 = Above threshold
5122 if (threshstate == 0)
5124 gc.setBelowThreshold(true);
5126 else if (threshstate == 1)
5128 gc.setAboveThreshold(true);
5131 gc.setAutoScaled(true); // default
5132 if (setting.isAutoScale() != null)
5134 gc.setAutoScaled(setting.isAutoScale());
5136 if (setting.isColourByLabel() != null)
5138 gc.setColourByLabel(setting.isColourByLabel());
5140 // and put in the feature colour table.
5141 featureColours.put(featureType, gc);
5145 featureColours.put(featureType,
5146 new FeatureColour(maxColour));
5148 renderOrder[fs] = featureType;
5149 if (setting.getOrder() != null)
5151 featureOrder.put(featureType, setting.getOrder().floatValue());
5155 featureOrder.put(featureType, new Float(
5156 fs / jm.getFeatureSettings().getSetting().size()));
5158 if (safeBoolean(setting.isDisplay()))
5160 fdi.setVisible(featureType);
5163 Map<String, Boolean> fgtable = new Hashtable<>();
5164 for (int gs = 0; gs < jm.getFeatureSettings().getGroup().size(); gs++)
5166 Group grp = jm.getFeatureSettings().getGroup().get(gs);
5167 fgtable.put(grp.getName(), new Boolean(grp.isDisplay()));
5169 // FeatureRendererSettings frs = new FeatureRendererSettings(renderOrder,
5170 // fgtable, featureColours, jms.getFeatureSettings().hasTransparency() ?
5171 // jms.getFeatureSettings().getTransparency() : 0.0, featureOrder);
5172 FeatureRendererSettings frs = new FeatureRendererSettings(renderOrder,
5173 fgtable, featureColours, 1.0f, featureOrder);
5174 fr.transferSettings(frs);
5177 if (view.getHiddenColumns().size() > 0)
5179 for (int c = 0; c < view.getHiddenColumns().size(); c++)
5181 final HiddenColumns hc = view.getHiddenColumns().get(c);
5182 viewport.hideColumns(safeInt(hc.getStart()),
5183 safeInt(hc.getEnd()) /* +1 */);
5186 if (view.getCalcIdParam() != null)
5188 for (CalcIdParam calcIdParam : view.getCalcIdParam())
5190 if (calcIdParam != null)
5192 if (recoverCalcIdParam(calcIdParam, viewport))
5197 warn("Couldn't recover parameters for "
5198 + calcIdParam.getCalcId());
5203 af.setMenusFromViewport(viewport);
5204 af.setTitle(view.getTitle());
5205 // TODO: we don't need to do this if the viewport is aready visible.
5207 * Add the AlignFrame to the desktop (it may be 'gathered' later), unless it
5208 * has a 'cdna/protein complement' view, in which case save it in order to
5209 * populate a SplitFrame once all views have been read in.
5211 String complementaryViewId = view.getComplementId();
5212 if (complementaryViewId == null)
5214 Desktop.addInternalFrame(af, view.getTitle(),
5215 safeInt(view.getWidth()), safeInt(view.getHeight()));
5216 // recompute any autoannotation
5217 af.alignPanel.updateAnnotation(false, true);
5218 reorderAutoannotation(af, al, autoAlan);
5219 af.alignPanel.alignmentChanged();
5223 splitFrameCandidates.put(view, af);
5229 * Reads saved data to restore Colour by Annotation settings
5231 * @param viewAnnColour
5235 * @param checkGroupAnnColour
5238 private ColourSchemeI constructAnnotationColour(
5239 AnnotationColourScheme viewAnnColour, AlignFrame af,
5240 AlignmentI al, JalviewModel model, boolean checkGroupAnnColour)
5242 boolean propagateAnnColour = false;
5243 AlignmentI annAlignment = af != null ? af.getViewport().getAlignment()
5245 if (checkGroupAnnColour && al.getGroups() != null
5246 && al.getGroups().size() > 0)
5248 // pre 2.8.1 behaviour
5249 // check to see if we should transfer annotation colours
5250 propagateAnnColour = true;
5251 for (SequenceGroup sg : al.getGroups())
5253 if (sg.getColourScheme() instanceof AnnotationColourGradient)
5255 propagateAnnColour = false;
5261 * 2.10.2- : saved annotationId is AlignmentAnnotation.annotationId
5263 String annotationId = viewAnnColour.getAnnotation();
5264 AlignmentAnnotation matchedAnnotation = annotationIds.get(annotationId);
5267 * pre 2.10.2: saved annotationId is AlignmentAnnotation.label
5269 if (matchedAnnotation == null
5270 && annAlignment.getAlignmentAnnotation() != null)
5272 for (int i = 0; i < annAlignment.getAlignmentAnnotation().length; i++)
5275 .equals(annAlignment.getAlignmentAnnotation()[i].label))
5277 matchedAnnotation = annAlignment.getAlignmentAnnotation()[i];
5282 if (matchedAnnotation == null)
5284 System.err.println("Failed to match annotation colour scheme for "
5288 if (matchedAnnotation.getThreshold() == null)
5290 matchedAnnotation.setThreshold(
5291 new GraphLine(safeFloat(viewAnnColour.getThreshold()),
5292 "Threshold", Color.black));
5295 AnnotationColourGradient cs = null;
5296 if (viewAnnColour.getColourScheme().equals("None"))
5298 cs = new AnnotationColourGradient(matchedAnnotation,
5299 new Color(safeInt(viewAnnColour.getMinColour())),
5300 new Color(safeInt(viewAnnColour.getMaxColour())),
5301 safeInt(viewAnnColour.getAboveThreshold()));
5303 else if (viewAnnColour.getColourScheme().startsWith("ucs"))
5305 cs = new AnnotationColourGradient(matchedAnnotation,
5306 getUserColourScheme(model, viewAnnColour.getColourScheme()),
5307 safeInt(viewAnnColour.getAboveThreshold()));
5311 cs = new AnnotationColourGradient(matchedAnnotation,
5312 ColourSchemeProperty.getColourScheme(af.getViewport(), al,
5313 viewAnnColour.getColourScheme()),
5314 safeInt(viewAnnColour.getAboveThreshold()));
5317 boolean perSequenceOnly = safeBoolean(viewAnnColour.isPerSequence());
5318 boolean useOriginalColours = safeBoolean(
5319 viewAnnColour.isPredefinedColours());
5320 cs.setSeqAssociated(perSequenceOnly);
5321 cs.setPredefinedColours(useOriginalColours);
5323 if (propagateAnnColour && al.getGroups() != null)
5325 // Also use these settings for all the groups
5326 for (int g = 0; g < al.getGroups().size(); g++)
5328 SequenceGroup sg = al.getGroups().get(g);
5329 if (sg.getGroupColourScheme() == null)
5334 AnnotationColourGradient groupScheme = new AnnotationColourGradient(
5335 matchedAnnotation, sg.getColourScheme(),
5336 safeInt(viewAnnColour.getAboveThreshold()));
5337 sg.setColourScheme(groupScheme);
5338 groupScheme.setSeqAssociated(perSequenceOnly);
5339 groupScheme.setPredefinedColours(useOriginalColours);
5345 private void reorderAutoannotation(AlignFrame af, AlignmentI al,
5346 List<JvAnnotRow> autoAlan)
5348 // copy over visualization settings for autocalculated annotation in the
5350 if (al.getAlignmentAnnotation() != null)
5353 * Kludge for magic autoannotation names (see JAL-811)
5355 String[] magicNames = new String[] { "Consensus", "Quality",
5357 JvAnnotRow nullAnnot = new JvAnnotRow(-1, null);
5358 Hashtable<String, JvAnnotRow> visan = new Hashtable<>();
5359 for (String nm : magicNames)
5361 visan.put(nm, nullAnnot);
5363 for (JvAnnotRow auan : autoAlan)
5365 visan.put(auan.template.label
5366 + (auan.template.getCalcId() == null ? ""
5367 : "\t" + auan.template.getCalcId()),
5370 int hSize = al.getAlignmentAnnotation().length;
5371 List<JvAnnotRow> reorder = new ArrayList<>();
5372 // work through any autoCalculated annotation already on the view
5373 // removing it if it should be placed in a different location on the
5374 // annotation panel.
5375 List<String> remains = new ArrayList<>(visan.keySet());
5376 for (int h = 0; h < hSize; h++)
5378 jalview.datamodel.AlignmentAnnotation jalan = al
5379 .getAlignmentAnnotation()[h];
5380 if (jalan.autoCalculated)
5383 JvAnnotRow valan = visan.get(k = jalan.label);
5384 if (jalan.getCalcId() != null)
5386 valan = visan.get(k = jalan.label + "\t" + jalan.getCalcId());
5391 // delete the auto calculated row from the alignment
5392 al.deleteAnnotation(jalan, false);
5396 if (valan != nullAnnot)
5398 if (jalan != valan.template)
5400 // newly created autoannotation row instance
5401 // so keep a reference to the visible annotation row
5402 // and copy over all relevant attributes
5403 if (valan.template.graphHeight >= 0)
5406 jalan.graphHeight = valan.template.graphHeight;
5408 jalan.visible = valan.template.visible;
5410 reorder.add(new JvAnnotRow(valan.order, jalan));
5415 // Add any (possibly stale) autocalculated rows that were not appended to
5416 // the view during construction
5417 for (String other : remains)
5419 JvAnnotRow othera = visan.get(other);
5420 if (othera != nullAnnot && othera.template.getCalcId() != null
5421 && othera.template.getCalcId().length() > 0)
5423 reorder.add(othera);
5426 // now put the automatic annotation in its correct place
5427 int s = 0, srt[] = new int[reorder.size()];
5428 JvAnnotRow[] rws = new JvAnnotRow[reorder.size()];
5429 for (JvAnnotRow jvar : reorder)
5432 srt[s++] = jvar.order;
5435 jalview.util.QuickSort.sort(srt, rws);
5436 // and re-insert the annotation at its correct position
5437 for (JvAnnotRow jvar : rws)
5439 al.addAnnotation(jvar.template, jvar.order);
5441 af.alignPanel.adjustAnnotationHeight();
5445 Hashtable skipList = null;
5448 * TODO remove this method
5451 * @return AlignFrame bound to sequenceSetId from view, if one exists. private
5452 * AlignFrame getSkippedFrame(Viewport view) { if (skipList==null) {
5453 * throw new Error("Implementation Error. No skipList defined for this
5454 * Jalview2XML instance."); } return (AlignFrame)
5455 * skipList.get(view.getSequenceSetId()); }
5459 * Check if the Jalview view contained in object should be skipped or not.
5462 * @return true if view's sequenceSetId is a key in skipList
5464 private boolean skipViewport(JalviewModel object)
5466 if (skipList == null)
5470 String id = object.getViewport().get(0).getSequenceSetId();
5471 if (skipList.containsKey(id))
5473 if (Cache.log != null && Cache.log.isDebugEnabled())
5475 Cache.log.debug("Skipping seuqence set id " + id);
5482 public void addToSkipList(AlignFrame af)
5484 if (skipList == null)
5486 skipList = new Hashtable();
5488 skipList.put(af.getViewport().getSequenceSetId(), af);
5491 public void clearSkipList()
5493 if (skipList != null)
5500 private void recoverDatasetFor(SequenceSet vamsasSet, AlignmentI al,
5501 boolean ignoreUnrefed, String uniqueSeqSetId)
5503 jalview.datamodel.AlignmentI ds = getDatasetFor(
5504 vamsasSet.getDatasetId());
5505 AlignmentI xtant_ds = ds;
5506 if (xtant_ds == null)
5508 // good chance we are about to create a new dataset, but check if we've
5509 // seen some of the dataset sequence IDs before.
5510 // TODO: skip this check if we are working with project generated by
5511 // version 2.11 or later
5512 xtant_ds = checkIfHasDataset(vamsasSet.getSequence());
5513 if (xtant_ds != null)
5516 addDatasetRef(vamsasSet.getDatasetId(), ds);
5519 Vector dseqs = null;
5522 // recovering an alignment View
5523 AlignmentI seqSetDS = getDatasetFor(UNIQSEQSETID + uniqueSeqSetId);
5524 if (seqSetDS != null)
5526 if (ds != null && ds != seqSetDS)
5528 warn("JAL-3171 regression: Overwriting a dataset reference for an alignment"
5529 + " - CDS/Protein crossreference data may be lost");
5530 if (xtant_ds != null)
5532 // This can only happen if the unique sequence set ID was bound to a
5533 // dataset that did not contain any of the sequences in the view
5534 // currently being restored.
5535 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.");
5539 addDatasetRef(vamsasSet.getDatasetId(), ds);
5544 // try even harder to restore dataset
5545 AlignmentI xtantDS = checkIfHasDataset(vamsasSet.getSequence());
5546 // create a list of new dataset sequences
5547 dseqs = new Vector();
5549 for (int i = 0, iSize = vamsasSet.getSequence().size(); i < iSize; i++)
5551 Sequence vamsasSeq = vamsasSet.getSequence().get(i);
5552 ensureJalviewDatasetSequence(vamsasSeq, ds, dseqs, ignoreUnrefed, i);
5554 // create a new dataset
5557 SequenceI[] dsseqs = new SequenceI[dseqs.size()];
5558 dseqs.copyInto(dsseqs);
5559 ds = new jalview.datamodel.Alignment(dsseqs);
5560 debug("Created new dataset " + vamsasSet.getDatasetId()
5561 + " for alignment " + System.identityHashCode(al));
5562 addDatasetRef(vamsasSet.getDatasetId(), ds);
5564 // set the dataset for the newly imported alignment.
5565 if (al.getDataset() == null && !ignoreUnrefed)
5568 // register dataset for the alignment's uniqueSeqSetId for legacy projects
5569 addDatasetRef(UNIQSEQSETID + uniqueSeqSetId, ds);
5571 updateSeqDatasetBinding(vamsasSet.getSequence(), ds);
5575 * XML dataset sequence ID to materialised dataset reference
5577 HashMap<String, AlignmentI> seqToDataset = new HashMap<>();
5580 * @return the first materialised dataset reference containing a dataset
5581 * sequence referenced in the given view
5583 * - sequences from the view
5585 AlignmentI checkIfHasDataset(List<Sequence> list)
5587 for (Sequence restoredSeq : list)
5589 AlignmentI datasetFor = seqToDataset.get(restoredSeq.getDsseqid());
5590 if (datasetFor != null)
5599 * Register ds as the containing dataset for the dataset sequences referenced
5600 * by sequences in list
5603 * - sequences in a view
5606 void updateSeqDatasetBinding(List<Sequence> list, AlignmentI ds)
5608 for (Sequence restoredSeq : list)
5610 AlignmentI prevDS = seqToDataset.put(restoredSeq.getDsseqid(), ds);
5611 if (prevDS != null && prevDS != ds)
5613 warn("Dataset sequence appears in many datasets: "
5614 + restoredSeq.getDsseqid());
5615 // TODO: try to merge!
5622 * sequence definition to create/merge dataset sequence for
5626 * vector to add new dataset sequence to
5627 * @param ignoreUnrefed
5628 * - when true, don't create new sequences from vamsasSeq if it's id
5629 * doesn't already have an asssociated Jalview sequence.
5631 * - used to reorder the sequence in the alignment according to the
5632 * vamsasSeq array ordering, to preserve ordering of dataset
5634 private void ensureJalviewDatasetSequence(Sequence vamsasSeq,
5635 AlignmentI ds, Vector dseqs, boolean ignoreUnrefed, int vseqpos)
5637 // JBP TODO: Check this is called for AlCodonFrames to support recovery of
5639 SequenceI sq = seqRefIds.get(vamsasSeq.getId());
5640 boolean reorder = false;
5641 SequenceI dsq = null;
5642 if (sq != null && sq.getDatasetSequence() != null)
5644 dsq = sq.getDatasetSequence();
5650 if (sq == null && ignoreUnrefed)
5654 String sqid = vamsasSeq.getDsseqid();
5657 // need to create or add a new dataset sequence reference to this sequence
5660 dsq = seqRefIds.get(sqid);
5665 // make a new dataset sequence
5666 dsq = sq.createDatasetSequence();
5669 // make up a new dataset reference for this sequence
5670 sqid = seqHash(dsq);
5672 dsq.setVamsasId(uniqueSetSuffix + sqid);
5673 seqRefIds.put(sqid, dsq);
5678 dseqs.addElement(dsq);
5683 ds.addSequence(dsq);
5689 { // make this dataset sequence sq's dataset sequence
5690 sq.setDatasetSequence(dsq);
5691 // and update the current dataset alignment
5696 if (!dseqs.contains(dsq))
5703 if (ds.findIndex(dsq) < 0)
5705 ds.addSequence(dsq);
5712 // TODO: refactor this as a merge dataset sequence function
5713 // now check that sq (the dataset sequence) sequence really is the union of
5714 // all references to it
5715 // boolean pre = sq.getStart() < dsq.getStart();
5716 // boolean post = sq.getEnd() > dsq.getEnd();
5720 // StringBuffer sb = new StringBuffer();
5721 String newres = jalview.analysis.AlignSeq.extractGaps(
5722 jalview.util.Comparison.GapChars, sq.getSequenceAsString());
5723 if (!newres.equalsIgnoreCase(dsq.getSequenceAsString())
5724 && newres.length() > dsq.getLength())
5726 // Update with the longer sequence.
5730 * if (pre) { sb.insert(0, newres .substring(0, dsq.getStart() -
5731 * sq.getStart())); dsq.setStart(sq.getStart()); } if (post) {
5732 * sb.append(newres.substring(newres.length() - sq.getEnd() -
5733 * dsq.getEnd())); dsq.setEnd(sq.getEnd()); }
5735 dsq.setSequence(newres);
5737 // TODO: merges will never happen if we 'know' we have the real dataset
5738 // sequence - this should be detected when id==dssid
5740 "DEBUG Notice: Merged dataset sequence (if you see this often, post at http://issues.jalview.org/browse/JAL-1474)"); // ("
5741 // + (pre ? "prepended" : "") + " "
5742 // + (post ? "appended" : ""));
5747 // sequence refs are identical. We may need to update the existing dataset
5748 // alignment with this one, though.
5749 if (ds != null && dseqs == null)
5751 int opos = ds.findIndex(dsq);
5752 SequenceI tseq = null;
5753 if (opos != -1 && vseqpos != opos)
5755 // remove from old position
5756 ds.deleteSequence(dsq);
5758 if (vseqpos < ds.getHeight())
5760 if (vseqpos != opos)
5762 // save sequence at destination position
5763 tseq = ds.getSequenceAt(vseqpos);
5764 ds.replaceSequenceAt(vseqpos, dsq);
5765 ds.addSequence(tseq);
5770 ds.addSequence(dsq);
5777 * TODO use AlignmentI here and in related methods - needs
5778 * AlignmentI.getDataset() changed to return AlignmentI instead of Alignment
5780 Hashtable<String, AlignmentI> datasetIds = null;
5782 IdentityHashMap<AlignmentI, String> dataset2Ids = null;
5784 private AlignmentI getDatasetFor(String datasetId)
5786 if (datasetIds == null)
5788 datasetIds = new Hashtable<>();
5791 if (datasetIds.containsKey(datasetId))
5793 return datasetIds.get(datasetId);
5798 private void addDatasetRef(String datasetId, AlignmentI dataset)
5800 if (datasetIds == null)
5802 datasetIds = new Hashtable<>();
5804 datasetIds.put(datasetId, dataset);
5808 * make a new dataset ID for this jalview dataset alignment
5813 private String getDatasetIdRef(AlignmentI dataset)
5815 if (dataset.getDataset() != null)
5817 warn("Serious issue! Dataset Object passed to getDatasetIdRef is not a Jalview DATASET alignment...");
5819 String datasetId = makeHashCode(dataset, null);
5820 if (datasetId == null)
5822 // make a new datasetId and record it
5823 if (dataset2Ids == null)
5825 dataset2Ids = new IdentityHashMap<>();
5829 datasetId = dataset2Ids.get(dataset);
5831 if (datasetId == null)
5833 datasetId = "ds" + dataset2Ids.size() + 1;
5834 dataset2Ids.put(dataset, datasetId);
5840 private void addDBRefs(SequenceI datasetSequence, Sequence sequence)
5842 for (int d = 0; d < sequence.getDBRef().size(); d++)
5844 DBRef dr = sequence.getDBRef().get(d);
5845 jalview.datamodel.DBRefEntry entry = new jalview.datamodel.DBRefEntry(
5846 dr.getSource(), dr.getVersion(), dr.getAccessionId());
5847 if (dr.getMapping() != null)
5849 entry.setMap(addMapping(dr.getMapping()));
5851 datasetSequence.addDBRef(entry);
5855 private jalview.datamodel.Mapping addMapping(Mapping m)
5857 SequenceI dsto = null;
5858 // Mapping m = dr.getMapping();
5859 int fr[] = new int[m.getMapListFrom().size() * 2];
5860 Iterator<MapListFrom> from = m.getMapListFrom().iterator();// enumerateMapListFrom();
5861 for (int _i = 0; from.hasNext(); _i += 2)
5863 MapListFrom mf = from.next();
5864 fr[_i] = mf.getStart();
5865 fr[_i + 1] = mf.getEnd();
5867 int fto[] = new int[m.getMapListTo().size() * 2];
5868 Iterator<MapListTo> to = m.getMapListTo().iterator();// enumerateMapListTo();
5869 for (int _i = 0; to.hasNext(); _i += 2)
5871 MapListTo mf = to.next();
5872 fto[_i] = mf.getStart();
5873 fto[_i + 1] = mf.getEnd();
5875 jalview.datamodel.Mapping jmap = new jalview.datamodel.Mapping(dsto, fr,
5876 fto, m.getMapFromUnit().intValue(),
5877 m.getMapToUnit().intValue());
5880 * (optional) choice of dseqFor or Sequence
5882 if (m.getDseqFor() != null)
5884 String dsfor = m.getDseqFor();
5885 if (seqRefIds.containsKey(dsfor))
5890 jmap.setTo(seqRefIds.get(dsfor));
5894 frefedSequence.add(newMappingRef(dsfor, jmap));
5897 else if (m.getSequence() != null)
5900 * local sequence definition
5902 Sequence ms = m.getSequence();
5903 SequenceI djs = null;
5904 String sqid = ms.getDsseqid();
5905 if (sqid != null && sqid.length() > 0)
5908 * recover dataset sequence
5910 djs = seqRefIds.get(sqid);
5915 "Warning - making up dataset sequence id for DbRef sequence map reference");
5916 sqid = ((Object) ms).toString(); // make up a new hascode for
5917 // undefined dataset sequence hash
5918 // (unlikely to happen)
5924 * make a new dataset sequence and add it to refIds hash
5926 djs = new jalview.datamodel.Sequence(ms.getName(),
5928 djs.setStart(jmap.getMap().getToLowest());
5929 djs.setEnd(jmap.getMap().getToHighest());
5930 djs.setVamsasId(uniqueSetSuffix + sqid);
5932 incompleteSeqs.put(sqid, djs);
5933 seqRefIds.put(sqid, djs);
5936 jalview.bin.Cache.log.debug("about to recurse on addDBRefs.");
5945 * Provides a 'copy' of an alignment view (on action New View) by 'saving' the
5946 * view as XML (but not to file), and then reloading it
5951 public AlignmentPanel copyAlignPanel(AlignmentPanel ap)
5954 JalviewModel jm = saveState(ap, null, null, null);
5957 jm.getVamsasModel().getSequenceSet().get(0).getDatasetId(),
5958 ap.getAlignment().getDataset());
5960 uniqueSetSuffix = "";
5961 // jm.getJalviewModelSequence().getViewport(0).setId(null);
5962 jm.getViewport().get(0).setId(null);
5963 // we don't overwrite the view we just copied
5965 if (this.frefedSequence == null)
5967 frefedSequence = new Vector<>();
5970 viewportsAdded.clear();
5972 AlignFrame af = loadFromObject(jm, null, false, null);
5973 af.getAlignPanels().clear();
5974 af.closeMenuItem_actionPerformed(true);
5977 * if(ap.av.getAlignment().getAlignmentAnnotation()!=null) { for(int i=0;
5978 * i<ap.av.getAlignment().getAlignmentAnnotation().length; i++) {
5979 * if(!ap.av.getAlignment().getAlignmentAnnotation()[i].autoCalculated) {
5980 * af.alignPanel.av.getAlignment().getAlignmentAnnotation()[i] =
5981 * ap.av.getAlignment().getAlignmentAnnotation()[i]; } } }
5984 return af.alignPanel;
5987 private Hashtable jvids2vobj;
5989 private void warn(String msg)
5994 private void warn(String msg, Exception e)
5996 if (Cache.log != null)
6000 Cache.log.warn(msg, e);
6004 Cache.log.warn(msg);
6009 System.err.println("Warning: " + msg);
6012 e.printStackTrace();
6017 private void debug(String string)
6019 debug(string, null);
6022 private void debug(String msg, Exception e)
6024 if (Cache.log != null)
6028 Cache.log.debug(msg, e);
6032 Cache.log.debug(msg);
6037 System.err.println("Warning: " + msg);
6040 e.printStackTrace();
6046 * set the object to ID mapping tables used to write/recover objects and XML
6047 * ID strings for the jalview project. If external tables are provided then
6048 * finalize and clearSeqRefs will not clear the tables when the Jalview2XML
6049 * object goes out of scope. - also populates the datasetIds hashtable with
6050 * alignment objects containing dataset sequences
6053 * Map from ID strings to jalview datamodel
6055 * Map from jalview datamodel to ID strings
6059 public void setObjectMappingTables(Hashtable vobj2jv,
6060 IdentityHashMap jv2vobj)
6062 this.jv2vobj = jv2vobj;
6063 this.vobj2jv = vobj2jv;
6064 Iterator ds = jv2vobj.keySet().iterator();
6066 while (ds.hasNext())
6068 Object jvobj = ds.next();
6069 id = jv2vobj.get(jvobj).toString();
6070 if (jvobj instanceof jalview.datamodel.Alignment)
6072 if (((jalview.datamodel.Alignment) jvobj).getDataset() == null)
6074 addDatasetRef(id, (jalview.datamodel.Alignment) jvobj);
6077 else if (jvobj instanceof jalview.datamodel.Sequence)
6079 // register sequence object so the XML parser can recover it.
6080 if (seqRefIds == null)
6082 seqRefIds = new HashMap<>();
6084 if (seqsToIds == null)
6086 seqsToIds = new IdentityHashMap<>();
6088 seqRefIds.put(jv2vobj.get(jvobj).toString(), (SequenceI) jvobj);
6089 seqsToIds.put((SequenceI) jvobj, id);
6091 else if (jvobj instanceof jalview.datamodel.AlignmentAnnotation)
6094 AlignmentAnnotation jvann = (AlignmentAnnotation) jvobj;
6095 annotationIds.put(anid = jv2vobj.get(jvobj).toString(), jvann);
6096 if (jvann.annotationId == null)
6098 jvann.annotationId = anid;
6100 if (!jvann.annotationId.equals(anid))
6102 // TODO verify that this is the correct behaviour
6103 this.warn("Overriding Annotation ID for " + anid
6104 + " from different id : " + jvann.annotationId);
6105 jvann.annotationId = anid;
6108 else if (jvobj instanceof String)
6110 if (jvids2vobj == null)
6112 jvids2vobj = new Hashtable();
6113 jvids2vobj.put(jvobj, jv2vobj.get(jvobj).toString());
6118 Cache.log.debug("Ignoring " + jvobj.getClass() + " (ID = " + id);
6124 * set the uniqueSetSuffix used to prefix/suffix object IDs for jalview
6125 * objects created from the project archive. If string is null (default for
6126 * construction) then suffix will be set automatically.
6130 public void setUniqueSetSuffix(String string)
6132 uniqueSetSuffix = string;
6137 * uses skipList2 as the skipList for skipping views on sequence sets
6138 * associated with keys in the skipList
6142 public void setSkipList(Hashtable skipList2)
6144 skipList = skipList2;
6148 * Reads the jar entry of given name and returns its contents, or null if the
6149 * entry is not found.
6152 * @param jarEntryName
6155 protected String readJarEntry(jarInputStreamProvider jprovider,
6156 String jarEntryName)
6158 String result = null;
6159 BufferedReader in = null;
6164 * Reopen the jar input stream and traverse its entries to find a matching
6167 JarInputStream jin = jprovider.getJarInputStream();
6168 JarEntry entry = null;
6171 entry = jin.getNextJarEntry();
6172 } while (entry != null && !entry.getName().equals(jarEntryName));
6176 StringBuilder out = new StringBuilder(256);
6177 in = new BufferedReader(new InputStreamReader(jin, UTF_8));
6180 while ((data = in.readLine()) != null)
6184 result = out.toString();
6188 warn("Couldn't find entry in Jalview Jar for " + jarEntryName);
6190 } catch (Exception ex)
6192 ex.printStackTrace();
6200 } catch (IOException e)
6211 * Returns an incrementing counter (0, 1, 2...)
6215 private synchronized int nextCounter()
6221 * Loads any saved PCA viewers
6226 protected void loadPCAViewers(JalviewModel model, AlignmentPanel ap)
6230 List<PcaViewer> pcaviewers = model.getPcaViewer();
6231 for (PcaViewer viewer : pcaviewers)
6233 String modelName = viewer.getScoreModelName();
6234 SimilarityParamsI params = new SimilarityParams(
6235 viewer.isIncludeGappedColumns(), viewer.isMatchGaps(),
6236 viewer.isIncludeGaps(),
6237 viewer.isDenominateByShortestLength());
6240 * create the panel (without computing the PCA)
6242 PCAPanel panel = new PCAPanel(ap, modelName, params);
6244 panel.setTitle(viewer.getTitle());
6245 panel.setBounds(new Rectangle(viewer.getXpos(), viewer.getYpos(),
6246 viewer.getWidth(), viewer.getHeight()));
6248 boolean showLabels = viewer.isShowLabels();
6249 panel.setShowLabels(showLabels);
6250 panel.getRotatableCanvas().setShowLabels(showLabels);
6251 panel.getRotatableCanvas()
6252 .setBgColour(new Color(viewer.getBgColour()));
6253 panel.getRotatableCanvas()
6254 .setApplyToAllViews(viewer.isLinkToAllViews());
6257 * load PCA output data
6259 ScoreModelI scoreModel = ScoreModels.getInstance()
6260 .getScoreModel(modelName, ap);
6261 PCA pca = new PCA(null, scoreModel, params);
6262 PcaDataType pcaData = viewer.getPcaData();
6264 MatrixI pairwise = loadDoubleMatrix(pcaData.getPairwiseMatrix());
6265 pca.setPairwiseScores(pairwise);
6267 MatrixI triDiag = loadDoubleMatrix(pcaData.getTridiagonalMatrix());
6268 pca.setTridiagonal(triDiag);
6270 MatrixI result = loadDoubleMatrix(pcaData.getEigenMatrix());
6271 pca.setEigenmatrix(result);
6273 panel.getPcaModel().setPCA(pca);
6276 * we haven't saved the input data! (JAL-2647 to do)
6278 panel.setInputData(null);
6281 * add the sequence points for the PCA display
6283 List<jalview.datamodel.SequencePoint> seqPoints = new ArrayList<>();
6284 for (SequencePoint sp : viewer.getSequencePoint())
6286 String seqId = sp.getSequenceRef();
6287 SequenceI seq = seqRefIds.get(seqId);
6290 throw new IllegalStateException(
6291 "Unmatched seqref for PCA: " + seqId);
6293 Point pt = new Point(sp.getXPos(), sp.getYPos(), sp.getZPos());
6294 jalview.datamodel.SequencePoint seqPoint = new jalview.datamodel.SequencePoint(
6296 seqPoints.add(seqPoint);
6298 panel.getRotatableCanvas().setPoints(seqPoints, seqPoints.size());
6301 * set min-max ranges and scale after setPoints (which recomputes them)
6303 panel.getRotatableCanvas().setScaleFactor(viewer.getScaleFactor());
6304 SeqPointMin spMin = viewer.getSeqPointMin();
6305 float[] min = new float[] { spMin.getXPos(), spMin.getYPos(),
6307 SeqPointMax spMax = viewer.getSeqPointMax();
6308 float[] max = new float[] { spMax.getXPos(), spMax.getYPos(),
6310 panel.getRotatableCanvas().setSeqMinMax(min, max);
6312 // todo: hold points list in PCAModel only
6313 panel.getPcaModel().setSequencePoints(seqPoints);
6315 panel.setSelectedDimensionIndex(viewer.getXDim(), X);
6316 panel.setSelectedDimensionIndex(viewer.getYDim(), Y);
6317 panel.setSelectedDimensionIndex(viewer.getZDim(), Z);
6319 // is this duplication needed?
6320 panel.setTop(seqPoints.size() - 1);
6321 panel.getPcaModel().setTop(seqPoints.size() - 1);
6324 * add the axes' end points for the display
6326 for (int i = 0; i < 3; i++)
6328 Axis axis = viewer.getAxis().get(i);
6329 panel.getRotatableCanvas().getAxisEndPoints()[i] = new Point(
6330 axis.getXPos(), axis.getYPos(), axis.getZPos());
6333 Desktop.addInternalFrame(panel, MessageManager.formatMessage(
6334 "label.calc_title", "PCA", modelName), 475, 450);
6336 } catch (Exception ex)
6338 Cache.log.error("Error loading PCA: " + ex.toString());
6343 * Populates an XML model of the feature colour scheme for one feature type
6345 * @param featureType
6349 public static Colour marshalColour(
6350 String featureType, FeatureColourI fcol)
6352 Colour col = new Colour();
6353 if (fcol.isSimpleColour())
6355 col.setRGB(Format.getHexString(fcol.getColour()));
6359 col.setRGB(Format.getHexString(fcol.getMaxColour()));
6360 col.setMin(fcol.getMin());
6361 col.setMax(fcol.getMax());
6362 col.setMinRGB(jalview.util.Format.getHexString(fcol.getMinColour()));
6363 col.setAutoScale(fcol.isAutoScaled());
6364 col.setThreshold(fcol.getThreshold());
6365 col.setColourByLabel(fcol.isColourByLabel());
6366 col.setThreshType(fcol.isAboveThreshold() ? ThresholdType.ABOVE
6367 : (fcol.isBelowThreshold() ? ThresholdType.BELOW
6368 : ThresholdType.NONE));
6369 if (fcol.isColourByAttribute())
6371 final String[] attName = fcol.getAttributeName();
6372 col.getAttributeName().add(attName[0]);
6373 if (attName.length > 1)
6375 col.getAttributeName().add(attName[1]);
6378 Color noColour = fcol.getNoColour();
6379 if (noColour == null)
6381 col.setNoValueColour(NoValueColour.NONE);
6383 else if (noColour == fcol.getMaxColour())
6385 col.setNoValueColour(NoValueColour.MAX);
6389 col.setNoValueColour(NoValueColour.MIN);
6392 col.setName(featureType);
6397 * Populates an XML model of the feature filter(s) for one feature type
6399 * @param firstMatcher
6400 * the first (or only) match condition)
6402 * remaining match conditions (if any)
6404 * if true, conditions are and-ed, else or-ed
6406 public static jalview.xml.binding.jalview.FeatureMatcherSet marshalFilter(
6407 FeatureMatcherI firstMatcher, Iterator<FeatureMatcherI> filters,
6410 jalview.xml.binding.jalview.FeatureMatcherSet result = new jalview.xml.binding.jalview.FeatureMatcherSet();
6412 if (filters.hasNext())
6417 CompoundMatcher compound = new CompoundMatcher();
6418 compound.setAnd(and);
6419 jalview.xml.binding.jalview.FeatureMatcherSet matcher1 = marshalFilter(
6420 firstMatcher, Collections.emptyIterator(), and);
6421 // compound.addMatcherSet(matcher1);
6422 compound.getMatcherSet().add(matcher1);
6423 FeatureMatcherI nextMatcher = filters.next();
6424 jalview.xml.binding.jalview.FeatureMatcherSet matcher2 = marshalFilter(
6425 nextMatcher, filters, and);
6426 // compound.addMatcherSet(matcher2);
6427 compound.getMatcherSet().add(matcher2);
6428 result.setCompoundMatcher(compound);
6433 * single condition matcher
6435 // MatchCondition matcherModel = new MatchCondition();
6436 jalview.xml.binding.jalview.FeatureMatcher matcherModel = new jalview.xml.binding.jalview.FeatureMatcher();
6437 matcherModel.setCondition(
6438 firstMatcher.getMatcher().getCondition().getStableName());
6439 matcherModel.setValue(firstMatcher.getMatcher().getPattern());
6440 if (firstMatcher.isByAttribute())
6442 matcherModel.setBy(FilterBy.BY_ATTRIBUTE);
6443 // matcherModel.setAttributeName(firstMatcher.getAttribute());
6444 String[] attName = firstMatcher.getAttribute();
6445 matcherModel.getAttributeName().add(attName[0]); // attribute
6446 if (attName.length > 1)
6448 matcherModel.getAttributeName().add(attName[1]); // sub-attribute
6451 else if (firstMatcher.isByLabel())
6453 matcherModel.setBy(FilterBy.BY_LABEL);
6455 else if (firstMatcher.isByScore())
6457 matcherModel.setBy(FilterBy.BY_SCORE);
6459 result.setMatchCondition(matcherModel);
6466 * Loads one XML model of a feature filter to a Jalview object
6468 * @param featureType
6469 * @param matcherSetModel
6472 public static FeatureMatcherSetI parseFilter(
6474 jalview.xml.binding.jalview.FeatureMatcherSet matcherSetModel)
6476 FeatureMatcherSetI result = new FeatureMatcherSet();
6479 parseFilterConditions(result, matcherSetModel, true);
6480 } catch (IllegalStateException e)
6482 // mixing AND and OR conditions perhaps
6484 String.format("Error reading filter conditions for '%s': %s",
6485 featureType, e.getMessage()));
6486 // return as much as was parsed up to the error
6493 * Adds feature match conditions to matcherSet as unmarshalled from XML
6494 * (possibly recursively for compound conditions)
6497 * @param matcherSetModel
6499 * if true, multiple conditions are AND-ed, else they are OR-ed
6500 * @throws IllegalStateException
6501 * if AND and OR conditions are mixed
6503 protected static void parseFilterConditions(
6504 FeatureMatcherSetI matcherSet,
6505 jalview.xml.binding.jalview.FeatureMatcherSet matcherSetModel,
6508 jalview.xml.binding.jalview.FeatureMatcher mc = matcherSetModel
6509 .getMatchCondition();
6515 FilterBy filterBy = mc.getBy();
6516 Condition cond = Condition.fromString(mc.getCondition());
6517 String pattern = mc.getValue();
6518 FeatureMatcherI matchCondition = null;
6519 if (filterBy == FilterBy.BY_LABEL)
6521 matchCondition = FeatureMatcher.byLabel(cond, pattern);
6523 else if (filterBy == FilterBy.BY_SCORE)
6525 matchCondition = FeatureMatcher.byScore(cond, pattern);
6528 else if (filterBy == FilterBy.BY_ATTRIBUTE)
6530 final List<String> attributeName = mc.getAttributeName();
6531 String[] attNames = attributeName
6532 .toArray(new String[attributeName.size()]);
6533 matchCondition = FeatureMatcher.byAttribute(cond, pattern,
6538 * note this throws IllegalStateException if AND-ing to a
6539 * previously OR-ed compound condition, or vice versa
6543 matcherSet.and(matchCondition);
6547 matcherSet.or(matchCondition);
6553 * compound condition
6555 List<jalview.xml.binding.jalview.FeatureMatcherSet> matchers = matcherSetModel
6556 .getCompoundMatcher().getMatcherSet();
6557 boolean anded = matcherSetModel.getCompoundMatcher().isAnd();
6558 if (matchers.size() == 2)
6560 parseFilterConditions(matcherSet, matchers.get(0), anded);
6561 parseFilterConditions(matcherSet, matchers.get(1), anded);
6565 System.err.println("Malformed compound filter condition");
6571 * Loads one XML model of a feature colour to a Jalview object
6573 * @param colourModel
6576 public static FeatureColourI parseColour(Colour colourModel)
6578 FeatureColourI colour = null;
6580 if (colourModel.getMax() != null)
6582 Color mincol = null;
6583 Color maxcol = null;
6584 Color noValueColour = null;
6588 mincol = new Color(Integer.parseInt(colourModel.getMinRGB(), 16));
6589 maxcol = new Color(Integer.parseInt(colourModel.getRGB(), 16));
6590 } catch (Exception e)
6592 Cache.log.warn("Couldn't parse out graduated feature color.", e);
6595 NoValueColour noCol = colourModel.getNoValueColour();
6596 if (noCol == NoValueColour.MIN)
6598 noValueColour = mincol;
6600 else if (noCol == NoValueColour.MAX)
6602 noValueColour = maxcol;
6605 colour = new FeatureColour(maxcol, mincol, maxcol, noValueColour,
6606 safeFloat(colourModel.getMin()),
6607 safeFloat(colourModel.getMax()));
6608 final List<String> attributeName = colourModel.getAttributeName();
6609 String[] attributes = attributeName
6610 .toArray(new String[attributeName.size()]);
6611 if (attributes != null && attributes.length > 0)
6613 colour.setAttributeName(attributes);
6615 if (colourModel.isAutoScale() != null)
6617 colour.setAutoScaled(colourModel.isAutoScale().booleanValue());
6619 if (colourModel.isColourByLabel() != null)
6621 colour.setColourByLabel(
6622 colourModel.isColourByLabel().booleanValue());
6624 if (colourModel.getThreshold() != null)
6626 colour.setThreshold(colourModel.getThreshold().floatValue());
6628 ThresholdType ttyp = colourModel.getThreshType();
6629 if (ttyp == ThresholdType.ABOVE)
6631 colour.setAboveThreshold(true);
6633 else if (ttyp == ThresholdType.BELOW)
6635 colour.setBelowThreshold(true);
6640 Color color = new Color(Integer.parseInt(colourModel.getRGB(), 16));
6641 colour = new FeatureColour(color);