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;
4191 tp.getTreeCanvas().setApplyToAllViews(tree.isLinkToAllViews());
4194 warn("There was a problem recovering stored Newick tree: \n"
4195 + tree.getNewick());
4199 tp.fitToWindow.setState(safeBoolean(tree.isFitToWindow()));
4200 tp.fitToWindow_actionPerformed(null);
4202 if (tree.getFontName() != null)
4205 new Font(tree.getFontName(), safeInt(tree.getFontStyle()),
4206 safeInt(tree.getFontSize())));
4211 new Font(view.getFontName(), safeInt(view.getFontStyle()),
4212 safeInt(view.getFontSize())));
4215 tp.showPlaceholders(safeBoolean(tree.isMarkUnlinked()));
4216 tp.showBootstrap(safeBoolean(tree.isShowBootstrap()));
4217 tp.showDistances(safeBoolean(tree.isShowDistances()));
4219 tp.getTreeCanvas().setThreshold(safeFloat(tree.getThreshold()));
4221 if (safeBoolean(tree.isCurrentTree()))
4223 af.getViewport().setCurrentTree(tp.getTree());
4227 } catch (Exception ex)
4229 ex.printStackTrace();
4234 * Load and link any saved structure viewers.
4241 protected void loadPDBStructures(jarInputStreamProvider jprovider,
4242 List<JSeq> jseqs, AlignFrame af, AlignmentPanel ap)
4245 * Run through all PDB ids on the alignment, and collect mappings between
4246 * distinct view ids and all sequences referring to that view.
4248 Map<String, StructureViewerModel> structureViewers = new LinkedHashMap<>();
4250 for (int i = 0; i < jseqs.size(); i++)
4252 JSeq jseq = jseqs.get(i);
4253 if (jseq.getPdbids().size() > 0)
4255 List<Pdbids> ids = jseq.getPdbids();
4256 for (int p = 0; p < ids.size(); p++)
4258 Pdbids pdbid = ids.get(p);
4259 final int structureStateCount = pdbid.getStructureState().size();
4260 for (int s = 0; s < structureStateCount; s++)
4262 // check to see if we haven't already created this structure view
4263 final StructureState structureState = pdbid
4264 .getStructureState().get(s);
4265 String sviewid = (structureState.getViewId() == null) ? null
4266 : structureState.getViewId() + uniqueSetSuffix;
4267 jalview.datamodel.PDBEntry jpdb = new jalview.datamodel.PDBEntry();
4268 // Originally : pdbid.getFile()
4269 // : TODO: verify external PDB file recovery still works in normal
4270 // jalview project load
4272 loadPDBFile(jprovider, pdbid.getId(), pdbid.getFile()));
4273 jpdb.setId(pdbid.getId());
4275 int x = safeInt(structureState.getXpos());
4276 int y = safeInt(structureState.getYpos());
4277 int width = safeInt(structureState.getWidth());
4278 int height = safeInt(structureState.getHeight());
4280 // Probably don't need to do this anymore...
4281 // Desktop.desktop.getComponentAt(x, y);
4282 // TODO: NOW: check that this recovers the PDB file correctly.
4283 String pdbFile = loadPDBFile(jprovider, pdbid.getId(),
4285 jalview.datamodel.SequenceI seq = seqRefIds
4286 .get(jseq.getId() + "");
4287 if (sviewid == null)
4289 sviewid = "_jalview_pre2_4_" + x + "," + y + "," + width + ","
4292 if (!structureViewers.containsKey(sviewid))
4294 structureViewers.put(sviewid,
4295 new StructureViewerModel(x, y, width, height, false,
4296 false, true, structureState.getViewId(),
4297 structureState.getType()));
4298 // Legacy pre-2.7 conversion JAL-823 :
4299 // do not assume any view has to be linked for colour by
4303 // assemble String[] { pdb files }, String[] { id for each
4304 // file }, orig_fileloc, SequenceI[][] {{ seqs_file 1 }, {
4305 // seqs_file 2}, boolean[] {
4306 // linkAlignPanel,superposeWithAlignpanel}} from hash
4307 StructureViewerModel jmoldat = structureViewers.get(sviewid);
4308 jmoldat.setAlignWithPanel(jmoldat.isAlignWithPanel()
4309 || structureState.isAlignwithAlignPanel());
4312 * Default colour by linked panel to false if not specified (e.g.
4313 * for pre-2.7 projects)
4315 boolean colourWithAlignPanel = jmoldat.isColourWithAlignPanel();
4316 colourWithAlignPanel |= structureState.isColourwithAlignPanel();
4317 jmoldat.setColourWithAlignPanel(colourWithAlignPanel);
4320 * Default colour by viewer to true if not specified (e.g. for
4323 boolean colourByViewer = jmoldat.isColourByViewer();
4324 colourByViewer &= structureState.isColourByJmol();
4325 jmoldat.setColourByViewer(colourByViewer);
4327 if (jmoldat.getStateData().length() < structureState
4328 .getValue()/*Content()*/.length())
4330 jmoldat.setStateData(structureState.getValue());// Content());
4332 if (pdbid.getFile() != null)
4334 File mapkey = new File(pdbid.getFile());
4335 StructureData seqstrmaps = jmoldat.getFileData().get(mapkey);
4336 if (seqstrmaps == null)
4338 jmoldat.getFileData().put(mapkey,
4339 seqstrmaps = jmoldat.new StructureData(pdbFile,
4342 if (!seqstrmaps.getSeqList().contains(seq))
4344 seqstrmaps.getSeqList().add(seq);
4350 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");
4357 // Instantiate the associated structure views
4358 for (Entry<String, StructureViewerModel> entry : structureViewers
4363 createOrLinkStructureViewer(entry, af, ap, jprovider);
4364 } catch (Exception e)
4367 "Error loading structure viewer: " + e.getMessage());
4368 // failed - try the next one
4380 protected void createOrLinkStructureViewer(
4381 Entry<String, StructureViewerModel> viewerData, AlignFrame af,
4382 AlignmentPanel ap, jarInputStreamProvider jprovider)
4384 final StructureViewerModel stateData = viewerData.getValue();
4387 * Search for any viewer windows already open from other alignment views
4388 * that exactly match the stored structure state
4390 StructureViewerBase comp = findMatchingViewer(viewerData);
4394 linkStructureViewer(ap, comp, stateData);
4399 * From 2.9: stateData.type contains JMOL or CHIMERA, data is in jar entry
4400 * "viewer_"+stateData.viewId
4402 if (ViewerType.CHIMERA.toString().equals(stateData.getType()))
4404 createChimeraViewer(viewerData, af, jprovider);
4409 * else Jmol (if pre-2.9, stateData contains JMOL state string)
4411 createJmolViewer(viewerData, af, jprovider);
4416 * Create a new Chimera viewer.
4422 protected void createChimeraViewer(
4423 Entry<String, StructureViewerModel> viewerData, AlignFrame af,
4424 jarInputStreamProvider jprovider)
4426 StructureViewerModel data = viewerData.getValue();
4427 String chimeraSessionFile = data.getStateData();
4430 * Copy Chimera session from jar entry "viewer_"+viewId to a temporary file
4432 * NB this is the 'saved' viewId as in the project file XML, _not_ the
4433 * 'uniquified' sviewid used to reconstruct the viewer here
4435 String viewerJarEntryName = getViewerJarEntryName(data.getViewId());
4436 chimeraSessionFile = copyJarEntry(jprovider, viewerJarEntryName,
4439 Set<Entry<File, StructureData>> fileData = data.getFileData()
4441 List<PDBEntry> pdbs = new ArrayList<>();
4442 List<SequenceI[]> allseqs = new ArrayList<>();
4443 for (Entry<File, StructureData> pdb : fileData)
4445 String filePath = pdb.getValue().getFilePath();
4446 String pdbId = pdb.getValue().getPdbId();
4447 // pdbs.add(new PDBEntry(filePath, pdbId));
4448 pdbs.add(new PDBEntry(pdbId, null, PDBEntry.Type.PDB, filePath));
4449 final List<SequenceI> seqList = pdb.getValue().getSeqList();
4450 SequenceI[] seqs = seqList.toArray(new SequenceI[seqList.size()]);
4454 boolean colourByChimera = data.isColourByViewer();
4455 boolean colourBySequence = data.isColourWithAlignPanel();
4457 // TODO use StructureViewer as a factory here, see JAL-1761
4458 final PDBEntry[] pdbArray = pdbs.toArray(new PDBEntry[pdbs.size()]);
4459 final SequenceI[][] seqsArray = allseqs
4460 .toArray(new SequenceI[allseqs.size()][]);
4461 String newViewId = viewerData.getKey();
4463 ChimeraViewFrame cvf = new ChimeraViewFrame(chimeraSessionFile,
4464 af.alignPanel, pdbArray, seqsArray, colourByChimera,
4465 colourBySequence, newViewId);
4466 cvf.setSize(data.getWidth(), data.getHeight());
4467 cvf.setLocation(data.getX(), data.getY());
4471 * Create a new Jmol window. First parse the Jmol state to translate filenames
4472 * loaded into the view, and record the order in which files are shown in the
4473 * Jmol view, so we can add the sequence mappings in same order.
4479 protected void createJmolViewer(
4480 final Entry<String, StructureViewerModel> viewerData,
4481 AlignFrame af, jarInputStreamProvider jprovider)
4483 final StructureViewerModel svattrib = viewerData.getValue();
4484 String state = svattrib.getStateData();
4487 * Pre-2.9: state element value is the Jmol state string
4489 * 2.9+: @type is "JMOL", state data is in a Jar file member named "viewer_"
4492 if (ViewerType.JMOL.toString().equals(svattrib.getType()))
4494 state = readJarEntry(jprovider,
4495 getViewerJarEntryName(svattrib.getViewId()));
4498 List<String> pdbfilenames = new ArrayList<>();
4499 List<SequenceI[]> seqmaps = new ArrayList<>();
4500 List<String> pdbids = new ArrayList<>();
4501 StringBuilder newFileLoc = new StringBuilder(64);
4502 int cp = 0, ncp, ecp;
4503 Map<File, StructureData> oldFiles = svattrib.getFileData();
4504 while ((ncp = state.indexOf("load ", cp)) > -1)
4508 // look for next filename in load statement
4509 newFileLoc.append(state.substring(cp,
4510 ncp = (state.indexOf("\"", ncp + 1) + 1)));
4511 String oldfilenam = state.substring(ncp,
4512 ecp = state.indexOf("\"", ncp));
4513 // recover the new mapping data for this old filename
4514 // have to normalize filename - since Jmol and jalview do
4516 // translation differently.
4517 StructureData filedat = oldFiles.get(new File(oldfilenam));
4518 if (filedat == null)
4520 String reformatedOldFilename = oldfilenam.replaceAll("/", "\\\\");
4521 filedat = oldFiles.get(new File(reformatedOldFilename));
4523 newFileLoc.append(Platform.escapeBackslashes(filedat.getFilePath()));
4524 pdbfilenames.add(filedat.getFilePath());
4525 pdbids.add(filedat.getPdbId());
4526 seqmaps.add(filedat.getSeqList().toArray(new SequenceI[0]));
4527 newFileLoc.append("\"");
4528 cp = ecp + 1; // advance beyond last \" and set cursor so we can
4529 // look for next file statement.
4530 } while ((ncp = state.indexOf("/*file*/", cp)) > -1);
4534 // just append rest of state
4535 newFileLoc.append(state.substring(cp));
4539 System.err.print("Ignoring incomplete Jmol state for PDB ids: ");
4540 newFileLoc = new StringBuilder(state);
4541 newFileLoc.append("; load append ");
4542 for (File id : oldFiles.keySet())
4544 // add this and any other pdb files that should be present in
4546 StructureData filedat = oldFiles.get(id);
4547 newFileLoc.append(filedat.getFilePath());
4548 pdbfilenames.add(filedat.getFilePath());
4549 pdbids.add(filedat.getPdbId());
4550 seqmaps.add(filedat.getSeqList().toArray(new SequenceI[0]));
4551 newFileLoc.append(" \"");
4552 newFileLoc.append(filedat.getFilePath());
4553 newFileLoc.append("\"");
4556 newFileLoc.append(";");
4559 if (newFileLoc.length() == 0)
4563 int histbug = newFileLoc.indexOf("history = ");
4567 * change "history = [true|false];" to "history = [1|0];"
4570 int diff = histbug == -1 ? -1 : newFileLoc.indexOf(";", histbug);
4571 String val = (diff == -1) ? null
4572 : newFileLoc.substring(histbug, diff);
4573 if (val != null && val.length() >= 4)
4575 if (val.contains("e")) // eh? what can it be?
4577 if (val.trim().equals("true"))
4585 newFileLoc.replace(histbug, diff, val);
4590 final String[] pdbf = pdbfilenames
4591 .toArray(new String[pdbfilenames.size()]);
4592 final String[] id = pdbids.toArray(new String[pdbids.size()]);
4593 final SequenceI[][] sq = seqmaps
4594 .toArray(new SequenceI[seqmaps.size()][]);
4595 final String fileloc = newFileLoc.toString();
4596 final String sviewid = viewerData.getKey();
4597 final AlignFrame alf = af;
4598 final Rectangle rect = new Rectangle(svattrib.getX(), svattrib.getY(),
4599 svattrib.getWidth(), svattrib.getHeight());
4602 javax.swing.SwingUtilities.invokeAndWait(new Runnable()
4607 JalviewStructureDisplayI sview = null;
4610 sview = new StructureViewer(
4611 alf.alignPanel.getStructureSelectionManager())
4612 .createView(StructureViewer.ViewerType.JMOL,
4613 pdbf, id, sq, alf.alignPanel, svattrib,
4614 fileloc, rect, sviewid);
4615 addNewStructureViewer(sview);
4616 } catch (OutOfMemoryError ex)
4618 new OOMWarning("restoring structure view for PDB id " + id,
4619 (OutOfMemoryError) ex.getCause());
4620 if (sview != null && sview.isVisible())
4622 sview.closeViewer(false);
4623 sview.setVisible(false);
4629 } catch (InvocationTargetException ex)
4631 warn("Unexpected error when opening Jmol view.", ex);
4633 } catch (InterruptedException e)
4635 // e.printStackTrace();
4641 * Generates a name for the entry in the project jar file to hold state
4642 * information for a structure viewer
4647 protected String getViewerJarEntryName(String viewId)
4649 return VIEWER_PREFIX + viewId;
4653 * Returns any open frame that matches given structure viewer data. The match
4654 * is based on the unique viewId, or (for older project versions) the frame's
4660 protected StructureViewerBase findMatchingViewer(
4661 Entry<String, StructureViewerModel> viewerData)
4663 final String sviewid = viewerData.getKey();
4664 final StructureViewerModel svattrib = viewerData.getValue();
4665 StructureViewerBase comp = null;
4666 JInternalFrame[] frames = getAllFrames();
4667 for (JInternalFrame frame : frames)
4669 if (frame instanceof StructureViewerBase)
4672 * Post jalview 2.4 schema includes structure view id
4674 if (sviewid != null && ((StructureViewerBase) frame).getViewId()
4677 comp = (StructureViewerBase) frame;
4678 break; // break added in 2.9
4681 * Otherwise test for matching position and size of viewer frame
4683 else if (frame.getX() == svattrib.getX()
4684 && frame.getY() == svattrib.getY()
4685 && frame.getHeight() == svattrib.getHeight()
4686 && frame.getWidth() == svattrib.getWidth())
4688 comp = (StructureViewerBase) frame;
4689 // no break in faint hope of an exact match on viewId
4697 * Link an AlignmentPanel to an existing structure viewer.
4702 * @param useinViewerSuperpos
4703 * @param usetoColourbyseq
4704 * @param viewerColouring
4706 protected void linkStructureViewer(AlignmentPanel ap,
4707 StructureViewerBase viewer, StructureViewerModel stateData)
4709 // NOTE: if the jalview project is part of a shared session then
4710 // view synchronization should/could be done here.
4712 final boolean useinViewerSuperpos = stateData.isAlignWithPanel();
4713 final boolean usetoColourbyseq = stateData.isColourWithAlignPanel();
4714 final boolean viewerColouring = stateData.isColourByViewer();
4715 Map<File, StructureData> oldFiles = stateData.getFileData();
4718 * Add mapping for sequences in this view to an already open viewer
4720 final AAStructureBindingModel binding = viewer.getBinding();
4721 for (File id : oldFiles.keySet())
4723 // add this and any other pdb files that should be present in the
4725 StructureData filedat = oldFiles.get(id);
4726 String pdbFile = filedat.getFilePath();
4727 SequenceI[] seq = filedat.getSeqList().toArray(new SequenceI[0]);
4728 binding.getSsm().setMapping(seq, null, pdbFile, DataSourceType.FILE,
4730 binding.addSequenceForStructFile(pdbFile, seq);
4732 // and add the AlignmentPanel's reference to the view panel
4733 viewer.addAlignmentPanel(ap);
4734 if (useinViewerSuperpos)
4736 viewer.useAlignmentPanelForSuperposition(ap);
4740 viewer.excludeAlignmentPanelForSuperposition(ap);
4742 if (usetoColourbyseq)
4744 viewer.useAlignmentPanelForColourbyseq(ap, !viewerColouring);
4748 viewer.excludeAlignmentPanelForColourbyseq(ap);
4753 * Get all frames within the Desktop.
4757 protected JInternalFrame[] getAllFrames()
4759 JInternalFrame[] frames = null;
4760 // TODO is this necessary - is it safe - risk of hanging?
4765 frames = Desktop.desktop.getAllFrames();
4766 } catch (ArrayIndexOutOfBoundsException e)
4768 // occasional No such child exceptions are thrown here...
4772 } catch (InterruptedException f)
4776 } while (frames == null);
4781 * Answers true if 'version' is equal to or later than 'supported', where each
4782 * is formatted as major/minor versions like "2.8.3" or "2.3.4b1" for bugfix
4783 * changes. Development and test values for 'version' are leniently treated
4787 * - minimum version we are comparing against
4789 * - version of data being processsed
4792 public static boolean isVersionStringLaterThan(String supported,
4795 if (supported == null || version == null
4796 || version.equalsIgnoreCase("DEVELOPMENT BUILD")
4797 || version.equalsIgnoreCase("Test")
4798 || version.equalsIgnoreCase("AUTOMATED BUILD"))
4800 System.err.println("Assuming project file with "
4801 + (version == null ? "null" : version)
4802 + " is compatible with Jalview version " + supported);
4807 return StringUtils.compareVersions(version, supported, "b") >= 0;
4811 Vector<JalviewStructureDisplayI> newStructureViewers = null;
4813 protected void addNewStructureViewer(JalviewStructureDisplayI sview)
4815 if (newStructureViewers != null)
4817 sview.getBinding().setFinishedLoadingFromArchive(false);
4818 newStructureViewers.add(sview);
4822 protected void setLoadingFinishedForNewStructureViewers()
4824 if (newStructureViewers != null)
4826 for (JalviewStructureDisplayI sview : newStructureViewers)
4828 sview.getBinding().setFinishedLoadingFromArchive(true);
4830 newStructureViewers.clear();
4831 newStructureViewers = null;
4835 AlignFrame loadViewport(String file, List<JSeq> JSEQ,
4836 List<SequenceI> hiddenSeqs, AlignmentI al,
4837 JalviewModel jm, Viewport view, String uniqueSeqSetId,
4838 String viewId, List<JvAnnotRow> autoAlan)
4840 AlignFrame af = null;
4841 af = new AlignFrame(al, safeInt(view.getWidth()),
4842 safeInt(view.getHeight()), uniqueSeqSetId, viewId);
4844 af.setFileName(file, FileFormat.Jalview);
4846 final AlignViewport viewport = af.getViewport();
4847 for (int i = 0; i < JSEQ.size(); i++)
4849 int colour = safeInt(JSEQ.get(i).getColour());
4850 viewport.setSequenceColour(viewport.getAlignment().getSequenceAt(i),
4856 viewport.setColourByReferenceSeq(true);
4857 viewport.setDisplayReferenceSeq(true);
4860 viewport.setGatherViewsHere(safeBoolean(view.isGatheredViews()));
4862 if (view.getSequenceSetId() != null)
4864 AlignmentViewport av = viewportsAdded.get(uniqueSeqSetId);
4866 viewport.setSequenceSetId(uniqueSeqSetId);
4869 // propagate shared settings to this new view
4870 viewport.setHistoryList(av.getHistoryList());
4871 viewport.setRedoList(av.getRedoList());
4875 viewportsAdded.put(uniqueSeqSetId, viewport);
4877 // TODO: check if this method can be called repeatedly without
4878 // side-effects if alignpanel already registered.
4879 PaintRefresher.Register(af.alignPanel, uniqueSeqSetId);
4881 // apply Hidden regions to view.
4882 if (hiddenSeqs != null)
4884 for (int s = 0; s < JSEQ.size(); s++)
4886 SequenceGroup hidden = new SequenceGroup();
4887 boolean isRepresentative = false;
4888 for (int r = 0; r < JSEQ.get(s).getHiddenSequences().size(); r++)
4890 isRepresentative = true;
4891 SequenceI sequenceToHide = al
4892 .getSequenceAt(JSEQ.get(s).getHiddenSequences().get(r));
4893 hidden.addSequence(sequenceToHide, false);
4894 // remove from hiddenSeqs list so we don't try to hide it twice
4895 hiddenSeqs.remove(sequenceToHide);
4897 if (isRepresentative)
4899 SequenceI representativeSequence = al.getSequenceAt(s);
4900 hidden.addSequence(representativeSequence, false);
4901 viewport.hideRepSequences(representativeSequence, hidden);
4905 SequenceI[] hseqs = hiddenSeqs
4906 .toArray(new SequenceI[hiddenSeqs.size()]);
4907 viewport.hideSequence(hseqs);
4910 // recover view properties and display parameters
4912 viewport.setShowAnnotation(safeBoolean(view.isShowAnnotation()));
4913 viewport.setAbovePIDThreshold(safeBoolean(view.isPidSelected()));
4914 final int pidThreshold = safeInt(view.getPidThreshold());
4915 viewport.setThreshold(pidThreshold);
4917 viewport.setColourText(safeBoolean(view.isShowColourText()));
4920 .setConservationSelected(
4921 safeBoolean(view.isConservationSelected()));
4922 viewport.setIncrement(safeInt(view.getConsThreshold()));
4923 viewport.setShowJVSuffix(safeBoolean(view.isShowFullId()));
4924 viewport.setRightAlignIds(safeBoolean(view.isRightAlignIds()));
4925 viewport.setFont(new Font(view.getFontName(),
4926 safeInt(view.getFontStyle()), safeInt(view.getFontSize())),
4928 ViewStyleI vs = viewport.getViewStyle();
4929 vs.setScaleProteinAsCdna(view.isScaleProteinAsCdna());
4930 viewport.setViewStyle(vs);
4931 // TODO: allow custom charWidth/Heights to be restored by updating them
4932 // after setting font - which means set above to false
4933 viewport.setRenderGaps(safeBoolean(view.isRenderGaps()));
4934 viewport.setWrapAlignment(safeBoolean(view.isWrapAlignment()));
4935 viewport.setShowAnnotation(safeBoolean(view.isShowAnnotation()));
4937 viewport.setShowBoxes(safeBoolean(view.isShowBoxes()));
4939 viewport.setShowText(safeBoolean(view.isShowText()));
4941 viewport.setTextColour(new Color(safeInt(view.getTextCol1())));
4942 viewport.setTextColour2(new Color(safeInt(view.getTextCol2())));
4943 viewport.setThresholdTextColour(safeInt(view.getTextColThreshold()));
4944 viewport.setShowUnconserved(view.isShowUnconserved());
4945 viewport.getRanges().setStartRes(safeInt(view.getStartRes()));
4947 if (view.getViewName() != null)
4949 viewport.setViewName(view.getViewName());
4950 af.setInitialTabVisible();
4952 af.setBounds(safeInt(view.getXpos()), safeInt(view.getYpos()),
4953 safeInt(view.getWidth()), safeInt(view.getHeight()));
4954 // startSeq set in af.alignPanel.updateLayout below
4955 af.alignPanel.updateLayout();
4956 ColourSchemeI cs = null;
4957 // apply colourschemes
4958 if (view.getBgColour() != null)
4960 if (view.getBgColour().startsWith("ucs"))
4962 cs = getUserColourScheme(jm, view.getBgColour());
4964 else if (view.getBgColour().startsWith("Annotation"))
4966 AnnotationColourScheme viewAnnColour = view.getAnnotationColours();
4967 cs = constructAnnotationColour(viewAnnColour, af, al, jm, true);
4974 cs = ColourSchemeProperty.getColourScheme(af.getViewport(), al,
4975 view.getBgColour());
4980 * turn off 'alignment colour applies to all groups'
4981 * while restoring global colour scheme
4983 viewport.setColourAppliesToAllGroups(false);
4984 viewport.setGlobalColourScheme(cs);
4985 viewport.getResidueShading().setThreshold(pidThreshold,
4986 view.isIgnoreGapsinConsensus());
4987 viewport.getResidueShading()
4988 .setConsensus(viewport.getSequenceConsensusHash());
4989 if (safeBoolean(view.isConservationSelected()) && cs != null)
4991 viewport.getResidueShading()
4992 .setConservationInc(safeInt(view.getConsThreshold()));
4994 af.changeColour(cs);
4995 viewport.setColourAppliesToAllGroups(true);
4998 .setShowSequenceFeatures(
4999 safeBoolean(view.isShowSequenceFeatures()));
5001 viewport.setCentreColumnLabels(view.isCentreColumnLabels());
5002 viewport.setIgnoreGapsConsensus(view.isIgnoreGapsinConsensus(), null);
5003 viewport.setFollowHighlight(view.isFollowHighlight());
5004 viewport.followSelection = view.isFollowSelection();
5005 viewport.setShowConsensusHistogram(view.isShowConsensusHistogram());
5006 viewport.setShowSequenceLogo(view.isShowSequenceLogo());
5007 viewport.setNormaliseSequenceLogo(view.isNormaliseSequenceLogo());
5008 viewport.setShowDBRefs(safeBoolean(view.isShowDbRefTooltip()));
5009 viewport.setShowNPFeats(safeBoolean(view.isShowNPfeatureTooltip()));
5010 viewport.setShowGroupConsensus(view.isShowGroupConsensus());
5011 viewport.setShowGroupConservation(view.isShowGroupConservation());
5013 // recover feature settings
5014 if (jm.getFeatureSettings() != null)
5016 FeatureRenderer fr = af.alignPanel.getSeqPanel().seqCanvas
5017 .getFeatureRenderer();
5018 FeaturesDisplayed fdi;
5019 viewport.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
5020 String[] renderOrder = new String[jm.getFeatureSettings()
5021 .getSetting().size()];
5022 Map<String, FeatureColourI> featureColours = new Hashtable<>();
5023 Map<String, Float> featureOrder = new Hashtable<>();
5025 for (int fs = 0; fs < jm.getFeatureSettings()
5026 .getSetting().size(); fs++)
5028 Setting setting = jm.getFeatureSettings().getSetting().get(fs);
5029 String featureType = setting.getType();
5032 * restore feature filters (if any)
5034 jalview.xml.binding.jalview.FeatureMatcherSet filters = setting
5036 if (filters != null)
5038 FeatureMatcherSetI filter = Jalview2XML
5039 .parseFilter(featureType, filters);
5040 if (!filter.isEmpty())
5042 fr.setFeatureFilter(featureType, filter);
5047 * restore feature colour scheme
5049 Color maxColour = new Color(setting.getColour());
5050 if (setting.getMincolour() != null)
5053 * minColour is always set unless a simple colour
5054 * (including for colour by label though it doesn't use it)
5056 Color minColour = new Color(setting.getMincolour().intValue());
5057 Color noValueColour = minColour;
5058 NoValueColour noColour = setting.getNoValueColour();
5059 if (noColour == NoValueColour.NONE)
5061 noValueColour = null;
5063 else if (noColour == NoValueColour.MAX)
5065 noValueColour = maxColour;
5067 float min = safeFloat(safeFloat(setting.getMin()));
5068 float max = setting.getMax() == null ? 1f
5069 : setting.getMax().floatValue();
5070 FeatureColourI gc = new FeatureColour(maxColour, minColour,
5072 noValueColour, min, max);
5073 if (setting.getAttributeName().size() > 0)
5075 gc.setAttributeName(setting.getAttributeName().toArray(
5076 new String[setting.getAttributeName().size()]));
5078 if (setting.getThreshold() != null)
5080 gc.setThreshold(setting.getThreshold().floatValue());
5081 int threshstate = safeInt(setting.getThreshstate());
5082 // -1 = None, 0 = Below, 1 = Above threshold
5083 if (threshstate == 0)
5085 gc.setBelowThreshold(true);
5087 else if (threshstate == 1)
5089 gc.setAboveThreshold(true);
5092 gc.setAutoScaled(true); // default
5093 if (setting.isAutoScale() != null)
5095 gc.setAutoScaled(setting.isAutoScale());
5097 if (setting.isColourByLabel() != null)
5099 gc.setColourByLabel(setting.isColourByLabel());
5101 // and put in the feature colour table.
5102 featureColours.put(featureType, gc);
5106 featureColours.put(featureType,
5107 new FeatureColour(maxColour));
5109 renderOrder[fs] = featureType;
5110 if (setting.getOrder() != null)
5112 featureOrder.put(featureType, setting.getOrder().floatValue());
5116 featureOrder.put(featureType, Float.valueOf(
5117 fs / jm.getFeatureSettings().getSetting().size()));
5119 if (safeBoolean(setting.isDisplay()))
5121 fdi.setVisible(featureType);
5124 Map<String, Boolean> fgtable = new Hashtable<>();
5125 for (int gs = 0; gs < jm.getFeatureSettings().getGroup().size(); gs++)
5127 Group grp = jm.getFeatureSettings().getGroup().get(gs);
5128 fgtable.put(grp.getName(), Boolean.valueOf(grp.isDisplay()));
5130 // FeatureRendererSettings frs = new FeatureRendererSettings(renderOrder,
5131 // fgtable, featureColours, jms.getFeatureSettings().hasTransparency() ?
5132 // jms.getFeatureSettings().getTransparency() : 0.0, featureOrder);
5133 FeatureRendererSettings frs = new FeatureRendererSettings(renderOrder,
5134 fgtable, featureColours, 1.0f, featureOrder);
5135 fr.transferSettings(frs);
5138 if (view.getHiddenColumns().size() > 0)
5140 for (int c = 0; c < view.getHiddenColumns().size(); c++)
5142 final HiddenColumns hc = view.getHiddenColumns().get(c);
5143 viewport.hideColumns(safeInt(hc.getStart()),
5144 safeInt(hc.getEnd()) /* +1 */);
5147 if (view.getCalcIdParam() != null)
5149 for (CalcIdParam calcIdParam : view.getCalcIdParam())
5151 if (calcIdParam != null)
5153 if (recoverCalcIdParam(calcIdParam, viewport))
5158 warn("Couldn't recover parameters for "
5159 + calcIdParam.getCalcId());
5164 af.setMenusFromViewport(viewport);
5165 af.setTitle(view.getTitle());
5166 // TODO: we don't need to do this if the viewport is aready visible.
5168 * Add the AlignFrame to the desktop (it may be 'gathered' later), unless it
5169 * has a 'cdna/protein complement' view, in which case save it in order to
5170 * populate a SplitFrame once all views have been read in.
5172 String complementaryViewId = view.getComplementId();
5173 if (complementaryViewId == null)
5175 Desktop.addInternalFrame(af, view.getTitle(),
5176 safeInt(view.getWidth()), safeInt(view.getHeight()));
5177 // recompute any autoannotation
5178 af.alignPanel.updateAnnotation(false, true);
5179 reorderAutoannotation(af, al, autoAlan);
5180 af.alignPanel.alignmentChanged();
5184 splitFrameCandidates.put(view, af);
5190 * Reads saved data to restore Colour by Annotation settings
5192 * @param viewAnnColour
5196 * @param checkGroupAnnColour
5199 private ColourSchemeI constructAnnotationColour(
5200 AnnotationColourScheme viewAnnColour, AlignFrame af,
5201 AlignmentI al, JalviewModel model, boolean checkGroupAnnColour)
5203 boolean propagateAnnColour = false;
5204 AlignmentI annAlignment = af != null ? af.getViewport().getAlignment()
5206 if (checkGroupAnnColour && al.getGroups() != null
5207 && al.getGroups().size() > 0)
5209 // pre 2.8.1 behaviour
5210 // check to see if we should transfer annotation colours
5211 propagateAnnColour = true;
5212 for (SequenceGroup sg : al.getGroups())
5214 if (sg.getColourScheme() instanceof AnnotationColourGradient)
5216 propagateAnnColour = false;
5222 * 2.10.2- : saved annotationId is AlignmentAnnotation.annotationId
5224 String annotationId = viewAnnColour.getAnnotation();
5225 AlignmentAnnotation matchedAnnotation = annotationIds.get(annotationId);
5228 * pre 2.10.2: saved annotationId is AlignmentAnnotation.label
5230 if (matchedAnnotation == null
5231 && annAlignment.getAlignmentAnnotation() != null)
5233 for (int i = 0; i < annAlignment.getAlignmentAnnotation().length; i++)
5236 .equals(annAlignment.getAlignmentAnnotation()[i].label))
5238 matchedAnnotation = annAlignment.getAlignmentAnnotation()[i];
5243 if (matchedAnnotation == null)
5245 System.err.println("Failed to match annotation colour scheme for "
5249 if (matchedAnnotation.getThreshold() == null)
5251 matchedAnnotation.setThreshold(
5252 new GraphLine(safeFloat(viewAnnColour.getThreshold()),
5253 "Threshold", Color.black));
5256 AnnotationColourGradient cs = null;
5257 if (viewAnnColour.getColourScheme().equals("None"))
5259 cs = new AnnotationColourGradient(matchedAnnotation,
5260 new Color(safeInt(viewAnnColour.getMinColour())),
5261 new Color(safeInt(viewAnnColour.getMaxColour())),
5262 safeInt(viewAnnColour.getAboveThreshold()));
5264 else if (viewAnnColour.getColourScheme().startsWith("ucs"))
5266 cs = new AnnotationColourGradient(matchedAnnotation,
5267 getUserColourScheme(model, viewAnnColour.getColourScheme()),
5268 safeInt(viewAnnColour.getAboveThreshold()));
5272 cs = new AnnotationColourGradient(matchedAnnotation,
5273 ColourSchemeProperty.getColourScheme(af.getViewport(), al,
5274 viewAnnColour.getColourScheme()),
5275 safeInt(viewAnnColour.getAboveThreshold()));
5278 boolean perSequenceOnly = safeBoolean(viewAnnColour.isPerSequence());
5279 boolean useOriginalColours = safeBoolean(
5280 viewAnnColour.isPredefinedColours());
5281 cs.setSeqAssociated(perSequenceOnly);
5282 cs.setPredefinedColours(useOriginalColours);
5284 if (propagateAnnColour && al.getGroups() != null)
5286 // Also use these settings for all the groups
5287 for (int g = 0; g < al.getGroups().size(); g++)
5289 SequenceGroup sg = al.getGroups().get(g);
5290 if (sg.getGroupColourScheme() == null)
5295 AnnotationColourGradient groupScheme = new AnnotationColourGradient(
5296 matchedAnnotation, sg.getColourScheme(),
5297 safeInt(viewAnnColour.getAboveThreshold()));
5298 sg.setColourScheme(groupScheme);
5299 groupScheme.setSeqAssociated(perSequenceOnly);
5300 groupScheme.setPredefinedColours(useOriginalColours);
5306 private void reorderAutoannotation(AlignFrame af, AlignmentI al,
5307 List<JvAnnotRow> autoAlan)
5309 // copy over visualization settings for autocalculated annotation in the
5311 if (al.getAlignmentAnnotation() != null)
5314 * Kludge for magic autoannotation names (see JAL-811)
5316 String[] magicNames = new String[] { "Consensus", "Quality",
5318 JvAnnotRow nullAnnot = new JvAnnotRow(-1, null);
5319 Hashtable<String, JvAnnotRow> visan = new Hashtable<>();
5320 for (String nm : magicNames)
5322 visan.put(nm, nullAnnot);
5324 for (JvAnnotRow auan : autoAlan)
5326 visan.put(auan.template.label
5327 + (auan.template.getCalcId() == null ? ""
5328 : "\t" + auan.template.getCalcId()),
5331 int hSize = al.getAlignmentAnnotation().length;
5332 List<JvAnnotRow> reorder = new ArrayList<>();
5333 // work through any autoCalculated annotation already on the view
5334 // removing it if it should be placed in a different location on the
5335 // annotation panel.
5336 List<String> remains = new ArrayList<>(visan.keySet());
5337 for (int h = 0; h < hSize; h++)
5339 jalview.datamodel.AlignmentAnnotation jalan = al
5340 .getAlignmentAnnotation()[h];
5341 if (jalan.autoCalculated)
5344 JvAnnotRow valan = visan.get(k = jalan.label);
5345 if (jalan.getCalcId() != null)
5347 valan = visan.get(k = jalan.label + "\t" + jalan.getCalcId());
5352 // delete the auto calculated row from the alignment
5353 al.deleteAnnotation(jalan, false);
5357 if (valan != nullAnnot)
5359 if (jalan != valan.template)
5361 // newly created autoannotation row instance
5362 // so keep a reference to the visible annotation row
5363 // and copy over all relevant attributes
5364 if (valan.template.graphHeight >= 0)
5367 jalan.graphHeight = valan.template.graphHeight;
5369 jalan.visible = valan.template.visible;
5371 reorder.add(new JvAnnotRow(valan.order, jalan));
5376 // Add any (possibly stale) autocalculated rows that were not appended to
5377 // the view during construction
5378 for (String other : remains)
5380 JvAnnotRow othera = visan.get(other);
5381 if (othera != nullAnnot && othera.template.getCalcId() != null
5382 && othera.template.getCalcId().length() > 0)
5384 reorder.add(othera);
5387 // now put the automatic annotation in its correct place
5388 int s = 0, srt[] = new int[reorder.size()];
5389 JvAnnotRow[] rws = new JvAnnotRow[reorder.size()];
5390 for (JvAnnotRow jvar : reorder)
5393 srt[s++] = jvar.order;
5396 jalview.util.QuickSort.sort(srt, rws);
5397 // and re-insert the annotation at its correct position
5398 for (JvAnnotRow jvar : rws)
5400 al.addAnnotation(jvar.template, jvar.order);
5402 af.alignPanel.adjustAnnotationHeight();
5406 Hashtable skipList = null;
5409 * TODO remove this method
5412 * @return AlignFrame bound to sequenceSetId from view, if one exists. private
5413 * AlignFrame getSkippedFrame(Viewport view) { if (skipList==null) {
5414 * throw new Error("Implementation Error. No skipList defined for this
5415 * Jalview2XML instance."); } return (AlignFrame)
5416 * skipList.get(view.getSequenceSetId()); }
5420 * Check if the Jalview view contained in object should be skipped or not.
5423 * @return true if view's sequenceSetId is a key in skipList
5425 private boolean skipViewport(JalviewModel object)
5427 if (skipList == null)
5431 String id = object.getViewport().get(0).getSequenceSetId();
5432 if (skipList.containsKey(id))
5434 if (Cache.log != null && Cache.log.isDebugEnabled())
5436 Cache.log.debug("Skipping seuqence set id " + id);
5443 public void addToSkipList(AlignFrame af)
5445 if (skipList == null)
5447 skipList = new Hashtable();
5449 skipList.put(af.getViewport().getSequenceSetId(), af);
5452 public void clearSkipList()
5454 if (skipList != null)
5461 private void recoverDatasetFor(SequenceSet vamsasSet, AlignmentI al,
5462 boolean ignoreUnrefed, String uniqueSeqSetId)
5464 jalview.datamodel.AlignmentI ds = getDatasetFor(
5465 vamsasSet.getDatasetId());
5466 AlignmentI xtant_ds = ds;
5467 if (xtant_ds == null)
5469 // good chance we are about to create a new dataset, but check if we've
5470 // seen some of the dataset sequence IDs before.
5471 // TODO: skip this check if we are working with project generated by
5472 // version 2.11 or later
5473 xtant_ds = checkIfHasDataset(vamsasSet.getSequence());
5474 if (xtant_ds != null)
5477 addDatasetRef(vamsasSet.getDatasetId(), ds);
5480 Vector dseqs = null;
5483 // recovering an alignment View
5484 AlignmentI seqSetDS = getDatasetFor(UNIQSEQSETID + uniqueSeqSetId);
5485 if (seqSetDS != null)
5487 if (ds != null && ds != seqSetDS)
5489 warn("JAL-3171 regression: Overwriting a dataset reference for an alignment"
5490 + " - CDS/Protein crossreference data may be lost");
5491 if (xtant_ds != null)
5493 // This can only happen if the unique sequence set ID was bound to a
5494 // dataset that did not contain any of the sequences in the view
5495 // currently being restored.
5496 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.");
5500 addDatasetRef(vamsasSet.getDatasetId(), ds);
5505 // try even harder to restore dataset
5506 AlignmentI xtantDS = checkIfHasDataset(vamsasSet.getSequence());
5507 // create a list of new dataset sequences
5508 dseqs = new Vector();
5510 for (int i = 0, iSize = vamsasSet.getSequence().size(); i < iSize; i++)
5512 Sequence vamsasSeq = vamsasSet.getSequence().get(i);
5513 ensureJalviewDatasetSequence(vamsasSeq, ds, dseqs, ignoreUnrefed, i);
5515 // create a new dataset
5518 SequenceI[] dsseqs = new SequenceI[dseqs.size()];
5519 dseqs.copyInto(dsseqs);
5520 ds = new jalview.datamodel.Alignment(dsseqs);
5521 debug("Created new dataset " + vamsasSet.getDatasetId()
5522 + " for alignment " + System.identityHashCode(al));
5523 addDatasetRef(vamsasSet.getDatasetId(), ds);
5525 // set the dataset for the newly imported alignment.
5526 if (al.getDataset() == null && !ignoreUnrefed)
5529 // register dataset for the alignment's uniqueSeqSetId for legacy projects
5530 addDatasetRef(UNIQSEQSETID + uniqueSeqSetId, ds);
5532 updateSeqDatasetBinding(vamsasSet.getSequence(), ds);
5536 * XML dataset sequence ID to materialised dataset reference
5538 HashMap<String, AlignmentI> seqToDataset = new HashMap<>();
5541 * @return the first materialised dataset reference containing a dataset
5542 * sequence referenced in the given view
5544 * - sequences from the view
5546 AlignmentI checkIfHasDataset(List<Sequence> list)
5548 for (Sequence restoredSeq : list)
5550 AlignmentI datasetFor = seqToDataset.get(restoredSeq.getDsseqid());
5551 if (datasetFor != null)
5560 * Register ds as the containing dataset for the dataset sequences referenced
5561 * by sequences in list
5564 * - sequences in a view
5567 void updateSeqDatasetBinding(List<Sequence> list, AlignmentI ds)
5569 for (Sequence restoredSeq : list)
5571 AlignmentI prevDS = seqToDataset.put(restoredSeq.getDsseqid(), ds);
5572 if (prevDS != null && prevDS != ds)
5574 warn("Dataset sequence appears in many datasets: "
5575 + restoredSeq.getDsseqid());
5576 // TODO: try to merge!
5583 * sequence definition to create/merge dataset sequence for
5587 * vector to add new dataset sequence to
5588 * @param ignoreUnrefed
5589 * - when true, don't create new sequences from vamsasSeq if it's id
5590 * doesn't already have an asssociated Jalview sequence.
5592 * - used to reorder the sequence in the alignment according to the
5593 * vamsasSeq array ordering, to preserve ordering of dataset
5595 private void ensureJalviewDatasetSequence(Sequence vamsasSeq,
5596 AlignmentI ds, Vector dseqs, boolean ignoreUnrefed, int vseqpos)
5598 // JBP TODO: Check this is called for AlCodonFrames to support recovery of
5600 SequenceI sq = seqRefIds.get(vamsasSeq.getId());
5601 boolean reorder = false;
5602 SequenceI dsq = null;
5603 if (sq != null && sq.getDatasetSequence() != null)
5605 dsq = sq.getDatasetSequence();
5611 if (sq == null && ignoreUnrefed)
5615 String sqid = vamsasSeq.getDsseqid();
5618 // need to create or add a new dataset sequence reference to this sequence
5621 dsq = seqRefIds.get(sqid);
5626 // make a new dataset sequence
5627 dsq = sq.createDatasetSequence();
5630 // make up a new dataset reference for this sequence
5631 sqid = seqHash(dsq);
5633 dsq.setVamsasId(uniqueSetSuffix + sqid);
5634 seqRefIds.put(sqid, dsq);
5639 dseqs.addElement(dsq);
5644 ds.addSequence(dsq);
5650 { // make this dataset sequence sq's dataset sequence
5651 sq.setDatasetSequence(dsq);
5652 // and update the current dataset alignment
5657 if (!dseqs.contains(dsq))
5664 if (ds.findIndex(dsq) < 0)
5666 ds.addSequence(dsq);
5673 // TODO: refactor this as a merge dataset sequence function
5674 // now check that sq (the dataset sequence) sequence really is the union of
5675 // all references to it
5676 // boolean pre = sq.getStart() < dsq.getStart();
5677 // boolean post = sq.getEnd() > dsq.getEnd();
5681 // StringBuffer sb = new StringBuffer();
5682 String newres = jalview.analysis.AlignSeq.extractGaps(
5683 jalview.util.Comparison.GapChars, sq.getSequenceAsString());
5684 if (!newres.equalsIgnoreCase(dsq.getSequenceAsString())
5685 && newres.length() > dsq.getLength())
5687 // Update with the longer sequence.
5691 * if (pre) { sb.insert(0, newres .substring(0, dsq.getStart() -
5692 * sq.getStart())); dsq.setStart(sq.getStart()); } if (post) {
5693 * sb.append(newres.substring(newres.length() - sq.getEnd() -
5694 * dsq.getEnd())); dsq.setEnd(sq.getEnd()); }
5696 dsq.setSequence(newres);
5698 // TODO: merges will never happen if we 'know' we have the real dataset
5699 // sequence - this should be detected when id==dssid
5701 "DEBUG Notice: Merged dataset sequence (if you see this often, post at http://issues.jalview.org/browse/JAL-1474)"); // ("
5702 // + (pre ? "prepended" : "") + " "
5703 // + (post ? "appended" : ""));
5708 // sequence refs are identical. We may need to update the existing dataset
5709 // alignment with this one, though.
5710 if (ds != null && dseqs == null)
5712 int opos = ds.findIndex(dsq);
5713 SequenceI tseq = null;
5714 if (opos != -1 && vseqpos != opos)
5716 // remove from old position
5717 ds.deleteSequence(dsq);
5719 if (vseqpos < ds.getHeight())
5721 if (vseqpos != opos)
5723 // save sequence at destination position
5724 tseq = ds.getSequenceAt(vseqpos);
5725 ds.replaceSequenceAt(vseqpos, dsq);
5726 ds.addSequence(tseq);
5731 ds.addSequence(dsq);
5738 * TODO use AlignmentI here and in related methods - needs
5739 * AlignmentI.getDataset() changed to return AlignmentI instead of Alignment
5741 Hashtable<String, AlignmentI> datasetIds = null;
5743 IdentityHashMap<AlignmentI, String> dataset2Ids = null;
5745 private AlignmentI getDatasetFor(String datasetId)
5747 if (datasetIds == null)
5749 datasetIds = new Hashtable<>();
5752 if (datasetIds.containsKey(datasetId))
5754 return datasetIds.get(datasetId);
5759 private void addDatasetRef(String datasetId, AlignmentI dataset)
5761 if (datasetIds == null)
5763 datasetIds = new Hashtable<>();
5765 datasetIds.put(datasetId, dataset);
5769 * make a new dataset ID for this jalview dataset alignment
5774 private String getDatasetIdRef(AlignmentI dataset)
5776 if (dataset.getDataset() != null)
5778 warn("Serious issue! Dataset Object passed to getDatasetIdRef is not a Jalview DATASET alignment...");
5780 String datasetId = makeHashCode(dataset, null);
5781 if (datasetId == null)
5783 // make a new datasetId and record it
5784 if (dataset2Ids == null)
5786 dataset2Ids = new IdentityHashMap<>();
5790 datasetId = dataset2Ids.get(dataset);
5792 if (datasetId == null)
5794 datasetId = "ds" + dataset2Ids.size() + 1;
5795 dataset2Ids.put(dataset, datasetId);
5801 private void addDBRefs(SequenceI datasetSequence, Sequence sequence)
5803 for (int d = 0; d < sequence.getDBRef().size(); d++)
5805 DBRef dr = sequence.getDBRef().get(d);
5806 jalview.datamodel.DBRefEntry entry = new jalview.datamodel.DBRefEntry(
5807 dr.getSource(), dr.getVersion(), dr.getAccessionId());
5808 if (dr.getMapping() != null)
5810 entry.setMap(addMapping(dr.getMapping()));
5812 datasetSequence.addDBRef(entry);
5816 private jalview.datamodel.Mapping addMapping(Mapping m)
5818 SequenceI dsto = null;
5819 // Mapping m = dr.getMapping();
5820 int fr[] = new int[m.getMapListFrom().size() * 2];
5821 Iterator<MapListFrom> from = m.getMapListFrom().iterator();// enumerateMapListFrom();
5822 for (int _i = 0; from.hasNext(); _i += 2)
5824 MapListFrom mf = from.next();
5825 fr[_i] = mf.getStart();
5826 fr[_i + 1] = mf.getEnd();
5828 int fto[] = new int[m.getMapListTo().size() * 2];
5829 Iterator<MapListTo> to = m.getMapListTo().iterator();// enumerateMapListTo();
5830 for (int _i = 0; to.hasNext(); _i += 2)
5832 MapListTo mf = to.next();
5833 fto[_i] = mf.getStart();
5834 fto[_i + 1] = mf.getEnd();
5836 jalview.datamodel.Mapping jmap = new jalview.datamodel.Mapping(dsto, fr,
5837 fto, m.getMapFromUnit().intValue(),
5838 m.getMapToUnit().intValue());
5841 * (optional) choice of dseqFor or Sequence
5843 if (m.getDseqFor() != null)
5845 String dsfor = m.getDseqFor();
5846 if (seqRefIds.containsKey(dsfor))
5851 jmap.setTo(seqRefIds.get(dsfor));
5855 frefedSequence.add(newMappingRef(dsfor, jmap));
5858 else if (m.getSequence() != null)
5861 * local sequence definition
5863 Sequence ms = m.getSequence();
5864 SequenceI djs = null;
5865 String sqid = ms.getDsseqid();
5866 if (sqid != null && sqid.length() > 0)
5869 * recover dataset sequence
5871 djs = seqRefIds.get(sqid);
5876 "Warning - making up dataset sequence id for DbRef sequence map reference");
5877 sqid = ((Object) ms).toString(); // make up a new hascode for
5878 // undefined dataset sequence hash
5879 // (unlikely to happen)
5885 * make a new dataset sequence and add it to refIds hash
5887 djs = new jalview.datamodel.Sequence(ms.getName(),
5889 djs.setStart(jmap.getMap().getToLowest());
5890 djs.setEnd(jmap.getMap().getToHighest());
5891 djs.setVamsasId(uniqueSetSuffix + sqid);
5893 incompleteSeqs.put(sqid, djs);
5894 seqRefIds.put(sqid, djs);
5897 jalview.bin.Cache.log.debug("about to recurse on addDBRefs.");
5906 * Provides a 'copy' of an alignment view (on action New View) by 'saving' the
5907 * view as XML (but not to file), and then reloading it
5912 public AlignmentPanel copyAlignPanel(AlignmentPanel ap)
5915 JalviewModel jm = saveState(ap, null, null, null);
5918 jm.getVamsasModel().getSequenceSet().get(0).getDatasetId(),
5919 ap.getAlignment().getDataset());
5921 uniqueSetSuffix = "";
5922 // jm.getJalviewModelSequence().getViewport(0).setId(null);
5923 jm.getViewport().get(0).setId(null);
5924 // we don't overwrite the view we just copied
5926 if (this.frefedSequence == null)
5928 frefedSequence = new Vector<>();
5931 viewportsAdded.clear();
5933 AlignFrame af = loadFromObject(jm, null, false, null);
5934 af.getAlignPanels().clear();
5935 af.closeMenuItem_actionPerformed(true);
5938 * if(ap.av.getAlignment().getAlignmentAnnotation()!=null) { for(int i=0;
5939 * i<ap.av.getAlignment().getAlignmentAnnotation().length; i++) {
5940 * if(!ap.av.getAlignment().getAlignmentAnnotation()[i].autoCalculated) {
5941 * af.alignPanel.av.getAlignment().getAlignmentAnnotation()[i] =
5942 * ap.av.getAlignment().getAlignmentAnnotation()[i]; } } }
5945 return af.alignPanel;
5948 private Hashtable jvids2vobj;
5950 private void warn(String msg)
5955 private void warn(String msg, Exception e)
5957 if (Cache.log != null)
5961 Cache.log.warn(msg, e);
5965 Cache.log.warn(msg);
5970 System.err.println("Warning: " + msg);
5973 e.printStackTrace();
5978 private void debug(String string)
5980 debug(string, null);
5983 private void debug(String msg, Exception e)
5985 if (Cache.log != null)
5989 Cache.log.debug(msg, e);
5993 Cache.log.debug(msg);
5998 System.err.println("Warning: " + msg);
6001 e.printStackTrace();
6007 * set the object to ID mapping tables used to write/recover objects and XML
6008 * ID strings for the jalview project. If external tables are provided then
6009 * finalize and clearSeqRefs will not clear the tables when the Jalview2XML
6010 * object goes out of scope. - also populates the datasetIds hashtable with
6011 * alignment objects containing dataset sequences
6014 * Map from ID strings to jalview datamodel
6016 * Map from jalview datamodel to ID strings
6020 public void setObjectMappingTables(Hashtable vobj2jv,
6021 IdentityHashMap jv2vobj)
6023 this.jv2vobj = jv2vobj;
6024 this.vobj2jv = vobj2jv;
6025 Iterator ds = jv2vobj.keySet().iterator();
6027 while (ds.hasNext())
6029 Object jvobj = ds.next();
6030 id = jv2vobj.get(jvobj).toString();
6031 if (jvobj instanceof jalview.datamodel.Alignment)
6033 if (((jalview.datamodel.Alignment) jvobj).getDataset() == null)
6035 addDatasetRef(id, (jalview.datamodel.Alignment) jvobj);
6038 else if (jvobj instanceof jalview.datamodel.Sequence)
6040 // register sequence object so the XML parser can recover it.
6041 if (seqRefIds == null)
6043 seqRefIds = new HashMap<>();
6045 if (seqsToIds == null)
6047 seqsToIds = new IdentityHashMap<>();
6049 seqRefIds.put(jv2vobj.get(jvobj).toString(), (SequenceI) jvobj);
6050 seqsToIds.put((SequenceI) jvobj, id);
6052 else if (jvobj instanceof jalview.datamodel.AlignmentAnnotation)
6055 AlignmentAnnotation jvann = (AlignmentAnnotation) jvobj;
6056 annotationIds.put(anid = jv2vobj.get(jvobj).toString(), jvann);
6057 if (jvann.annotationId == null)
6059 jvann.annotationId = anid;
6061 if (!jvann.annotationId.equals(anid))
6063 // TODO verify that this is the correct behaviour
6064 this.warn("Overriding Annotation ID for " + anid
6065 + " from different id : " + jvann.annotationId);
6066 jvann.annotationId = anid;
6069 else if (jvobj instanceof String)
6071 if (jvids2vobj == null)
6073 jvids2vobj = new Hashtable();
6074 jvids2vobj.put(jvobj, jv2vobj.get(jvobj).toString());
6079 Cache.log.debug("Ignoring " + jvobj.getClass() + " (ID = " + id);
6085 * set the uniqueSetSuffix used to prefix/suffix object IDs for jalview
6086 * objects created from the project archive. If string is null (default for
6087 * construction) then suffix will be set automatically.
6091 public void setUniqueSetSuffix(String string)
6093 uniqueSetSuffix = string;
6098 * uses skipList2 as the skipList for skipping views on sequence sets
6099 * associated with keys in the skipList
6103 public void setSkipList(Hashtable skipList2)
6105 skipList = skipList2;
6109 * Reads the jar entry of given name and returns its contents, or null if the
6110 * entry is not found.
6113 * @param jarEntryName
6116 protected String readJarEntry(jarInputStreamProvider jprovider,
6117 String jarEntryName)
6119 String result = null;
6120 BufferedReader in = null;
6125 * Reopen the jar input stream and traverse its entries to find a matching
6128 JarInputStream jin = jprovider.getJarInputStream();
6129 JarEntry entry = null;
6132 entry = jin.getNextJarEntry();
6133 } while (entry != null && !entry.getName().equals(jarEntryName));
6137 StringBuilder out = new StringBuilder(256);
6138 in = new BufferedReader(new InputStreamReader(jin, UTF_8));
6141 while ((data = in.readLine()) != null)
6145 result = out.toString();
6149 warn("Couldn't find entry in Jalview Jar for " + jarEntryName);
6151 } catch (Exception ex)
6153 ex.printStackTrace();
6161 } catch (IOException e)
6172 * Returns an incrementing counter (0, 1, 2...)
6176 private synchronized int nextCounter()
6182 * Loads any saved PCA viewers
6187 protected void loadPCAViewers(JalviewModel model, AlignmentPanel ap)
6191 List<PcaViewer> pcaviewers = model.getPcaViewer();
6192 for (PcaViewer viewer : pcaviewers)
6194 String modelName = viewer.getScoreModelName();
6195 SimilarityParamsI params = new SimilarityParams(
6196 viewer.isIncludeGappedColumns(), viewer.isMatchGaps(),
6197 viewer.isIncludeGaps(),
6198 viewer.isDenominateByShortestLength());
6201 * create the panel (without computing the PCA)
6203 PCAPanel panel = new PCAPanel(ap, modelName, params);
6205 panel.setTitle(viewer.getTitle());
6206 panel.setBounds(new Rectangle(viewer.getXpos(), viewer.getYpos(),
6207 viewer.getWidth(), viewer.getHeight()));
6209 boolean showLabels = viewer.isShowLabels();
6210 panel.setShowLabels(showLabels);
6211 panel.getRotatableCanvas().setShowLabels(showLabels);
6212 panel.getRotatableCanvas()
6213 .setBgColour(new Color(viewer.getBgColour()));
6214 panel.getRotatableCanvas()
6215 .setApplyToAllViews(viewer.isLinkToAllViews());
6218 * load PCA output data
6220 ScoreModelI scoreModel = ScoreModels.getInstance()
6221 .getScoreModel(modelName, ap);
6222 PCA pca = new PCA(null, scoreModel, params);
6223 PcaDataType pcaData = viewer.getPcaData();
6225 MatrixI pairwise = loadDoubleMatrix(pcaData.getPairwiseMatrix());
6226 pca.setPairwiseScores(pairwise);
6228 MatrixI triDiag = loadDoubleMatrix(pcaData.getTridiagonalMatrix());
6229 pca.setTridiagonal(triDiag);
6231 MatrixI result = loadDoubleMatrix(pcaData.getEigenMatrix());
6232 pca.setEigenmatrix(result);
6234 panel.getPcaModel().setPCA(pca);
6237 * we haven't saved the input data! (JAL-2647 to do)
6239 panel.setInputData(null);
6242 * add the sequence points for the PCA display
6244 List<jalview.datamodel.SequencePoint> seqPoints = new ArrayList<>();
6245 for (SequencePoint sp : viewer.getSequencePoint())
6247 String seqId = sp.getSequenceRef();
6248 SequenceI seq = seqRefIds.get(seqId);
6251 throw new IllegalStateException(
6252 "Unmatched seqref for PCA: " + seqId);
6254 Point pt = new Point(sp.getXPos(), sp.getYPos(), sp.getZPos());
6255 jalview.datamodel.SequencePoint seqPoint = new jalview.datamodel.SequencePoint(
6257 seqPoints.add(seqPoint);
6259 panel.getRotatableCanvas().setPoints(seqPoints, seqPoints.size());
6262 * set min-max ranges and scale after setPoints (which recomputes them)
6264 panel.getRotatableCanvas().setScaleFactor(viewer.getScaleFactor());
6265 SeqPointMin spMin = viewer.getSeqPointMin();
6266 float[] min = new float[] { spMin.getXPos(), spMin.getYPos(),
6268 SeqPointMax spMax = viewer.getSeqPointMax();
6269 float[] max = new float[] { spMax.getXPos(), spMax.getYPos(),
6271 panel.getRotatableCanvas().setSeqMinMax(min, max);
6273 // todo: hold points list in PCAModel only
6274 panel.getPcaModel().setSequencePoints(seqPoints);
6276 panel.setSelectedDimensionIndex(viewer.getXDim(), X);
6277 panel.setSelectedDimensionIndex(viewer.getYDim(), Y);
6278 panel.setSelectedDimensionIndex(viewer.getZDim(), Z);
6280 // is this duplication needed?
6281 panel.setTop(seqPoints.size() - 1);
6282 panel.getPcaModel().setTop(seqPoints.size() - 1);
6285 * add the axes' end points for the display
6287 for (int i = 0; i < 3; i++)
6289 Axis axis = viewer.getAxis().get(i);
6290 panel.getRotatableCanvas().getAxisEndPoints()[i] = new Point(
6291 axis.getXPos(), axis.getYPos(), axis.getZPos());
6294 Desktop.addInternalFrame(panel, MessageManager.formatMessage(
6295 "label.calc_title", "PCA", modelName), 475, 450);
6297 } catch (Exception ex)
6299 Cache.log.error("Error loading PCA: " + ex.toString());
6304 * Populates an XML model of the feature colour scheme for one feature type
6306 * @param featureType
6310 public static Colour marshalColour(
6311 String featureType, FeatureColourI fcol)
6313 Colour col = new Colour();
6314 if (fcol.isSimpleColour())
6316 col.setRGB(Format.getHexString(fcol.getColour()));
6320 col.setRGB(Format.getHexString(fcol.getMaxColour()));
6321 col.setMin(fcol.getMin());
6322 col.setMax(fcol.getMax());
6323 col.setMinRGB(jalview.util.Format.getHexString(fcol.getMinColour()));
6324 col.setAutoScale(fcol.isAutoScaled());
6325 col.setThreshold(fcol.getThreshold());
6326 col.setColourByLabel(fcol.isColourByLabel());
6327 col.setThreshType(fcol.isAboveThreshold() ? ThresholdType.ABOVE
6328 : (fcol.isBelowThreshold() ? ThresholdType.BELOW
6329 : ThresholdType.NONE));
6330 if (fcol.isColourByAttribute())
6332 final String[] attName = fcol.getAttributeName();
6333 col.getAttributeName().add(attName[0]);
6334 if (attName.length > 1)
6336 col.getAttributeName().add(attName[1]);
6339 Color noColour = fcol.getNoColour();
6340 if (noColour == null)
6342 col.setNoValueColour(NoValueColour.NONE);
6344 else if (noColour == fcol.getMaxColour())
6346 col.setNoValueColour(NoValueColour.MAX);
6350 col.setNoValueColour(NoValueColour.MIN);
6353 col.setName(featureType);
6358 * Populates an XML model of the feature filter(s) for one feature type
6360 * @param firstMatcher
6361 * the first (or only) match condition)
6363 * remaining match conditions (if any)
6365 * if true, conditions are and-ed, else or-ed
6367 public static jalview.xml.binding.jalview.FeatureMatcherSet marshalFilter(
6368 FeatureMatcherI firstMatcher, Iterator<FeatureMatcherI> filters,
6371 jalview.xml.binding.jalview.FeatureMatcherSet result = new jalview.xml.binding.jalview.FeatureMatcherSet();
6373 if (filters.hasNext())
6378 CompoundMatcher compound = new CompoundMatcher();
6379 compound.setAnd(and);
6380 jalview.xml.binding.jalview.FeatureMatcherSet matcher1 = marshalFilter(
6381 firstMatcher, Collections.emptyIterator(), and);
6382 // compound.addMatcherSet(matcher1);
6383 compound.getMatcherSet().add(matcher1);
6384 FeatureMatcherI nextMatcher = filters.next();
6385 jalview.xml.binding.jalview.FeatureMatcherSet matcher2 = marshalFilter(
6386 nextMatcher, filters, and);
6387 // compound.addMatcherSet(matcher2);
6388 compound.getMatcherSet().add(matcher2);
6389 result.setCompoundMatcher(compound);
6394 * single condition matcher
6396 // MatchCondition matcherModel = new MatchCondition();
6397 jalview.xml.binding.jalview.FeatureMatcher matcherModel = new jalview.xml.binding.jalview.FeatureMatcher();
6398 matcherModel.setCondition(
6399 firstMatcher.getMatcher().getCondition().getStableName());
6400 matcherModel.setValue(firstMatcher.getMatcher().getPattern());
6401 if (firstMatcher.isByAttribute())
6403 matcherModel.setBy(FilterBy.BY_ATTRIBUTE);
6404 // matcherModel.setAttributeName(firstMatcher.getAttribute());
6405 String[] attName = firstMatcher.getAttribute();
6406 matcherModel.getAttributeName().add(attName[0]); // attribute
6407 if (attName.length > 1)
6409 matcherModel.getAttributeName().add(attName[1]); // sub-attribute
6412 else if (firstMatcher.isByLabel())
6414 matcherModel.setBy(FilterBy.BY_LABEL);
6416 else if (firstMatcher.isByScore())
6418 matcherModel.setBy(FilterBy.BY_SCORE);
6420 result.setMatchCondition(matcherModel);
6427 * Loads one XML model of a feature filter to a Jalview object
6429 * @param featureType
6430 * @param matcherSetModel
6433 public static FeatureMatcherSetI parseFilter(
6435 jalview.xml.binding.jalview.FeatureMatcherSet matcherSetModel)
6437 FeatureMatcherSetI result = new FeatureMatcherSet();
6440 parseFilterConditions(result, matcherSetModel, true);
6441 } catch (IllegalStateException e)
6443 // mixing AND and OR conditions perhaps
6445 String.format("Error reading filter conditions for '%s': %s",
6446 featureType, e.getMessage()));
6447 // return as much as was parsed up to the error
6454 * Adds feature match conditions to matcherSet as unmarshalled from XML
6455 * (possibly recursively for compound conditions)
6458 * @param matcherSetModel
6460 * if true, multiple conditions are AND-ed, else they are OR-ed
6461 * @throws IllegalStateException
6462 * if AND and OR conditions are mixed
6464 protected static void parseFilterConditions(
6465 FeatureMatcherSetI matcherSet,
6466 jalview.xml.binding.jalview.FeatureMatcherSet matcherSetModel,
6469 jalview.xml.binding.jalview.FeatureMatcher mc = matcherSetModel
6470 .getMatchCondition();
6476 FilterBy filterBy = mc.getBy();
6477 Condition cond = Condition.fromString(mc.getCondition());
6478 String pattern = mc.getValue();
6479 FeatureMatcherI matchCondition = null;
6480 if (filterBy == FilterBy.BY_LABEL)
6482 matchCondition = FeatureMatcher.byLabel(cond, pattern);
6484 else if (filterBy == FilterBy.BY_SCORE)
6486 matchCondition = FeatureMatcher.byScore(cond, pattern);
6489 else if (filterBy == FilterBy.BY_ATTRIBUTE)
6491 final List<String> attributeName = mc.getAttributeName();
6492 String[] attNames = attributeName
6493 .toArray(new String[attributeName.size()]);
6494 matchCondition = FeatureMatcher.byAttribute(cond, pattern,
6499 * note this throws IllegalStateException if AND-ing to a
6500 * previously OR-ed compound condition, or vice versa
6504 matcherSet.and(matchCondition);
6508 matcherSet.or(matchCondition);
6514 * compound condition
6516 List<jalview.xml.binding.jalview.FeatureMatcherSet> matchers = matcherSetModel
6517 .getCompoundMatcher().getMatcherSet();
6518 boolean anded = matcherSetModel.getCompoundMatcher().isAnd();
6519 if (matchers.size() == 2)
6521 parseFilterConditions(matcherSet, matchers.get(0), anded);
6522 parseFilterConditions(matcherSet, matchers.get(1), anded);
6526 System.err.println("Malformed compound filter condition");
6532 * Loads one XML model of a feature colour to a Jalview object
6534 * @param colourModel
6537 public static FeatureColourI parseColour(Colour colourModel)
6539 FeatureColourI colour = null;
6541 if (colourModel.getMax() != null)
6543 Color mincol = null;
6544 Color maxcol = null;
6545 Color noValueColour = null;
6549 mincol = new Color(Integer.parseInt(colourModel.getMinRGB(), 16));
6550 maxcol = new Color(Integer.parseInt(colourModel.getRGB(), 16));
6551 } catch (Exception e)
6553 Cache.log.warn("Couldn't parse out graduated feature color.", e);
6556 NoValueColour noCol = colourModel.getNoValueColour();
6557 if (noCol == NoValueColour.MIN)
6559 noValueColour = mincol;
6561 else if (noCol == NoValueColour.MAX)
6563 noValueColour = maxcol;
6566 colour = new FeatureColour(maxcol, mincol, maxcol, noValueColour,
6567 safeFloat(colourModel.getMin()),
6568 safeFloat(colourModel.getMax()));
6569 final List<String> attributeName = colourModel.getAttributeName();
6570 String[] attributes = attributeName
6571 .toArray(new String[attributeName.size()]);
6572 if (attributes != null && attributes.length > 0)
6574 colour.setAttributeName(attributes);
6576 if (colourModel.isAutoScale() != null)
6578 colour.setAutoScaled(colourModel.isAutoScale().booleanValue());
6580 if (colourModel.isColourByLabel() != null)
6582 colour.setColourByLabel(
6583 colourModel.isColourByLabel().booleanValue());
6585 if (colourModel.getThreshold() != null)
6587 colour.setThreshold(colourModel.getThreshold().floatValue());
6589 ThresholdType ttyp = colourModel.getThreshType();
6590 if (ttyp == ThresholdType.ABOVE)
6592 colour.setAboveThreshold(true);
6594 else if (ttyp == ThresholdType.BELOW)
6596 colour.setBelowThreshold(true);
6601 Color color = new Color(Integer.parseInt(colourModel.getRGB(), 16));
6602 colour = new FeatureColour(color);