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.GraphLine;
42 import jalview.datamodel.PDBEntry;
43 import jalview.datamodel.Point;
44 import jalview.datamodel.RnaViewerModel;
45 import jalview.datamodel.SequenceFeature;
46 import jalview.datamodel.SequenceGroup;
47 import jalview.datamodel.SequenceI;
48 import jalview.datamodel.StructureViewerModel;
49 import jalview.datamodel.StructureViewerModel.StructureData;
50 import jalview.datamodel.features.FeatureMatcher;
51 import jalview.datamodel.features.FeatureMatcherI;
52 import jalview.datamodel.features.FeatureMatcherSet;
53 import jalview.datamodel.features.FeatureMatcherSetI;
54 import jalview.ext.varna.RnaModel;
55 import jalview.gui.AlignFrame;
56 import jalview.gui.AlignViewport;
57 import jalview.gui.AlignmentPanel;
58 import jalview.gui.AppVarna;
59 import jalview.gui.ChimeraViewFrame;
60 import jalview.gui.Desktop;
61 import jalview.gui.FeatureRenderer;
62 import jalview.gui.JvOptionPane;
63 import jalview.gui.OOMWarning;
64 import jalview.gui.PCAPanel;
65 import jalview.gui.PaintRefresher;
66 import jalview.gui.SplitFrame;
67 import jalview.gui.StructureViewer;
68 import jalview.gui.StructureViewer.ViewerType;
69 import jalview.gui.StructureViewerBase;
70 import jalview.gui.TreePanel;
71 import jalview.io.BackupFiles;
72 import jalview.io.DataSourceType;
73 import jalview.io.FileFormat;
74 import jalview.io.NewickFile;
75 import jalview.math.Matrix;
76 import jalview.math.MatrixI;
77 import jalview.renderer.ResidueShaderI;
78 import jalview.schemes.AnnotationColourGradient;
79 import jalview.schemes.ColourSchemeI;
80 import jalview.schemes.ColourSchemeProperty;
81 import jalview.schemes.FeatureColour;
82 import jalview.schemes.ResidueProperties;
83 import jalview.schemes.UserColourScheme;
84 import jalview.structure.StructureSelectionManager;
85 import jalview.structures.models.AAStructureBindingModel;
86 import jalview.util.Format;
87 import jalview.util.MessageManager;
88 import jalview.util.Platform;
89 import jalview.util.StringUtils;
90 import jalview.util.jarInputStreamProvider;
91 import jalview.util.matcher.Condition;
92 import jalview.viewmodel.AlignmentViewport;
93 import jalview.viewmodel.PCAModel;
94 import jalview.viewmodel.ViewportRanges;
95 import jalview.viewmodel.seqfeatures.FeatureRendererSettings;
96 import jalview.viewmodel.seqfeatures.FeaturesDisplayed;
97 import jalview.ws.jws2.Jws2Discoverer;
98 import jalview.ws.jws2.dm.AAConSettings;
99 import jalview.ws.jws2.jabaws2.Jws2Instance;
100 import jalview.ws.params.ArgumentI;
101 import jalview.ws.params.AutoCalcSetting;
102 import jalview.ws.params.WsParamSetI;
103 import jalview.xml.binding.jalview.AlcodonFrame;
104 import jalview.xml.binding.jalview.AlcodonFrame.AlcodMap;
105 import jalview.xml.binding.jalview.Annotation;
106 import jalview.xml.binding.jalview.Annotation.ThresholdLine;
107 import jalview.xml.binding.jalview.AnnotationColourScheme;
108 import jalview.xml.binding.jalview.AnnotationElement;
109 import jalview.xml.binding.jalview.DoubleMatrix;
110 import jalview.xml.binding.jalview.DoubleVector;
111 import jalview.xml.binding.jalview.Feature;
112 import jalview.xml.binding.jalview.Feature.OtherData;
113 import jalview.xml.binding.jalview.FeatureMatcherSet.CompoundMatcher;
114 import jalview.xml.binding.jalview.FilterBy;
115 import jalview.xml.binding.jalview.JalviewModel;
116 import jalview.xml.binding.jalview.JalviewModel.FeatureSettings;
117 import jalview.xml.binding.jalview.JalviewModel.FeatureSettings.Group;
118 import jalview.xml.binding.jalview.JalviewModel.FeatureSettings.Setting;
119 import jalview.xml.binding.jalview.JalviewModel.JGroup;
120 import jalview.xml.binding.jalview.JalviewModel.JSeq;
121 import jalview.xml.binding.jalview.JalviewModel.JSeq.Pdbids;
122 import jalview.xml.binding.jalview.JalviewModel.JSeq.Pdbids.StructureState;
123 import jalview.xml.binding.jalview.JalviewModel.JSeq.RnaViewer;
124 import jalview.xml.binding.jalview.JalviewModel.JSeq.RnaViewer.SecondaryStructure;
125 import jalview.xml.binding.jalview.JalviewModel.PcaViewer;
126 import jalview.xml.binding.jalview.JalviewModel.PcaViewer.Axis;
127 import jalview.xml.binding.jalview.JalviewModel.PcaViewer.SeqPointMax;
128 import jalview.xml.binding.jalview.JalviewModel.PcaViewer.SeqPointMin;
129 import jalview.xml.binding.jalview.JalviewModel.PcaViewer.SequencePoint;
130 import jalview.xml.binding.jalview.JalviewModel.Tree;
131 import jalview.xml.binding.jalview.JalviewModel.UserColours;
132 import jalview.xml.binding.jalview.JalviewModel.Viewport;
133 import jalview.xml.binding.jalview.JalviewModel.Viewport.CalcIdParam;
134 import jalview.xml.binding.jalview.JalviewModel.Viewport.HiddenColumns;
135 import jalview.xml.binding.jalview.JalviewUserColours;
136 import jalview.xml.binding.jalview.JalviewUserColours.Colour;
137 import jalview.xml.binding.jalview.MapListType.MapListFrom;
138 import jalview.xml.binding.jalview.MapListType.MapListTo;
139 import jalview.xml.binding.jalview.Mapping;
140 import jalview.xml.binding.jalview.NoValueColour;
141 import jalview.xml.binding.jalview.ObjectFactory;
142 import jalview.xml.binding.jalview.PcaDataType;
143 import jalview.xml.binding.jalview.Pdbentry.Property;
144 import jalview.xml.binding.jalview.Sequence;
145 import jalview.xml.binding.jalview.Sequence.DBRef;
146 import jalview.xml.binding.jalview.SequenceSet;
147 import jalview.xml.binding.jalview.SequenceSet.SequenceSetProperties;
148 import jalview.xml.binding.jalview.ThresholdType;
149 import jalview.xml.binding.jalview.VAMSAS;
151 import java.awt.Color;
152 import java.awt.Font;
153 import java.awt.Rectangle;
154 import java.io.BufferedReader;
155 import java.io.DataInputStream;
156 import java.io.DataOutputStream;
158 import java.io.FileInputStream;
159 import java.io.FileOutputStream;
160 import java.io.IOException;
161 import java.io.InputStreamReader;
162 import java.io.OutputStreamWriter;
163 import java.io.PrintWriter;
164 import java.lang.reflect.InvocationTargetException;
165 import java.math.BigInteger;
166 import java.net.MalformedURLException;
168 import java.util.ArrayList;
169 import java.util.Arrays;
170 import java.util.Collections;
171 import java.util.Enumeration;
172 import java.util.GregorianCalendar;
173 import java.util.HashMap;
174 import java.util.HashSet;
175 import java.util.Hashtable;
176 import java.util.IdentityHashMap;
177 import java.util.Iterator;
178 import java.util.LinkedHashMap;
179 import java.util.List;
180 import java.util.Map;
181 import java.util.Map.Entry;
182 import java.util.Set;
183 import java.util.Vector;
184 import java.util.jar.JarEntry;
185 import java.util.jar.JarInputStream;
186 import java.util.jar.JarOutputStream;
188 import javax.swing.JInternalFrame;
189 import javax.swing.SwingUtilities;
190 import javax.xml.bind.JAXBContext;
191 import javax.xml.bind.JAXBElement;
192 import javax.xml.bind.Marshaller;
193 import javax.xml.datatype.DatatypeConfigurationException;
194 import javax.xml.datatype.DatatypeFactory;
195 import javax.xml.datatype.XMLGregorianCalendar;
196 import javax.xml.stream.XMLInputFactory;
197 import javax.xml.stream.XMLStreamReader;
200 * Write out the current jalview desktop state as a Jalview XML stream.
202 * Note: the vamsas objects referred to here are primitive versions of the
203 * VAMSAS project schema elements - they are not the same and most likely never
207 * @version $Revision: 1.134 $
209 public class Jalview2XML
211 private static final String VIEWER_PREFIX = "viewer_";
213 private static final String RNA_PREFIX = "rna_";
215 private static final String UTF_8 = "UTF-8";
218 * prefix for recovering datasets for alignments with multiple views where
219 * non-existent dataset IDs were written for some views
221 private static final String UNIQSEQSETID = "uniqueSeqSetId.";
223 // use this with nextCounter() to make unique names for entities
224 private int counter = 0;
227 * SequenceI reference -> XML ID string in jalview XML. Populated as XML reps
228 * of sequence objects are created.
230 IdentityHashMap<SequenceI, String> seqsToIds = null;
233 * jalview XML Sequence ID to jalview sequence object reference (both dataset
234 * and alignment sequences. Populated as XML reps of sequence objects are
237 Map<String, SequenceI> seqRefIds = null;
239 Map<String, SequenceI> incompleteSeqs = null;
241 List<SeqFref> frefedSequence = null;
243 boolean raiseGUI = true; // whether errors are raised in dialog boxes or not
246 * Map of reconstructed AlignFrame objects that appear to have come from
247 * SplitFrame objects (have a dna/protein complement view).
249 private Map<Viewport, AlignFrame> splitFrameCandidates = new HashMap<>();
252 * Map from displayed rna structure models to their saved session state jar
255 private Map<RnaModel, String> rnaSessions = new HashMap<>();
258 * A helper method for safely using the value of an optional attribute that
259 * may be null if not present in the XML. Answers the boolean value, or false
265 public static boolean safeBoolean(Boolean b)
267 return b == null ? false : b.booleanValue();
271 * A helper method for safely using the value of an optional attribute that
272 * may be null if not present in the XML. Answers the integer value, or zero
278 public static int safeInt(Integer i)
280 return i == null ? 0 : i.intValue();
284 * A helper method for safely using the value of an optional attribute that
285 * may be null if not present in the XML. Answers the float value, or zero if
291 public static float safeFloat(Float f)
293 return f == null ? 0f : f.floatValue();
297 * create/return unique hash string for sq
300 * @return new or existing unique string for sq
302 String seqHash(SequenceI sq)
304 if (seqsToIds == null)
308 if (seqsToIds.containsKey(sq))
310 return seqsToIds.get(sq);
314 // create sequential key
315 String key = "sq" + (seqsToIds.size() + 1);
316 key = makeHashCode(sq, key); // check we don't have an external reference
318 seqsToIds.put(sq, key);
325 if (seqsToIds == null)
327 seqsToIds = new IdentityHashMap<>();
329 if (seqRefIds == null)
331 seqRefIds = new HashMap<>();
333 if (incompleteSeqs == null)
335 incompleteSeqs = new HashMap<>();
337 if (frefedSequence == null)
339 frefedSequence = new ArrayList<>();
347 public Jalview2XML(boolean raiseGUI)
349 this.raiseGUI = raiseGUI;
353 * base class for resolving forward references to sequences by their ID
358 abstract class SeqFref
364 public SeqFref(String _sref, String type)
370 public String getSref()
375 public SequenceI getSrefSeq()
377 return seqRefIds.get(sref);
380 public boolean isResolvable()
382 return seqRefIds.get(sref) != null;
385 public SequenceI getSrefDatasetSeq()
387 SequenceI sq = seqRefIds.get(sref);
390 while (sq.getDatasetSequence() != null)
392 sq = sq.getDatasetSequence();
399 * @return true if the forward reference was fully resolved
401 abstract boolean resolve();
404 public String toString()
406 return type + " reference to " + sref;
411 * create forward reference for a mapping
417 public SeqFref newMappingRef(final String sref,
418 final jalview.datamodel.Mapping _jmap)
420 SeqFref fref = new SeqFref(sref, "Mapping")
422 public jalview.datamodel.Mapping jmap = _jmap;
427 SequenceI seq = getSrefDatasetSeq();
439 public SeqFref newAlcodMapRef(final String sref,
440 final AlignedCodonFrame _cf,
441 final jalview.datamodel.Mapping _jmap)
444 SeqFref fref = new SeqFref(sref, "Codon Frame")
446 AlignedCodonFrame cf = _cf;
448 public jalview.datamodel.Mapping mp = _jmap;
451 public boolean isResolvable()
453 return super.isResolvable() && mp.getTo() != null;
459 SequenceI seq = getSrefDatasetSeq();
464 cf.addMap(seq, mp.getTo(), mp.getMap());
471 public void resolveFrefedSequences()
473 Iterator<SeqFref> nextFref = frefedSequence.iterator();
474 int toresolve = frefedSequence.size();
475 int unresolved = 0, failedtoresolve = 0;
476 while (nextFref.hasNext())
478 SeqFref ref = nextFref.next();
479 if (ref.isResolvable())
491 } catch (Exception x)
494 "IMPLEMENTATION ERROR: Failed to resolve forward reference for sequence "
507 System.err.println("Jalview Project Import: There were " + unresolved
508 + " forward references left unresolved on the stack.");
510 if (failedtoresolve > 0)
512 System.err.println("SERIOUS! " + failedtoresolve
513 + " resolvable forward references failed to resolve.");
515 if (incompleteSeqs != null && incompleteSeqs.size() > 0)
518 "Jalview Project Import: There are " + incompleteSeqs.size()
519 + " sequences which may have incomplete metadata.");
520 if (incompleteSeqs.size() < 10)
522 for (SequenceI s : incompleteSeqs.values())
524 System.err.println(s.toString());
530 "Too many to report. Skipping output of incomplete sequences.");
536 * This maintains a map of viewports, the key being the seqSetId. Important to
537 * set historyItem and redoList for multiple views
539 Map<String, AlignViewport> viewportsAdded = new HashMap<>();
541 Map<String, AlignmentAnnotation> annotationIds = new HashMap<>();
543 String uniqueSetSuffix = "";
546 * List of pdbfiles added to Jar
548 List<String> pdbfiles = null;
550 // SAVES SEVERAL ALIGNMENT WINDOWS TO SAME JARFILE
551 public void saveState(File statefile)
553 FileOutputStream fos = null;
558 fos = new FileOutputStream(statefile);
560 JarOutputStream jout = new JarOutputStream(fos);
564 } catch (Exception e)
566 Cache.log.error("Couln't write Jalview state to " + statefile, e);
567 // TODO: inform user of the problem - they need to know if their data was
569 if (errorMessage == null)
571 errorMessage = "Did't write Jalview Archive to output file '"
572 + statefile + "' - See console error log for details";
576 errorMessage += "(Didn't write Jalview Archive to output file '"
587 } catch (IOException e)
597 * Writes a jalview project archive to the given Jar output stream.
601 public void saveState(JarOutputStream jout)
603 AlignFrame[] frames = Desktop.getAlignFrames();
609 saveAllFrames(Arrays.asList(frames), jout);
613 * core method for storing state for a set of AlignFrames.
616 * - frames involving all data to be exported (including containing
619 * - project output stream
621 private void saveAllFrames(List<AlignFrame> frames, JarOutputStream jout)
623 Hashtable<String, AlignFrame> dsses = new Hashtable<>();
626 * ensure cached data is clear before starting
628 // todo tidy up seqRefIds, seqsToIds initialisation / reset
630 splitFrameCandidates.clear();
635 // NOTE UTF-8 MUST BE USED FOR WRITING UNICODE CHARS
636 // //////////////////////////////////////////////////
638 List<String> shortNames = new ArrayList<>();
639 List<String> viewIds = new ArrayList<>();
642 for (int i = frames.size() - 1; i > -1; i--)
644 AlignFrame af = frames.get(i);
646 if (skipList != null && skipList
647 .containsKey(af.getViewport().getSequenceSetId()))
652 String shortName = makeFilename(af, shortNames);
654 int apSize = af.getAlignPanels().size();
656 for (int ap = 0; ap < apSize; ap++)
658 AlignmentPanel apanel = (AlignmentPanel) af.getAlignPanels()
660 String fileName = apSize == 1 ? shortName : ap + shortName;
661 if (!fileName.endsWith(".xml"))
663 fileName = fileName + ".xml";
666 saveState(apanel, fileName, jout, viewIds);
668 String dssid = getDatasetIdRef(
669 af.getViewport().getAlignment().getDataset());
670 if (!dsses.containsKey(dssid))
672 dsses.put(dssid, af);
677 writeDatasetFor(dsses, "" + jout.hashCode() + " " + uniqueSetSuffix,
683 } catch (Exception foo)
688 } catch (Exception ex)
690 // TODO: inform user of the problem - they need to know if their data was
692 if (errorMessage == null)
694 errorMessage = "Couldn't write Jalview Archive - see error output for details";
696 ex.printStackTrace();
701 * Generates a distinct file name, based on the title of the AlignFrame, by
702 * appending _n for increasing n until an unused name is generated. The new
703 * name (without its extension) is added to the list.
707 * @return the generated name, with .xml extension
709 protected String makeFilename(AlignFrame af, List<String> namesUsed)
711 String shortName = af.getTitle();
713 if (shortName.indexOf(File.separatorChar) > -1)
715 shortName = shortName
716 .substring(shortName.lastIndexOf(File.separatorChar) + 1);
721 while (namesUsed.contains(shortName))
723 if (shortName.endsWith("_" + (count - 1)))
725 shortName = shortName.substring(0, shortName.lastIndexOf("_"));
728 shortName = shortName.concat("_" + count);
732 namesUsed.add(shortName);
734 if (!shortName.endsWith(".xml"))
736 shortName = shortName + ".xml";
741 // USE THIS METHOD TO SAVE A SINGLE ALIGNMENT WINDOW
742 public boolean saveAlignment(AlignFrame af, String jarFile,
747 // create backupfiles object and get new temp filename destination
748 BackupFiles backupfiles = new BackupFiles(jarFile);
749 FileOutputStream fos = new FileOutputStream(
750 backupfiles.getTempFilePath());
752 JarOutputStream jout = new JarOutputStream(fos);
753 List<AlignFrame> frames = new ArrayList<>();
755 // resolve splitframes
756 if (af.getViewport().getCodingComplement() != null)
758 frames = ((SplitFrame) af.getSplitViewContainer()).getAlignFrames();
764 saveAllFrames(frames, jout);
768 } catch (Exception foo)
773 boolean success = true;
775 backupfiles.setWriteSuccess(success);
776 success = backupfiles.rollBackupsAndRenameTempFile();
779 } catch (Exception ex)
781 errorMessage = "Couldn't Write alignment view to Jalview Archive - see error output for details";
782 ex.printStackTrace();
787 private void writeDatasetFor(Hashtable<String, AlignFrame> dsses,
788 String fileName, JarOutputStream jout)
791 for (String dssids : dsses.keySet())
793 AlignFrame _af = dsses.get(dssids);
794 String jfileName = fileName + " Dataset for " + _af.getTitle();
795 if (!jfileName.endsWith(".xml"))
797 jfileName = jfileName + ".xml";
799 saveState(_af.alignPanel, jfileName, true, jout, null);
804 * create a JalviewModel from an alignment view and marshall it to a
808 * panel to create jalview model for
810 * name of alignment panel written to output stream
817 public JalviewModel saveState(AlignmentPanel ap, String fileName,
818 JarOutputStream jout, List<String> viewIds)
820 return saveState(ap, fileName, false, jout, viewIds);
824 * create a JalviewModel from an alignment view and marshall it to a
828 * panel to create jalview model for
830 * name of alignment panel written to output stream
832 * when true, only write the dataset for the alignment, not the data
833 * associated with the view.
839 public JalviewModel saveState(AlignmentPanel ap, String fileName,
840 boolean storeDS, JarOutputStream jout, List<String> viewIds)
844 viewIds = new ArrayList<>();
849 List<UserColourScheme> userColours = new ArrayList<>();
851 AlignViewport av = ap.av;
852 ViewportRanges vpRanges = av.getRanges();
854 final ObjectFactory objectFactory = new ObjectFactory();
855 JalviewModel object = objectFactory.createJalviewModel();
856 object.setVamsasModel(new VAMSAS());
858 // object.setCreationDate(new java.util.Date(System.currentTimeMillis()));
861 GregorianCalendar c = new GregorianCalendar();
862 DatatypeFactory datatypeFactory = DatatypeFactory.newInstance();
863 XMLGregorianCalendar now = datatypeFactory.newXMLGregorianCalendar(c);// gregorianCalendar);
864 object.setCreationDate(now);
865 } catch (DatatypeConfigurationException e)
867 System.err.println("error writing date: " + e.toString());
870 jalview.bin.Cache.getDefault("VERSION", "Development Build"));
873 * rjal is full height alignment, jal is actual alignment with full metadata
874 * but excludes hidden sequences.
876 jalview.datamodel.AlignmentI rjal = av.getAlignment(), jal = rjal;
878 if (av.hasHiddenRows())
880 rjal = jal.getHiddenSequences().getFullAlignment();
883 SequenceSet vamsasSet = new SequenceSet();
885 // JalviewModelSequence jms = new JalviewModelSequence();
887 vamsasSet.setGapChar(jal.getGapCharacter() + "");
889 if (jal.getDataset() != null)
891 // dataset id is the dataset's hashcode
892 vamsasSet.setDatasetId(getDatasetIdRef(jal.getDataset()));
895 // switch jal and the dataset
896 jal = jal.getDataset();
900 if (jal.getProperties() != null)
902 Enumeration en = jal.getProperties().keys();
903 while (en.hasMoreElements())
905 String key = en.nextElement().toString();
906 SequenceSetProperties ssp = new SequenceSetProperties();
908 ssp.setValue(jal.getProperties().get(key).toString());
909 // vamsasSet.addSequenceSetProperties(ssp);
910 vamsasSet.getSequenceSetProperties().add(ssp);
915 Set<String> calcIdSet = new HashSet<>();
916 // record the set of vamsas sequence XML POJO we create.
917 HashMap<String, Sequence> vamsasSetIds = new HashMap<>();
919 for (final SequenceI jds : rjal.getSequences())
921 final SequenceI jdatasq = jds.getDatasetSequence() == null ? jds
922 : jds.getDatasetSequence();
923 String id = seqHash(jds);
924 if (vamsasSetIds.get(id) == null)
926 if (seqRefIds.get(id) != null && !storeDS)
928 // This happens for two reasons: 1. multiple views are being
930 // 2. the hashCode has collided with another sequence's code. This
932 // HAPPEN! (PF00072.15.stk does this)
933 // JBPNote: Uncomment to debug writing out of files that do not read
934 // back in due to ArrayOutOfBoundExceptions.
935 // System.err.println("vamsasSeq backref: "+id+"");
936 // System.err.println(jds.getName()+"
937 // "+jds.getStart()+"-"+jds.getEnd()+" "+jds.getSequenceAsString());
938 // System.err.println("Hashcode: "+seqHash(jds));
939 // SequenceI rsq = (SequenceI) seqRefIds.get(id + "");
940 // System.err.println(rsq.getName()+"
941 // "+rsq.getStart()+"-"+rsq.getEnd()+" "+rsq.getSequenceAsString());
942 // System.err.println("Hashcode: "+seqHash(rsq));
946 vamsasSeq = createVamsasSequence(id, jds);
947 // vamsasSet.addSequence(vamsasSeq);
948 vamsasSet.getSequence().add(vamsasSeq);
949 vamsasSetIds.put(id, vamsasSeq);
950 seqRefIds.put(id, jds);
954 jseq.setStart(jds.getStart());
955 jseq.setEnd(jds.getEnd());
956 jseq.setColour(av.getSequenceColour(jds).getRGB());
958 jseq.setId(id); // jseq id should be a string not a number
961 // Store any sequences this sequence represents
962 if (av.hasHiddenRows())
964 // use rjal, contains the full height alignment
966 av.getAlignment().getHiddenSequences().isHidden(jds));
968 if (av.isHiddenRepSequence(jds))
970 jalview.datamodel.SequenceI[] reps = av
971 .getRepresentedSequences(jds).getSequencesInOrder(rjal);
973 for (int h = 0; h < reps.length; h++)
977 // jseq.addHiddenSequences(rjal.findIndex(reps[h]));
978 jseq.getHiddenSequences().add(rjal.findIndex(reps[h]));
983 // mark sequence as reference - if it is the reference for this view
986 jseq.setViewreference(jds == jal.getSeqrep());
990 // TODO: omit sequence features from each alignment view's XML dump if we
991 // are storing dataset
992 List<SequenceFeature> sfs = jds.getSequenceFeatures();
993 for (SequenceFeature sf : sfs)
995 // Features features = new Features();
996 Feature features = new Feature();
998 features.setBegin(sf.getBegin());
999 features.setEnd(sf.getEnd());
1000 features.setDescription(sf.getDescription());
1001 features.setType(sf.getType());
1002 features.setFeatureGroup(sf.getFeatureGroup());
1003 features.setScore(sf.getScore());
1004 if (sf.links != null)
1006 for (int l = 0; l < sf.links.size(); l++)
1008 OtherData keyValue = new OtherData();
1009 keyValue.setKey("LINK_" + l);
1010 keyValue.setValue(sf.links.elementAt(l).toString());
1011 // features.addOtherData(keyValue);
1012 features.getOtherData().add(keyValue);
1015 if (sf.otherDetails != null)
1018 * save feature attributes, which may be simple strings or
1019 * map valued (have sub-attributes)
1021 for (Entry<String, Object> entry : sf.otherDetails.entrySet())
1023 String key = entry.getKey();
1024 Object value = entry.getValue();
1025 if (value instanceof Map<?, ?>)
1027 for (Entry<String, Object> subAttribute : ((Map<String, Object>) value)
1030 OtherData otherData = new OtherData();
1031 otherData.setKey(key);
1032 otherData.setKey2(subAttribute.getKey());
1033 otherData.setValue(subAttribute.getValue().toString());
1034 // features.addOtherData(otherData);
1035 features.getOtherData().add(otherData);
1040 OtherData otherData = new OtherData();
1041 otherData.setKey(key);
1042 otherData.setValue(value.toString());
1043 // features.addOtherData(otherData);
1044 features.getOtherData().add(otherData);
1049 // jseq.addFeatures(features);
1050 jseq.getFeatures().add(features);
1053 if (jdatasq.getAllPDBEntries() != null)
1055 Enumeration<PDBEntry> en = jdatasq.getAllPDBEntries().elements();
1056 while (en.hasMoreElements())
1058 Pdbids pdb = new Pdbids();
1059 jalview.datamodel.PDBEntry entry = en.nextElement();
1061 String pdbId = entry.getId();
1063 pdb.setType(entry.getType());
1066 * Store any structure views associated with this sequence. This
1067 * section copes with duplicate entries in the project, so a dataset
1068 * only view *should* be coped with sensibly.
1070 // This must have been loaded, is it still visible?
1071 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
1072 String matchedFile = null;
1073 for (int f = frames.length - 1; f > -1; f--)
1075 if (frames[f] instanceof StructureViewerBase)
1077 StructureViewerBase viewFrame = (StructureViewerBase) frames[f];
1078 matchedFile = saveStructureState(ap, jds, pdb, entry, viewIds,
1079 matchedFile, viewFrame);
1081 * Only store each structure viewer's state once in the project
1082 * jar. First time through only (storeDS==false)
1084 String viewId = viewFrame.getViewId();
1085 if (!storeDS && !viewIds.contains(viewId))
1087 viewIds.add(viewId);
1090 String viewerState = viewFrame.getStateInfo();
1091 writeJarEntry(jout, getViewerJarEntryName(viewId),
1092 viewerState.getBytes());
1093 } catch (IOException e)
1096 "Error saving viewer state: " + e.getMessage());
1102 if (matchedFile != null || entry.getFile() != null)
1104 if (entry.getFile() != null)
1107 matchedFile = entry.getFile();
1109 pdb.setFile(matchedFile); // entry.getFile());
1110 if (pdbfiles == null)
1112 pdbfiles = new ArrayList<>();
1115 if (!pdbfiles.contains(pdbId))
1117 pdbfiles.add(pdbId);
1118 copyFileToJar(jout, matchedFile, pdbId);
1122 Enumeration<String> props = entry.getProperties();
1123 if (props.hasMoreElements())
1125 // PdbentryItem item = new PdbentryItem();
1126 while (props.hasMoreElements())
1128 Property prop = new Property();
1129 String key = props.nextElement();
1131 prop.setValue(entry.getProperty(key).toString());
1132 // item.addProperty(prop);
1133 pdb.getProperty().add(prop);
1135 // pdb.addPdbentryItem(item);
1138 // jseq.addPdbids(pdb);
1139 jseq.getPdbids().add(pdb);
1143 saveRnaViewers(jout, jseq, jds, viewIds, ap, storeDS);
1145 // jms.addJSeq(jseq);
1146 object.getJSeq().add(jseq);
1149 if (!storeDS && av.hasHiddenRows())
1151 jal = av.getAlignment();
1155 if (storeDS && jal.getCodonFrames() != null)
1157 List<AlignedCodonFrame> jac = jal.getCodonFrames();
1158 for (AlignedCodonFrame acf : jac)
1160 AlcodonFrame alc = new AlcodonFrame();
1161 if (acf.getProtMappings() != null
1162 && acf.getProtMappings().length > 0)
1164 boolean hasMap = false;
1165 SequenceI[] dnas = acf.getdnaSeqs();
1166 jalview.datamodel.Mapping[] pmaps = acf.getProtMappings();
1167 for (int m = 0; m < pmaps.length; m++)
1169 AlcodMap alcmap = new AlcodMap();
1170 alcmap.setDnasq(seqHash(dnas[m]));
1172 createVamsasMapping(pmaps[m], dnas[m], null, false));
1173 // alc.addAlcodMap(alcmap);
1174 alc.getAlcodMap().add(alcmap);
1179 // vamsasSet.addAlcodonFrame(alc);
1180 vamsasSet.getAlcodonFrame().add(alc);
1183 // TODO: delete this ? dead code from 2.8.3->2.9 ?
1185 // AlcodonFrame alc = new AlcodonFrame();
1186 // vamsasSet.addAlcodonFrame(alc);
1187 // for (int p = 0; p < acf.aaWidth; p++)
1189 // Alcodon cmap = new Alcodon();
1190 // if (acf.codons[p] != null)
1192 // // Null codons indicate a gapped column in the translated peptide
1194 // cmap.setPos1(acf.codons[p][0]);
1195 // cmap.setPos2(acf.codons[p][1]);
1196 // cmap.setPos3(acf.codons[p][2]);
1198 // alc.addAlcodon(cmap);
1200 // if (acf.getProtMappings() != null
1201 // && acf.getProtMappings().length > 0)
1203 // SequenceI[] dnas = acf.getdnaSeqs();
1204 // jalview.datamodel.Mapping[] pmaps = acf.getProtMappings();
1205 // for (int m = 0; m < pmaps.length; m++)
1207 // AlcodMap alcmap = new AlcodMap();
1208 // alcmap.setDnasq(seqHash(dnas[m]));
1209 // alcmap.setMapping(createVamsasMapping(pmaps[m], dnas[m], null,
1211 // alc.addAlcodMap(alcmap);
1218 // /////////////////////////////////
1219 if (!storeDS && av.getCurrentTree() != null)
1221 // FIND ANY ASSOCIATED TREES
1222 // NOT IMPLEMENTED FOR HEADLESS STATE AT PRESENT
1223 if (Desktop.desktop != null)
1225 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
1227 for (int t = 0; t < frames.length; t++)
1229 if (frames[t] instanceof TreePanel)
1231 TreePanel tp = (TreePanel) frames[t];
1233 if (tp.getTreeCanvas().getViewport().getAlignment() == jal)
1235 JalviewModel.Tree tree = new JalviewModel.Tree();
1236 tree.setTitle(tp.getTitle());
1237 tree.setCurrentTree((av.getCurrentTree() == tp.getTree()));
1238 tree.setNewick(tp.getTree().print());
1239 tree.setThreshold(tp.getTreeCanvas().getThreshold());
1241 tree.setFitToWindow(tp.fitToWindow.getState());
1242 tree.setFontName(tp.getTreeFont().getName());
1243 tree.setFontSize(tp.getTreeFont().getSize());
1244 tree.setFontStyle(tp.getTreeFont().getStyle());
1245 tree.setMarkUnlinked(tp.placeholdersMenu.getState());
1247 tree.setShowBootstrap(tp.bootstrapMenu.getState());
1248 tree.setShowDistances(tp.distanceMenu.getState());
1250 tree.setHeight(tp.getHeight());
1251 tree.setWidth(tp.getWidth());
1252 tree.setXpos(tp.getX());
1253 tree.setYpos(tp.getY());
1254 tree.setId(makeHashCode(tp, null));
1255 tree.setLinkToAllViews(
1256 tp.getTreeCanvas().isApplyToAllViews());
1258 // jms.addTree(tree);
1259 object.getTree().add(tree);
1269 if (!storeDS && Desktop.desktop != null)
1271 for (JInternalFrame frame : Desktop.desktop.getAllFrames())
1273 if (frame instanceof PCAPanel)
1275 PCAPanel panel = (PCAPanel) frame;
1276 if (panel.getAlignViewport().getAlignment() == jal)
1278 savePCA(panel, object);
1286 * store forward refs from an annotationRow to any groups
1288 IdentityHashMap<SequenceGroup, String> groupRefs = new IdentityHashMap<>();
1291 for (SequenceI sq : jal.getSequences())
1293 // Store annotation on dataset sequences only
1294 AlignmentAnnotation[] aa = sq.getAnnotation();
1295 if (aa != null && aa.length > 0)
1297 storeAlignmentAnnotation(aa, groupRefs, av, calcIdSet, storeDS,
1304 if (jal.getAlignmentAnnotation() != null)
1306 // Store the annotation shown on the alignment.
1307 AlignmentAnnotation[] aa = jal.getAlignmentAnnotation();
1308 storeAlignmentAnnotation(aa, groupRefs, av, calcIdSet, storeDS,
1313 if (jal.getGroups() != null)
1315 JGroup[] groups = new JGroup[jal.getGroups().size()];
1317 for (jalview.datamodel.SequenceGroup sg : jal.getGroups())
1319 JGroup jGroup = new JGroup();
1320 groups[++i] = jGroup;
1322 jGroup.setStart(sg.getStartRes());
1323 jGroup.setEnd(sg.getEndRes());
1324 jGroup.setName(sg.getName());
1325 if (groupRefs.containsKey(sg))
1327 // group has references so set its ID field
1328 jGroup.setId(groupRefs.get(sg));
1330 ColourSchemeI colourScheme = sg.getColourScheme();
1331 if (colourScheme != null)
1333 ResidueShaderI groupColourScheme = sg.getGroupColourScheme();
1334 if (groupColourScheme.conservationApplied())
1336 jGroup.setConsThreshold(groupColourScheme.getConservationInc());
1338 if (colourScheme instanceof jalview.schemes.UserColourScheme)
1341 setUserColourScheme(colourScheme, userColours,
1346 jGroup.setColour(colourScheme.getSchemeName());
1349 else if (colourScheme instanceof jalview.schemes.AnnotationColourGradient)
1351 jGroup.setColour("AnnotationColourGradient");
1352 jGroup.setAnnotationColours(constructAnnotationColours(
1353 (jalview.schemes.AnnotationColourGradient) colourScheme,
1354 userColours, object));
1356 else if (colourScheme instanceof jalview.schemes.UserColourScheme)
1359 setUserColourScheme(colourScheme, userColours, object));
1363 jGroup.setColour(colourScheme.getSchemeName());
1366 jGroup.setPidThreshold(groupColourScheme.getThreshold());
1369 jGroup.setOutlineColour(sg.getOutlineColour().getRGB());
1370 jGroup.setDisplayBoxes(sg.getDisplayBoxes());
1371 jGroup.setDisplayText(sg.getDisplayText());
1372 jGroup.setColourText(sg.getColourText());
1373 jGroup.setTextCol1(sg.textColour.getRGB());
1374 jGroup.setTextCol2(sg.textColour2.getRGB());
1375 jGroup.setTextColThreshold(sg.thresholdTextColour);
1376 jGroup.setShowUnconserved(sg.getShowNonconserved());
1377 jGroup.setIgnoreGapsinConsensus(sg.getIgnoreGapsConsensus());
1378 jGroup.setShowConsensusHistogram(sg.isShowConsensusHistogram());
1379 jGroup.setShowSequenceLogo(sg.isShowSequenceLogo());
1380 jGroup.setNormaliseSequenceLogo(sg.isNormaliseSequenceLogo());
1381 for (SequenceI seq : sg.getSequences())
1383 // jGroup.addSeq(seqHash(seq));
1384 jGroup.getSeq().add(seqHash(seq));
1388 //jms.setJGroup(groups);
1390 for (JGroup grp : groups)
1392 object.getJGroup().add(grp);
1397 // /////////SAVE VIEWPORT
1398 Viewport view = new Viewport();
1399 view.setTitle(ap.alignFrame.getTitle());
1400 view.setSequenceSetId(
1401 makeHashCode(av.getSequenceSetId(), av.getSequenceSetId()));
1402 view.setId(av.getViewId());
1403 if (av.getCodingComplement() != null)
1405 view.setComplementId(av.getCodingComplement().getViewId());
1407 view.setViewName(av.getViewName());
1408 view.setGatheredViews(av.isGatherViewsHere());
1410 Rectangle size = ap.av.getExplodedGeometry();
1411 Rectangle position = size;
1414 size = ap.alignFrame.getBounds();
1415 if (av.getCodingComplement() != null)
1417 position = ((SplitFrame) ap.alignFrame.getSplitViewContainer())
1425 view.setXpos(position.x);
1426 view.setYpos(position.y);
1428 view.setWidth(size.width);
1429 view.setHeight(size.height);
1431 view.setStartRes(vpRanges.getStartRes());
1432 view.setStartSeq(vpRanges.getStartSeq());
1434 if (av.getGlobalColourScheme() instanceof jalview.schemes.UserColourScheme)
1436 view.setBgColour(setUserColourScheme(av.getGlobalColourScheme(),
1437 userColours, object));
1440 .getGlobalColourScheme() instanceof jalview.schemes.AnnotationColourGradient)
1442 AnnotationColourScheme ac = constructAnnotationColours(
1443 (jalview.schemes.AnnotationColourGradient) av
1444 .getGlobalColourScheme(),
1445 userColours, object);
1447 view.setAnnotationColours(ac);
1448 view.setBgColour("AnnotationColourGradient");
1452 view.setBgColour(ColourSchemeProperty
1453 .getColourName(av.getGlobalColourScheme()));
1456 ResidueShaderI vcs = av.getResidueShading();
1457 ColourSchemeI cs = av.getGlobalColourScheme();
1461 if (vcs.conservationApplied())
1463 view.setConsThreshold(vcs.getConservationInc());
1464 if (cs instanceof jalview.schemes.UserColourScheme)
1466 view.setBgColour(setUserColourScheme(cs, userColours, object));
1469 view.setPidThreshold(vcs.getThreshold());
1472 view.setConservationSelected(av.getConservationSelected());
1473 view.setPidSelected(av.getAbovePIDThreshold());
1474 final Font font = av.getFont();
1475 view.setFontName(font.getName());
1476 view.setFontSize(font.getSize());
1477 view.setFontStyle(font.getStyle());
1478 view.setScaleProteinAsCdna(av.getViewStyle().isScaleProteinAsCdna());
1479 view.setRenderGaps(av.isRenderGaps());
1480 view.setShowAnnotation(av.isShowAnnotation());
1481 view.setShowBoxes(av.getShowBoxes());
1482 view.setShowColourText(av.getColourText());
1483 view.setShowFullId(av.getShowJVSuffix());
1484 view.setRightAlignIds(av.isRightAlignIds());
1485 view.setShowSequenceFeatures(av.isShowSequenceFeatures());
1486 view.setShowText(av.getShowText());
1487 view.setShowUnconserved(av.getShowUnconserved());
1488 view.setWrapAlignment(av.getWrapAlignment());
1489 view.setTextCol1(av.getTextColour().getRGB());
1490 view.setTextCol2(av.getTextColour2().getRGB());
1491 view.setTextColThreshold(av.getThresholdTextColour());
1492 view.setShowConsensusHistogram(av.isShowConsensusHistogram());
1493 view.setShowSequenceLogo(av.isShowSequenceLogo());
1494 view.setNormaliseSequenceLogo(av.isNormaliseSequenceLogo());
1495 view.setShowGroupConsensus(av.isShowGroupConsensus());
1496 view.setShowGroupConservation(av.isShowGroupConservation());
1497 view.setShowNPfeatureTooltip(av.isShowNPFeats());
1498 view.setShowDbRefTooltip(av.isShowDBRefs());
1499 view.setFollowHighlight(av.isFollowHighlight());
1500 view.setFollowSelection(av.followSelection);
1501 view.setIgnoreGapsinConsensus(av.isIgnoreGapsConsensus());
1502 if (av.getFeaturesDisplayed() != null)
1504 FeatureSettings fs = new FeatureSettings();
1506 FeatureRenderer fr = ap.getSeqPanel().seqCanvas
1507 .getFeatureRenderer();
1508 String[] renderOrder = fr.getRenderOrder().toArray(new String[0]);
1510 Vector<String> settingsAdded = new Vector<>();
1511 if (renderOrder != null)
1513 for (String featureType : renderOrder)
1515 FeatureSettings.Setting setting = new FeatureSettings.Setting();
1516 setting.setType(featureType);
1519 * save any filter for the feature type
1521 FeatureMatcherSetI filter = fr.getFeatureFilter(featureType);
1522 if (filter != null) {
1523 Iterator<FeatureMatcherI> filters = filter.getMatchers().iterator();
1524 FeatureMatcherI firstFilter = filters.next();
1525 setting.setMatcherSet(Jalview2XML.marshalFilter(
1526 firstFilter, filters, filter.isAnded()));
1530 * save colour scheme for the feature type
1532 FeatureColourI fcol = fr.getFeatureStyle(featureType);
1533 if (!fcol.isSimpleColour())
1535 setting.setColour(fcol.getMaxColour().getRGB());
1536 setting.setMincolour(fcol.getMinColour().getRGB());
1537 setting.setMin(fcol.getMin());
1538 setting.setMax(fcol.getMax());
1539 setting.setColourByLabel(fcol.isColourByLabel());
1540 if (fcol.isColourByAttribute())
1542 String[] attName = fcol.getAttributeName();
1543 setting.getAttributeName().add(attName[0]);
1544 if (attName.length > 1)
1546 setting.getAttributeName().add(attName[1]);
1549 setting.setAutoScale(fcol.isAutoScaled());
1550 setting.setThreshold(fcol.getThreshold());
1551 Color noColour = fcol.getNoColour();
1552 if (noColour == null)
1554 setting.setNoValueColour(NoValueColour.NONE);
1556 else if (noColour.equals(fcol.getMaxColour()))
1558 setting.setNoValueColour(NoValueColour.MAX);
1562 setting.setNoValueColour(NoValueColour.MIN);
1564 // -1 = No threshold, 0 = Below, 1 = Above
1565 setting.setThreshstate(fcol.isAboveThreshold() ? 1
1566 : (fcol.isBelowThreshold() ? 0 : -1));
1570 setting.setColour(fcol.getColour().getRGB());
1574 av.getFeaturesDisplayed().isVisible(featureType));
1576 .getOrder(featureType);
1579 setting.setOrder(rorder);
1581 /// fs.addSetting(setting);
1582 fs.getSetting().add(setting);
1583 settingsAdded.addElement(featureType);
1587 // is groups actually supposed to be a map here ?
1588 Iterator<String> en = fr.getFeatureGroups().iterator();
1589 Vector<String> groupsAdded = new Vector<>();
1590 while (en.hasNext())
1592 String grp = en.next();
1593 if (groupsAdded.contains(grp))
1597 Group g = new Group();
1599 g.setDisplay(((Boolean) fr.checkGroupVisibility(grp, false))
1602 fs.getGroup().add(g);
1603 groupsAdded.addElement(grp);
1605 // jms.setFeatureSettings(fs);
1606 object.setFeatureSettings(fs);
1609 if (av.hasHiddenColumns())
1611 jalview.datamodel.HiddenColumns hidden = av.getAlignment()
1612 .getHiddenColumns();
1615 warn("REPORT BUG: avoided null columnselection bug (DMAM reported). Please contact Jim about this.");
1619 Iterator<int[]> hiddenRegions = hidden.iterator();
1620 while (hiddenRegions.hasNext())
1622 int[] region = hiddenRegions.next();
1623 HiddenColumns hc = new HiddenColumns();
1624 hc.setStart(region[0]);
1625 hc.setEnd(region[1]);
1626 // view.addHiddenColumns(hc);
1627 view.getHiddenColumns().add(hc);
1631 if (calcIdSet.size() > 0)
1633 for (String calcId : calcIdSet)
1635 if (calcId.trim().length() > 0)
1637 CalcIdParam cidp = createCalcIdParam(calcId, av);
1638 // Some calcIds have no parameters.
1641 // view.addCalcIdParam(cidp);
1642 view.getCalcIdParam().add(cidp);
1648 // jms.addViewport(view);
1649 object.getViewport().add(view);
1651 // object.setJalviewModelSequence(jms);
1652 // object.getVamsasModel().addSequenceSet(vamsasSet);
1653 object.getVamsasModel().getSequenceSet().add(vamsasSet);
1655 if (jout != null && fileName != null)
1657 // We may not want to write the object to disk,
1658 // eg we can copy the alignViewport to a new view object
1659 // using save and then load
1662 System.out.println("Writing jar entry " + fileName);
1663 JarEntry entry = new JarEntry(fileName);
1664 jout.putNextEntry(entry);
1665 PrintWriter pout = new PrintWriter(
1666 new OutputStreamWriter(jout, UTF_8));
1667 JAXBContext jaxbContext = JAXBContext
1668 .newInstance(JalviewModel.class);
1669 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
1671 // output pretty printed
1672 // jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
1673 jaxbMarshaller.marshal(
1674 new ObjectFactory().createJalviewModel(object), pout);
1676 // jaxbMarshaller.marshal(object, pout);
1677 // marshaller.marshal(object);
1680 } catch (Exception ex)
1682 // TODO: raise error in GUI if marshalling failed.
1683 System.err.println("Error writing Jalview project");
1684 ex.printStackTrace();
1691 * Writes PCA viewer attributes and computed values to an XML model object and
1692 * adds it to the JalviewModel. Any exceptions are reported by logging.
1694 protected void savePCA(PCAPanel panel, JalviewModel object)
1698 PcaViewer viewer = new PcaViewer();
1699 viewer.setHeight(panel.getHeight());
1700 viewer.setWidth(panel.getWidth());
1701 viewer.setXpos(panel.getX());
1702 viewer.setYpos(panel.getY());
1703 viewer.setTitle(panel.getTitle());
1704 PCAModel pcaModel = panel.getPcaModel();
1705 viewer.setScoreModelName(pcaModel.getScoreModelName());
1706 viewer.setXDim(panel.getSelectedDimensionIndex(X));
1707 viewer.setYDim(panel.getSelectedDimensionIndex(Y));
1708 viewer.setZDim(panel.getSelectedDimensionIndex(Z));
1710 panel.getRotatableCanvas().getBackgroundColour().getRGB());
1711 viewer.setScaleFactor(panel.getRotatableCanvas().getScaleFactor());
1712 float[] spMin = panel.getRotatableCanvas().getSeqMin();
1713 SeqPointMin spmin = new SeqPointMin();
1714 spmin.setXPos(spMin[0]);
1715 spmin.setYPos(spMin[1]);
1716 spmin.setZPos(spMin[2]);
1717 viewer.setSeqPointMin(spmin);
1718 float[] spMax = panel.getRotatableCanvas().getSeqMax();
1719 SeqPointMax spmax = new SeqPointMax();
1720 spmax.setXPos(spMax[0]);
1721 spmax.setYPos(spMax[1]);
1722 spmax.setZPos(spMax[2]);
1723 viewer.setSeqPointMax(spmax);
1724 viewer.setShowLabels(panel.getRotatableCanvas().isShowLabels());
1725 viewer.setLinkToAllViews(
1726 panel.getRotatableCanvas().isApplyToAllViews());
1727 SimilarityParamsI sp = pcaModel.getSimilarityParameters();
1728 viewer.setIncludeGaps(sp.includeGaps());
1729 viewer.setMatchGaps(sp.matchGaps());
1730 viewer.setIncludeGappedColumns(sp.includeGappedColumns());
1731 viewer.setDenominateByShortestLength(sp.denominateByShortestLength());
1734 * sequence points on display
1736 for (jalview.datamodel.SequencePoint spt : pcaModel
1737 .getSequencePoints())
1739 SequencePoint point = new SequencePoint();
1740 point.setSequenceRef(seqHash(spt.getSequence()));
1741 point.setXPos(spt.coord.x);
1742 point.setYPos(spt.coord.y);
1743 point.setZPos(spt.coord.z);
1744 viewer.getSequencePoint().add(point);
1748 * (end points of) axes on display
1750 for (Point p : panel.getRotatableCanvas().getAxisEndPoints())
1753 Axis axis = new Axis();
1757 viewer.getAxis().add(axis);
1761 * raw PCA data (note we are not restoring PCA inputs here -
1762 * alignment view, score model, similarity parameters)
1764 PcaDataType data = new PcaDataType();
1765 viewer.setPcaData(data);
1766 PCA pca = pcaModel.getPcaData();
1768 DoubleMatrix pm = new DoubleMatrix();
1769 saveDoubleMatrix(pca.getPairwiseScores(), pm);
1770 data.setPairwiseMatrix(pm);
1772 DoubleMatrix tm = new DoubleMatrix();
1773 saveDoubleMatrix(pca.getTridiagonal(), tm);
1774 data.setTridiagonalMatrix(tm);
1776 DoubleMatrix eigenMatrix = new DoubleMatrix();
1777 data.setEigenMatrix(eigenMatrix);
1778 saveDoubleMatrix(pca.getEigenmatrix(), eigenMatrix);
1780 object.getPcaViewer().add(viewer);
1781 } catch (Throwable t)
1783 Cache.log.error("Error saving PCA: " + t.getMessage());
1788 * Stores values from a matrix into an XML element, including (if present) the
1793 * @see #loadDoubleMatrix(DoubleMatrix)
1795 protected void saveDoubleMatrix(MatrixI m, DoubleMatrix xmlMatrix)
1797 xmlMatrix.setRows(m.height());
1798 xmlMatrix.setColumns(m.width());
1799 for (int i = 0; i < m.height(); i++)
1801 DoubleVector row = new DoubleVector();
1802 for (int j = 0; j < m.width(); j++)
1804 row.getV().add(m.getValue(i, j));
1806 xmlMatrix.getRow().add(row);
1808 if (m.getD() != null)
1810 DoubleVector dVector = new DoubleVector();
1811 for (double d : m.getD())
1813 dVector.getV().add(d);
1815 xmlMatrix.setD(dVector);
1817 if (m.getE() != null)
1819 DoubleVector eVector = new DoubleVector();
1820 for (double e : m.getE())
1822 eVector.getV().add(e);
1824 xmlMatrix.setE(eVector);
1829 * Loads XML matrix data into a new Matrix object, including the D and/or E
1830 * vectors (if present)
1834 * @see Jalview2XML#saveDoubleMatrix(MatrixI, DoubleMatrix)
1836 protected MatrixI loadDoubleMatrix(DoubleMatrix mData)
1838 int rows = mData.getRows();
1839 double[][] vals = new double[rows][];
1841 for (int i = 0; i < rows; i++)
1843 List<Double> dVector = mData.getRow().get(i).getV();
1844 vals[i] = new double[dVector.size()];
1846 for (Double d : dVector)
1852 MatrixI m = new Matrix(vals);
1854 if (mData.getD() != null)
1856 List<Double> dVector = mData.getD().getV();
1857 double[] vec = new double[dVector.size()];
1859 for (Double d : dVector)
1865 if (mData.getE() != null)
1867 List<Double> dVector = mData.getE().getV();
1868 double[] vec = new double[dVector.size()];
1870 for (Double d : dVector)
1881 * Save any Varna viewers linked to this sequence. Writes an rnaViewer element
1882 * for each viewer, with
1884 * <li>viewer geometry (position, size, split pane divider location)</li>
1885 * <li>index of the selected structure in the viewer (currently shows gapped
1887 * <li>the id of the annotation holding RNA secondary structure</li>
1888 * <li>(currently only one SS is shown per viewer, may be more in future)</li>
1890 * Varna viewer state is also written out (in native Varna XML) to separate
1891 * project jar entries. A separate entry is written for each RNA structure
1892 * displayed, with the naming convention
1894 * <li>rna_viewId_sequenceId_annotationId_[gapped|trimmed]</li>
1902 * @param storeDataset
1904 protected void saveRnaViewers(JarOutputStream jout, JSeq jseq,
1905 final SequenceI jds, List<String> viewIds, AlignmentPanel ap,
1906 boolean storeDataset)
1908 if (Desktop.desktop == null)
1912 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
1913 for (int f = frames.length - 1; f > -1; f--)
1915 if (frames[f] instanceof AppVarna)
1917 AppVarna varna = (AppVarna) frames[f];
1919 * link the sequence to every viewer that is showing it and is linked to
1920 * its alignment panel
1922 if (varna.isListeningFor(jds) && ap == varna.getAlignmentPanel())
1924 String viewId = varna.getViewId();
1925 RnaViewer rna = new RnaViewer();
1926 rna.setViewId(viewId);
1927 rna.setTitle(varna.getTitle());
1928 rna.setXpos(varna.getX());
1929 rna.setYpos(varna.getY());
1930 rna.setWidth(varna.getWidth());
1931 rna.setHeight(varna.getHeight());
1932 rna.setDividerLocation(varna.getDividerLocation());
1933 rna.setSelectedRna(varna.getSelectedIndex());
1934 // jseq.addRnaViewer(rna);
1935 jseq.getRnaViewer().add(rna);
1938 * Store each Varna panel's state once in the project per sequence.
1939 * First time through only (storeDataset==false)
1941 // boolean storeSessions = false;
1942 // String sequenceViewId = viewId + seqsToIds.get(jds);
1943 // if (!storeDataset && !viewIds.contains(sequenceViewId))
1945 // viewIds.add(sequenceViewId);
1946 // storeSessions = true;
1948 for (RnaModel model : varna.getModels())
1950 if (model.seq == jds)
1953 * VARNA saves each view (sequence or alignment secondary
1954 * structure, gapped or trimmed) as a separate XML file
1956 String jarEntryName = rnaSessions.get(model);
1957 if (jarEntryName == null)
1960 String varnaStateFile = varna.getStateInfo(model.rna);
1961 jarEntryName = RNA_PREFIX + viewId + "_" + nextCounter();
1962 copyFileToJar(jout, varnaStateFile, jarEntryName);
1963 rnaSessions.put(model, jarEntryName);
1965 SecondaryStructure ss = new SecondaryStructure();
1966 String annotationId = varna.getAnnotation(jds).annotationId;
1967 ss.setAnnotationId(annotationId);
1968 ss.setViewerState(jarEntryName);
1969 ss.setGapped(model.gapped);
1970 ss.setTitle(model.title);
1971 // rna.addSecondaryStructure(ss);
1972 rna.getSecondaryStructure().add(ss);
1981 * Copy the contents of a file to a new entry added to the output jar
1985 * @param jarEntryName
1987 protected void copyFileToJar(JarOutputStream jout, String infilePath,
1988 String jarEntryName)
1990 DataInputStream dis = null;
1993 File file = new File(infilePath);
1994 if (file.exists() && jout != null)
1996 dis = new DataInputStream(new FileInputStream(file));
1997 byte[] data = new byte[(int) file.length()];
1998 dis.readFully(data);
1999 writeJarEntry(jout, jarEntryName, data);
2001 } catch (Exception ex)
2003 ex.printStackTrace();
2011 } catch (IOException e)
2020 * Write the data to a new entry of given name in the output jar file
2023 * @param jarEntryName
2025 * @throws IOException
2027 protected void writeJarEntry(JarOutputStream jout, String jarEntryName,
2028 byte[] data) throws IOException
2032 System.out.println("Writing jar entry " + jarEntryName);
2033 jout.putNextEntry(new JarEntry(jarEntryName));
2034 DataOutputStream dout = new DataOutputStream(jout);
2035 dout.write(data, 0, data.length);
2042 * Save the state of a structure viewer
2047 * the archive XML element under which to save the state
2050 * @param matchedFile
2054 protected String saveStructureState(AlignmentPanel ap, SequenceI jds,
2055 Pdbids pdb, PDBEntry entry, List<String> viewIds,
2056 String matchedFile, StructureViewerBase viewFrame)
2058 final AAStructureBindingModel bindingModel = viewFrame.getBinding();
2061 * Look for any bindings for this viewer to the PDB file of interest
2062 * (including part matches excluding chain id)
2064 for (int peid = 0; peid < bindingModel.getPdbCount(); peid++)
2066 final PDBEntry pdbentry = bindingModel.getPdbEntry(peid);
2067 final String pdbId = pdbentry.getId();
2068 if (!pdbId.equals(entry.getId())
2069 && !(entry.getId().length() > 4 && entry.getId().toLowerCase()
2070 .startsWith(pdbId.toLowerCase())))
2073 * not interested in a binding to a different PDB entry here
2077 if (matchedFile == null)
2079 matchedFile = pdbentry.getFile();
2081 else if (!matchedFile.equals(pdbentry.getFile()))
2084 "Probably lost some PDB-Sequence mappings for this structure file (which apparently has same PDB Entry code): "
2085 + pdbentry.getFile());
2089 // can get at it if the ID
2090 // match is ambiguous (e.g.
2093 for (int smap = 0; smap < viewFrame.getBinding()
2094 .getSequence()[peid].length; smap++)
2096 // if (jal.findIndex(jmol.jmb.sequence[peid][smap]) > -1)
2097 if (jds == viewFrame.getBinding().getSequence()[peid][smap])
2099 StructureState state = new StructureState();
2100 state.setVisible(true);
2101 state.setXpos(viewFrame.getX());
2102 state.setYpos(viewFrame.getY());
2103 state.setWidth(viewFrame.getWidth());
2104 state.setHeight(viewFrame.getHeight());
2105 final String viewId = viewFrame.getViewId();
2106 state.setViewId(viewId);
2107 state.setAlignwithAlignPanel(viewFrame.isUsedforaligment(ap));
2108 state.setColourwithAlignPanel(viewFrame.isUsedforcolourby(ap));
2109 state.setColourByJmol(viewFrame.isColouredByViewer());
2110 state.setType(viewFrame.getViewerType().toString());
2111 // pdb.addStructureState(state);
2112 pdb.getStructureState().add(state);
2120 * Populates the AnnotationColourScheme xml for save. This captures the
2121 * settings of the options in the 'Colour by Annotation' dialog.
2124 * @param userColours
2128 private AnnotationColourScheme constructAnnotationColours(
2129 AnnotationColourGradient acg, List<UserColourScheme> userColours,
2132 AnnotationColourScheme ac = new AnnotationColourScheme();
2133 ac.setAboveThreshold(acg.getAboveThreshold());
2134 ac.setThreshold(acg.getAnnotationThreshold());
2135 // 2.10.2 save annotationId (unique) not annotation label
2136 ac.setAnnotation(acg.getAnnotation().annotationId);
2137 if (acg.getBaseColour() instanceof UserColourScheme)
2140 setUserColourScheme(acg.getBaseColour(), userColours, jm));
2145 ColourSchemeProperty.getColourName(acg.getBaseColour()));
2148 ac.setMaxColour(acg.getMaxColour().getRGB());
2149 ac.setMinColour(acg.getMinColour().getRGB());
2150 ac.setPerSequence(acg.isSeqAssociated());
2151 ac.setPredefinedColours(acg.isPredefinedColours());
2155 private void storeAlignmentAnnotation(AlignmentAnnotation[] aa,
2156 IdentityHashMap<SequenceGroup, String> groupRefs,
2157 AlignmentViewport av, Set<String> calcIdSet, boolean storeDS,
2158 SequenceSet vamsasSet)
2161 for (int i = 0; i < aa.length; i++)
2163 Annotation an = new Annotation();
2165 AlignmentAnnotation annotation = aa[i];
2166 if (annotation.annotationId != null)
2168 annotationIds.put(annotation.annotationId, annotation);
2171 an.setId(annotation.annotationId);
2173 an.setVisible(annotation.visible);
2175 an.setDescription(annotation.description);
2177 if (annotation.sequenceRef != null)
2179 // 2.9 JAL-1781 xref on sequence id rather than name
2180 an.setSequenceRef(seqsToIds.get(annotation.sequenceRef));
2182 if (annotation.groupRef != null)
2184 String groupIdr = groupRefs.get(annotation.groupRef);
2185 if (groupIdr == null)
2187 // make a locally unique String
2188 groupRefs.put(annotation.groupRef,
2189 groupIdr = ("" + System.currentTimeMillis()
2190 + annotation.groupRef.getName()
2191 + groupRefs.size()));
2193 an.setGroupRef(groupIdr.toString());
2196 // store all visualization attributes for annotation
2197 an.setGraphHeight(annotation.graphHeight);
2198 an.setCentreColLabels(annotation.centreColLabels);
2199 an.setScaleColLabels(annotation.scaleColLabel);
2200 an.setShowAllColLabels(annotation.showAllColLabels);
2201 an.setBelowAlignment(annotation.belowAlignment);
2203 if (annotation.graph > 0)
2206 an.setGraphType(annotation.graph);
2207 an.setGraphGroup(annotation.graphGroup);
2208 if (annotation.getThreshold() != null)
2210 ThresholdLine line = new ThresholdLine();
2211 line.setLabel(annotation.getThreshold().label);
2212 line.setValue(annotation.getThreshold().value);
2213 line.setColour(annotation.getThreshold().colour.getRGB());
2214 an.setThresholdLine(line);
2222 an.setLabel(annotation.label);
2224 if (annotation == av.getAlignmentQualityAnnot()
2225 || annotation == av.getAlignmentConservationAnnotation()
2226 || annotation == av.getAlignmentConsensusAnnotation()
2227 || annotation.autoCalculated)
2229 // new way of indicating autocalculated annotation -
2230 an.setAutoCalculated(annotation.autoCalculated);
2232 if (annotation.hasScore())
2234 an.setScore(annotation.getScore());
2237 if (annotation.getCalcId() != null)
2239 calcIdSet.add(annotation.getCalcId());
2240 an.setCalcId(annotation.getCalcId());
2242 if (annotation.hasProperties())
2244 for (String pr : annotation.getProperties())
2246 jalview.xml.binding.jalview.Annotation.Property prop = new jalview.xml.binding.jalview.Annotation.Property();
2248 prop.setValue(annotation.getProperty(pr));
2249 // an.addProperty(prop);
2250 an.getProperty().add(prop);
2254 AnnotationElement ae;
2255 if (annotation.annotations != null)
2257 an.setScoreOnly(false);
2258 for (int a = 0; a < annotation.annotations.length; a++)
2260 if ((annotation == null) || (annotation.annotations[a] == null))
2265 ae = new AnnotationElement();
2266 if (annotation.annotations[a].description != null)
2268 ae.setDescription(annotation.annotations[a].description);
2270 if (annotation.annotations[a].displayCharacter != null)
2272 ae.setDisplayCharacter(
2273 annotation.annotations[a].displayCharacter);
2276 if (!Float.isNaN(annotation.annotations[a].value))
2278 ae.setValue(annotation.annotations[a].value);
2282 if (annotation.annotations[a].secondaryStructure > ' ')
2284 ae.setSecondaryStructure(
2285 annotation.annotations[a].secondaryStructure + "");
2288 if (annotation.annotations[a].colour != null
2289 && annotation.annotations[a].colour != java.awt.Color.black)
2291 ae.setColour(annotation.annotations[a].colour.getRGB());
2294 // an.addAnnotationElement(ae);
2295 an.getAnnotationElement().add(ae);
2296 if (annotation.autoCalculated)
2298 // only write one non-null entry into the annotation row -
2299 // sufficient to get the visualization attributes necessary to
2307 an.setScoreOnly(true);
2309 if (!storeDS || (storeDS && !annotation.autoCalculated))
2311 // skip autocalculated annotation - these are only provided for
2313 // vamsasSet.addAnnotation(an);
2314 vamsasSet.getAnnotation().add(an);
2320 private CalcIdParam createCalcIdParam(String calcId, AlignViewport av)
2322 AutoCalcSetting settings = av.getCalcIdSettingsFor(calcId);
2323 if (settings != null)
2325 CalcIdParam vCalcIdParam = new CalcIdParam();
2326 vCalcIdParam.setCalcId(calcId);
2327 // vCalcIdParam.addServiceURL(settings.getServiceURI());
2328 vCalcIdParam.getServiceURL().add(settings.getServiceURI());
2329 // generic URI allowing a third party to resolve another instance of the
2330 // service used for this calculation
2331 for (String url : settings.getServiceURLs())
2333 // vCalcIdParam.addServiceURL(urls);
2334 vCalcIdParam.getServiceURL().add(url);
2336 vCalcIdParam.setVersion("1.0");
2337 if (settings.getPreset() != null)
2339 WsParamSetI setting = settings.getPreset();
2340 vCalcIdParam.setName(setting.getName());
2341 vCalcIdParam.setDescription(setting.getDescription());
2345 vCalcIdParam.setName("");
2346 vCalcIdParam.setDescription("Last used parameters");
2348 // need to be able to recover 1) settings 2) user-defined presets or
2349 // recreate settings from preset 3) predefined settings provided by
2350 // service - or settings that can be transferred (or discarded)
2351 vCalcIdParam.setParameters(
2352 settings.getWsParamFile().replace("\n", "|\\n|"));
2353 vCalcIdParam.setAutoUpdate(settings.isAutoUpdate());
2354 // todo - decide if updateImmediately is needed for any projects.
2356 return vCalcIdParam;
2361 private boolean recoverCalcIdParam(CalcIdParam calcIdParam,
2364 if (calcIdParam.getVersion().equals("1.0"))
2366 final String[] calcIds = calcIdParam.getServiceURL().toArray(new String[0]);
2367 Jws2Instance service = Jws2Discoverer.getDiscoverer()
2368 .getPreferredServiceFor(calcIds);
2369 if (service != null)
2371 WsParamSetI parmSet = null;
2374 parmSet = service.getParamStore().parseServiceParameterFile(
2375 calcIdParam.getName(), calcIdParam.getDescription(),
2377 calcIdParam.getParameters().replace("|\\n|", "\n"));
2378 } catch (IOException x)
2380 warn("Couldn't parse parameter data for "
2381 + calcIdParam.getCalcId(), x);
2384 List<ArgumentI> argList = null;
2385 if (calcIdParam.getName().length() > 0)
2387 parmSet = service.getParamStore()
2388 .getPreset(calcIdParam.getName());
2389 if (parmSet != null)
2391 // TODO : check we have a good match with settings in AACon -
2392 // otherwise we'll need to create a new preset
2397 argList = parmSet.getArguments();
2400 AAConSettings settings = new AAConSettings(
2401 calcIdParam.isAutoUpdate(), service, parmSet, argList);
2402 av.setCalcIdSettingsFor(calcIdParam.getCalcId(), settings,
2403 calcIdParam.isNeedsUpdate());
2408 warn("Cannot resolve a service for the parameters used in this project. Try configuring a JABAWS server.");
2412 throw new Error(MessageManager.formatMessage(
2413 "error.unsupported_version_calcIdparam", new Object[]
2414 { calcIdParam.toString() }));
2418 * External mapping between jalview objects and objects yielding a valid and
2419 * unique object ID string. This is null for normal Jalview project IO, but
2420 * non-null when a jalview project is being read or written as part of a
2423 IdentityHashMap jv2vobj = null;
2426 * Construct a unique ID for jvobj using either existing bindings or if none
2427 * exist, the result of the hashcode call for the object.
2430 * jalview data object
2431 * @return unique ID for referring to jvobj
2433 private String makeHashCode(Object jvobj, String altCode)
2435 if (jv2vobj != null)
2437 Object id = jv2vobj.get(jvobj);
2440 return id.toString();
2442 // check string ID mappings
2443 if (jvids2vobj != null && jvobj instanceof String)
2445 id = jvids2vobj.get(jvobj);
2449 return id.toString();
2451 // give up and warn that something has gone wrong
2452 warn("Cannot find ID for object in external mapping : " + jvobj);
2458 * return local jalview object mapped to ID, if it exists
2462 * @return null or object bound to idcode
2464 private Object retrieveExistingObj(String idcode)
2466 if (idcode != null && vobj2jv != null)
2468 return vobj2jv.get(idcode);
2474 * binding from ID strings from external mapping table to jalview data model
2477 private Hashtable vobj2jv;
2479 private Sequence createVamsasSequence(String id, SequenceI jds)
2481 return createVamsasSequence(true, id, jds, null);
2484 private Sequence createVamsasSequence(boolean recurse, String id,
2485 SequenceI jds, SequenceI parentseq)
2487 Sequence vamsasSeq = new Sequence();
2488 vamsasSeq.setId(id);
2489 vamsasSeq.setName(jds.getName());
2490 vamsasSeq.setSequence(jds.getSequenceAsString());
2491 vamsasSeq.setDescription(jds.getDescription());
2492 jalview.datamodel.DBRefEntry[] dbrefs = null;
2493 if (jds.getDatasetSequence() != null)
2495 vamsasSeq.setDsseqid(seqHash(jds.getDatasetSequence()));
2499 // seqId==dsseqid so we can tell which sequences really are
2500 // dataset sequences only
2501 vamsasSeq.setDsseqid(id);
2502 dbrefs = jds.getDBRefs();
2503 if (parentseq == null)
2510 for (int d = 0; d < dbrefs.length; d++)
2512 DBRef dbref = new DBRef();
2513 dbref.setSource(dbrefs[d].getSource());
2514 dbref.setVersion(dbrefs[d].getVersion());
2515 dbref.setAccessionId(dbrefs[d].getAccessionId());
2516 if (dbrefs[d].hasMap())
2518 Mapping mp = createVamsasMapping(dbrefs[d].getMap(), parentseq,
2520 dbref.setMapping(mp);
2522 // vamsasSeq.addDBRef(dbref);
2523 vamsasSeq.getDBRef().add(dbref);
2529 private Mapping createVamsasMapping(jalview.datamodel.Mapping jmp,
2530 SequenceI parentseq, SequenceI jds, boolean recurse)
2533 if (jmp.getMap() != null)
2537 jalview.util.MapList mlst = jmp.getMap();
2538 List<int[]> r = mlst.getFromRanges();
2539 for (int[] range : r)
2541 MapListFrom mfrom = new MapListFrom();
2542 mfrom.setStart(range[0]);
2543 mfrom.setEnd(range[1]);
2544 // mp.addMapListFrom(mfrom);
2545 mp.getMapListFrom().add(mfrom);
2547 r = mlst.getToRanges();
2548 for (int[] range : r)
2550 MapListTo mto = new MapListTo();
2551 mto.setStart(range[0]);
2552 mto.setEnd(range[1]);
2553 // mp.addMapListTo(mto);
2554 mp.getMapListTo().add(mto);
2556 mp.setMapFromUnit(BigInteger.valueOf(mlst.getFromRatio()));
2557 mp.setMapToUnit(BigInteger.valueOf(mlst.getToRatio()));
2558 if (jmp.getTo() != null)
2560 // MappingChoice mpc = new MappingChoice();
2562 // check/create ID for the sequence referenced by getTo()
2565 SequenceI ps = null;
2566 if (parentseq != jmp.getTo()
2567 && parentseq.getDatasetSequence() != jmp.getTo())
2569 // chaining dbref rather than a handshaking one
2570 jmpid = seqHash(ps = jmp.getTo());
2574 jmpid = seqHash(ps = parentseq);
2576 // mpc.setDseqFor(jmpid);
2577 mp.setDseqFor(jmpid);
2578 if (!seqRefIds.containsKey(jmpid))
2580 jalview.bin.Cache.log.debug("creatign new DseqFor ID");
2581 seqRefIds.put(jmpid, ps);
2585 jalview.bin.Cache.log.debug("reusing DseqFor ID");
2588 // mp.setMappingChoice(mpc);
2594 String setUserColourScheme(jalview.schemes.ColourSchemeI cs,
2595 List<UserColourScheme> userColours, JalviewModel jm)
2598 jalview.schemes.UserColourScheme ucs = (jalview.schemes.UserColourScheme) cs;
2599 boolean newucs = false;
2600 if (!userColours.contains(ucs))
2602 userColours.add(ucs);
2605 id = "ucs" + userColours.indexOf(ucs);
2608 // actually create the scheme's entry in the XML model
2609 java.awt.Color[] colours = ucs.getColours();
2610 UserColours uc = new UserColours();
2611 // UserColourScheme jbucs = new UserColourScheme();
2612 JalviewUserColours jbucs = new JalviewUserColours();
2614 for (int i = 0; i < colours.length; i++)
2616 Colour col = new Colour();
2617 col.setName(ResidueProperties.aa[i]);
2618 col.setRGB(jalview.util.Format.getHexString(colours[i]));
2619 // jbucs.addColour(col);
2620 jbucs.getColour().add(col);
2622 if (ucs.getLowerCaseColours() != null)
2624 colours = ucs.getLowerCaseColours();
2625 for (int i = 0; i < colours.length; i++)
2627 Colour col = new Colour();
2628 col.setName(ResidueProperties.aa[i].toLowerCase());
2629 col.setRGB(jalview.util.Format.getHexString(colours[i]));
2630 // jbucs.addColour(col);
2631 jbucs.getColour().add(col);
2636 uc.setUserColourScheme(jbucs);
2637 // jm.addUserColours(uc);
2638 jm.getUserColours().add(uc);
2644 jalview.schemes.UserColourScheme getUserColourScheme(
2645 JalviewModel jm, String id)
2647 List<UserColours> uc = jm.getUserColours();
2648 UserColours colours = null;
2650 for (int i = 0; i < uc.length; i++)
2652 if (uc[i].getId().equals(id))
2659 for (UserColours c : uc)
2661 if (c.getId().equals(id))
2668 java.awt.Color[] newColours = new java.awt.Color[24];
2670 for (int i = 0; i < 24; i++)
2672 newColours[i] = new java.awt.Color(Integer.parseInt(
2673 // colours.getUserColourScheme().getColour(i).getRGB(), 16));
2674 colours.getUserColourScheme().getColour().get(i).getRGB(),
2678 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(
2681 if (colours.getUserColourScheme().getColour().size()/*Count()*/ > 24)
2683 newColours = new java.awt.Color[23];
2684 for (int i = 0; i < 23; i++)
2686 newColours[i] = new java.awt.Color(Integer.parseInt(
2687 colours.getUserColourScheme().getColour().get(i + 24)
2691 ucs.setLowerCaseColours(newColours);
2698 * contains last error message (if any) encountered by XML loader.
2700 String errorMessage = null;
2703 * flag to control whether the Jalview2XML_V1 parser should be deferred to if
2704 * exceptions are raised during project XML parsing
2706 public boolean attemptversion1parse = false;
2709 * Load a jalview project archive from a jar file
2712 * - HTTP URL or filename
2714 public AlignFrame loadJalviewAlign(final String file)
2717 jalview.gui.AlignFrame af = null;
2721 // create list to store references for any new Jmol viewers created
2722 newStructureViewers = new Vector<>();
2723 // UNMARSHALLER SEEMS TO CLOSE JARINPUTSTREAM, MOST ANNOYING
2724 // Workaround is to make sure caller implements the JarInputStreamProvider
2726 // so we can re-open the jar input stream for each entry.
2728 jarInputStreamProvider jprovider = createjarInputStreamProvider(file);
2729 af = loadJalviewAlign(jprovider);
2732 af.setMenusForViewport();
2734 } catch (MalformedURLException e)
2736 errorMessage = "Invalid URL format for '" + file + "'";
2742 SwingUtilities.invokeAndWait(new Runnable()
2747 setLoadingFinishedForNewStructureViewers();
2750 } catch (Exception x)
2752 System.err.println("Error loading alignment: " + x.getMessage());
2758 private jarInputStreamProvider createjarInputStreamProvider(
2759 final String file) throws MalformedURLException
2762 errorMessage = null;
2763 uniqueSetSuffix = null;
2765 viewportsAdded.clear();
2766 frefedSequence = null;
2768 if (file.startsWith("http://"))
2770 url = new URL(file);
2772 final URL _url = url;
2773 return new jarInputStreamProvider()
2777 public JarInputStream getJarInputStream() throws IOException
2781 return new JarInputStream(_url.openStream());
2785 return new JarInputStream(new FileInputStream(file));
2790 public String getFilename()
2798 * Recover jalview session from a jalview project archive. Caller may
2799 * initialise uniqueSetSuffix, seqRefIds, viewportsAdded and frefedSequence
2800 * themselves. Any null fields will be initialised with default values,
2801 * non-null fields are left alone.
2806 public AlignFrame loadJalviewAlign(final jarInputStreamProvider jprovider)
2808 errorMessage = null;
2809 if (uniqueSetSuffix == null)
2811 uniqueSetSuffix = System.currentTimeMillis() % 100000 + "";
2813 if (seqRefIds == null)
2817 AlignFrame af = null, _af = null;
2818 IdentityHashMap<AlignmentI, AlignmentI> importedDatasets = new IdentityHashMap<>();
2819 Map<String, AlignFrame> gatherToThisFrame = new HashMap<>();
2820 final String file = jprovider.getFilename();
2823 JarInputStream jin = null;
2824 JarEntry jarentry = null;
2829 jin = jprovider.getJarInputStream();
2830 for (int i = 0; i < entryCount; i++)
2832 jarentry = jin.getNextJarEntry();
2835 if (jarentry != null && jarentry.getName().endsWith(".xml"))
2837 InputStreamReader in = new InputStreamReader(jin, UTF_8);
2838 // JalviewModel object = new JalviewModel();
2840 JAXBContext jc = JAXBContext
2841 .newInstance("jalview.xml.binding.jalview");
2842 XMLStreamReader streamReader = XMLInputFactory.newInstance()
2843 .createXMLStreamReader(jin);
2844 javax.xml.bind.Unmarshaller um = jc.createUnmarshaller();
2845 JAXBElement<JalviewModel> jbe = um
2846 .unmarshal(streamReader, JalviewModel.class);
2847 JalviewModel object = jbe.getValue();
2850 Unmarshaller unmar = new Unmarshaller(object);
2851 unmar.setValidation(false);
2852 object = (JalviewModel) unmar.unmarshal(in);
2854 if (true) // !skipViewport(object))
2856 _af = loadFromObject(object, file, true, jprovider);
2857 if (_af != null && object.getViewport().size() > 0)
2858 // getJalviewModelSequence().getViewportCount() > 0)
2862 // store a reference to the first view
2865 if (_af.getViewport().isGatherViewsHere())
2867 // if this is a gathered view, keep its reference since
2868 // after gathering views, only this frame will remain
2870 gatherToThisFrame.put(_af.getViewport().getSequenceSetId(),
2873 // Save dataset to register mappings once all resolved
2874 importedDatasets.put(
2875 af.getViewport().getAlignment().getDataset(),
2876 af.getViewport().getAlignment().getDataset());
2881 else if (jarentry != null)
2883 // Some other file here.
2886 } while (jarentry != null);
2887 resolveFrefedSequences();
2888 } catch (IOException ex)
2890 ex.printStackTrace();
2891 errorMessage = "Couldn't locate Jalview XML file : " + file;
2893 "Exception whilst loading jalview XML file : " + ex + "\n");
2894 } catch (Exception ex)
2896 System.err.println("Parsing as Jalview Version 2 file failed.");
2897 ex.printStackTrace(System.err);
2898 if (attemptversion1parse)
2900 // used to attempt to parse as V1 castor-generated xml
2902 if (Desktop.instance != null)
2904 Desktop.instance.stopLoading();
2908 System.out.println("Successfully loaded archive file");
2911 ex.printStackTrace();
2914 "Exception whilst loading jalview XML file : " + ex + "\n");
2915 } catch (OutOfMemoryError e)
2917 // Don't use the OOM Window here
2918 errorMessage = "Out of memory loading jalview XML file";
2919 System.err.println("Out of memory whilst loading jalview XML file");
2920 e.printStackTrace();
2924 * Regather multiple views (with the same sequence set id) to the frame (if
2925 * any) that is flagged as the one to gather to, i.e. convert them to tabbed
2926 * views instead of separate frames. Note this doesn't restore a state where
2927 * some expanded views in turn have tabbed views - the last "first tab" read
2928 * in will play the role of gatherer for all.
2930 for (AlignFrame fr : gatherToThisFrame.values())
2932 Desktop.instance.gatherViews(fr);
2935 restoreSplitFrames();
2936 for (AlignmentI ds : importedDatasets.keySet())
2938 if (ds.getCodonFrames() != null)
2940 StructureSelectionManager
2941 .getStructureSelectionManager(Desktop.instance)
2942 .registerMappings(ds.getCodonFrames());
2945 if (errorMessage != null)
2950 if (Desktop.instance != null)
2952 Desktop.instance.stopLoading();
2959 * Try to reconstruct and display SplitFrame windows, where each contains
2960 * complementary dna and protein alignments. Done by pairing up AlignFrame
2961 * objects (created earlier) which have complementary viewport ids associated.
2963 protected void restoreSplitFrames()
2965 List<SplitFrame> gatherTo = new ArrayList<>();
2966 List<AlignFrame> addedToSplitFrames = new ArrayList<>();
2967 Map<String, AlignFrame> dna = new HashMap<>();
2970 * Identify the DNA alignments
2972 for (Entry<Viewport, AlignFrame> candidate : splitFrameCandidates
2975 AlignFrame af = candidate.getValue();
2976 if (af.getViewport().getAlignment().isNucleotide())
2978 dna.put(candidate.getKey().getId(), af);
2983 * Try to match up the protein complements
2985 for (Entry<Viewport, AlignFrame> candidate : splitFrameCandidates
2988 AlignFrame af = candidate.getValue();
2989 if (!af.getViewport().getAlignment().isNucleotide())
2991 String complementId = candidate.getKey().getComplementId();
2992 // only non-null complements should be in the Map
2993 if (complementId != null && dna.containsKey(complementId))
2995 final AlignFrame dnaFrame = dna.get(complementId);
2996 SplitFrame sf = createSplitFrame(dnaFrame, af);
2997 addedToSplitFrames.add(dnaFrame);
2998 addedToSplitFrames.add(af);
2999 dnaFrame.setMenusForViewport();
3000 af.setMenusForViewport();
3001 if (af.getViewport().isGatherViewsHere())
3010 * Open any that we failed to pair up (which shouldn't happen!) as
3011 * standalone AlignFrame's.
3013 for (Entry<Viewport, AlignFrame> candidate : splitFrameCandidates
3016 AlignFrame af = candidate.getValue();
3017 if (!addedToSplitFrames.contains(af))
3019 Viewport view = candidate.getKey();
3020 Desktop.addInternalFrame(af, view.getTitle(),
3021 safeInt(view.getWidth()), safeInt(view.getHeight()));
3022 af.setMenusForViewport();
3023 System.err.println("Failed to restore view " + view.getTitle()
3024 + " to split frame");
3029 * Gather back into tabbed views as flagged.
3031 for (SplitFrame sf : gatherTo)
3033 Desktop.instance.gatherViews(sf);
3036 splitFrameCandidates.clear();
3040 * Construct and display one SplitFrame holding DNA and protein alignments.
3043 * @param proteinFrame
3046 protected SplitFrame createSplitFrame(AlignFrame dnaFrame,
3047 AlignFrame proteinFrame)
3049 SplitFrame splitFrame = new SplitFrame(dnaFrame, proteinFrame);
3050 String title = MessageManager.getString("label.linked_view_title");
3051 int width = (int) dnaFrame.getBounds().getWidth();
3052 int height = (int) (dnaFrame.getBounds().getHeight()
3053 + proteinFrame.getBounds().getHeight() + 50);
3056 * SplitFrame location is saved to both enclosed frames
3058 splitFrame.setLocation(dnaFrame.getX(), dnaFrame.getY());
3059 Desktop.addInternalFrame(splitFrame, title, width, height);
3062 * And compute cDNA consensus (couldn't do earlier with consensus as
3063 * mappings were not yet present)
3065 proteinFrame.getViewport().alignmentChanged(proteinFrame.alignPanel);
3071 * check errorMessage for a valid error message and raise an error box in the
3072 * GUI or write the current errorMessage to stderr and then clear the error
3075 protected void reportErrors()
3077 reportErrors(false);
3080 protected void reportErrors(final boolean saving)
3082 if (errorMessage != null)
3084 final String finalErrorMessage = errorMessage;
3087 javax.swing.SwingUtilities.invokeLater(new Runnable()
3092 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
3094 "Error " + (saving ? "saving" : "loading")
3096 JvOptionPane.WARNING_MESSAGE);
3102 System.err.println("Problem loading Jalview file: " + errorMessage);
3105 errorMessage = null;
3108 Map<String, String> alreadyLoadedPDB = new HashMap<>();
3111 * when set, local views will be updated from view stored in JalviewXML
3112 * Currently (28th Sep 2008) things will go horribly wrong in vamsas document
3113 * sync if this is set to true.
3115 private final boolean updateLocalViews = false;
3118 * Returns the path to a temporary file holding the PDB file for the given PDB
3119 * id. The first time of asking, searches for a file of that name in the
3120 * Jalview project jar, and copies it to a new temporary file. Any repeat
3121 * requests just return the path to the file previously created.
3127 String loadPDBFile(jarInputStreamProvider jprovider, String pdbId,
3130 if (alreadyLoadedPDB.containsKey(pdbId))
3132 return alreadyLoadedPDB.get(pdbId).toString();
3135 String tempFile = copyJarEntry(jprovider, pdbId, "jalview_pdb",
3137 if (tempFile != null)
3139 alreadyLoadedPDB.put(pdbId, tempFile);
3145 * Copies the jar entry of given name to a new temporary file and returns the
3146 * path to the file, or null if the entry is not found.
3149 * @param jarEntryName
3151 * a prefix for the temporary file name, must be at least three
3154 * null or original file - so new file can be given the same suffix
3158 protected String copyJarEntry(jarInputStreamProvider jprovider,
3159 String jarEntryName, String prefix, String origFile)
3161 BufferedReader in = null;
3162 PrintWriter out = null;
3163 String suffix = ".tmp";
3164 if (origFile == null)
3166 origFile = jarEntryName;
3168 int sfpos = origFile.lastIndexOf(".");
3169 if (sfpos > -1 && sfpos < (origFile.length() - 3))
3171 suffix = "." + origFile.substring(sfpos + 1);
3175 JarInputStream jin = jprovider.getJarInputStream();
3177 * if (jprovider.startsWith("http://")) { jin = new JarInputStream(new
3178 * URL(jprovider).openStream()); } else { jin = new JarInputStream(new
3179 * FileInputStream(jprovider)); }
3182 JarEntry entry = null;
3185 entry = jin.getNextJarEntry();
3186 } while (entry != null && !entry.getName().equals(jarEntryName));
3189 in = new BufferedReader(new InputStreamReader(jin, UTF_8));
3190 File outFile = File.createTempFile(prefix, suffix);
3191 outFile.deleteOnExit();
3192 out = new PrintWriter(new FileOutputStream(outFile));
3195 while ((data = in.readLine()) != null)
3200 String t = outFile.getAbsolutePath();
3205 warn("Couldn't find entry in Jalview Jar for " + jarEntryName);
3207 } catch (Exception ex)
3209 ex.printStackTrace();
3217 } catch (IOException e)
3231 private class JvAnnotRow
3233 public JvAnnotRow(int i, AlignmentAnnotation jaa)
3240 * persisted version of annotation row from which to take vis properties
3242 public jalview.datamodel.AlignmentAnnotation template;
3245 * original position of the annotation row in the alignment
3251 * Load alignment frame from jalview XML DOM object
3253 * @param jalviewModel
3256 * filename source string
3257 * @param loadTreesAndStructures
3258 * when false only create Viewport
3260 * data source provider
3261 * @return alignment frame created from view stored in DOM
3263 AlignFrame loadFromObject(JalviewModel jalviewModel, String file,
3264 boolean loadTreesAndStructures, jarInputStreamProvider jprovider)
3266 SequenceSet vamsasSet = jalviewModel.getVamsasModel().getSequenceSet().get(0);
3267 List<Sequence> vamsasSeqs = vamsasSet.getSequence();
3269 // JalviewModelSequence jms = object.getJalviewModelSequence();
3271 // Viewport view = (jms.getViewportCount() > 0) ? jms.getViewport(0)
3273 Viewport view = (jalviewModel.getViewport().size() > 0)
3274 ? jalviewModel.getViewport().get(0)
3277 // ////////////////////////////////
3278 // INITIALISE ALIGNMENT SEQUENCESETID AND VIEWID
3281 // If we just load in the same jar file again, the sequenceSetId
3282 // will be the same, and we end up with multiple references
3283 // to the same sequenceSet. We must modify this id on load
3284 // so that each load of the file gives a unique id
3287 * used to resolve correct alignment dataset for alignments with multiple
3290 String uniqueSeqSetId = null;
3291 String viewId = null;
3294 uniqueSeqSetId = view.getSequenceSetId() + uniqueSetSuffix;
3295 viewId = (view.getId() == null ? null
3296 : view.getId() + uniqueSetSuffix);
3299 // ////////////////////////////////
3302 List<SequenceI> hiddenSeqs = null;
3304 List<SequenceI> tmpseqs = new ArrayList<>();
3306 boolean multipleView = false;
3307 SequenceI referenceseqForView = null;
3308 // JSeq[] jseqs = object.getJalviewModelSequence().getJSeq();
3309 List<JSeq> jseqs = jalviewModel.getJSeq();
3310 int vi = 0; // counter in vamsasSeq array
3311 for (int i = 0; i < jseqs.size(); i++)
3313 JSeq jseq = jseqs.get(i);
3314 String seqId = jseq.getId();
3316 SequenceI tmpSeq = seqRefIds.get(seqId);
3319 if (!incompleteSeqs.containsKey(seqId))
3321 // may not need this check, but keep it for at least 2.9,1 release
3322 if (tmpSeq.getStart() != jseq.getStart()
3323 || tmpSeq.getEnd() != jseq.getEnd())
3326 "Warning JAL-2154 regression: updating start/end for sequence "
3327 + tmpSeq.toString() + " to " + jseq);
3332 incompleteSeqs.remove(seqId);
3334 if (vamsasSeqs.size() > vi
3335 && vamsasSeqs.get(vi).getId().equals(seqId))
3337 // most likely we are reading a dataset XML document so
3338 // update from vamsasSeq section of XML for this sequence
3339 tmpSeq.setName(vamsasSeqs.get(vi).getName());
3340 tmpSeq.setDescription(vamsasSeqs.get(vi).getDescription());
3341 tmpSeq.setSequence(vamsasSeqs.get(vi).getSequence());
3346 // reading multiple views, so vamsasSeq set is a subset of JSeq
3347 multipleView = true;
3349 tmpSeq.setStart(jseq.getStart());
3350 tmpSeq.setEnd(jseq.getEnd());
3351 tmpseqs.add(tmpSeq);
3355 Sequence vamsasSeq = vamsasSeqs.get(vi);
3356 tmpSeq = new jalview.datamodel.Sequence(vamsasSeq.getName(),
3357 vamsasSeq.getSequence());
3358 tmpSeq.setDescription(vamsasSeq.getDescription());
3359 tmpSeq.setStart(jseq.getStart());
3360 tmpSeq.setEnd(jseq.getEnd());
3361 tmpSeq.setVamsasId(uniqueSetSuffix + seqId);
3362 seqRefIds.put(vamsasSeq.getId(), tmpSeq);
3363 tmpseqs.add(tmpSeq);
3367 if (safeBoolean(jseq.isViewreference()))
3369 referenceseqForView = tmpseqs.get(tmpseqs.size() - 1);
3372 if (jseq.isHidden() != null && jseq.isHidden().booleanValue())
3374 if (hiddenSeqs == null)
3376 hiddenSeqs = new ArrayList<>();
3379 hiddenSeqs.add(tmpSeq);
3384 // Create the alignment object from the sequence set
3385 // ///////////////////////////////
3386 SequenceI[] orderedSeqs = tmpseqs
3387 .toArray(new SequenceI[tmpseqs.size()]);
3389 AlignmentI al = null;
3390 // so we must create or recover the dataset alignment before going further
3391 // ///////////////////////////////
3392 if (vamsasSet.getDatasetId() == null || vamsasSet.getDatasetId() == "")
3394 // older jalview projects do not have a dataset - so creat alignment and
3396 al = new Alignment(orderedSeqs);
3397 al.setDataset(null);
3401 boolean isdsal = jalviewModel.getViewport().isEmpty();
3404 // we are importing a dataset record, so
3405 // recover reference to an alignment already materialsed as dataset
3406 al = getDatasetFor(vamsasSet.getDatasetId());
3410 // materialse the alignment
3411 al = new Alignment(orderedSeqs);
3415 addDatasetRef(vamsasSet.getDatasetId(), al);
3418 // finally, verify all data in vamsasSet is actually present in al
3419 // passing on flag indicating if it is actually a stored dataset
3420 recoverDatasetFor(vamsasSet, al, isdsal, uniqueSeqSetId);
3423 if (referenceseqForView != null)
3425 al.setSeqrep(referenceseqForView);
3427 // / Add the alignment properties
3428 for (int i = 0; i < vamsasSet.getSequenceSetProperties().size(); i++)
3430 SequenceSetProperties ssp = vamsasSet.getSequenceSetProperties()
3432 al.setProperty(ssp.getKey(), ssp.getValue());
3435 // ///////////////////////////////
3437 Hashtable pdbloaded = new Hashtable(); // TODO nothing writes to this??
3440 // load sequence features, database references and any associated PDB
3441 // structures for the alignment
3443 // prior to 2.10, this part would only be executed the first time a
3444 // sequence was encountered, but not afterwards.
3445 // now, for 2.10 projects, this is also done if the xml doc includes
3446 // dataset sequences not actually present in any particular view.
3448 for (int i = 0; i < vamsasSeqs.size(); i++)
3450 JSeq jseq = jseqs.get(i);
3451 if (jseq.getFeatures().size() > 0)
3453 List<Feature> features = jseq.getFeatures();
3454 for (int f = 0; f < features.size(); f++)
3456 Feature feat = features.get(f);
3457 SequenceFeature sf = new SequenceFeature(feat.getType(),
3458 feat.getDescription(), feat.getBegin(), feat.getEnd(),
3459 safeFloat(feat.getScore()), feat.getFeatureGroup());
3460 sf.setStatus(feat.getStatus());
3463 * load any feature attributes - include map-valued attributes
3465 Map<String, Map<String, String>> mapAttributes = new HashMap<>();
3466 for (int od = 0; od < feat.getOtherData().size(); od++)
3468 OtherData keyValue = feat.getOtherData().get(od);
3469 String attributeName = keyValue.getKey();
3470 String attributeValue = keyValue.getValue();
3471 if (attributeName.startsWith("LINK"))
3473 sf.addLink(attributeValue);
3477 String subAttribute = keyValue.getKey2();
3478 if (subAttribute == null)
3480 // simple string-valued attribute
3481 sf.setValue(attributeName, attributeValue);
3485 // attribute 'key' has sub-attribute 'key2'
3486 if (!mapAttributes.containsKey(attributeName))
3488 mapAttributes.put(attributeName, new HashMap<>());
3490 mapAttributes.get(attributeName).put(subAttribute,
3495 for (Entry<String, Map<String, String>> mapAttribute : mapAttributes
3498 sf.setValue(mapAttribute.getKey(), mapAttribute.getValue());
3501 // adds feature to datasequence's feature set (since Jalview 2.10)
3502 al.getSequenceAt(i).addSequenceFeature(sf);
3505 if (vamsasSeqs.get(i).getDBRef().size() > 0)
3507 // adds dbrefs to datasequence's set (since Jalview 2.10)
3509 al.getSequenceAt(i).getDatasetSequence() == null
3510 ? al.getSequenceAt(i)
3511 : al.getSequenceAt(i).getDatasetSequence(),
3514 if (jseq.getPdbids().size() > 0)
3516 List<Pdbids> ids = jseq.getPdbids();
3517 for (int p = 0; p < ids.size(); p++)
3519 Pdbids pdbid = ids.get(p);
3520 jalview.datamodel.PDBEntry entry = new jalview.datamodel.PDBEntry();
3521 entry.setId(pdbid.getId());
3522 if (pdbid.getType() != null)
3524 if (PDBEntry.Type.getType(pdbid.getType()) != null)
3526 entry.setType(PDBEntry.Type.getType(pdbid.getType()));
3530 entry.setType(PDBEntry.Type.FILE);
3533 // jprovider is null when executing 'New View'
3534 if (pdbid.getFile() != null && jprovider != null)
3536 if (!pdbloaded.containsKey(pdbid.getFile()))
3538 entry.setFile(loadPDBFile(jprovider, pdbid.getId(),
3543 entry.setFile(pdbloaded.get(pdbid.getId()).toString());
3547 if (pdbid.getPdbentryItem() != null)
3549 for (PdbentryItem item : pdbid.getPdbentryItem())
3551 for (Property pr : item.getProperty())
3553 entry.setProperty(pr.getName(), pr.getValue());
3558 for (Property prop : pdbid.getProperty())
3560 entry.setProperty(prop.getName(), prop.getValue());
3562 StructureSelectionManager
3563 .getStructureSelectionManager(Desktop.instance)
3564 .registerPDBEntry(entry);
3565 // adds PDBEntry to datasequence's set (since Jalview 2.10)
3566 if (al.getSequenceAt(i).getDatasetSequence() != null)
3568 al.getSequenceAt(i).getDatasetSequence().addPDBId(entry);
3572 al.getSequenceAt(i).addPDBId(entry);
3577 } // end !multipleview
3579 // ///////////////////////////////
3580 // LOAD SEQUENCE MAPPINGS
3582 if (vamsasSet.getAlcodonFrame().size() > 0)
3584 // TODO Potentially this should only be done once for all views of an
3586 List<AlcodonFrame> alc = vamsasSet.getAlcodonFrame();
3587 for (int i = 0; i < alc.size(); i++)
3589 AlignedCodonFrame cf = new AlignedCodonFrame();
3590 if (alc.get(i).getAlcodMap().size() > 0)
3592 List<AlcodMap> maps = alc.get(i).getAlcodMap();
3593 for (int m = 0; m < maps.size(); m++)
3595 AlcodMap map = maps.get(m);
3596 SequenceI dnaseq = seqRefIds.get(map.getDnasq());
3598 jalview.datamodel.Mapping mapping = null;
3599 // attach to dna sequence reference.
3600 if (map.getMapping() != null)
3602 mapping = addMapping(map.getMapping());
3603 if (dnaseq != null && mapping.getTo() != null)
3605 cf.addMap(dnaseq, mapping.getTo(), mapping.getMap());
3611 newAlcodMapRef(map.getDnasq(), cf, mapping));
3615 al.addCodonFrame(cf);
3620 // ////////////////////////////////
3622 List<JvAnnotRow> autoAlan = new ArrayList<>();
3625 * store any annotations which forward reference a group's ID
3627 Map<String, List<AlignmentAnnotation>> groupAnnotRefs = new Hashtable<>();
3629 if (vamsasSet.getAnnotation().size()/*Count()*/ > 0)
3631 List<Annotation> an = vamsasSet.getAnnotation();
3633 for (int i = 0; i < an.size(); i++)
3635 Annotation annotation = an.get(i);
3638 * test if annotation is automatically calculated for this view only
3640 boolean autoForView = false;
3641 if (annotation.getLabel().equals("Quality")
3642 || annotation.getLabel().equals("Conservation")
3643 || annotation.getLabel().equals("Consensus"))
3645 // Kludge for pre 2.5 projects which lacked the autocalculated flag
3647 // JAXB has no has() test; schema defaults value to false
3648 // if (!annotation.hasAutoCalculated())
3650 // annotation.setAutoCalculated(true);
3653 if (autoForView || annotation.isAutoCalculated())
3655 // remove ID - we don't recover annotation from other views for
3656 // view-specific annotation
3657 annotation.setId(null);
3660 // set visibility for other annotation in this view
3661 String annotationId = annotation.getId();
3662 if (annotationId != null && annotationIds.containsKey(annotationId))
3664 AlignmentAnnotation jda = annotationIds.get(annotationId);
3665 // in principle Visible should always be true for annotation displayed
3666 // in multiple views
3667 if (annotation.isVisible() != null)
3669 jda.visible = annotation.isVisible();
3672 al.addAnnotation(jda);
3676 // Construct new annotation from model.
3677 List<AnnotationElement> ae = annotation.getAnnotationElement();
3678 jalview.datamodel.Annotation[] anot = null;
3679 java.awt.Color firstColour = null;
3681 if (!annotation.isScoreOnly())
3683 anot = new jalview.datamodel.Annotation[al.getWidth()];
3684 for (int aa = 0; aa < ae.size() && aa < anot.length; aa++)
3686 AnnotationElement annElement = ae.get(aa);
3687 anpos = annElement.getPosition();
3689 if (anpos >= anot.length)
3694 float value = safeFloat(annElement.getValue());
3695 anot[anpos] = new jalview.datamodel.Annotation(
3696 annElement.getDisplayCharacter(),
3697 annElement.getDescription(),
3698 (annElement.getSecondaryStructure() == null
3699 || annElement.getSecondaryStructure()
3703 .getSecondaryStructure()
3706 anot[anpos].colour = new Color(safeInt(annElement.getColour()));
3707 if (firstColour == null)
3709 firstColour = anot[anpos].colour;
3713 jalview.datamodel.AlignmentAnnotation jaa = null;
3715 if (annotation.isGraph())
3717 float llim = 0, hlim = 0;
3718 // if (autoForView || an[i].isAutoCalculated()) {
3721 jaa = new jalview.datamodel.AlignmentAnnotation(
3722 annotation.getLabel(), annotation.getDescription(), anot,
3723 llim, hlim, safeInt(annotation.getGraphType()));
3725 jaa.graphGroup = safeInt(annotation.getGraphGroup());
3726 jaa._linecolour = firstColour;
3727 if (annotation.getThresholdLine() != null)
3729 jaa.setThreshold(new jalview.datamodel.GraphLine(
3730 safeFloat(annotation.getThresholdLine().getValue()),
3731 annotation.getThresholdLine().getLabel(),
3732 new java.awt.Color(safeInt(
3733 annotation.getThresholdLine().getColour()))));
3735 if (autoForView || annotation.isAutoCalculated())
3737 // Hardwire the symbol display line to ensure that labels for
3738 // histograms are displayed
3744 jaa = new jalview.datamodel.AlignmentAnnotation(
3745 annotation.getLabel(), annotation.getDescription(), anot);
3746 jaa._linecolour = firstColour;
3748 // register new annotation
3749 if (annotation.getId() != null)
3751 annotationIds.put(annotation.getId(), jaa);
3752 jaa.annotationId = annotation.getId();
3754 // recover sequence association
3755 String sequenceRef = annotation.getSequenceRef();
3756 if (sequenceRef != null)
3758 // from 2.9 sequenceRef is to sequence id (JAL-1781)
3759 SequenceI sequence = seqRefIds.get(sequenceRef);
3760 if (sequence == null)
3762 // in pre-2.9 projects sequence ref is to sequence name
3763 sequence = al.findName(sequenceRef);
3765 if (sequence != null)
3767 jaa.createSequenceMapping(sequence, 1, true);
3768 sequence.addAlignmentAnnotation(jaa);
3771 // and make a note of any group association
3772 if (annotation.getGroupRef() != null
3773 && annotation.getGroupRef().length() > 0)
3775 List<jalview.datamodel.AlignmentAnnotation> aal = groupAnnotRefs
3776 .get(annotation.getGroupRef());
3779 aal = new ArrayList<>();
3780 groupAnnotRefs.put(annotation.getGroupRef(), aal);
3785 if (annotation.getScore() != null)
3787 jaa.setScore(annotation.getScore().doubleValue());
3789 if (annotation.isVisible() != null)
3791 jaa.visible = annotation.isVisible().booleanValue();
3794 if (annotation.isCentreColLabels() != null)
3796 jaa.centreColLabels = annotation.isCentreColLabels()
3800 if (annotation.isScaleColLabels() != null)
3802 jaa.scaleColLabel = annotation.isScaleColLabels().booleanValue();
3804 if (annotation.isAutoCalculated())
3806 // newer files have an 'autoCalculated' flag and store calculation
3807 // state in viewport properties
3808 jaa.autoCalculated = true; // means annotation will be marked for
3809 // update at end of load.
3811 if (annotation.getGraphHeight() != null)
3813 jaa.graphHeight = annotation.getGraphHeight().intValue();
3815 jaa.belowAlignment = annotation.isBelowAlignment();
3816 jaa.setCalcId(annotation.getCalcId());
3817 if (annotation.getProperty().size() > 0)
3819 for (Annotation.Property prop : annotation
3822 jaa.setProperty(prop.getName(), prop.getValue());
3825 if (jaa.autoCalculated)
3827 autoAlan.add(new JvAnnotRow(i, jaa));
3830 // if (!autoForView)
3832 // add autocalculated group annotation and any user created annotation
3834 al.addAnnotation(jaa);
3838 // ///////////////////////
3840 // Create alignment markup and styles for this view
3841 if (jalviewModel.getJGroup().size() > 0)
3843 List<JGroup> groups = jalviewModel.getJGroup();
3844 boolean addAnnotSchemeGroup = false;
3845 for (int i = 0; i < groups.size(); i++)
3847 JGroup jGroup = groups.get(i);
3848 ColourSchemeI cs = null;
3849 if (jGroup.getColour() != null)
3851 if (jGroup.getColour().startsWith("ucs"))
3853 cs = getUserColourScheme(jalviewModel, jGroup.getColour());
3855 else if (jGroup.getColour().equals("AnnotationColourGradient")
3856 && jGroup.getAnnotationColours() != null)
3858 addAnnotSchemeGroup = true;
3862 cs = ColourSchemeProperty.getColourScheme(null, al,
3863 jGroup.getColour());
3866 int pidThreshold = safeInt(jGroup.getPidThreshold());
3868 Vector<SequenceI> seqs = new Vector<>();
3870 for (int s = 0; s < jGroup.getSeq().size(); s++)
3872 String seqId = jGroup.getSeq().get(s);
3873 SequenceI ts = seqRefIds.get(seqId);
3877 seqs.addElement(ts);
3881 if (seqs.size() < 1)
3886 SequenceGroup sg = new SequenceGroup(seqs, jGroup.getName(), cs,
3887 safeBoolean(jGroup.isDisplayBoxes()),
3888 safeBoolean(jGroup.isDisplayText()),
3889 safeBoolean(jGroup.isColourText()),
3890 safeInt(jGroup.getStart()), safeInt(jGroup.getEnd()));
3891 sg.getGroupColourScheme().setThreshold(pidThreshold, true);
3892 sg.getGroupColourScheme()
3893 .setConservationInc(safeInt(jGroup.getConsThreshold()));
3894 sg.setOutlineColour(new Color(safeInt(jGroup.getOutlineColour())));
3896 sg.textColour = new Color(safeInt(jGroup.getTextCol1()));
3897 sg.textColour2 = new Color(safeInt(jGroup.getTextCol2()));
3898 sg.setShowNonconserved(safeBoolean(jGroup.isShowUnconserved()));
3899 sg.thresholdTextColour = safeInt(jGroup.getTextColThreshold());
3900 // attributes with a default in the schema are never null
3901 sg.setShowConsensusHistogram(jGroup.isShowConsensusHistogram());
3902 sg.setshowSequenceLogo(jGroup.isShowSequenceLogo());
3903 sg.setNormaliseSequenceLogo(jGroup.isNormaliseSequenceLogo());
3904 sg.setIgnoreGapsConsensus(jGroup.isIgnoreGapsinConsensus());
3905 if (jGroup.getConsThreshold() != null
3906 && jGroup.getConsThreshold().intValue() != 0)
3908 Conservation c = new Conservation("All", sg.getSequences(null), 0,
3911 c.verdict(false, 25);
3912 sg.cs.setConservation(c);
3915 if (jGroup.getId() != null && groupAnnotRefs.size() > 0)
3917 // re-instate unique group/annotation row reference
3918 List<AlignmentAnnotation> jaal = groupAnnotRefs
3919 .get(jGroup.getId());
3922 for (AlignmentAnnotation jaa : jaal)
3925 if (jaa.autoCalculated)
3927 // match up and try to set group autocalc alignment row for this
3929 if (jaa.label.startsWith("Consensus for "))
3931 sg.setConsensus(jaa);
3933 // match up and try to set group autocalc alignment row for this
3935 if (jaa.label.startsWith("Conservation for "))
3937 sg.setConservationRow(jaa);
3944 if (addAnnotSchemeGroup)
3946 // reconstruct the annotation colourscheme
3947 sg.setColourScheme(constructAnnotationColour(
3948 jGroup.getAnnotationColours(), null, al, jalviewModel, false));
3954 // only dataset in this model, so just return.
3957 // ///////////////////////////////
3960 AlignFrame af = null;
3961 AlignViewport av = null;
3962 // now check to see if we really need to create a new viewport.
3963 if (multipleView && viewportsAdded.size() == 0)
3965 // We recovered an alignment for which a viewport already exists.
3966 // TODO: fix up any settings necessary for overlaying stored state onto
3967 // state recovered from another document. (may not be necessary).
3968 // we may need a binding from a viewport in memory to one recovered from
3970 // and then recover its containing af to allow the settings to be applied.
3971 // TODO: fix for vamsas demo
3973 "About to recover a viewport for existing alignment: Sequence set ID is "
3975 Object seqsetobj = retrieveExistingObj(uniqueSeqSetId);
3976 if (seqsetobj != null)
3978 if (seqsetobj instanceof String)
3980 uniqueSeqSetId = (String) seqsetobj;
3982 "Recovered extant sequence set ID mapping for ID : New Sequence set ID is "
3988 "Warning : Collision between sequence set ID string and existing jalview object mapping.");
3994 * indicate that annotation colours are applied across all groups (pre
3995 * Jalview 2.8.1 behaviour)
3997 boolean doGroupAnnColour = Jalview2XML.isVersionStringLaterThan("2.8.1",
3998 jalviewModel.getVersion());
4000 AlignmentPanel ap = null;
4001 boolean isnewview = true;
4004 // Check to see if this alignment already has a view id == viewId
4005 jalview.gui.AlignmentPanel views[] = Desktop
4006 .getAlignmentPanels(uniqueSeqSetId);
4007 if (views != null && views.length > 0)
4009 for (int v = 0; v < views.length; v++)
4011 if (views[v].av.getViewId().equalsIgnoreCase(viewId))
4013 // recover the existing alignpanel, alignframe, viewport
4014 af = views[v].alignFrame;
4017 // TODO: could even skip resetting view settings if we don't want to
4018 // change the local settings from other jalview processes
4027 af = loadViewport(file, jseqs, hiddenSeqs, al, jalviewModel, view,
4028 uniqueSeqSetId, viewId, autoAlan);
4029 av = af.getViewport();
4034 * Load any trees, PDB structures and viewers
4036 * Not done if flag is false (when this method is used for New View)
4038 if (loadTreesAndStructures)
4040 loadTrees(jalviewModel, view, af, av, ap);
4041 loadPCAViewers(jalviewModel, ap);
4042 loadPDBStructures(jprovider, jseqs, af, ap);
4043 loadRnaViewers(jprovider, jseqs, ap);
4045 // and finally return.
4050 * Instantiate and link any saved RNA (Varna) viewers. The state of the Varna
4051 * panel is restored from separate jar entries, two (gapped and trimmed) per
4052 * sequence and secondary structure.
4054 * Currently each viewer shows just one sequence and structure (gapped and
4055 * trimmed), however this method is designed to support multiple sequences or
4056 * structures in viewers if wanted in future.
4062 private void loadRnaViewers(jarInputStreamProvider jprovider,
4063 List<JSeq> jseqs, AlignmentPanel ap)
4066 * scan the sequences for references to viewers; create each one the first
4067 * time it is referenced, add Rna models to existing viewers
4069 for (JSeq jseq : jseqs)
4071 for (int i = 0; i < jseq.getRnaViewer().size(); i++)
4073 RnaViewer viewer = jseq.getRnaViewer().get(i);
4074 AppVarna appVarna = findOrCreateVarnaViewer(viewer, uniqueSetSuffix,
4077 for (int j = 0; j < viewer.getSecondaryStructure().size(); j++)
4079 SecondaryStructure ss = viewer.getSecondaryStructure().get(j);
4080 SequenceI seq = seqRefIds.get(jseq.getId());
4081 AlignmentAnnotation ann = this.annotationIds
4082 .get(ss.getAnnotationId());
4085 * add the structure to the Varna display (with session state copied
4086 * from the jar to a temporary file)
4088 boolean gapped = safeBoolean(ss.isGapped());
4089 String rnaTitle = ss.getTitle();
4090 String sessionState = ss.getViewerState();
4091 String tempStateFile = copyJarEntry(jprovider, sessionState,
4093 RnaModel rna = new RnaModel(rnaTitle, ann, seq, null, gapped);
4094 appVarna.addModelSession(rna, rnaTitle, tempStateFile);
4096 appVarna.setInitialSelection(safeInt(viewer.getSelectedRna()));
4102 * Locate and return an already instantiated matching AppVarna, or create one
4106 * @param viewIdSuffix
4110 protected AppVarna findOrCreateVarnaViewer(RnaViewer viewer,
4111 String viewIdSuffix, AlignmentPanel ap)
4114 * on each load a suffix is appended to the saved viewId, to avoid conflicts
4115 * if load is repeated
4117 String postLoadId = viewer.getViewId() + viewIdSuffix;
4118 for (JInternalFrame frame : getAllFrames())
4120 if (frame instanceof AppVarna)
4122 AppVarna varna = (AppVarna) frame;
4123 if (postLoadId.equals(varna.getViewId()))
4125 // this viewer is already instantiated
4126 // could in future here add ap as another 'parent' of the
4127 // AppVarna window; currently just 1-to-many
4134 * viewer not found - make it
4136 RnaViewerModel model = new RnaViewerModel(postLoadId, viewer.getTitle(),
4137 safeInt(viewer.getXpos()), safeInt(viewer.getYpos()),
4138 safeInt(viewer.getWidth()), safeInt(viewer.getHeight()),
4139 safeInt(viewer.getDividerLocation()));
4140 AppVarna varna = new AppVarna(model, ap);
4146 * Load any saved trees
4154 protected void loadTrees(JalviewModel jm, Viewport view,
4155 AlignFrame af, AlignViewport av, AlignmentPanel ap)
4157 // TODO result of automated refactoring - are all these parameters needed?
4160 for (int t = 0; t < jm.getTree().size(); t++)
4163 Tree tree = jm.getTree().get(t);
4165 TreePanel tp = (TreePanel) retrieveExistingObj(tree.getId());
4168 tp = af.showNewickTree(new NewickFile(tree.getNewick()),
4169 tree.getTitle(), safeInt(tree.getWidth()),
4170 safeInt(tree.getHeight()), safeInt(tree.getXpos()),
4171 safeInt(tree.getYpos()));
4172 if (tree.getId() != null)
4174 // perhaps bind the tree id to something ?
4179 // update local tree attributes ?
4180 // TODO: should check if tp has been manipulated by user - if so its
4181 // settings shouldn't be modified
4182 tp.setTitle(tree.getTitle());
4183 tp.setBounds(new Rectangle(safeInt(tree.getXpos()),
4184 safeInt(tree.getYpos()), safeInt(tree.getWidth()),
4185 safeInt(tree.getHeight())));
4186 tp.setViewport(av); // af.viewport;
4187 // TODO: verify 'associate with all views' works still
4188 tp.getTreeCanvas().setViewport(av); // af.viewport;
4189 tp.getTreeCanvas().setAssociatedPanel(ap); // af.alignPanel;
4190 // FIXME: should we use safeBoolean here ?
4191 tp.getTreeCanvas().setApplyToAllViews(tree.isLinkToAllViews());
4196 warn("There was a problem recovering stored Newick tree: \n"
4197 + tree.getNewick());
4201 tp.fitToWindow.setState(safeBoolean(tree.isFitToWindow()));
4202 tp.fitToWindow_actionPerformed(null);
4204 if (tree.getFontName() != null)
4207 new Font(tree.getFontName(), safeInt(tree.getFontStyle()),
4208 safeInt(tree.getFontSize())));
4213 new Font(view.getFontName(), safeInt(view.getFontStyle()),
4214 safeInt(view.getFontSize())));
4217 tp.showPlaceholders(safeBoolean(tree.isMarkUnlinked()));
4218 tp.showBootstrap(safeBoolean(tree.isShowBootstrap()));
4219 tp.showDistances(safeBoolean(tree.isShowDistances()));
4221 tp.getTreeCanvas().setThreshold(safeFloat(tree.getThreshold()));
4223 if (safeBoolean(tree.isCurrentTree()))
4225 af.getViewport().setCurrentTree(tp.getTree());
4229 } catch (Exception ex)
4231 ex.printStackTrace();
4236 * Load and link any saved structure viewers.
4243 protected void loadPDBStructures(jarInputStreamProvider jprovider,
4244 List<JSeq> jseqs, AlignFrame af, AlignmentPanel ap)
4247 * Run through all PDB ids on the alignment, and collect mappings between
4248 * distinct view ids and all sequences referring to that view.
4250 Map<String, StructureViewerModel> structureViewers = new LinkedHashMap<>();
4252 for (int i = 0; i < jseqs.size(); i++)
4254 JSeq jseq = jseqs.get(i);
4255 if (jseq.getPdbids().size() > 0)
4257 List<Pdbids> ids = jseq.getPdbids();
4258 for (int p = 0; p < ids.size(); p++)
4260 Pdbids pdbid = ids.get(p);
4261 final int structureStateCount = pdbid.getStructureState().size();
4262 for (int s = 0; s < structureStateCount; s++)
4264 // check to see if we haven't already created this structure view
4265 final StructureState structureState = pdbid
4266 .getStructureState().get(s);
4267 String sviewid = (structureState.getViewId() == null) ? null
4268 : structureState.getViewId() + uniqueSetSuffix;
4269 jalview.datamodel.PDBEntry jpdb = new jalview.datamodel.PDBEntry();
4270 // Originally : pdbid.getFile()
4271 // : TODO: verify external PDB file recovery still works in normal
4272 // jalview project load
4274 loadPDBFile(jprovider, pdbid.getId(), pdbid.getFile()));
4275 jpdb.setId(pdbid.getId());
4277 int x = safeInt(structureState.getXpos());
4278 int y = safeInt(structureState.getYpos());
4279 int width = safeInt(structureState.getWidth());
4280 int height = safeInt(structureState.getHeight());
4282 // Probably don't need to do this anymore...
4283 // Desktop.desktop.getComponentAt(x, y);
4284 // TODO: NOW: check that this recovers the PDB file correctly.
4285 String pdbFile = loadPDBFile(jprovider, pdbid.getId(),
4287 jalview.datamodel.SequenceI seq = seqRefIds
4288 .get(jseq.getId() + "");
4289 if (sviewid == null)
4291 sviewid = "_jalview_pre2_4_" + x + "," + y + "," + width + ","
4294 if (!structureViewers.containsKey(sviewid))
4296 structureViewers.put(sviewid,
4297 new StructureViewerModel(x, y, width, height, false,
4298 false, true, structureState.getViewId(),
4299 structureState.getType()));
4300 // Legacy pre-2.7 conversion JAL-823 :
4301 // do not assume any view has to be linked for colour by
4305 // assemble String[] { pdb files }, String[] { id for each
4306 // file }, orig_fileloc, SequenceI[][] {{ seqs_file 1 }, {
4307 // seqs_file 2}, boolean[] {
4308 // linkAlignPanel,superposeWithAlignpanel}} from hash
4309 StructureViewerModel jmoldat = structureViewers.get(sviewid);
4310 jmoldat.setAlignWithPanel(jmoldat.isAlignWithPanel()
4311 || structureState.isAlignwithAlignPanel());
4314 * Default colour by linked panel to false if not specified (e.g.
4315 * for pre-2.7 projects)
4317 boolean colourWithAlignPanel = jmoldat.isColourWithAlignPanel();
4318 colourWithAlignPanel |= structureState.isColourwithAlignPanel();
4319 jmoldat.setColourWithAlignPanel(colourWithAlignPanel);
4322 * Default colour by viewer to true if not specified (e.g. for
4325 boolean colourByViewer = jmoldat.isColourByViewer();
4326 colourByViewer &= structureState.isColourByJmol();
4327 jmoldat.setColourByViewer(colourByViewer);
4329 if (jmoldat.getStateData().length() < structureState
4330 .getValue()/*Content()*/.length())
4332 jmoldat.setStateData(structureState.getValue());// Content());
4334 if (pdbid.getFile() != null)
4336 File mapkey = new File(pdbid.getFile());
4337 StructureData seqstrmaps = jmoldat.getFileData().get(mapkey);
4338 if (seqstrmaps == null)
4340 jmoldat.getFileData().put(mapkey,
4341 seqstrmaps = jmoldat.new StructureData(pdbFile,
4344 if (!seqstrmaps.getSeqList().contains(seq))
4346 seqstrmaps.getSeqList().add(seq);
4352 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");
4359 // Instantiate the associated structure views
4360 for (Entry<String, StructureViewerModel> entry : structureViewers
4365 createOrLinkStructureViewer(entry, af, ap, jprovider);
4366 } catch (Exception e)
4369 "Error loading structure viewer: " + e.getMessage());
4370 // failed - try the next one
4382 protected void createOrLinkStructureViewer(
4383 Entry<String, StructureViewerModel> viewerData, AlignFrame af,
4384 AlignmentPanel ap, jarInputStreamProvider jprovider)
4386 final StructureViewerModel stateData = viewerData.getValue();
4389 * Search for any viewer windows already open from other alignment views
4390 * that exactly match the stored structure state
4392 StructureViewerBase comp = findMatchingViewer(viewerData);
4396 linkStructureViewer(ap, comp, stateData);
4401 * From 2.9: stateData.type contains JMOL or CHIMERA, data is in jar entry
4402 * "viewer_"+stateData.viewId
4404 if (ViewerType.CHIMERA.toString().equals(stateData.getType()))
4406 createChimeraViewer(viewerData, af, jprovider);
4411 * else Jmol (if pre-2.9, stateData contains JMOL state string)
4413 createJmolViewer(viewerData, af, jprovider);
4418 * Create a new Chimera viewer.
4424 protected void createChimeraViewer(
4425 Entry<String, StructureViewerModel> viewerData, AlignFrame af,
4426 jarInputStreamProvider jprovider)
4428 StructureViewerModel data = viewerData.getValue();
4429 String chimeraSessionFile = data.getStateData();
4432 * Copy Chimera session from jar entry "viewer_"+viewId to a temporary file
4434 * NB this is the 'saved' viewId as in the project file XML, _not_ the
4435 * 'uniquified' sviewid used to reconstruct the viewer here
4437 String viewerJarEntryName = getViewerJarEntryName(data.getViewId());
4438 chimeraSessionFile = copyJarEntry(jprovider, viewerJarEntryName,
4441 Set<Entry<File, StructureData>> fileData = data.getFileData()
4443 List<PDBEntry> pdbs = new ArrayList<>();
4444 List<SequenceI[]> allseqs = new ArrayList<>();
4445 for (Entry<File, StructureData> pdb : fileData)
4447 String filePath = pdb.getValue().getFilePath();
4448 String pdbId = pdb.getValue().getPdbId();
4449 // pdbs.add(new PDBEntry(filePath, pdbId));
4450 pdbs.add(new PDBEntry(pdbId, null, PDBEntry.Type.PDB, filePath));
4451 final List<SequenceI> seqList = pdb.getValue().getSeqList();
4452 SequenceI[] seqs = seqList.toArray(new SequenceI[seqList.size()]);
4456 boolean colourByChimera = data.isColourByViewer();
4457 boolean colourBySequence = data.isColourWithAlignPanel();
4459 // TODO use StructureViewer as a factory here, see JAL-1761
4460 final PDBEntry[] pdbArray = pdbs.toArray(new PDBEntry[pdbs.size()]);
4461 final SequenceI[][] seqsArray = allseqs
4462 .toArray(new SequenceI[allseqs.size()][]);
4463 String newViewId = viewerData.getKey();
4465 ChimeraViewFrame cvf = new ChimeraViewFrame(chimeraSessionFile,
4466 af.alignPanel, pdbArray, seqsArray, colourByChimera,
4467 colourBySequence, newViewId);
4468 cvf.setSize(data.getWidth(), data.getHeight());
4469 cvf.setLocation(data.getX(), data.getY());
4473 * Create a new Jmol window. First parse the Jmol state to translate filenames
4474 * loaded into the view, and record the order in which files are shown in the
4475 * Jmol view, so we can add the sequence mappings in same order.
4481 protected void createJmolViewer(
4482 final Entry<String, StructureViewerModel> viewerData,
4483 AlignFrame af, jarInputStreamProvider jprovider)
4485 final StructureViewerModel svattrib = viewerData.getValue();
4486 String state = svattrib.getStateData();
4489 * Pre-2.9: state element value is the Jmol state string
4491 * 2.9+: @type is "JMOL", state data is in a Jar file member named "viewer_"
4494 if (ViewerType.JMOL.toString().equals(svattrib.getType()))
4496 state = readJarEntry(jprovider,
4497 getViewerJarEntryName(svattrib.getViewId()));
4500 List<String> pdbfilenames = new ArrayList<>();
4501 List<SequenceI[]> seqmaps = new ArrayList<>();
4502 List<String> pdbids = new ArrayList<>();
4503 StringBuilder newFileLoc = new StringBuilder(64);
4504 int cp = 0, ncp, ecp;
4505 Map<File, StructureData> oldFiles = svattrib.getFileData();
4506 while ((ncp = state.indexOf("load ", cp)) > -1)
4510 // look for next filename in load statement
4511 newFileLoc.append(state.substring(cp,
4512 ncp = (state.indexOf("\"", ncp + 1) + 1)));
4513 String oldfilenam = state.substring(ncp,
4514 ecp = state.indexOf("\"", ncp));
4515 // recover the new mapping data for this old filename
4516 // have to normalize filename - since Jmol and jalview do
4518 // translation differently.
4519 StructureData filedat = oldFiles.get(new File(oldfilenam));
4520 if (filedat == null)
4522 String reformatedOldFilename = oldfilenam.replaceAll("/", "\\\\");
4523 filedat = oldFiles.get(new File(reformatedOldFilename));
4525 newFileLoc.append(Platform.escapeString(filedat.getFilePath()));
4526 pdbfilenames.add(filedat.getFilePath());
4527 pdbids.add(filedat.getPdbId());
4528 seqmaps.add(filedat.getSeqList().toArray(new SequenceI[0]));
4529 newFileLoc.append("\"");
4530 cp = ecp + 1; // advance beyond last \" and set cursor so we can
4531 // look for next file statement.
4532 } while ((ncp = state.indexOf("/*file*/", cp)) > -1);
4536 // just append rest of state
4537 newFileLoc.append(state.substring(cp));
4541 System.err.print("Ignoring incomplete Jmol state for PDB ids: ");
4542 newFileLoc = new StringBuilder(state);
4543 newFileLoc.append("; load append ");
4544 for (File id : oldFiles.keySet())
4546 // add this and any other pdb files that should be present in
4548 StructureData filedat = oldFiles.get(id);
4549 newFileLoc.append(filedat.getFilePath());
4550 pdbfilenames.add(filedat.getFilePath());
4551 pdbids.add(filedat.getPdbId());
4552 seqmaps.add(filedat.getSeqList().toArray(new SequenceI[0]));
4553 newFileLoc.append(" \"");
4554 newFileLoc.append(filedat.getFilePath());
4555 newFileLoc.append("\"");
4558 newFileLoc.append(";");
4561 if (newFileLoc.length() == 0)
4565 int histbug = newFileLoc.indexOf("history = ");
4569 * change "history = [true|false];" to "history = [1|0];"
4572 int diff = histbug == -1 ? -1 : newFileLoc.indexOf(";", histbug);
4573 String val = (diff == -1) ? null
4574 : newFileLoc.substring(histbug, diff);
4575 if (val != null && val.length() >= 4)
4577 if (val.contains("e")) // eh? what can it be?
4579 if (val.trim().equals("true"))
4587 newFileLoc.replace(histbug, diff, val);
4592 final String[] pdbf = pdbfilenames
4593 .toArray(new String[pdbfilenames.size()]);
4594 final String[] id = pdbids.toArray(new String[pdbids.size()]);
4595 final SequenceI[][] sq = seqmaps
4596 .toArray(new SequenceI[seqmaps.size()][]);
4597 final String fileloc = newFileLoc.toString();
4598 final String sviewid = viewerData.getKey();
4599 final AlignFrame alf = af;
4600 final Rectangle rect = new Rectangle(svattrib.getX(), svattrib.getY(),
4601 svattrib.getWidth(), svattrib.getHeight());
4604 javax.swing.SwingUtilities.invokeAndWait(new Runnable()
4609 JalviewStructureDisplayI sview = null;
4612 sview = new StructureViewer(
4613 alf.alignPanel.getStructureSelectionManager())
4614 .createView(StructureViewer.ViewerType.JMOL,
4615 pdbf, id, sq, alf.alignPanel, svattrib,
4616 fileloc, rect, sviewid);
4617 addNewStructureViewer(sview);
4618 } catch (OutOfMemoryError ex)
4620 new OOMWarning("restoring structure view for PDB id " + id,
4621 (OutOfMemoryError) ex.getCause());
4622 if (sview != null && sview.isVisible())
4624 sview.closeViewer(false);
4625 sview.setVisible(false);
4631 } catch (InvocationTargetException ex)
4633 warn("Unexpected error when opening Jmol view.", ex);
4635 } catch (InterruptedException e)
4637 // e.printStackTrace();
4643 * Generates a name for the entry in the project jar file to hold state
4644 * information for a structure viewer
4649 protected String getViewerJarEntryName(String viewId)
4651 return VIEWER_PREFIX + viewId;
4655 * Returns any open frame that matches given structure viewer data. The match
4656 * is based on the unique viewId, or (for older project versions) the frame's
4662 protected StructureViewerBase findMatchingViewer(
4663 Entry<String, StructureViewerModel> viewerData)
4665 final String sviewid = viewerData.getKey();
4666 final StructureViewerModel svattrib = viewerData.getValue();
4667 StructureViewerBase comp = null;
4668 JInternalFrame[] frames = getAllFrames();
4669 for (JInternalFrame frame : frames)
4671 if (frame instanceof StructureViewerBase)
4674 * Post jalview 2.4 schema includes structure view id
4676 if (sviewid != null && ((StructureViewerBase) frame).getViewId()
4679 comp = (StructureViewerBase) frame;
4680 break; // break added in 2.9
4683 * Otherwise test for matching position and size of viewer frame
4685 else if (frame.getX() == svattrib.getX()
4686 && frame.getY() == svattrib.getY()
4687 && frame.getHeight() == svattrib.getHeight()
4688 && frame.getWidth() == svattrib.getWidth())
4690 comp = (StructureViewerBase) frame;
4691 // no break in faint hope of an exact match on viewId
4699 * Link an AlignmentPanel to an existing structure viewer.
4704 * @param useinViewerSuperpos
4705 * @param usetoColourbyseq
4706 * @param viewerColouring
4708 protected void linkStructureViewer(AlignmentPanel ap,
4709 StructureViewerBase viewer, StructureViewerModel stateData)
4711 // NOTE: if the jalview project is part of a shared session then
4712 // view synchronization should/could be done here.
4714 final boolean useinViewerSuperpos = stateData.isAlignWithPanel();
4715 final boolean usetoColourbyseq = stateData.isColourWithAlignPanel();
4716 final boolean viewerColouring = stateData.isColourByViewer();
4717 Map<File, StructureData> oldFiles = stateData.getFileData();
4720 * Add mapping for sequences in this view to an already open viewer
4722 final AAStructureBindingModel binding = viewer.getBinding();
4723 for (File id : oldFiles.keySet())
4725 // add this and any other pdb files that should be present in the
4727 StructureData filedat = oldFiles.get(id);
4728 String pdbFile = filedat.getFilePath();
4729 SequenceI[] seq = filedat.getSeqList().toArray(new SequenceI[0]);
4730 binding.getSsm().setMapping(seq, null, pdbFile, DataSourceType.FILE,
4732 binding.addSequenceForStructFile(pdbFile, seq);
4734 // and add the AlignmentPanel's reference to the view panel
4735 viewer.addAlignmentPanel(ap);
4736 if (useinViewerSuperpos)
4738 viewer.useAlignmentPanelForSuperposition(ap);
4742 viewer.excludeAlignmentPanelForSuperposition(ap);
4744 if (usetoColourbyseq)
4746 viewer.useAlignmentPanelForColourbyseq(ap, !viewerColouring);
4750 viewer.excludeAlignmentPanelForColourbyseq(ap);
4755 * Get all frames within the Desktop.
4759 protected JInternalFrame[] getAllFrames()
4761 JInternalFrame[] frames = null;
4762 // TODO is this necessary - is it safe - risk of hanging?
4767 frames = Desktop.desktop.getAllFrames();
4768 } catch (ArrayIndexOutOfBoundsException e)
4770 // occasional No such child exceptions are thrown here...
4774 } catch (InterruptedException f)
4778 } while (frames == null);
4783 * Answers true if 'version' is equal to or later than 'supported', where each
4784 * is formatted as major/minor versions like "2.8.3" or "2.3.4b1" for bugfix
4785 * changes. Development and test values for 'version' are leniently treated
4789 * - minimum version we are comparing against
4791 * - version of data being processsed
4794 public static boolean isVersionStringLaterThan(String supported,
4797 if (supported == null || version == null
4798 || version.equalsIgnoreCase("DEVELOPMENT BUILD")
4799 || version.equalsIgnoreCase("Test")
4800 || version.equalsIgnoreCase("AUTOMATED BUILD"))
4802 System.err.println("Assuming project file with "
4803 + (version == null ? "null" : version)
4804 + " is compatible with Jalview version " + supported);
4809 return StringUtils.compareVersions(version, supported, "b") >= 0;
4813 Vector<JalviewStructureDisplayI> newStructureViewers = null;
4815 protected void addNewStructureViewer(JalviewStructureDisplayI sview)
4817 if (newStructureViewers != null)
4819 sview.getBinding().setFinishedLoadingFromArchive(false);
4820 newStructureViewers.add(sview);
4824 protected void setLoadingFinishedForNewStructureViewers()
4826 if (newStructureViewers != null)
4828 for (JalviewStructureDisplayI sview : newStructureViewers)
4830 sview.getBinding().setFinishedLoadingFromArchive(true);
4832 newStructureViewers.clear();
4833 newStructureViewers = null;
4837 AlignFrame loadViewport(String file, List<JSeq> JSEQ,
4838 List<SequenceI> hiddenSeqs, AlignmentI al,
4839 JalviewModel jm, Viewport view, String uniqueSeqSetId,
4840 String viewId, List<JvAnnotRow> autoAlan)
4842 AlignFrame af = null;
4843 af = new AlignFrame(al, safeInt(view.getWidth()),
4844 safeInt(view.getHeight()), uniqueSeqSetId, viewId);
4846 af.setFileName(file, FileFormat.Jalview);
4848 final AlignViewport viewport = af.getViewport();
4849 for (int i = 0; i < JSEQ.size(); i++)
4851 int colour = safeInt(JSEQ.get(i).getColour());
4852 viewport.setSequenceColour(viewport.getAlignment().getSequenceAt(i),
4858 viewport.setColourByReferenceSeq(true);
4859 viewport.setDisplayReferenceSeq(true);
4862 viewport.setGatherViewsHere(safeBoolean(view.isGatheredViews()));
4864 if (view.getSequenceSetId() != null)
4866 AlignmentViewport av = viewportsAdded.get(uniqueSeqSetId);
4868 viewport.setSequenceSetId(uniqueSeqSetId);
4871 // propagate shared settings to this new view
4872 viewport.setHistoryList(av.getHistoryList());
4873 viewport.setRedoList(av.getRedoList());
4877 viewportsAdded.put(uniqueSeqSetId, viewport);
4879 // TODO: check if this method can be called repeatedly without
4880 // side-effects if alignpanel already registered.
4881 PaintRefresher.Register(af.alignPanel, uniqueSeqSetId);
4883 // apply Hidden regions to view.
4884 if (hiddenSeqs != null)
4886 for (int s = 0; s < JSEQ.size(); s++)
4888 SequenceGroup hidden = new SequenceGroup();
4889 boolean isRepresentative = false;
4890 for (int r = 0; r < JSEQ.get(s).getHiddenSequences().size(); r++)
4892 isRepresentative = true;
4893 SequenceI sequenceToHide = al
4894 .getSequenceAt(JSEQ.get(s).getHiddenSequences().get(r));
4895 hidden.addSequence(sequenceToHide, false);
4896 // remove from hiddenSeqs list so we don't try to hide it twice
4897 hiddenSeqs.remove(sequenceToHide);
4899 if (isRepresentative)
4901 SequenceI representativeSequence = al.getSequenceAt(s);
4902 hidden.addSequence(representativeSequence, false);
4903 viewport.hideRepSequences(representativeSequence, hidden);
4907 SequenceI[] hseqs = hiddenSeqs
4908 .toArray(new SequenceI[hiddenSeqs.size()]);
4909 viewport.hideSequence(hseqs);
4912 // recover view properties and display parameters
4914 viewport.setShowAnnotation(safeBoolean(view.isShowAnnotation()));
4915 viewport.setAbovePIDThreshold(safeBoolean(view.isPidSelected()));
4916 final int pidThreshold = safeInt(view.getPidThreshold());
4917 viewport.setThreshold(pidThreshold);
4919 viewport.setColourText(safeBoolean(view.isShowColourText()));
4922 .setConservationSelected(
4923 safeBoolean(view.isConservationSelected()));
4924 viewport.setIncrement(safeInt(view.getConsThreshold()));
4925 viewport.setShowJVSuffix(safeBoolean(view.isShowFullId()));
4926 viewport.setRightAlignIds(safeBoolean(view.isRightAlignIds()));
4927 viewport.setFont(new Font(view.getFontName(),
4928 safeInt(view.getFontStyle()), safeInt(view.getFontSize())),
4930 ViewStyleI vs = viewport.getViewStyle();
4931 vs.setScaleProteinAsCdna(view.isScaleProteinAsCdna());
4932 viewport.setViewStyle(vs);
4933 // TODO: allow custom charWidth/Heights to be restored by updating them
4934 // after setting font - which means set above to false
4935 viewport.setRenderGaps(safeBoolean(view.isRenderGaps()));
4936 viewport.setWrapAlignment(safeBoolean(view.isWrapAlignment()));
4937 viewport.setShowAnnotation(safeBoolean(view.isShowAnnotation()));
4939 viewport.setShowBoxes(safeBoolean(view.isShowBoxes()));
4941 viewport.setShowText(safeBoolean(view.isShowText()));
4943 viewport.setTextColour(new Color(safeInt(view.getTextCol1())));
4944 viewport.setTextColour2(new Color(safeInt(view.getTextCol2())));
4945 viewport.setThresholdTextColour(safeInt(view.getTextColThreshold()));
4946 viewport.setShowUnconserved(view.isShowUnconserved());
4947 viewport.getRanges().setStartRes(safeInt(view.getStartRes()));
4949 if (view.getViewName() != null)
4951 viewport.setViewName(view.getViewName());
4952 af.setInitialTabVisible();
4954 af.setBounds(safeInt(view.getXpos()), safeInt(view.getYpos()),
4955 safeInt(view.getWidth()), safeInt(view.getHeight()));
4956 // startSeq set in af.alignPanel.updateLayout below
4957 af.alignPanel.updateLayout();
4958 ColourSchemeI cs = null;
4959 // apply colourschemes
4960 if (view.getBgColour() != null)
4962 if (view.getBgColour().startsWith("ucs"))
4964 cs = getUserColourScheme(jm, view.getBgColour());
4966 else if (view.getBgColour().startsWith("Annotation"))
4968 AnnotationColourScheme viewAnnColour = view.getAnnotationColours();
4969 cs = constructAnnotationColour(viewAnnColour, af, al, jm, true);
4976 cs = ColourSchemeProperty.getColourScheme(af.getViewport(), al,
4977 view.getBgColour());
4982 * turn off 'alignment colour applies to all groups'
4983 * while restoring global colour scheme
4985 viewport.setColourAppliesToAllGroups(false);
4986 viewport.setGlobalColourScheme(cs);
4987 viewport.getResidueShading().setThreshold(pidThreshold,
4988 view.isIgnoreGapsinConsensus());
4989 viewport.getResidueShading()
4990 .setConsensus(viewport.getSequenceConsensusHash());
4991 if (safeBoolean(view.isConservationSelected()) && cs != null)
4993 viewport.getResidueShading()
4994 .setConservationInc(safeInt(view.getConsThreshold()));
4996 af.changeColour(cs);
4997 viewport.setColourAppliesToAllGroups(true);
5000 .setShowSequenceFeatures(
5001 safeBoolean(view.isShowSequenceFeatures()));
5003 viewport.setCentreColumnLabels(view.isCentreColumnLabels());
5004 viewport.setIgnoreGapsConsensus(view.isIgnoreGapsinConsensus(), null);
5005 viewport.setFollowHighlight(view.isFollowHighlight());
5006 viewport.followSelection = view.isFollowSelection();
5007 viewport.setShowConsensusHistogram(view.isShowConsensusHistogram());
5008 viewport.setShowSequenceLogo(view.isShowSequenceLogo());
5009 viewport.setNormaliseSequenceLogo(view.isNormaliseSequenceLogo());
5010 viewport.setShowDBRefs(safeBoolean(view.isShowDbRefTooltip()));
5011 viewport.setShowNPFeats(safeBoolean(view.isShowNPfeatureTooltip()));
5012 viewport.setShowGroupConsensus(view.isShowGroupConsensus());
5013 viewport.setShowGroupConservation(view.isShowGroupConservation());
5015 // recover feature settings
5016 if (jm.getFeatureSettings() != null)
5018 FeatureRenderer fr = af.alignPanel.getSeqPanel().seqCanvas
5019 .getFeatureRenderer();
5020 FeaturesDisplayed fdi;
5021 viewport.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
5022 String[] renderOrder = new String[jm.getFeatureSettings()
5023 .getSetting().size()];
5024 Map<String, FeatureColourI> featureColours = new Hashtable<>();
5025 Map<String, Float> featureOrder = new Hashtable<>();
5027 for (int fs = 0; fs < jm.getFeatureSettings()
5028 .getSetting().size(); fs++)
5030 Setting setting = jm.getFeatureSettings().getSetting().get(fs);
5031 String featureType = setting.getType();
5034 * restore feature filters (if any)
5036 jalview.xml.binding.jalview.FeatureMatcherSet filters = setting
5038 if (filters != null)
5040 FeatureMatcherSetI filter = Jalview2XML
5041 .parseFilter(featureType, filters);
5042 if (!filter.isEmpty())
5044 fr.setFeatureFilter(featureType, filter);
5049 * restore feature colour scheme
5051 Color maxColour = new Color(setting.getColour());
5052 if (setting.getMincolour() != null)
5055 * minColour is always set unless a simple colour
5056 * (including for colour by label though it doesn't use it)
5058 Color minColour = new Color(setting.getMincolour().intValue());
5059 Color noValueColour = minColour;
5060 NoValueColour noColour = setting.getNoValueColour();
5061 if (noColour == NoValueColour.NONE)
5063 noValueColour = null;
5065 else if (noColour == NoValueColour.MAX)
5067 noValueColour = maxColour;
5069 float min = safeFloat(safeFloat(setting.getMin()));
5070 float max = setting.getMax() == null ? 1f
5071 : setting.getMax().floatValue();
5072 FeatureColourI gc = new FeatureColour(maxColour, minColour,
5074 noValueColour, min, max);
5075 if (setting.getAttributeName().size() > 0)
5077 gc.setAttributeName(setting.getAttributeName().toArray(
5078 new String[setting.getAttributeName().size()]));
5080 if (setting.getThreshold() != null)
5082 gc.setThreshold(setting.getThreshold().floatValue());
5083 int threshstate = safeInt(setting.getThreshstate());
5084 // -1 = None, 0 = Below, 1 = Above threshold
5085 if (threshstate == 0)
5087 gc.setBelowThreshold(true);
5089 else if (threshstate == 1)
5091 gc.setAboveThreshold(true);
5094 gc.setAutoScaled(true); // default
5095 if (setting.isAutoScale() != null)
5097 gc.setAutoScaled(setting.isAutoScale());
5099 if (setting.isColourByLabel() != null)
5101 gc.setColourByLabel(setting.isColourByLabel());
5103 // and put in the feature colour table.
5104 featureColours.put(featureType, gc);
5108 featureColours.put(featureType,
5109 new FeatureColour(maxColour));
5111 renderOrder[fs] = featureType;
5112 if (setting.getOrder() != null)
5114 featureOrder.put(featureType, setting.getOrder().floatValue());
5118 featureOrder.put(featureType, new Float(
5119 fs / jm.getFeatureSettings().getSetting().size()));
5121 if (safeBoolean(setting.isDisplay()))
5123 fdi.setVisible(featureType);
5126 Map<String, Boolean> fgtable = new Hashtable<>();
5127 for (int gs = 0; gs < jm.getFeatureSettings().getGroup().size(); gs++)
5129 Group grp = jm.getFeatureSettings().getGroup().get(gs);
5130 fgtable.put(grp.getName(), new Boolean(grp.isDisplay()));
5132 // FeatureRendererSettings frs = new FeatureRendererSettings(renderOrder,
5133 // fgtable, featureColours, jms.getFeatureSettings().hasTransparency() ?
5134 // jms.getFeatureSettings().getTransparency() : 0.0, featureOrder);
5135 FeatureRendererSettings frs = new FeatureRendererSettings(renderOrder,
5136 fgtable, featureColours, 1.0f, featureOrder);
5137 fr.transferSettings(frs);
5140 if (view.getHiddenColumns().size() > 0)
5142 for (int c = 0; c < view.getHiddenColumns().size(); c++)
5144 final HiddenColumns hc = view.getHiddenColumns().get(c);
5145 viewport.hideColumns(safeInt(hc.getStart()),
5146 safeInt(hc.getEnd()) /* +1 */);
5149 if (view.getCalcIdParam() != null)
5151 for (CalcIdParam calcIdParam : view.getCalcIdParam())
5153 if (calcIdParam != null)
5155 if (recoverCalcIdParam(calcIdParam, viewport))
5160 warn("Couldn't recover parameters for "
5161 + calcIdParam.getCalcId());
5166 af.setMenusFromViewport(viewport);
5167 af.setTitle(view.getTitle());
5168 // TODO: we don't need to do this if the viewport is aready visible.
5170 * Add the AlignFrame to the desktop (it may be 'gathered' later), unless it
5171 * has a 'cdna/protein complement' view, in which case save it in order to
5172 * populate a SplitFrame once all views have been read in.
5174 String complementaryViewId = view.getComplementId();
5175 if (complementaryViewId == null)
5177 Desktop.addInternalFrame(af, view.getTitle(),
5178 safeInt(view.getWidth()), safeInt(view.getHeight()));
5179 // recompute any autoannotation
5180 af.alignPanel.updateAnnotation(false, true);
5181 reorderAutoannotation(af, al, autoAlan);
5182 af.alignPanel.alignmentChanged();
5186 splitFrameCandidates.put(view, af);
5192 * Reads saved data to restore Colour by Annotation settings
5194 * @param viewAnnColour
5198 * @param checkGroupAnnColour
5201 private ColourSchemeI constructAnnotationColour(
5202 AnnotationColourScheme viewAnnColour, AlignFrame af,
5203 AlignmentI al, JalviewModel model, boolean checkGroupAnnColour)
5205 boolean propagateAnnColour = false;
5206 AlignmentI annAlignment = af != null ? af.getViewport().getAlignment()
5208 if (checkGroupAnnColour && al.getGroups() != null
5209 && al.getGroups().size() > 0)
5211 // pre 2.8.1 behaviour
5212 // check to see if we should transfer annotation colours
5213 propagateAnnColour = true;
5214 for (SequenceGroup sg : al.getGroups())
5216 if (sg.getColourScheme() instanceof AnnotationColourGradient)
5218 propagateAnnColour = false;
5224 * 2.10.2- : saved annotationId is AlignmentAnnotation.annotationId
5226 String annotationId = viewAnnColour.getAnnotation();
5227 AlignmentAnnotation matchedAnnotation = annotationIds.get(annotationId);
5230 * pre 2.10.2: saved annotationId is AlignmentAnnotation.label
5232 if (matchedAnnotation == null
5233 && annAlignment.getAlignmentAnnotation() != null)
5235 for (int i = 0; i < annAlignment.getAlignmentAnnotation().length; i++)
5238 .equals(annAlignment.getAlignmentAnnotation()[i].label))
5240 matchedAnnotation = annAlignment.getAlignmentAnnotation()[i];
5245 if (matchedAnnotation == null)
5247 System.err.println("Failed to match annotation colour scheme for "
5251 if (matchedAnnotation.getThreshold() == null)
5253 matchedAnnotation.setThreshold(
5254 new GraphLine(safeFloat(viewAnnColour.getThreshold()),
5255 "Threshold", Color.black));
5258 AnnotationColourGradient cs = null;
5259 if (viewAnnColour.getColourScheme().equals("None"))
5261 cs = new AnnotationColourGradient(matchedAnnotation,
5262 new Color(safeInt(viewAnnColour.getMinColour())),
5263 new Color(safeInt(viewAnnColour.getMaxColour())),
5264 safeInt(viewAnnColour.getAboveThreshold()));
5266 else if (viewAnnColour.getColourScheme().startsWith("ucs"))
5268 cs = new AnnotationColourGradient(matchedAnnotation,
5269 getUserColourScheme(model, viewAnnColour.getColourScheme()),
5270 safeInt(viewAnnColour.getAboveThreshold()));
5274 cs = new AnnotationColourGradient(matchedAnnotation,
5275 ColourSchemeProperty.getColourScheme(af.getViewport(), al,
5276 viewAnnColour.getColourScheme()),
5277 safeInt(viewAnnColour.getAboveThreshold()));
5280 boolean perSequenceOnly = safeBoolean(viewAnnColour.isPerSequence());
5281 boolean useOriginalColours = safeBoolean(
5282 viewAnnColour.isPredefinedColours());
5283 cs.setSeqAssociated(perSequenceOnly);
5284 cs.setPredefinedColours(useOriginalColours);
5286 if (propagateAnnColour && al.getGroups() != null)
5288 // Also use these settings for all the groups
5289 for (int g = 0; g < al.getGroups().size(); g++)
5291 SequenceGroup sg = al.getGroups().get(g);
5292 if (sg.getGroupColourScheme() == null)
5297 AnnotationColourGradient groupScheme = new AnnotationColourGradient(
5298 matchedAnnotation, sg.getColourScheme(),
5299 safeInt(viewAnnColour.getAboveThreshold()));
5300 sg.setColourScheme(groupScheme);
5301 groupScheme.setSeqAssociated(perSequenceOnly);
5302 groupScheme.setPredefinedColours(useOriginalColours);
5308 private void reorderAutoannotation(AlignFrame af, AlignmentI al,
5309 List<JvAnnotRow> autoAlan)
5311 // copy over visualization settings for autocalculated annotation in the
5313 if (al.getAlignmentAnnotation() != null)
5316 * Kludge for magic autoannotation names (see JAL-811)
5318 String[] magicNames = new String[] { "Consensus", "Quality",
5320 JvAnnotRow nullAnnot = new JvAnnotRow(-1, null);
5321 Hashtable<String, JvAnnotRow> visan = new Hashtable<>();
5322 for (String nm : magicNames)
5324 visan.put(nm, nullAnnot);
5326 for (JvAnnotRow auan : autoAlan)
5328 visan.put(auan.template.label
5329 + (auan.template.getCalcId() == null ? ""
5330 : "\t" + auan.template.getCalcId()),
5333 int hSize = al.getAlignmentAnnotation().length;
5334 List<JvAnnotRow> reorder = new ArrayList<>();
5335 // work through any autoCalculated annotation already on the view
5336 // removing it if it should be placed in a different location on the
5337 // annotation panel.
5338 List<String> remains = new ArrayList<>(visan.keySet());
5339 for (int h = 0; h < hSize; h++)
5341 jalview.datamodel.AlignmentAnnotation jalan = al
5342 .getAlignmentAnnotation()[h];
5343 if (jalan.autoCalculated)
5346 JvAnnotRow valan = visan.get(k = jalan.label);
5347 if (jalan.getCalcId() != null)
5349 valan = visan.get(k = jalan.label + "\t" + jalan.getCalcId());
5354 // delete the auto calculated row from the alignment
5355 al.deleteAnnotation(jalan, false);
5359 if (valan != nullAnnot)
5361 if (jalan != valan.template)
5363 // newly created autoannotation row instance
5364 // so keep a reference to the visible annotation row
5365 // and copy over all relevant attributes
5366 if (valan.template.graphHeight >= 0)
5369 jalan.graphHeight = valan.template.graphHeight;
5371 jalan.visible = valan.template.visible;
5373 reorder.add(new JvAnnotRow(valan.order, jalan));
5378 // Add any (possibly stale) autocalculated rows that were not appended to
5379 // the view during construction
5380 for (String other : remains)
5382 JvAnnotRow othera = visan.get(other);
5383 if (othera != nullAnnot && othera.template.getCalcId() != null
5384 && othera.template.getCalcId().length() > 0)
5386 reorder.add(othera);
5389 // now put the automatic annotation in its correct place
5390 int s = 0, srt[] = new int[reorder.size()];
5391 JvAnnotRow[] rws = new JvAnnotRow[reorder.size()];
5392 for (JvAnnotRow jvar : reorder)
5395 srt[s++] = jvar.order;
5398 jalview.util.QuickSort.sort(srt, rws);
5399 // and re-insert the annotation at its correct position
5400 for (JvAnnotRow jvar : rws)
5402 al.addAnnotation(jvar.template, jvar.order);
5404 af.alignPanel.adjustAnnotationHeight();
5408 Hashtable skipList = null;
5411 * TODO remove this method
5414 * @return AlignFrame bound to sequenceSetId from view, if one exists. private
5415 * AlignFrame getSkippedFrame(Viewport view) { if (skipList==null) {
5416 * throw new Error("Implementation Error. No skipList defined for this
5417 * Jalview2XML instance."); } return (AlignFrame)
5418 * skipList.get(view.getSequenceSetId()); }
5422 * Check if the Jalview view contained in object should be skipped or not.
5425 * @return true if view's sequenceSetId is a key in skipList
5427 private boolean skipViewport(JalviewModel object)
5429 if (skipList == null)
5433 String id = object.getViewport().get(0).getSequenceSetId();
5434 if (skipList.containsKey(id))
5436 if (Cache.log != null && Cache.log.isDebugEnabled())
5438 Cache.log.debug("Skipping seuqence set id " + id);
5445 public void addToSkipList(AlignFrame af)
5447 if (skipList == null)
5449 skipList = new Hashtable();
5451 skipList.put(af.getViewport().getSequenceSetId(), af);
5454 public void clearSkipList()
5456 if (skipList != null)
5463 private void recoverDatasetFor(SequenceSet vamsasSet, AlignmentI al,
5464 boolean ignoreUnrefed, String uniqueSeqSetId)
5466 jalview.datamodel.AlignmentI ds = getDatasetFor(
5467 vamsasSet.getDatasetId());
5468 AlignmentI xtant_ds = ds;
5469 if (xtant_ds == null)
5471 // good chance we are about to create a new dataset, but check if we've
5472 // seen some of the dataset sequence IDs before.
5473 // TODO: skip this check if we are working with project generated by
5474 // version 2.11 or later
5475 xtant_ds = checkIfHasDataset(vamsasSet.getSequence());
5476 if (xtant_ds != null)
5479 addDatasetRef(vamsasSet.getDatasetId(), ds);
5482 Vector dseqs = null;
5485 // recovering an alignment View
5486 AlignmentI seqSetDS = getDatasetFor(UNIQSEQSETID + uniqueSeqSetId);
5487 if (seqSetDS != null)
5489 if (ds != null && ds != seqSetDS)
5491 warn("JAL-3171 regression: Overwriting a dataset reference for an alignment"
5492 + " - CDS/Protein crossreference data may be lost");
5493 if (xtant_ds != null)
5495 // This can only happen if the unique sequence set ID was bound to a
5496 // dataset that did not contain any of the sequences in the view
5497 // currently being restored.
5498 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.");
5502 addDatasetRef(vamsasSet.getDatasetId(), ds);
5507 // try even harder to restore dataset
5508 AlignmentI xtantDS = checkIfHasDataset(vamsasSet.getSequence());
5509 // create a list of new dataset sequences
5510 dseqs = new Vector();
5512 for (int i = 0, iSize = vamsasSet.getSequence().size(); i < iSize; i++)
5514 Sequence vamsasSeq = vamsasSet.getSequence().get(i);
5515 ensureJalviewDatasetSequence(vamsasSeq, ds, dseqs, ignoreUnrefed, i);
5517 // create a new dataset
5520 SequenceI[] dsseqs = new SequenceI[dseqs.size()];
5521 dseqs.copyInto(dsseqs);
5522 ds = new jalview.datamodel.Alignment(dsseqs);
5523 debug("Created new dataset " + vamsasSet.getDatasetId()
5524 + " for alignment " + System.identityHashCode(al));
5525 addDatasetRef(vamsasSet.getDatasetId(), ds);
5527 // set the dataset for the newly imported alignment.
5528 if (al.getDataset() == null && !ignoreUnrefed)
5531 // register dataset for the alignment's uniqueSeqSetId for legacy projects
5532 addDatasetRef(UNIQSEQSETID + uniqueSeqSetId, ds);
5534 updateSeqDatasetBinding(vamsasSet.getSequence(), ds);
5538 * XML dataset sequence ID to materialised dataset reference
5540 HashMap<String, AlignmentI> seqToDataset = new HashMap<>();
5543 * @return the first materialised dataset reference containing a dataset
5544 * sequence referenced in the given view
5546 * - sequences from the view
5548 AlignmentI checkIfHasDataset(List<Sequence> list)
5550 for (Sequence restoredSeq : list)
5552 AlignmentI datasetFor = seqToDataset.get(restoredSeq.getDsseqid());
5553 if (datasetFor != null)
5562 * Register ds as the containing dataset for the dataset sequences referenced
5563 * by sequences in list
5566 * - sequences in a view
5569 void updateSeqDatasetBinding(List<Sequence> list, AlignmentI ds)
5571 for (Sequence restoredSeq : list)
5573 AlignmentI prevDS = seqToDataset.put(restoredSeq.getDsseqid(), ds);
5574 if (prevDS != null && prevDS != ds)
5576 warn("Dataset sequence appears in many datasets: "
5577 + restoredSeq.getDsseqid());
5578 // TODO: try to merge!
5585 * sequence definition to create/merge dataset sequence for
5589 * vector to add new dataset sequence to
5590 * @param ignoreUnrefed
5591 * - when true, don't create new sequences from vamsasSeq if it's id
5592 * doesn't already have an asssociated Jalview sequence.
5594 * - used to reorder the sequence in the alignment according to the
5595 * vamsasSeq array ordering, to preserve ordering of dataset
5597 private void ensureJalviewDatasetSequence(Sequence vamsasSeq,
5598 AlignmentI ds, Vector dseqs, boolean ignoreUnrefed, int vseqpos)
5600 // JBP TODO: Check this is called for AlCodonFrames to support recovery of
5602 SequenceI sq = seqRefIds.get(vamsasSeq.getId());
5603 boolean reorder = false;
5604 SequenceI dsq = null;
5605 if (sq != null && sq.getDatasetSequence() != null)
5607 dsq = sq.getDatasetSequence();
5613 if (sq == null && ignoreUnrefed)
5617 String sqid = vamsasSeq.getDsseqid();
5620 // need to create or add a new dataset sequence reference to this sequence
5623 dsq = seqRefIds.get(sqid);
5628 // make a new dataset sequence
5629 dsq = sq.createDatasetSequence();
5632 // make up a new dataset reference for this sequence
5633 sqid = seqHash(dsq);
5635 dsq.setVamsasId(uniqueSetSuffix + sqid);
5636 seqRefIds.put(sqid, dsq);
5641 dseqs.addElement(dsq);
5646 ds.addSequence(dsq);
5652 { // make this dataset sequence sq's dataset sequence
5653 sq.setDatasetSequence(dsq);
5654 // and update the current dataset alignment
5659 if (!dseqs.contains(dsq))
5666 if (ds.findIndex(dsq) < 0)
5668 ds.addSequence(dsq);
5675 // TODO: refactor this as a merge dataset sequence function
5676 // now check that sq (the dataset sequence) sequence really is the union of
5677 // all references to it
5678 // boolean pre = sq.getStart() < dsq.getStart();
5679 // boolean post = sq.getEnd() > dsq.getEnd();
5683 // StringBuffer sb = new StringBuffer();
5684 String newres = jalview.analysis.AlignSeq.extractGaps(
5685 jalview.util.Comparison.GapChars, sq.getSequenceAsString());
5686 if (!newres.equalsIgnoreCase(dsq.getSequenceAsString())
5687 && newres.length() > dsq.getLength())
5689 // Update with the longer sequence.
5693 * if (pre) { sb.insert(0, newres .substring(0, dsq.getStart() -
5694 * sq.getStart())); dsq.setStart(sq.getStart()); } if (post) {
5695 * sb.append(newres.substring(newres.length() - sq.getEnd() -
5696 * dsq.getEnd())); dsq.setEnd(sq.getEnd()); }
5698 dsq.setSequence(newres);
5700 // TODO: merges will never happen if we 'know' we have the real dataset
5701 // sequence - this should be detected when id==dssid
5703 "DEBUG Notice: Merged dataset sequence (if you see this often, post at http://issues.jalview.org/browse/JAL-1474)"); // ("
5704 // + (pre ? "prepended" : "") + " "
5705 // + (post ? "appended" : ""));
5710 // sequence refs are identical. We may need to update the existing dataset
5711 // alignment with this one, though.
5712 if (ds != null && dseqs == null)
5714 int opos = ds.findIndex(dsq);
5715 SequenceI tseq = null;
5716 if (opos != -1 && vseqpos != opos)
5718 // remove from old position
5719 ds.deleteSequence(dsq);
5721 if (vseqpos < ds.getHeight())
5723 if (vseqpos != opos)
5725 // save sequence at destination position
5726 tseq = ds.getSequenceAt(vseqpos);
5727 ds.replaceSequenceAt(vseqpos, dsq);
5728 ds.addSequence(tseq);
5733 ds.addSequence(dsq);
5740 * TODO use AlignmentI here and in related methods - needs
5741 * AlignmentI.getDataset() changed to return AlignmentI instead of Alignment
5743 Hashtable<String, AlignmentI> datasetIds = null;
5745 IdentityHashMap<AlignmentI, String> dataset2Ids = null;
5747 private AlignmentI getDatasetFor(String datasetId)
5749 if (datasetIds == null)
5751 datasetIds = new Hashtable<>();
5754 if (datasetIds.containsKey(datasetId))
5756 return datasetIds.get(datasetId);
5761 private void addDatasetRef(String datasetId, AlignmentI dataset)
5763 if (datasetIds == null)
5765 datasetIds = new Hashtable<>();
5767 datasetIds.put(datasetId, dataset);
5771 * make a new dataset ID for this jalview dataset alignment
5776 private String getDatasetIdRef(AlignmentI dataset)
5778 if (dataset.getDataset() != null)
5780 warn("Serious issue! Dataset Object passed to getDatasetIdRef is not a Jalview DATASET alignment...");
5782 String datasetId = makeHashCode(dataset, null);
5783 if (datasetId == null)
5785 // make a new datasetId and record it
5786 if (dataset2Ids == null)
5788 dataset2Ids = new IdentityHashMap<>();
5792 datasetId = dataset2Ids.get(dataset);
5794 if (datasetId == null)
5796 datasetId = "ds" + dataset2Ids.size() + 1;
5797 dataset2Ids.put(dataset, datasetId);
5803 private void addDBRefs(SequenceI datasetSequence, Sequence sequence)
5805 for (int d = 0; d < sequence.getDBRef().size(); d++)
5807 DBRef dr = sequence.getDBRef().get(d);
5808 jalview.datamodel.DBRefEntry entry = new jalview.datamodel.DBRefEntry(
5809 dr.getSource(), dr.getVersion(), dr.getAccessionId());
5810 if (dr.getMapping() != null)
5812 entry.setMap(addMapping(dr.getMapping()));
5814 datasetSequence.addDBRef(entry);
5818 private jalview.datamodel.Mapping addMapping(Mapping m)
5820 SequenceI dsto = null;
5821 // Mapping m = dr.getMapping();
5822 int fr[] = new int[m.getMapListFrom().size() * 2];
5823 Iterator<MapListFrom> from = m.getMapListFrom().iterator();// enumerateMapListFrom();
5824 for (int _i = 0; from.hasNext(); _i += 2)
5826 MapListFrom mf = from.next();
5827 fr[_i] = mf.getStart();
5828 fr[_i + 1] = mf.getEnd();
5830 int fto[] = new int[m.getMapListTo().size() * 2];
5831 Iterator<MapListTo> to = m.getMapListTo().iterator();// enumerateMapListTo();
5832 for (int _i = 0; to.hasNext(); _i += 2)
5834 MapListTo mf = to.next();
5835 fto[_i] = mf.getStart();
5836 fto[_i + 1] = mf.getEnd();
5838 jalview.datamodel.Mapping jmap = new jalview.datamodel.Mapping(dsto, fr,
5839 fto, m.getMapFromUnit().intValue(),
5840 m.getMapToUnit().intValue());
5843 * (optional) choice of dseqFor or Sequence
5845 if (m.getDseqFor() != null)
5847 String dsfor = m.getDseqFor();
5848 if (seqRefIds.containsKey(dsfor))
5853 jmap.setTo(seqRefIds.get(dsfor));
5857 frefedSequence.add(newMappingRef(dsfor, jmap));
5860 else if (m.getSequence() != null)
5863 * local sequence definition
5865 Sequence ms = m.getSequence();
5866 SequenceI djs = null;
5867 String sqid = ms.getDsseqid();
5868 if (sqid != null && sqid.length() > 0)
5871 * recover dataset sequence
5873 djs = seqRefIds.get(sqid);
5878 "Warning - making up dataset sequence id for DbRef sequence map reference");
5879 sqid = ((Object) ms).toString(); // make up a new hascode for
5880 // undefined dataset sequence hash
5881 // (unlikely to happen)
5887 * make a new dataset sequence and add it to refIds hash
5889 djs = new jalview.datamodel.Sequence(ms.getName(),
5891 djs.setStart(jmap.getMap().getToLowest());
5892 djs.setEnd(jmap.getMap().getToHighest());
5893 djs.setVamsasId(uniqueSetSuffix + sqid);
5895 incompleteSeqs.put(sqid, djs);
5896 seqRefIds.put(sqid, djs);
5899 jalview.bin.Cache.log.debug("about to recurse on addDBRefs.");
5908 * Provides a 'copy' of an alignment view (on action New View) by 'saving' the
5909 * view as XML (but not to file), and then reloading it
5914 public AlignmentPanel copyAlignPanel(AlignmentPanel ap)
5917 JalviewModel jm = saveState(ap, null, null, null);
5920 jm.getVamsasModel().getSequenceSet().get(0).getDatasetId(),
5921 ap.getAlignment().getDataset());
5923 uniqueSetSuffix = "";
5924 // jm.getJalviewModelSequence().getViewport(0).setId(null);
5925 jm.getViewport().get(0).setId(null);
5926 // we don't overwrite the view we just copied
5928 if (this.frefedSequence == null)
5930 frefedSequence = new Vector<>();
5933 viewportsAdded.clear();
5935 AlignFrame af = loadFromObject(jm, null, false, null);
5936 af.getAlignPanels().clear();
5937 af.closeMenuItem_actionPerformed(true);
5940 * if(ap.av.getAlignment().getAlignmentAnnotation()!=null) { for(int i=0;
5941 * i<ap.av.getAlignment().getAlignmentAnnotation().length; i++) {
5942 * if(!ap.av.getAlignment().getAlignmentAnnotation()[i].autoCalculated) {
5943 * af.alignPanel.av.getAlignment().getAlignmentAnnotation()[i] =
5944 * ap.av.getAlignment().getAlignmentAnnotation()[i]; } } }
5947 return af.alignPanel;
5950 private Hashtable jvids2vobj;
5952 private void warn(String msg)
5957 private void warn(String msg, Exception e)
5959 if (Cache.log != null)
5963 Cache.log.warn(msg, e);
5967 Cache.log.warn(msg);
5972 System.err.println("Warning: " + msg);
5975 e.printStackTrace();
5980 private void debug(String string)
5982 debug(string, null);
5985 private void debug(String msg, Exception e)
5987 if (Cache.log != null)
5991 Cache.log.debug(msg, e);
5995 Cache.log.debug(msg);
6000 System.err.println("Warning: " + msg);
6003 e.printStackTrace();
6009 * set the object to ID mapping tables used to write/recover objects and XML
6010 * ID strings for the jalview project. If external tables are provided then
6011 * finalize and clearSeqRefs will not clear the tables when the Jalview2XML
6012 * object goes out of scope. - also populates the datasetIds hashtable with
6013 * alignment objects containing dataset sequences
6016 * Map from ID strings to jalview datamodel
6018 * Map from jalview datamodel to ID strings
6022 public void setObjectMappingTables(Hashtable vobj2jv,
6023 IdentityHashMap jv2vobj)
6025 this.jv2vobj = jv2vobj;
6026 this.vobj2jv = vobj2jv;
6027 Iterator ds = jv2vobj.keySet().iterator();
6029 while (ds.hasNext())
6031 Object jvobj = ds.next();
6032 id = jv2vobj.get(jvobj).toString();
6033 if (jvobj instanceof jalview.datamodel.Alignment)
6035 if (((jalview.datamodel.Alignment) jvobj).getDataset() == null)
6037 addDatasetRef(id, (jalview.datamodel.Alignment) jvobj);
6040 else if (jvobj instanceof jalview.datamodel.Sequence)
6042 // register sequence object so the XML parser can recover it.
6043 if (seqRefIds == null)
6045 seqRefIds = new HashMap<>();
6047 if (seqsToIds == null)
6049 seqsToIds = new IdentityHashMap<>();
6051 seqRefIds.put(jv2vobj.get(jvobj).toString(), (SequenceI) jvobj);
6052 seqsToIds.put((SequenceI) jvobj, id);
6054 else if (jvobj instanceof jalview.datamodel.AlignmentAnnotation)
6057 AlignmentAnnotation jvann = (AlignmentAnnotation) jvobj;
6058 annotationIds.put(anid = jv2vobj.get(jvobj).toString(), jvann);
6059 if (jvann.annotationId == null)
6061 jvann.annotationId = anid;
6063 if (!jvann.annotationId.equals(anid))
6065 // TODO verify that this is the correct behaviour
6066 this.warn("Overriding Annotation ID for " + anid
6067 + " from different id : " + jvann.annotationId);
6068 jvann.annotationId = anid;
6071 else if (jvobj instanceof String)
6073 if (jvids2vobj == null)
6075 jvids2vobj = new Hashtable();
6076 jvids2vobj.put(jvobj, jv2vobj.get(jvobj).toString());
6081 Cache.log.debug("Ignoring " + jvobj.getClass() + " (ID = " + id);
6087 * set the uniqueSetSuffix used to prefix/suffix object IDs for jalview
6088 * objects created from the project archive. If string is null (default for
6089 * construction) then suffix will be set automatically.
6093 public void setUniqueSetSuffix(String string)
6095 uniqueSetSuffix = string;
6100 * uses skipList2 as the skipList for skipping views on sequence sets
6101 * associated with keys in the skipList
6105 public void setSkipList(Hashtable skipList2)
6107 skipList = skipList2;
6111 * Reads the jar entry of given name and returns its contents, or null if the
6112 * entry is not found.
6115 * @param jarEntryName
6118 protected String readJarEntry(jarInputStreamProvider jprovider,
6119 String jarEntryName)
6121 String result = null;
6122 BufferedReader in = null;
6127 * Reopen the jar input stream and traverse its entries to find a matching
6130 JarInputStream jin = jprovider.getJarInputStream();
6131 JarEntry entry = null;
6134 entry = jin.getNextJarEntry();
6135 } while (entry != null && !entry.getName().equals(jarEntryName));
6139 StringBuilder out = new StringBuilder(256);
6140 in = new BufferedReader(new InputStreamReader(jin, UTF_8));
6143 while ((data = in.readLine()) != null)
6147 result = out.toString();
6151 warn("Couldn't find entry in Jalview Jar for " + jarEntryName);
6153 } catch (Exception ex)
6155 ex.printStackTrace();
6163 } catch (IOException e)
6174 * Returns an incrementing counter (0, 1, 2...)
6178 private synchronized int nextCounter()
6184 * Loads any saved PCA viewers
6189 protected void loadPCAViewers(JalviewModel model, AlignmentPanel ap)
6193 List<PcaViewer> pcaviewers = model.getPcaViewer();
6194 for (PcaViewer viewer : pcaviewers)
6196 String modelName = viewer.getScoreModelName();
6197 SimilarityParamsI params = new SimilarityParams(
6198 viewer.isIncludeGappedColumns(), viewer.isMatchGaps(),
6199 viewer.isIncludeGaps(),
6200 viewer.isDenominateByShortestLength());
6203 * create the panel (without computing the PCA)
6205 PCAPanel panel = new PCAPanel(ap, modelName, params);
6207 panel.setTitle(viewer.getTitle());
6208 panel.setBounds(new Rectangle(viewer.getXpos(), viewer.getYpos(),
6209 viewer.getWidth(), viewer.getHeight()));
6211 boolean showLabels = viewer.isShowLabels();
6212 panel.setShowLabels(showLabels);
6213 panel.getRotatableCanvas().setShowLabels(showLabels);
6214 panel.getRotatableCanvas()
6215 .setBgColour(new Color(viewer.getBgColour()));
6216 panel.getRotatableCanvas()
6217 .setApplyToAllViews(viewer.isLinkToAllViews());
6220 * load PCA output data
6222 ScoreModelI scoreModel = ScoreModels.getInstance()
6223 .getScoreModel(modelName, ap);
6224 PCA pca = new PCA(null, scoreModel, params);
6225 PcaDataType pcaData = viewer.getPcaData();
6227 MatrixI pairwise = loadDoubleMatrix(pcaData.getPairwiseMatrix());
6228 pca.setPairwiseScores(pairwise);
6230 MatrixI triDiag = loadDoubleMatrix(pcaData.getTridiagonalMatrix());
6231 pca.setTridiagonal(triDiag);
6233 MatrixI result = loadDoubleMatrix(pcaData.getEigenMatrix());
6234 pca.setEigenmatrix(result);
6236 panel.getPcaModel().setPCA(pca);
6239 * we haven't saved the input data! (JAL-2647 to do)
6241 panel.setInputData(null);
6244 * add the sequence points for the PCA display
6246 List<jalview.datamodel.SequencePoint> seqPoints = new ArrayList<>();
6247 for (SequencePoint sp : viewer.getSequencePoint())
6249 String seqId = sp.getSequenceRef();
6250 SequenceI seq = seqRefIds.get(seqId);
6253 throw new IllegalStateException(
6254 "Unmatched seqref for PCA: " + seqId);
6256 Point pt = new Point(sp.getXPos(), sp.getYPos(), sp.getZPos());
6257 jalview.datamodel.SequencePoint seqPoint = new jalview.datamodel.SequencePoint(
6259 seqPoints.add(seqPoint);
6261 panel.getRotatableCanvas().setPoints(seqPoints, seqPoints.size());
6264 * set min-max ranges and scale after setPoints (which recomputes them)
6266 panel.getRotatableCanvas().setScaleFactor(viewer.getScaleFactor());
6267 SeqPointMin spMin = viewer.getSeqPointMin();
6268 float[] min = new float[] { spMin.getXPos(), spMin.getYPos(),
6270 SeqPointMax spMax = viewer.getSeqPointMax();
6271 float[] max = new float[] { spMax.getXPos(), spMax.getYPos(),
6273 panel.getRotatableCanvas().setSeqMinMax(min, max);
6275 // todo: hold points list in PCAModel only
6276 panel.getPcaModel().setSequencePoints(seqPoints);
6278 panel.setSelectedDimensionIndex(viewer.getXDim(), X);
6279 panel.setSelectedDimensionIndex(viewer.getYDim(), Y);
6280 panel.setSelectedDimensionIndex(viewer.getZDim(), Z);
6282 // is this duplication needed?
6283 panel.setTop(seqPoints.size() - 1);
6284 panel.getPcaModel().setTop(seqPoints.size() - 1);
6287 * add the axes' end points for the display
6289 for (int i = 0; i < 3; i++)
6291 Axis axis = viewer.getAxis().get(i);
6292 panel.getRotatableCanvas().getAxisEndPoints()[i] = new Point(
6293 axis.getXPos(), axis.getYPos(), axis.getZPos());
6296 Desktop.addInternalFrame(panel, MessageManager.formatMessage(
6297 "label.calc_title", "PCA", modelName), 475, 450);
6299 } catch (Exception ex)
6301 Cache.log.error("Error loading PCA: " + ex.toString());
6306 * Populates an XML model of the feature colour scheme for one feature type
6308 * @param featureType
6312 public static Colour marshalColour(
6313 String featureType, FeatureColourI fcol)
6315 Colour col = new Colour();
6316 if (fcol.isSimpleColour())
6318 col.setRGB(Format.getHexString(fcol.getColour()));
6322 col.setRGB(Format.getHexString(fcol.getMaxColour()));
6323 col.setMin(fcol.getMin());
6324 col.setMax(fcol.getMax());
6325 col.setMinRGB(jalview.util.Format.getHexString(fcol.getMinColour()));
6326 col.setAutoScale(fcol.isAutoScaled());
6327 col.setThreshold(fcol.getThreshold());
6328 col.setColourByLabel(fcol.isColourByLabel());
6329 col.setThreshType(fcol.isAboveThreshold() ? ThresholdType.ABOVE
6330 : (fcol.isBelowThreshold() ? ThresholdType.BELOW
6331 : ThresholdType.NONE));
6332 if (fcol.isColourByAttribute())
6334 final String[] attName = fcol.getAttributeName();
6335 col.getAttributeName().add(attName[0]);
6336 if (attName.length > 1)
6338 col.getAttributeName().add(attName[1]);
6341 Color noColour = fcol.getNoColour();
6342 if (noColour == null)
6344 col.setNoValueColour(NoValueColour.NONE);
6346 else if (noColour == fcol.getMaxColour())
6348 col.setNoValueColour(NoValueColour.MAX);
6352 col.setNoValueColour(NoValueColour.MIN);
6355 col.setName(featureType);
6360 * Populates an XML model of the feature filter(s) for one feature type
6362 * @param firstMatcher
6363 * the first (or only) match condition)
6365 * remaining match conditions (if any)
6367 * if true, conditions are and-ed, else or-ed
6369 public static jalview.xml.binding.jalview.FeatureMatcherSet marshalFilter(
6370 FeatureMatcherI firstMatcher, Iterator<FeatureMatcherI> filters,
6373 jalview.xml.binding.jalview.FeatureMatcherSet result = new jalview.xml.binding.jalview.FeatureMatcherSet();
6375 if (filters.hasNext())
6380 CompoundMatcher compound = new CompoundMatcher();
6381 compound.setAnd(and);
6382 jalview.xml.binding.jalview.FeatureMatcherSet matcher1 = marshalFilter(
6383 firstMatcher, Collections.emptyIterator(), and);
6384 // compound.addMatcherSet(matcher1);
6385 compound.getMatcherSet().add(matcher1);
6386 FeatureMatcherI nextMatcher = filters.next();
6387 jalview.xml.binding.jalview.FeatureMatcherSet matcher2 = marshalFilter(
6388 nextMatcher, filters, and);
6389 // compound.addMatcherSet(matcher2);
6390 compound.getMatcherSet().add(matcher2);
6391 result.setCompoundMatcher(compound);
6396 * single condition matcher
6398 // MatchCondition matcherModel = new MatchCondition();
6399 jalview.xml.binding.jalview.FeatureMatcher matcherModel = new jalview.xml.binding.jalview.FeatureMatcher();
6400 matcherModel.setCondition(
6401 firstMatcher.getMatcher().getCondition().getStableName());
6402 matcherModel.setValue(firstMatcher.getMatcher().getPattern());
6403 if (firstMatcher.isByAttribute())
6405 matcherModel.setBy(FilterBy.BY_ATTRIBUTE);
6406 // matcherModel.setAttributeName(firstMatcher.getAttribute());
6407 String[] attName = firstMatcher.getAttribute();
6408 matcherModel.getAttributeName().add(attName[0]); // attribute
6409 if (attName.length > 1)
6411 matcherModel.getAttributeName().add(attName[1]); // sub-attribute
6414 else if (firstMatcher.isByLabel())
6416 matcherModel.setBy(FilterBy.BY_LABEL);
6418 else if (firstMatcher.isByScore())
6420 matcherModel.setBy(FilterBy.BY_SCORE);
6422 result.setMatchCondition(matcherModel);
6429 * Loads one XML model of a feature filter to a Jalview object
6431 * @param featureType
6432 * @param matcherSetModel
6435 public static FeatureMatcherSetI parseFilter(
6437 jalview.xml.binding.jalview.FeatureMatcherSet matcherSetModel)
6439 FeatureMatcherSetI result = new FeatureMatcherSet();
6442 parseFilterConditions(result, matcherSetModel, true);
6443 } catch (IllegalStateException e)
6445 // mixing AND and OR conditions perhaps
6447 String.format("Error reading filter conditions for '%s': %s",
6448 featureType, e.getMessage()));
6449 // return as much as was parsed up to the error
6456 * Adds feature match conditions to matcherSet as unmarshalled from XML
6457 * (possibly recursively for compound conditions)
6460 * @param matcherSetModel
6462 * if true, multiple conditions are AND-ed, else they are OR-ed
6463 * @throws IllegalStateException
6464 * if AND and OR conditions are mixed
6466 protected static void parseFilterConditions(
6467 FeatureMatcherSetI matcherSet,
6468 jalview.xml.binding.jalview.FeatureMatcherSet matcherSetModel,
6471 jalview.xml.binding.jalview.FeatureMatcher mc = matcherSetModel
6472 .getMatchCondition();
6478 FilterBy filterBy = mc.getBy();
6479 Condition cond = Condition.fromString(mc.getCondition());
6480 String pattern = mc.getValue();
6481 FeatureMatcherI matchCondition = null;
6482 if (filterBy == FilterBy.BY_LABEL)
6484 matchCondition = FeatureMatcher.byLabel(cond, pattern);
6486 else if (filterBy == FilterBy.BY_SCORE)
6488 matchCondition = FeatureMatcher.byScore(cond, pattern);
6491 else if (filterBy == FilterBy.BY_ATTRIBUTE)
6493 final List<String> attributeName = mc.getAttributeName();
6494 String[] attNames = attributeName
6495 .toArray(new String[attributeName.size()]);
6496 matchCondition = FeatureMatcher.byAttribute(cond, pattern,
6501 * note this throws IllegalStateException if AND-ing to a
6502 * previously OR-ed compound condition, or vice versa
6506 matcherSet.and(matchCondition);
6510 matcherSet.or(matchCondition);
6516 * compound condition
6518 List<jalview.xml.binding.jalview.FeatureMatcherSet> matchers = matcherSetModel
6519 .getCompoundMatcher().getMatcherSet();
6520 boolean anded = matcherSetModel.getCompoundMatcher().isAnd();
6521 if (matchers.size() == 2)
6523 parseFilterConditions(matcherSet, matchers.get(0), anded);
6524 parseFilterConditions(matcherSet, matchers.get(1), anded);
6528 System.err.println("Malformed compound filter condition");
6534 * Loads one XML model of a feature colour to a Jalview object
6536 * @param colourModel
6539 public static FeatureColourI parseColour(Colour colourModel)
6541 FeatureColourI colour = null;
6543 if (colourModel.getMax() != null)
6545 Color mincol = null;
6546 Color maxcol = null;
6547 Color noValueColour = null;
6551 mincol = new Color(Integer.parseInt(colourModel.getMinRGB(), 16));
6552 maxcol = new Color(Integer.parseInt(colourModel.getRGB(), 16));
6553 } catch (Exception e)
6555 Cache.log.warn("Couldn't parse out graduated feature color.", e);
6558 NoValueColour noCol = colourModel.getNoValueColour();
6559 if (noCol == NoValueColour.MIN)
6561 noValueColour = mincol;
6563 else if (noCol == NoValueColour.MAX)
6565 noValueColour = maxcol;
6568 colour = new FeatureColour(maxcol, mincol, maxcol, noValueColour,
6569 safeFloat(colourModel.getMin()),
6570 safeFloat(colourModel.getMax()));
6571 final List<String> attributeName = colourModel.getAttributeName();
6572 String[] attributes = attributeName
6573 .toArray(new String[attributeName.size()]);
6574 if (attributes != null && attributes.length > 0)
6576 colour.setAttributeName(attributes);
6578 if (colourModel.isAutoScale() != null)
6580 colour.setAutoScaled(colourModel.isAutoScale().booleanValue());
6582 if (colourModel.isColourByLabel() != null)
6584 colour.setColourByLabel(
6585 colourModel.isColourByLabel().booleanValue());
6587 if (colourModel.getThreshold() != null)
6589 colour.setThreshold(colourModel.getThreshold().floatValue());
6591 ThresholdType ttyp = colourModel.getThreshType();
6592 if (ttyp == ThresholdType.ABOVE)
6594 colour.setAboveThreshold(true);
6596 else if (ttyp == ThresholdType.BELOW)
6598 colour.setBelowThreshold(true);
6603 Color color = new Color(Integer.parseInt(colourModel.getRGB(), 16));
6604 colour = new FeatureColour(color);