2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.project;
23 import static jalview.math.RotatableMatrix.Axis.X;
24 import static jalview.math.RotatableMatrix.Axis.Y;
25 import static jalview.math.RotatableMatrix.Axis.Z;
27 import jalview.analysis.Conservation;
28 import jalview.analysis.PCA;
29 import jalview.analysis.scoremodels.ScoreModels;
30 import jalview.analysis.scoremodels.SimilarityParams;
31 import jalview.api.FeatureColourI;
32 import jalview.api.ViewStyleI;
33 import jalview.api.analysis.ScoreModelI;
34 import jalview.api.analysis.SimilarityParamsI;
35 import jalview.api.structures.JalviewStructureDisplayI;
36 import jalview.bin.Cache;
37 import jalview.datamodel.AlignedCodonFrame;
38 import jalview.datamodel.Alignment;
39 import jalview.datamodel.AlignmentAnnotation;
40 import jalview.datamodel.AlignmentI;
41 import jalview.datamodel.DBRefEntry;
42 import jalview.datamodel.GeneLocus;
43 import jalview.datamodel.GraphLine;
44 import jalview.datamodel.HiddenMarkovModel;
45 import jalview.datamodel.PDBEntry;
46 import jalview.datamodel.Point;
47 import jalview.datamodel.RnaViewerModel;
48 import jalview.datamodel.SequenceFeature;
49 import jalview.datamodel.SequenceGroup;
50 import jalview.datamodel.SequenceI;
51 import jalview.datamodel.StructureViewerModel;
52 import jalview.datamodel.StructureViewerModel.StructureData;
53 import jalview.datamodel.features.FeatureMatcher;
54 import jalview.datamodel.features.FeatureMatcherI;
55 import jalview.datamodel.features.FeatureMatcherSet;
56 import jalview.datamodel.features.FeatureMatcherSetI;
57 import jalview.ext.varna.RnaModel;
58 import jalview.gui.AlignFrame;
59 import jalview.gui.AlignViewport;
60 import jalview.gui.AlignmentPanel;
61 import jalview.gui.AppVarna;
62 import jalview.gui.ChimeraViewFrame;
63 import jalview.gui.Desktop;
64 import jalview.gui.JvOptionPane;
65 import jalview.gui.OOMWarning;
66 import jalview.gui.PCAPanel;
67 import jalview.gui.PaintRefresher;
68 import jalview.gui.SplitFrame;
69 import jalview.gui.StructureViewer;
70 import jalview.gui.StructureViewer.ViewerType;
71 import jalview.gui.StructureViewerBase;
72 import jalview.gui.TreePanel;
73 import jalview.io.BackupFiles;
74 import jalview.io.DataSourceType;
75 import jalview.io.FileFormat;
76 import jalview.io.HMMFile;
77 import jalview.io.NewickFile;
78 import jalview.math.Matrix;
79 import jalview.math.MatrixI;
80 import jalview.renderer.ResidueShaderI;
81 import jalview.schemes.AnnotationColourGradient;
82 import jalview.schemes.ColourSchemeI;
83 import jalview.schemes.ColourSchemeProperty;
84 import jalview.schemes.FeatureColour;
85 import jalview.schemes.ResidueProperties;
86 import jalview.schemes.UserColourScheme;
87 import jalview.structure.StructureSelectionManager;
88 import jalview.structures.models.AAStructureBindingModel;
89 import jalview.util.Format;
90 import jalview.util.MessageManager;
91 import jalview.util.Platform;
92 import jalview.util.StringUtils;
93 import jalview.util.jarInputStreamProvider;
94 import jalview.util.matcher.Condition;
95 import jalview.viewmodel.AlignmentViewport;
96 import jalview.viewmodel.PCAModel;
97 import jalview.viewmodel.ViewportRanges;
98 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
99 import jalview.viewmodel.seqfeatures.FeatureRendererSettings;
100 import jalview.viewmodel.seqfeatures.FeaturesDisplayed;
101 import jalview.ws.api.ServiceWithParameters;
102 import jalview.ws.jws2.PreferredServiceRegistry;
103 import jalview.ws.jws2.dm.AAConSettings;
104 import jalview.ws.params.ArgumentI;
105 import jalview.ws.params.AutoCalcSetting;
106 import jalview.ws.params.WsParamSetI;
107 import jalview.xml.binding.jalview.AlcodonFrame;
108 import jalview.xml.binding.jalview.AlcodonFrame.AlcodMap;
109 import jalview.xml.binding.jalview.Annotation;
110 import jalview.xml.binding.jalview.Annotation.ThresholdLine;
111 import jalview.xml.binding.jalview.AnnotationColourScheme;
112 import jalview.xml.binding.jalview.AnnotationElement;
113 import jalview.xml.binding.jalview.DoubleMatrix;
114 import jalview.xml.binding.jalview.DoubleVector;
115 import jalview.xml.binding.jalview.Feature;
116 import jalview.xml.binding.jalview.Feature.OtherData;
117 import jalview.xml.binding.jalview.FeatureMatcherSet.CompoundMatcher;
118 import jalview.xml.binding.jalview.FilterBy;
119 import jalview.xml.binding.jalview.JalviewModel;
120 import jalview.xml.binding.jalview.JalviewModel.FeatureSettings;
121 import jalview.xml.binding.jalview.JalviewModel.FeatureSettings.Group;
122 import jalview.xml.binding.jalview.JalviewModel.FeatureSettings.Setting;
123 import jalview.xml.binding.jalview.JalviewModel.JGroup;
124 import jalview.xml.binding.jalview.JalviewModel.JSeq;
125 import jalview.xml.binding.jalview.JalviewModel.JSeq.Pdbids;
126 import jalview.xml.binding.jalview.JalviewModel.JSeq.Pdbids.StructureState;
127 import jalview.xml.binding.jalview.JalviewModel.JSeq.RnaViewer;
128 import jalview.xml.binding.jalview.JalviewModel.JSeq.RnaViewer.SecondaryStructure;
129 import jalview.xml.binding.jalview.JalviewModel.PcaViewer;
130 import jalview.xml.binding.jalview.JalviewModel.PcaViewer.Axis;
131 import jalview.xml.binding.jalview.JalviewModel.PcaViewer.SeqPointMax;
132 import jalview.xml.binding.jalview.JalviewModel.PcaViewer.SeqPointMin;
133 import jalview.xml.binding.jalview.JalviewModel.PcaViewer.SequencePoint;
134 import jalview.xml.binding.jalview.JalviewModel.Tree;
135 import jalview.xml.binding.jalview.JalviewModel.UserColours;
136 import jalview.xml.binding.jalview.JalviewModel.Viewport;
137 import jalview.xml.binding.jalview.JalviewModel.Viewport.CalcIdParam;
138 import jalview.xml.binding.jalview.JalviewModel.Viewport.HiddenColumns;
139 import jalview.xml.binding.jalview.JalviewUserColours;
140 import jalview.xml.binding.jalview.JalviewUserColours.Colour;
141 import jalview.xml.binding.jalview.MapListType.MapListFrom;
142 import jalview.xml.binding.jalview.MapListType.MapListTo;
143 import jalview.xml.binding.jalview.Mapping;
144 import jalview.xml.binding.jalview.NoValueColour;
145 import jalview.xml.binding.jalview.ObjectFactory;
146 import jalview.xml.binding.jalview.PcaDataType;
147 import jalview.xml.binding.jalview.Pdbentry.Property;
148 import jalview.xml.binding.jalview.Sequence;
149 import jalview.xml.binding.jalview.Sequence.DBRef;
150 import jalview.xml.binding.jalview.SequenceSet;
151 import jalview.xml.binding.jalview.SequenceSet.SequenceSetProperties;
152 import jalview.xml.binding.jalview.ThresholdType;
153 import jalview.xml.binding.jalview.VAMSAS;
155 import java.awt.Color;
156 import java.awt.Font;
157 import java.awt.Rectangle;
158 import java.io.BufferedReader;
159 import java.io.DataInputStream;
160 import java.io.DataOutputStream;
162 import java.io.FileInputStream;
163 import java.io.FileOutputStream;
164 import java.io.IOException;
165 import java.io.InputStreamReader;
166 import java.io.OutputStreamWriter;
167 import java.io.PrintWriter;
168 import java.lang.reflect.InvocationTargetException;
169 import java.math.BigInteger;
170 import java.net.MalformedURLException;
172 import java.util.ArrayList;
173 import java.util.Arrays;
174 import java.util.Collections;
175 import java.util.Enumeration;
176 import java.util.GregorianCalendar;
177 import java.util.HashMap;
178 import java.util.HashSet;
179 import java.util.Hashtable;
180 import java.util.IdentityHashMap;
181 import java.util.Iterator;
182 import java.util.LinkedHashMap;
183 import java.util.List;
184 import java.util.Map;
185 import java.util.Map.Entry;
186 import java.util.Set;
187 import java.util.Vector;
188 import java.util.jar.JarEntry;
189 import java.util.jar.JarInputStream;
190 import java.util.jar.JarOutputStream;
192 import javax.swing.JInternalFrame;
193 import javax.swing.SwingUtilities;
194 import javax.xml.bind.JAXBContext;
195 import javax.xml.bind.JAXBElement;
196 import javax.xml.bind.Marshaller;
197 import javax.xml.datatype.DatatypeConfigurationException;
198 import javax.xml.datatype.DatatypeFactory;
199 import javax.xml.datatype.XMLGregorianCalendar;
200 import javax.xml.stream.XMLInputFactory;
201 import javax.xml.stream.XMLStreamReader;
204 * Write out the current jalview desktop state as a Jalview XML stream.
206 * Note: the vamsas objects referred to here are primitive versions of the
207 * VAMSAS project schema elements - they are not the same and most likely never
211 * @version $Revision: 1.134 $
213 public class Jalview2XML
215 private static final String VIEWER_PREFIX = "viewer_";
217 private static final String RNA_PREFIX = "rna_";
219 private static final String HMMER_PREFIX = "hmmer_";
221 private static final String UTF_8 = "UTF-8";
224 * prefix for recovering datasets for alignments with multiple views where
225 * non-existent dataset IDs were written for some views
227 private static final String UNIQSEQSETID = "uniqueSeqSetId.";
229 // use this with nextCounter() to make unique names for entities
230 private int counter = 0;
233 * SequenceI reference -> XML ID string in jalview XML. Populated as XML reps
234 * of sequence objects are created.
236 IdentityHashMap<SequenceI, String> seqsToIds = null;
239 * jalview XML Sequence ID to jalview sequence object reference (both dataset
240 * and alignment sequences. Populated as XML reps of sequence objects are
243 Map<String, SequenceI> seqRefIds = null;
245 Map<String, SequenceI> incompleteSeqs = null;
247 List<SeqFref> frefedSequence = null;
249 boolean raiseGUI = true; // whether errors are raised in dialog boxes or not
252 * Map of reconstructed AlignFrame objects that appear to have come from
253 * SplitFrame objects (have a dna/protein complement view).
255 private Map<Viewport, AlignFrame> splitFrameCandidates = new HashMap<>();
258 * Map from displayed rna structure models to their saved session state jar
261 private Map<RnaModel, String> rnaSessions = new HashMap<>();
264 * A helper method for safely using the value of an optional attribute that
265 * may be null if not present in the XML. Answers the boolean value, or false
271 public static boolean safeBoolean(Boolean b)
273 return b == null ? false : b.booleanValue();
277 * A helper method for safely using the value of an optional attribute that
278 * may be null if not present in the XML. Answers the integer value, or zero
284 public static int safeInt(Integer i)
286 return i == null ? 0 : i.intValue();
290 * A helper method for safely using the value of an optional attribute that
291 * may be null if not present in the XML. Answers the float value, or zero if
297 public static float safeFloat(Float f)
299 return f == null ? 0f : f.floatValue();
303 * create/return unique hash string for sq
306 * @return new or existing unique string for sq
308 String seqHash(SequenceI sq)
310 if (seqsToIds == null)
314 if (seqsToIds.containsKey(sq))
316 return seqsToIds.get(sq);
320 // create sequential key
321 String key = "sq" + (seqsToIds.size() + 1);
322 key = makeHashCode(sq, key); // check we don't have an external reference
324 seqsToIds.put(sq, key);
331 if (seqsToIds == null)
333 seqsToIds = new IdentityHashMap<>();
335 if (seqRefIds == null)
337 seqRefIds = new HashMap<>();
339 if (incompleteSeqs == null)
341 incompleteSeqs = new HashMap<>();
343 if (frefedSequence == null)
345 frefedSequence = new ArrayList<>();
353 public Jalview2XML(boolean raiseGUI)
355 this.raiseGUI = raiseGUI;
359 * base class for resolving forward references to sequences by their ID
364 abstract class SeqFref
370 public SeqFref(String _sref, String type)
376 public String getSref()
381 public SequenceI getSrefSeq()
383 return seqRefIds.get(sref);
386 public boolean isResolvable()
388 return seqRefIds.get(sref) != null;
391 public SequenceI getSrefDatasetSeq()
393 SequenceI sq = seqRefIds.get(sref);
396 while (sq.getDatasetSequence() != null)
398 sq = sq.getDatasetSequence();
405 * @return true if the forward reference was fully resolved
407 abstract boolean resolve();
410 public String toString()
412 return type + " reference to " + sref;
417 * create forward reference for a mapping
423 public SeqFref newMappingRef(final String sref,
424 final jalview.datamodel.Mapping _jmap)
426 SeqFref fref = new SeqFref(sref, "Mapping")
428 public jalview.datamodel.Mapping jmap = _jmap;
433 SequenceI seq = getSrefDatasetSeq();
445 public SeqFref newAlcodMapRef(final String sref,
446 final AlignedCodonFrame _cf,
447 final jalview.datamodel.Mapping _jmap)
450 SeqFref fref = new SeqFref(sref, "Codon Frame")
452 AlignedCodonFrame cf = _cf;
454 public jalview.datamodel.Mapping mp = _jmap;
457 public boolean isResolvable()
459 return super.isResolvable() && mp.getTo() != null;
465 SequenceI seq = getSrefDatasetSeq();
470 cf.addMap(seq, mp.getTo(), mp.getMap());
477 public void resolveFrefedSequences()
479 Iterator<SeqFref> nextFref = frefedSequence.iterator();
480 int toresolve = frefedSequence.size();
481 int unresolved = 0, failedtoresolve = 0;
482 while (nextFref.hasNext())
484 SeqFref ref = nextFref.next();
485 if (ref.isResolvable())
497 } catch (Exception x)
500 "IMPLEMENTATION ERROR: Failed to resolve forward reference for sequence "
513 System.err.println("Jalview Project Import: There were " + unresolved
514 + " forward references left unresolved on the stack.");
516 if (failedtoresolve > 0)
518 System.err.println("SERIOUS! " + failedtoresolve
519 + " resolvable forward references failed to resolve.");
521 if (incompleteSeqs != null && incompleteSeqs.size() > 0)
524 "Jalview Project Import: There are " + incompleteSeqs.size()
525 + " sequences which may have incomplete metadata.");
526 if (incompleteSeqs.size() < 10)
528 for (SequenceI s : incompleteSeqs.values())
530 System.err.println(s.toString());
536 "Too many to report. Skipping output of incomplete sequences.");
542 * This maintains a map of viewports, the key being the seqSetId. Important to
543 * set historyItem and redoList for multiple views
545 Map<String, AlignViewport> viewportsAdded = new HashMap<>();
547 Map<String, AlignmentAnnotation> annotationIds = new HashMap<>();
549 String uniqueSetSuffix = "";
552 * List of pdbfiles added to Jar
554 List<String> pdbfiles = null;
556 // SAVES SEVERAL ALIGNMENT WINDOWS TO SAME JARFILE
557 public void saveState(File statefile)
559 FileOutputStream fos = null;
564 fos = new FileOutputStream(statefile);
566 JarOutputStream jout = new JarOutputStream(fos);
570 } catch (Exception e)
572 Cache.log.error("Couln't write Jalview state to " + statefile, e);
573 // TODO: inform user of the problem - they need to know if their data was
575 if (errorMessage == null)
577 errorMessage = "Did't write Jalview Archive to output file '"
578 + statefile + "' - See console error log for details";
582 errorMessage += "(Didn't write Jalview Archive to output file '"
593 } catch (IOException e)
603 * Writes a jalview project archive to the given Jar output stream.
607 public void saveState(JarOutputStream jout)
609 AlignFrame[] frames = Desktop.getAlignFrames();
615 saveAllFrames(Arrays.asList(frames), jout);
619 * core method for storing state for a set of AlignFrames.
622 * - frames involving all data to be exported (including containing
625 * - project output stream
627 private void saveAllFrames(List<AlignFrame> frames, JarOutputStream jout)
629 Hashtable<String, AlignFrame> dsses = new Hashtable<>();
632 * ensure cached data is clear before starting
634 // todo tidy up seqRefIds, seqsToIds initialisation / reset
636 splitFrameCandidates.clear();
641 // NOTE UTF-8 MUST BE USED FOR WRITING UNICODE CHARS
642 // //////////////////////////////////////////////////
644 List<String> shortNames = new ArrayList<>();
645 List<String> viewIds = new ArrayList<>();
648 for (int i = frames.size() - 1; i > -1; i--)
650 AlignFrame af = frames.get(i);
652 if (skipList != null && skipList
653 .containsKey(af.getViewport().getSequenceSetId()))
658 String shortName = makeFilename(af, shortNames);
660 int apSize = af.getAlignPanels().size();
662 for (int ap = 0; ap < apSize; ap++)
664 AlignmentPanel apanel = (AlignmentPanel) af.getAlignPanels()
666 String fileName = apSize == 1 ? shortName : ap + shortName;
667 if (!fileName.endsWith(".xml"))
669 fileName = fileName + ".xml";
672 saveState(apanel, fileName, jout, viewIds);
674 String dssid = getDatasetIdRef(
675 af.getViewport().getAlignment().getDataset());
676 if (!dsses.containsKey(dssid))
678 dsses.put(dssid, af);
683 writeDatasetFor(dsses, "" + jout.hashCode() + " " + uniqueSetSuffix,
689 } catch (Exception foo)
694 } catch (Exception ex)
696 // TODO: inform user of the problem - they need to know if their data was
698 if (errorMessage == null)
700 errorMessage = "Couldn't write Jalview Archive - see error output for details";
702 ex.printStackTrace();
707 * Generates a distinct file name, based on the title of the AlignFrame, by
708 * appending _n for increasing n until an unused name is generated. The new
709 * name (without its extension) is added to the list.
713 * @return the generated name, with .xml extension
715 protected String makeFilename(AlignFrame af, List<String> namesUsed)
717 String shortName = af.getTitle();
719 if (shortName.indexOf(File.separatorChar) > -1)
721 shortName = shortName
722 .substring(shortName.lastIndexOf(File.separatorChar) + 1);
727 while (namesUsed.contains(shortName))
729 if (shortName.endsWith("_" + (count - 1)))
731 shortName = shortName.substring(0, shortName.lastIndexOf("_"));
734 shortName = shortName.concat("_" + count);
738 namesUsed.add(shortName);
740 if (!shortName.endsWith(".xml"))
742 shortName = shortName + ".xml";
747 // USE THIS METHOD TO SAVE A SINGLE ALIGNMENT WINDOW
748 public boolean saveAlignment(AlignFrame af, String jarFile,
753 // create backupfiles object and get new temp filename destination
754 BackupFiles backupfiles = new BackupFiles(jarFile);
755 FileOutputStream fos = new FileOutputStream(
756 backupfiles.getTempFilePath());
758 JarOutputStream jout = new JarOutputStream(fos);
759 List<AlignFrame> frames = new ArrayList<>();
761 // resolve splitframes
762 if (af.getViewport().getCodingComplement() != null)
764 frames = ((SplitFrame) af.getSplitViewContainer()).getAlignFrames();
770 saveAllFrames(frames, jout);
774 } catch (Exception foo)
779 boolean success = true;
781 backupfiles.setWriteSuccess(success);
782 success = backupfiles.rollBackupsAndRenameTempFile();
785 } catch (Exception ex)
787 errorMessage = "Couldn't Write alignment view to Jalview Archive - see error output for details";
788 ex.printStackTrace();
793 private void writeDatasetFor(Hashtable<String, AlignFrame> dsses,
794 String fileName, JarOutputStream jout)
797 for (String dssids : dsses.keySet())
799 AlignFrame _af = dsses.get(dssids);
800 String jfileName = fileName + " Dataset for " + _af.getTitle();
801 if (!jfileName.endsWith(".xml"))
803 jfileName = jfileName + ".xml";
805 saveState(_af.alignPanel, jfileName, true, jout, null);
810 * create a JalviewModel from an alignment view and marshall it to a
814 * panel to create jalview model for
816 * name of alignment panel written to output stream
823 public JalviewModel saveState(AlignmentPanel ap, String fileName,
824 JarOutputStream jout, List<String> viewIds)
826 return saveState(ap, fileName, false, jout, viewIds);
830 * create a JalviewModel from an alignment view and marshall it to a
834 * panel to create jalview model for
836 * name of alignment panel written to output stream
838 * when true, only write the dataset for the alignment, not the data
839 * associated with the view.
845 public JalviewModel saveState(AlignmentPanel ap, String fileName,
846 boolean storeDS, JarOutputStream jout, List<String> viewIds)
850 viewIds = new ArrayList<>();
855 List<UserColourScheme> userColours = new ArrayList<>();
857 AlignViewport av = ap.av;
858 ViewportRanges vpRanges = av.getRanges();
860 final ObjectFactory objectFactory = new ObjectFactory();
861 JalviewModel object = objectFactory.createJalviewModel();
862 object.setVamsasModel(new VAMSAS());
864 // object.setCreationDate(new java.util.Date(System.currentTimeMillis()));
867 GregorianCalendar c = new GregorianCalendar();
868 DatatypeFactory datatypeFactory = DatatypeFactory.newInstance();
869 XMLGregorianCalendar now = datatypeFactory.newXMLGregorianCalendar(c);// gregorianCalendar);
870 object.setCreationDate(now);
871 } catch (DatatypeConfigurationException e)
873 System.err.println("error writing date: " + e.toString());
876 jalview.bin.Cache.getDefault("VERSION", "Development Build"));
879 * rjal is full height alignment, jal is actual alignment with full metadata
880 * but excludes hidden sequences.
882 jalview.datamodel.AlignmentI rjal = av.getAlignment(), jal = rjal;
884 if (av.hasHiddenRows())
886 rjal = jal.getHiddenSequences().getFullAlignment();
889 SequenceSet vamsasSet = new SequenceSet();
891 // JalviewModelSequence jms = new JalviewModelSequence();
893 vamsasSet.setGapChar(jal.getGapCharacter() + "");
895 if (jal.getDataset() != null)
897 // dataset id is the dataset's hashcode
898 vamsasSet.setDatasetId(getDatasetIdRef(jal.getDataset()));
901 // switch jal and the dataset
902 jal = jal.getDataset();
906 if (jal.getProperties() != null)
908 Enumeration en = jal.getProperties().keys();
909 while (en.hasMoreElements())
911 String key = en.nextElement().toString();
912 SequenceSetProperties ssp = new SequenceSetProperties();
914 ssp.setValue(jal.getProperties().get(key).toString());
915 // vamsasSet.addSequenceSetProperties(ssp);
916 vamsasSet.getSequenceSetProperties().add(ssp);
921 Set<String> calcIdSet = new HashSet<>();
922 // record the set of vamsas sequence XML POJO we create.
923 HashMap<String, Sequence> vamsasSetIds = new HashMap<>();
925 for (final SequenceI jds : rjal.getSequences())
927 final SequenceI jdatasq = jds.getDatasetSequence() == null ? jds
928 : jds.getDatasetSequence();
929 String id = seqHash(jds);
930 if (vamsasSetIds.get(id) == null)
932 if (seqRefIds.get(id) != null && !storeDS)
934 // This happens for two reasons: 1. multiple views are being
936 // 2. the hashCode has collided with another sequence's code. This
938 // HAPPEN! (PF00072.15.stk does this)
939 // JBPNote: Uncomment to debug writing out of files that do not read
940 // back in due to ArrayOutOfBoundExceptions.
941 // System.err.println("vamsasSeq backref: "+id+"");
942 // System.err.println(jds.getName()+"
943 // "+jds.getStart()+"-"+jds.getEnd()+" "+jds.getSequenceAsString());
944 // System.err.println("Hashcode: "+seqHash(jds));
945 // SequenceI rsq = (SequenceI) seqRefIds.get(id + "");
946 // System.err.println(rsq.getName()+"
947 // "+rsq.getStart()+"-"+rsq.getEnd()+" "+rsq.getSequenceAsString());
948 // System.err.println("Hashcode: "+seqHash(rsq));
952 vamsasSeq = createVamsasSequence(id, jds);
953 // vamsasSet.addSequence(vamsasSeq);
954 vamsasSet.getSequence().add(vamsasSeq);
955 vamsasSetIds.put(id, vamsasSeq);
956 seqRefIds.put(id, jds);
960 jseq.setStart(jds.getStart());
961 jseq.setEnd(jds.getEnd());
962 jseq.setColour(av.getSequenceColour(jds).getRGB());
964 jseq.setId(id); // jseq id should be a string not a number
967 // Store any sequences this sequence represents
968 if (av.hasHiddenRows())
970 // use rjal, contains the full height alignment
972 av.getAlignment().getHiddenSequences().isHidden(jds));
974 if (av.isHiddenRepSequence(jds))
976 jalview.datamodel.SequenceI[] reps = av
977 .getRepresentedSequences(jds).getSequencesInOrder(rjal);
979 for (int h = 0; h < reps.length; h++)
983 // jseq.addHiddenSequences(rjal.findIndex(reps[h]));
984 jseq.getHiddenSequences().add(rjal.findIndex(reps[h]));
989 // mark sequence as reference - if it is the reference for this view
992 jseq.setViewreference(jds == jal.getSeqrep());
996 // TODO: omit sequence features from each alignment view's XML dump if we
997 // are storing dataset
998 List<SequenceFeature> sfs = jds.getSequenceFeatures();
999 for (SequenceFeature sf : sfs)
1001 // Features features = new Features();
1002 Feature features = new Feature();
1004 features.setBegin(sf.getBegin());
1005 features.setEnd(sf.getEnd());
1006 features.setDescription(sf.getDescription());
1007 features.setType(sf.getType());
1008 features.setFeatureGroup(sf.getFeatureGroup());
1009 features.setScore(sf.getScore());
1010 if (sf.links != null)
1012 for (int l = 0; l < sf.links.size(); l++)
1014 OtherData keyValue = new OtherData();
1015 keyValue.setKey("LINK_" + l);
1016 keyValue.setValue(sf.links.elementAt(l).toString());
1017 // features.addOtherData(keyValue);
1018 features.getOtherData().add(keyValue);
1021 if (sf.otherDetails != null)
1024 * save feature attributes, which may be simple strings or
1025 * map valued (have sub-attributes)
1027 for (Entry<String, Object> entry : sf.otherDetails.entrySet())
1029 String key = entry.getKey();
1030 Object value = entry.getValue();
1031 if (value instanceof Map<?, ?>)
1033 for (Entry<String, Object> subAttribute : ((Map<String, Object>) value)
1036 OtherData otherData = new OtherData();
1037 otherData.setKey(key);
1038 otherData.setKey2(subAttribute.getKey());
1039 otherData.setValue(subAttribute.getValue().toString());
1040 // features.addOtherData(otherData);
1041 features.getOtherData().add(otherData);
1046 OtherData otherData = new OtherData();
1047 otherData.setKey(key);
1048 otherData.setValue(value.toString());
1049 // features.addOtherData(otherData);
1050 features.getOtherData().add(otherData);
1055 // jseq.addFeatures(features);
1056 jseq.getFeatures().add(features);
1060 * save PDB entries for sequence
1062 if (jdatasq.getAllPDBEntries() != null)
1064 Enumeration<PDBEntry> en = jdatasq.getAllPDBEntries().elements();
1065 while (en.hasMoreElements())
1067 Pdbids pdb = new Pdbids();
1068 jalview.datamodel.PDBEntry entry = en.nextElement();
1070 String pdbId = entry.getId();
1072 pdb.setType(entry.getType());
1075 * Store any structure views associated with this sequence. This
1076 * section copes with duplicate entries in the project, so a dataset
1077 * only view *should* be coped with sensibly.
1079 // This must have been loaded, is it still visible?
1080 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
1081 String matchedFile = null;
1082 for (int f = frames.length - 1; f > -1; f--)
1084 if (frames[f] instanceof StructureViewerBase)
1086 StructureViewerBase viewFrame = (StructureViewerBase) frames[f];
1087 matchedFile = saveStructureState(ap, jds, pdb, entry, viewIds,
1088 matchedFile, viewFrame);
1090 * Only store each structure viewer's state once in the project
1091 * jar. First time through only (storeDS==false)
1093 String viewId = viewFrame.getViewId();
1094 if (!storeDS && !viewIds.contains(viewId))
1096 viewIds.add(viewId);
1099 String viewerState = viewFrame.getStateInfo();
1100 writeJarEntry(jout, getViewerJarEntryName(viewId),
1101 viewerState.getBytes());
1102 } catch (IOException e)
1105 "Error saving viewer state: " + e.getMessage());
1111 if (matchedFile != null || entry.getFile() != null)
1113 if (entry.getFile() != null)
1116 matchedFile = entry.getFile();
1118 pdb.setFile(matchedFile); // entry.getFile());
1119 if (pdbfiles == null)
1121 pdbfiles = new ArrayList<>();
1124 if (!pdbfiles.contains(pdbId))
1126 pdbfiles.add(pdbId);
1127 copyFileToJar(jout, matchedFile, pdbId);
1131 Enumeration<String> props = entry.getProperties();
1132 if (props.hasMoreElements())
1134 // PdbentryItem item = new PdbentryItem();
1135 while (props.hasMoreElements())
1137 Property prop = new Property();
1138 String key = props.nextElement();
1140 prop.setValue(entry.getProperty(key).toString());
1141 // item.addProperty(prop);
1142 pdb.getProperty().add(prop);
1144 // pdb.addPdbentryItem(item);
1147 // jseq.addPdbids(pdb);
1148 jseq.getPdbids().add(pdb);
1152 saveRnaViewers(jout, jseq, jds, viewIds, ap, storeDS);
1154 if (jds.hasHMMProfile())
1156 saveHmmerProfile(jout, jseq, jds);
1159 // jms.addJSeq(jseq);
1160 object.getJSeq().add(jseq);
1163 if (!storeDS && av.hasHiddenRows())
1165 jal = av.getAlignment();
1169 if (storeDS && jal.getCodonFrames() != null)
1171 List<AlignedCodonFrame> jac = jal.getCodonFrames();
1172 for (AlignedCodonFrame acf : jac)
1174 AlcodonFrame alc = new AlcodonFrame();
1175 if (acf.getProtMappings() != null
1176 && acf.getProtMappings().length > 0)
1178 boolean hasMap = false;
1179 SequenceI[] dnas = acf.getdnaSeqs();
1180 jalview.datamodel.Mapping[] pmaps = acf.getProtMappings();
1181 for (int m = 0; m < pmaps.length; m++)
1183 AlcodMap alcmap = new AlcodMap();
1184 alcmap.setDnasq(seqHash(dnas[m]));
1186 createVamsasMapping(pmaps[m], dnas[m], null, false));
1187 // alc.addAlcodMap(alcmap);
1188 alc.getAlcodMap().add(alcmap);
1193 // vamsasSet.addAlcodonFrame(alc);
1194 vamsasSet.getAlcodonFrame().add(alc);
1197 // TODO: delete this ? dead code from 2.8.3->2.9 ?
1199 // AlcodonFrame alc = new AlcodonFrame();
1200 // vamsasSet.addAlcodonFrame(alc);
1201 // for (int p = 0; p < acf.aaWidth; p++)
1203 // Alcodon cmap = new Alcodon();
1204 // if (acf.codons[p] != null)
1206 // // Null codons indicate a gapped column in the translated peptide
1208 // cmap.setPos1(acf.codons[p][0]);
1209 // cmap.setPos2(acf.codons[p][1]);
1210 // cmap.setPos3(acf.codons[p][2]);
1212 // alc.addAlcodon(cmap);
1214 // if (acf.getProtMappings() != null
1215 // && acf.getProtMappings().length > 0)
1217 // SequenceI[] dnas = acf.getdnaSeqs();
1218 // jalview.datamodel.Mapping[] pmaps = acf.getProtMappings();
1219 // for (int m = 0; m < pmaps.length; m++)
1221 // AlcodMap alcmap = new AlcodMap();
1222 // alcmap.setDnasq(seqHash(dnas[m]));
1223 // alcmap.setMapping(createVamsasMapping(pmaps[m], dnas[m], null,
1225 // alc.addAlcodMap(alcmap);
1232 // /////////////////////////////////
1233 if (!storeDS && av.getCurrentTree() != null)
1235 // FIND ANY ASSOCIATED TREES
1236 // NOT IMPLEMENTED FOR HEADLESS STATE AT PRESENT
1237 if (Desktop.desktop != null)
1239 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
1241 for (int t = 0; t < frames.length; t++)
1243 if (frames[t] instanceof TreePanel)
1245 TreePanel tp = (TreePanel) frames[t];
1247 if (tp.getTreeCanvas().getViewport().getAlignment() == jal)
1249 JalviewModel.Tree tree = new JalviewModel.Tree();
1250 tree.setTitle(tp.getTitle());
1251 tree.setCurrentTree((av.getCurrentTree() == tp.getTree()));
1252 tree.setNewick(tp.getTree().print());
1253 tree.setThreshold(tp.getTreeCanvas().getThreshold());
1255 tree.setFitToWindow(tp.fitToWindow.getState());
1256 tree.setFontName(tp.getTreeFont().getName());
1257 tree.setFontSize(tp.getTreeFont().getSize());
1258 tree.setFontStyle(tp.getTreeFont().getStyle());
1259 tree.setMarkUnlinked(tp.placeholdersMenu.getState());
1261 tree.setShowBootstrap(tp.bootstrapMenu.getState());
1262 tree.setShowDistances(tp.distanceMenu.getState());
1264 tree.setHeight(tp.getHeight());
1265 tree.setWidth(tp.getWidth());
1266 tree.setXpos(tp.getX());
1267 tree.setYpos(tp.getY());
1268 tree.setId(makeHashCode(tp, null));
1269 tree.setLinkToAllViews(
1270 tp.getTreeCanvas().isApplyToAllViews());
1272 // jms.addTree(tree);
1273 object.getTree().add(tree);
1283 if (!storeDS && Desktop.desktop != null)
1285 for (JInternalFrame frame : Desktop.desktop.getAllFrames())
1287 if (frame instanceof PCAPanel)
1289 PCAPanel panel = (PCAPanel) frame;
1290 if (panel.getAlignViewport().getAlignment() == jal)
1292 savePCA(panel, object);
1300 * store forward refs from an annotationRow to any groups
1302 IdentityHashMap<SequenceGroup, String> groupRefs = new IdentityHashMap<>();
1305 for (SequenceI sq : jal.getSequences())
1307 // Store annotation on dataset sequences only
1308 AlignmentAnnotation[] aa = sq.getAnnotation();
1309 if (aa != null && aa.length > 0)
1311 storeAlignmentAnnotation(aa, groupRefs, av, calcIdSet, storeDS,
1318 if (jal.getAlignmentAnnotation() != null)
1320 // Store the annotation shown on the alignment.
1321 AlignmentAnnotation[] aa = jal.getAlignmentAnnotation();
1322 storeAlignmentAnnotation(aa, groupRefs, av, calcIdSet, storeDS,
1327 if (jal.getGroups() != null)
1329 JGroup[] groups = new JGroup[jal.getGroups().size()];
1331 for (jalview.datamodel.SequenceGroup sg : jal.getGroups())
1333 JGroup jGroup = new JGroup();
1334 groups[++i] = jGroup;
1336 jGroup.setStart(sg.getStartRes());
1337 jGroup.setEnd(sg.getEndRes());
1338 jGroup.setName(sg.getName());
1339 if (groupRefs.containsKey(sg))
1341 // group has references so set its ID field
1342 jGroup.setId(groupRefs.get(sg));
1344 ColourSchemeI colourScheme = sg.getColourScheme();
1345 if (colourScheme != null)
1347 ResidueShaderI groupColourScheme = sg.getGroupColourScheme();
1348 if (groupColourScheme.conservationApplied())
1350 jGroup.setConsThreshold(groupColourScheme.getConservationInc());
1352 if (colourScheme instanceof jalview.schemes.UserColourScheme)
1355 setUserColourScheme(colourScheme, userColours,
1360 jGroup.setColour(colourScheme.getSchemeName());
1363 else if (colourScheme instanceof jalview.schemes.AnnotationColourGradient)
1365 jGroup.setColour("AnnotationColourGradient");
1366 jGroup.setAnnotationColours(constructAnnotationColours(
1367 (jalview.schemes.AnnotationColourGradient) colourScheme,
1368 userColours, object));
1370 else if (colourScheme instanceof jalview.schemes.UserColourScheme)
1373 setUserColourScheme(colourScheme, userColours, object));
1377 jGroup.setColour(colourScheme.getSchemeName());
1380 jGroup.setPidThreshold(groupColourScheme.getThreshold());
1383 jGroup.setOutlineColour(sg.getOutlineColour().getRGB());
1384 jGroup.setDisplayBoxes(sg.getDisplayBoxes());
1385 jGroup.setDisplayText(sg.getDisplayText());
1386 jGroup.setColourText(sg.getColourText());
1387 jGroup.setTextCol1(sg.textColour.getRGB());
1388 jGroup.setTextCol2(sg.textColour2.getRGB());
1389 jGroup.setTextColThreshold(sg.thresholdTextColour);
1390 jGroup.setShowUnconserved(sg.getShowNonconserved());
1391 jGroup.setIgnoreGapsinConsensus(sg.getIgnoreGapsConsensus());
1392 jGroup.setShowConsensusHistogram(sg.isShowConsensusHistogram());
1393 jGroup.setShowSequenceLogo(sg.isShowSequenceLogo());
1394 jGroup.setNormaliseSequenceLogo(sg.isNormaliseSequenceLogo());
1395 for (SequenceI seq : sg.getSequences())
1397 // jGroup.addSeq(seqHash(seq));
1398 jGroup.getSeq().add(seqHash(seq));
1402 //jms.setJGroup(groups);
1404 for (JGroup grp : groups)
1406 object.getJGroup().add(grp);
1411 // /////////SAVE VIEWPORT
1412 Viewport view = new Viewport();
1413 view.setTitle(ap.alignFrame.getTitle());
1414 view.setSequenceSetId(
1415 makeHashCode(av.getSequenceSetId(), av.getSequenceSetId()));
1416 view.setId(av.getViewId());
1417 if (av.getCodingComplement() != null)
1419 view.setComplementId(av.getCodingComplement().getViewId());
1421 view.setViewName(av.getViewName());
1422 view.setGatheredViews(av.isGatherViewsHere());
1424 Rectangle size = ap.av.getExplodedGeometry();
1425 Rectangle position = size;
1428 size = ap.alignFrame.getBounds();
1429 if (av.getCodingComplement() != null)
1431 position = ((SplitFrame) ap.alignFrame.getSplitViewContainer())
1439 view.setXpos(position.x);
1440 view.setYpos(position.y);
1442 view.setWidth(size.width);
1443 view.setHeight(size.height);
1445 view.setStartRes(vpRanges.getStartRes());
1446 view.setStartSeq(vpRanges.getStartSeq());
1448 if (av.getGlobalColourScheme() instanceof jalview.schemes.UserColourScheme)
1450 view.setBgColour(setUserColourScheme(av.getGlobalColourScheme(),
1451 userColours, object));
1454 .getGlobalColourScheme() instanceof jalview.schemes.AnnotationColourGradient)
1456 AnnotationColourScheme ac = constructAnnotationColours(
1457 (jalview.schemes.AnnotationColourGradient) av
1458 .getGlobalColourScheme(),
1459 userColours, object);
1461 view.setAnnotationColours(ac);
1462 view.setBgColour("AnnotationColourGradient");
1466 view.setBgColour(ColourSchemeProperty
1467 .getColourName(av.getGlobalColourScheme()));
1470 ResidueShaderI vcs = av.getResidueShading();
1471 ColourSchemeI cs = av.getGlobalColourScheme();
1475 if (vcs.conservationApplied())
1477 view.setConsThreshold(vcs.getConservationInc());
1478 if (cs instanceof jalview.schemes.UserColourScheme)
1480 view.setBgColour(setUserColourScheme(cs, userColours, object));
1483 view.setPidThreshold(vcs.getThreshold());
1486 view.setConservationSelected(av.getConservationSelected());
1487 view.setPidSelected(av.getAbovePIDThreshold());
1488 final Font font = av.getFont();
1489 view.setFontName(font.getName());
1490 view.setFontSize(font.getSize());
1491 view.setFontStyle(font.getStyle());
1492 view.setScaleProteinAsCdna(av.getViewStyle().isScaleProteinAsCdna());
1493 view.setRenderGaps(av.isRenderGaps());
1494 view.setShowAnnotation(av.isShowAnnotation());
1495 view.setShowBoxes(av.getShowBoxes());
1496 view.setShowColourText(av.getColourText());
1497 view.setShowFullId(av.getShowJVSuffix());
1498 view.setRightAlignIds(av.isRightAlignIds());
1499 view.setShowSequenceFeatures(av.isShowSequenceFeatures());
1500 view.setShowText(av.getShowText());
1501 view.setShowUnconserved(av.getShowUnconserved());
1502 view.setWrapAlignment(av.getWrapAlignment());
1503 view.setTextCol1(av.getTextColour().getRGB());
1504 view.setTextCol2(av.getTextColour2().getRGB());
1505 view.setTextColThreshold(av.getThresholdTextColour());
1506 view.setShowConsensusHistogram(av.isShowConsensusHistogram());
1507 view.setShowSequenceLogo(av.isShowSequenceLogo());
1508 view.setNormaliseSequenceLogo(av.isNormaliseSequenceLogo());
1509 view.setShowGroupConsensus(av.isShowGroupConsensus());
1510 view.setShowGroupConservation(av.isShowGroupConservation());
1511 view.setShowNPfeatureTooltip(av.isShowNPFeats());
1512 view.setShowDbRefTooltip(av.isShowDBRefs());
1513 view.setFollowHighlight(av.isFollowHighlight());
1514 view.setFollowSelection(av.followSelection);
1515 view.setIgnoreGapsinConsensus(av.isIgnoreGapsConsensus());
1516 view.setShowComplementFeatures(av.isShowComplementFeatures());
1517 view.setShowComplementFeaturesOnTop(
1518 av.isShowComplementFeaturesOnTop());
1519 if (av.getFeaturesDisplayed() != null)
1521 FeatureSettings fs = new FeatureSettings();
1523 FeatureRendererModel fr = ap.getSeqPanel().seqCanvas
1524 .getFeatureRenderer();
1525 String[] renderOrder = fr.getRenderOrder().toArray(new String[0]);
1527 Vector<String> settingsAdded = new Vector<>();
1528 if (renderOrder != null)
1530 for (String featureType : renderOrder)
1532 FeatureSettings.Setting setting = new FeatureSettings.Setting();
1533 setting.setType(featureType);
1536 * save any filter for the feature type
1538 FeatureMatcherSetI filter = fr.getFeatureFilter(featureType);
1539 if (filter != null) {
1540 Iterator<FeatureMatcherI> filters = filter.getMatchers().iterator();
1541 FeatureMatcherI firstFilter = filters.next();
1542 setting.setMatcherSet(Jalview2XML.marshalFilter(
1543 firstFilter, filters, filter.isAnded()));
1547 * save colour scheme for the feature type
1549 FeatureColourI fcol = fr.getFeatureStyle(featureType);
1550 if (!fcol.isSimpleColour())
1552 setting.setColour(fcol.getMaxColour().getRGB());
1553 setting.setMincolour(fcol.getMinColour().getRGB());
1554 setting.setMin(fcol.getMin());
1555 setting.setMax(fcol.getMax());
1556 setting.setColourByLabel(fcol.isColourByLabel());
1557 if (fcol.isColourByAttribute())
1559 String[] attName = fcol.getAttributeName();
1560 setting.getAttributeName().add(attName[0]);
1561 if (attName.length > 1)
1563 setting.getAttributeName().add(attName[1]);
1566 setting.setAutoScale(fcol.isAutoScaled());
1567 setting.setThreshold(fcol.getThreshold());
1568 Color noColour = fcol.getNoColour();
1569 if (noColour == null)
1571 setting.setNoValueColour(NoValueColour.NONE);
1573 else if (noColour.equals(fcol.getMaxColour()))
1575 setting.setNoValueColour(NoValueColour.MAX);
1579 setting.setNoValueColour(NoValueColour.MIN);
1581 // -1 = No threshold, 0 = Below, 1 = Above
1582 setting.setThreshstate(fcol.isAboveThreshold() ? 1
1583 : (fcol.isBelowThreshold() ? 0 : -1));
1587 setting.setColour(fcol.getColour().getRGB());
1591 av.getFeaturesDisplayed().isVisible(featureType));
1593 .getOrder(featureType);
1596 setting.setOrder(rorder);
1598 /// fs.addSetting(setting);
1599 fs.getSetting().add(setting);
1600 settingsAdded.addElement(featureType);
1604 // is groups actually supposed to be a map here ?
1605 Iterator<String> en = fr.getFeatureGroups().iterator();
1606 Vector<String> groupsAdded = new Vector<>();
1607 while (en.hasNext())
1609 String grp = en.next();
1610 if (groupsAdded.contains(grp))
1614 Group g = new Group();
1616 g.setDisplay(((Boolean) fr.checkGroupVisibility(grp, false))
1619 fs.getGroup().add(g);
1620 groupsAdded.addElement(grp);
1622 // jms.setFeatureSettings(fs);
1623 object.setFeatureSettings(fs);
1626 if (av.hasHiddenColumns())
1628 jalview.datamodel.HiddenColumns hidden = av.getAlignment()
1629 .getHiddenColumns();
1632 warn("REPORT BUG: avoided null columnselection bug (DMAM reported). Please contact Jim about this.");
1636 Iterator<int[]> hiddenRegions = hidden.iterator();
1637 while (hiddenRegions.hasNext())
1639 int[] region = hiddenRegions.next();
1640 HiddenColumns hc = new HiddenColumns();
1641 hc.setStart(region[0]);
1642 hc.setEnd(region[1]);
1643 // view.addHiddenColumns(hc);
1644 view.getHiddenColumns().add(hc);
1648 if (calcIdSet.size() > 0)
1650 for (String calcId : calcIdSet)
1652 if (calcId.trim().length() > 0)
1654 CalcIdParam cidp = createCalcIdParam(calcId, av);
1655 // Some calcIds have no parameters.
1658 // view.addCalcIdParam(cidp);
1659 view.getCalcIdParam().add(cidp);
1665 // jms.addViewport(view);
1666 object.getViewport().add(view);
1668 // object.setJalviewModelSequence(jms);
1669 // object.getVamsasModel().addSequenceSet(vamsasSet);
1670 object.getVamsasModel().getSequenceSet().add(vamsasSet);
1672 if (jout != null && fileName != null)
1674 // We may not want to write the object to disk,
1675 // eg we can copy the alignViewport to a new view object
1676 // using save and then load
1679 System.out.println("Writing jar entry " + fileName);
1680 JarEntry entry = new JarEntry(fileName);
1681 jout.putNextEntry(entry);
1682 PrintWriter pout = new PrintWriter(
1683 new OutputStreamWriter(jout, UTF_8));
1684 JAXBContext jaxbContext = JAXBContext
1685 .newInstance(JalviewModel.class);
1686 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
1688 // output pretty printed
1689 // jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
1690 jaxbMarshaller.marshal(
1691 new ObjectFactory().createJalviewModel(object), pout);
1693 // jaxbMarshaller.marshal(object, pout);
1694 // marshaller.marshal(object);
1697 } catch (Exception ex)
1699 // TODO: raise error in GUI if marshalling failed.
1700 System.err.println("Error writing Jalview project");
1701 ex.printStackTrace();
1707 * Saves the HMMER profile associated with the sequence as a file in the jar,
1708 * in HMMER format, and saves the name of the file as a child element of the
1709 * XML sequence element
1715 protected void saveHmmerProfile(JarOutputStream jout, JSeq xmlSeq,
1718 HiddenMarkovModel profile = seq.getHMM();
1719 if (profile == null)
1721 warn("Want to save HMM profile for " + seq.getName()
1722 + " but none found");
1725 HMMFile hmmFile = new HMMFile(profile);
1726 String hmmAsString = hmmFile.print();
1727 String jarEntryName = HMMER_PREFIX + nextCounter();
1730 writeJarEntry(jout, jarEntryName, hmmAsString.getBytes());
1731 xmlSeq.setHmmerProfile(jarEntryName);
1732 } catch (IOException e)
1734 warn("Error saving HMM profile: " + e.getMessage());
1740 * Writes PCA viewer attributes and computed values to an XML model object and
1741 * adds it to the JalviewModel. Any exceptions are reported by logging.
1743 protected void savePCA(PCAPanel panel, JalviewModel object)
1747 PcaViewer viewer = new PcaViewer();
1748 viewer.setHeight(panel.getHeight());
1749 viewer.setWidth(panel.getWidth());
1750 viewer.setXpos(panel.getX());
1751 viewer.setYpos(panel.getY());
1752 viewer.setTitle(panel.getTitle());
1753 PCAModel pcaModel = panel.getPcaModel();
1754 viewer.setScoreModelName(pcaModel.getScoreModelName());
1755 viewer.setXDim(panel.getSelectedDimensionIndex(X));
1756 viewer.setYDim(panel.getSelectedDimensionIndex(Y));
1757 viewer.setZDim(panel.getSelectedDimensionIndex(Z));
1759 panel.getRotatableCanvas().getBackgroundColour().getRGB());
1760 viewer.setScaleFactor(panel.getRotatableCanvas().getScaleFactor());
1761 float[] spMin = panel.getRotatableCanvas().getSeqMin();
1762 SeqPointMin spmin = new SeqPointMin();
1763 spmin.setXPos(spMin[0]);
1764 spmin.setYPos(spMin[1]);
1765 spmin.setZPos(spMin[2]);
1766 viewer.setSeqPointMin(spmin);
1767 float[] spMax = panel.getRotatableCanvas().getSeqMax();
1768 SeqPointMax spmax = new SeqPointMax();
1769 spmax.setXPos(spMax[0]);
1770 spmax.setYPos(spMax[1]);
1771 spmax.setZPos(spMax[2]);
1772 viewer.setSeqPointMax(spmax);
1773 viewer.setShowLabels(panel.getRotatableCanvas().isShowLabels());
1774 viewer.setLinkToAllViews(
1775 panel.getRotatableCanvas().isApplyToAllViews());
1776 SimilarityParamsI sp = pcaModel.getSimilarityParameters();
1777 viewer.setIncludeGaps(sp.includeGaps());
1778 viewer.setMatchGaps(sp.matchGaps());
1779 viewer.setIncludeGappedColumns(sp.includeGappedColumns());
1780 viewer.setDenominateByShortestLength(sp.denominateByShortestLength());
1783 * sequence points on display
1785 for (jalview.datamodel.SequencePoint spt : pcaModel
1786 .getSequencePoints())
1788 SequencePoint point = new SequencePoint();
1789 point.setSequenceRef(seqHash(spt.getSequence()));
1790 point.setXPos(spt.coord.x);
1791 point.setYPos(spt.coord.y);
1792 point.setZPos(spt.coord.z);
1793 viewer.getSequencePoint().add(point);
1797 * (end points of) axes on display
1799 for (Point p : panel.getRotatableCanvas().getAxisEndPoints())
1802 Axis axis = new Axis();
1806 viewer.getAxis().add(axis);
1810 * raw PCA data (note we are not restoring PCA inputs here -
1811 * alignment view, score model, similarity parameters)
1813 PcaDataType data = new PcaDataType();
1814 viewer.setPcaData(data);
1815 PCA pca = pcaModel.getPcaData();
1817 DoubleMatrix pm = new DoubleMatrix();
1818 saveDoubleMatrix(pca.getPairwiseScores(), pm);
1819 data.setPairwiseMatrix(pm);
1821 DoubleMatrix tm = new DoubleMatrix();
1822 saveDoubleMatrix(pca.getTridiagonal(), tm);
1823 data.setTridiagonalMatrix(tm);
1825 DoubleMatrix eigenMatrix = new DoubleMatrix();
1826 data.setEigenMatrix(eigenMatrix);
1827 saveDoubleMatrix(pca.getEigenmatrix(), eigenMatrix);
1829 object.getPcaViewer().add(viewer);
1830 } catch (Throwable t)
1832 Cache.log.error("Error saving PCA: " + t.getMessage());
1837 * Stores values from a matrix into an XML element, including (if present) the
1842 * @see #loadDoubleMatrix(DoubleMatrix)
1844 protected void saveDoubleMatrix(MatrixI m, DoubleMatrix xmlMatrix)
1846 xmlMatrix.setRows(m.height());
1847 xmlMatrix.setColumns(m.width());
1848 for (int i = 0; i < m.height(); i++)
1850 DoubleVector row = new DoubleVector();
1851 for (int j = 0; j < m.width(); j++)
1853 row.getV().add(m.getValue(i, j));
1855 xmlMatrix.getRow().add(row);
1857 if (m.getD() != null)
1859 DoubleVector dVector = new DoubleVector();
1860 for (double d : m.getD())
1862 dVector.getV().add(d);
1864 xmlMatrix.setD(dVector);
1866 if (m.getE() != null)
1868 DoubleVector eVector = new DoubleVector();
1869 for (double e : m.getE())
1871 eVector.getV().add(e);
1873 xmlMatrix.setE(eVector);
1878 * Loads XML matrix data into a new Matrix object, including the D and/or E
1879 * vectors (if present)
1883 * @see Jalview2XML#saveDoubleMatrix(MatrixI, DoubleMatrix)
1885 protected MatrixI loadDoubleMatrix(DoubleMatrix mData)
1887 int rows = mData.getRows();
1888 double[][] vals = new double[rows][];
1890 for (int i = 0; i < rows; i++)
1892 List<Double> dVector = mData.getRow().get(i).getV();
1893 vals[i] = new double[dVector.size()];
1895 for (Double d : dVector)
1901 MatrixI m = new Matrix(vals);
1903 if (mData.getD() != null)
1905 List<Double> dVector = mData.getD().getV();
1906 double[] vec = new double[dVector.size()];
1908 for (Double d : dVector)
1914 if (mData.getE() != null)
1916 List<Double> dVector = mData.getE().getV();
1917 double[] vec = new double[dVector.size()];
1919 for (Double d : dVector)
1930 * Save any Varna viewers linked to this sequence. Writes an rnaViewer element
1931 * for each viewer, with
1933 * <li>viewer geometry (position, size, split pane divider location)</li>
1934 * <li>index of the selected structure in the viewer (currently shows gapped
1936 * <li>the id of the annotation holding RNA secondary structure</li>
1937 * <li>(currently only one SS is shown per viewer, may be more in future)</li>
1939 * Varna viewer state is also written out (in native Varna XML) to separate
1940 * project jar entries. A separate entry is written for each RNA structure
1941 * displayed, with the naming convention
1943 * <li>rna_viewId_sequenceId_annotationId_[gapped|trimmed]</li>
1951 * @param storeDataset
1953 protected void saveRnaViewers(JarOutputStream jout, JSeq jseq,
1954 final SequenceI jds, List<String> viewIds, AlignmentPanel ap,
1955 boolean storeDataset)
1957 if (Desktop.desktop == null)
1961 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
1962 for (int f = frames.length - 1; f > -1; f--)
1964 if (frames[f] instanceof AppVarna)
1966 AppVarna varna = (AppVarna) frames[f];
1968 * link the sequence to every viewer that is showing it and is linked to
1969 * its alignment panel
1971 if (varna.isListeningFor(jds) && ap == varna.getAlignmentPanel())
1973 String viewId = varna.getViewId();
1974 RnaViewer rna = new RnaViewer();
1975 rna.setViewId(viewId);
1976 rna.setTitle(varna.getTitle());
1977 rna.setXpos(varna.getX());
1978 rna.setYpos(varna.getY());
1979 rna.setWidth(varna.getWidth());
1980 rna.setHeight(varna.getHeight());
1981 rna.setDividerLocation(varna.getDividerLocation());
1982 rna.setSelectedRna(varna.getSelectedIndex());
1983 // jseq.addRnaViewer(rna);
1984 jseq.getRnaViewer().add(rna);
1987 * Store each Varna panel's state once in the project per sequence.
1988 * First time through only (storeDataset==false)
1990 // boolean storeSessions = false;
1991 // String sequenceViewId = viewId + seqsToIds.get(jds);
1992 // if (!storeDataset && !viewIds.contains(sequenceViewId))
1994 // viewIds.add(sequenceViewId);
1995 // storeSessions = true;
1997 for (RnaModel model : varna.getModels())
1999 if (model.seq == jds)
2002 * VARNA saves each view (sequence or alignment secondary
2003 * structure, gapped or trimmed) as a separate XML file
2005 String jarEntryName = rnaSessions.get(model);
2006 if (jarEntryName == null)
2009 String varnaStateFile = varna.getStateInfo(model.rna);
2010 jarEntryName = RNA_PREFIX + viewId + "_" + nextCounter();
2011 copyFileToJar(jout, varnaStateFile, jarEntryName);
2012 rnaSessions.put(model, jarEntryName);
2014 SecondaryStructure ss = new SecondaryStructure();
2015 String annotationId = varna.getAnnotation(jds).annotationId;
2016 ss.setAnnotationId(annotationId);
2017 ss.setViewerState(jarEntryName);
2018 ss.setGapped(model.gapped);
2019 ss.setTitle(model.title);
2020 // rna.addSecondaryStructure(ss);
2021 rna.getSecondaryStructure().add(ss);
2030 * Copy the contents of a file to a new entry added to the output jar
2034 * @param jarEntryName
2036 protected void copyFileToJar(JarOutputStream jout, String infilePath,
2037 String jarEntryName)
2039 DataInputStream dis = null;
2042 File file = new File(infilePath);
2043 if (file.exists() && jout != null)
2045 dis = new DataInputStream(new FileInputStream(file));
2046 byte[] data = new byte[(int) file.length()];
2047 dis.readFully(data);
2048 writeJarEntry(jout, jarEntryName, data);
2050 } catch (Exception ex)
2052 ex.printStackTrace();
2060 } catch (IOException e)
2069 * Write the data to a new entry of given name in the output jar file
2072 * @param jarEntryName
2074 * @throws IOException
2076 protected void writeJarEntry(JarOutputStream jout, String jarEntryName,
2077 byte[] data) throws IOException
2081 System.out.println("Writing jar entry " + jarEntryName);
2082 jout.putNextEntry(new JarEntry(jarEntryName));
2083 DataOutputStream dout = new DataOutputStream(jout);
2084 dout.write(data, 0, data.length);
2091 * Save the state of a structure viewer
2096 * the archive XML element under which to save the state
2099 * @param matchedFile
2103 protected String saveStructureState(AlignmentPanel ap, SequenceI jds,
2104 Pdbids pdb, PDBEntry entry, List<String> viewIds,
2105 String matchedFile, StructureViewerBase viewFrame)
2107 final AAStructureBindingModel bindingModel = viewFrame.getBinding();
2110 * Look for any bindings for this viewer to the PDB file of interest
2111 * (including part matches excluding chain id)
2113 for (int peid = 0; peid < bindingModel.getPdbCount(); peid++)
2115 final PDBEntry pdbentry = bindingModel.getPdbEntry(peid);
2116 final String pdbId = pdbentry.getId();
2117 if (!pdbId.equals(entry.getId())
2118 && !(entry.getId().length() > 4 && entry.getId().toLowerCase()
2119 .startsWith(pdbId.toLowerCase())))
2122 * not interested in a binding to a different PDB entry here
2126 if (matchedFile == null)
2128 matchedFile = pdbentry.getFile();
2130 else if (!matchedFile.equals(pdbentry.getFile()))
2133 "Probably lost some PDB-Sequence mappings for this structure file (which apparently has same PDB Entry code): "
2134 + pdbentry.getFile());
2138 // can get at it if the ID
2139 // match is ambiguous (e.g.
2142 for (int smap = 0; smap < viewFrame.getBinding()
2143 .getSequence()[peid].length; smap++)
2145 // if (jal.findIndex(jmol.jmb.sequence[peid][smap]) > -1)
2146 if (jds == viewFrame.getBinding().getSequence()[peid][smap])
2148 StructureState state = new StructureState();
2149 state.setVisible(true);
2150 state.setXpos(viewFrame.getX());
2151 state.setYpos(viewFrame.getY());
2152 state.setWidth(viewFrame.getWidth());
2153 state.setHeight(viewFrame.getHeight());
2154 final String viewId = viewFrame.getViewId();
2155 state.setViewId(viewId);
2156 state.setAlignwithAlignPanel(viewFrame.isUsedforaligment(ap));
2157 state.setColourwithAlignPanel(viewFrame.isUsedforcolourby(ap));
2158 state.setColourByJmol(viewFrame.isColouredByViewer());
2159 state.setType(viewFrame.getViewerType().toString());
2160 // pdb.addStructureState(state);
2161 pdb.getStructureState().add(state);
2169 * Populates the AnnotationColourScheme xml for save. This captures the
2170 * settings of the options in the 'Colour by Annotation' dialog.
2173 * @param userColours
2177 private AnnotationColourScheme constructAnnotationColours(
2178 AnnotationColourGradient acg, List<UserColourScheme> userColours,
2181 AnnotationColourScheme ac = new AnnotationColourScheme();
2182 ac.setAboveThreshold(acg.getAboveThreshold());
2183 ac.setThreshold(acg.getAnnotationThreshold());
2184 // 2.10.2 save annotationId (unique) not annotation label
2185 ac.setAnnotation(acg.getAnnotation().annotationId);
2186 if (acg.getBaseColour() instanceof UserColourScheme)
2189 setUserColourScheme(acg.getBaseColour(), userColours, jm));
2194 ColourSchemeProperty.getColourName(acg.getBaseColour()));
2197 ac.setMaxColour(acg.getMaxColour().getRGB());
2198 ac.setMinColour(acg.getMinColour().getRGB());
2199 ac.setPerSequence(acg.isSeqAssociated());
2200 ac.setPredefinedColours(acg.isPredefinedColours());
2204 private void storeAlignmentAnnotation(AlignmentAnnotation[] aa,
2205 IdentityHashMap<SequenceGroup, String> groupRefs,
2206 AlignmentViewport av, Set<String> calcIdSet, boolean storeDS,
2207 SequenceSet vamsasSet)
2210 for (int i = 0; i < aa.length; i++)
2212 Annotation an = new Annotation();
2214 AlignmentAnnotation annotation = aa[i];
2215 if (annotation.annotationId != null)
2217 annotationIds.put(annotation.annotationId, annotation);
2220 an.setId(annotation.annotationId);
2222 an.setVisible(annotation.visible);
2224 an.setDescription(annotation.description);
2226 if (annotation.sequenceRef != null)
2228 // 2.9 JAL-1781 xref on sequence id rather than name
2229 an.setSequenceRef(seqsToIds.get(annotation.sequenceRef));
2231 if (annotation.groupRef != null)
2233 String groupIdr = groupRefs.get(annotation.groupRef);
2234 if (groupIdr == null)
2236 // make a locally unique String
2237 groupRefs.put(annotation.groupRef,
2238 groupIdr = ("" + System.currentTimeMillis()
2239 + annotation.groupRef.getName()
2240 + groupRefs.size()));
2242 an.setGroupRef(groupIdr.toString());
2245 // store all visualization attributes for annotation
2246 an.setGraphHeight(annotation.graphHeight);
2247 an.setCentreColLabels(annotation.centreColLabels);
2248 an.setScaleColLabels(annotation.scaleColLabel);
2249 an.setShowAllColLabels(annotation.showAllColLabels);
2250 an.setBelowAlignment(annotation.belowAlignment);
2252 if (annotation.graph > 0)
2255 an.setGraphType(annotation.graph);
2256 an.setGraphGroup(annotation.graphGroup);
2257 if (annotation.getThreshold() != null)
2259 ThresholdLine line = new ThresholdLine();
2260 line.setLabel(annotation.getThreshold().label);
2261 line.setValue(annotation.getThreshold().value);
2262 line.setColour(annotation.getThreshold().colour.getRGB());
2263 an.setThresholdLine(line);
2271 an.setLabel(annotation.label);
2273 if (annotation == av.getAlignmentQualityAnnot()
2274 || annotation == av.getAlignmentConservationAnnotation()
2275 || annotation == av.getAlignmentConsensusAnnotation()
2276 || annotation.autoCalculated)
2278 // new way of indicating autocalculated annotation -
2279 an.setAutoCalculated(annotation.autoCalculated);
2281 if (annotation.hasScore())
2283 an.setScore(annotation.getScore());
2286 if (annotation.getCalcId() != null)
2288 calcIdSet.add(annotation.getCalcId());
2289 an.setCalcId(annotation.getCalcId());
2291 if (annotation.hasProperties())
2293 for (String pr : annotation.getProperties())
2295 jalview.xml.binding.jalview.Annotation.Property prop = new jalview.xml.binding.jalview.Annotation.Property();
2297 prop.setValue(annotation.getProperty(pr));
2298 // an.addProperty(prop);
2299 an.getProperty().add(prop);
2303 AnnotationElement ae;
2304 if (annotation.annotations != null)
2306 an.setScoreOnly(false);
2307 for (int a = 0; a < annotation.annotations.length; a++)
2309 if ((annotation == null) || (annotation.annotations[a] == null))
2314 ae = new AnnotationElement();
2315 if (annotation.annotations[a].description != null)
2317 ae.setDescription(annotation.annotations[a].description);
2319 if (annotation.annotations[a].displayCharacter != null)
2321 ae.setDisplayCharacter(
2322 annotation.annotations[a].displayCharacter);
2325 if (!Float.isNaN(annotation.annotations[a].value))
2327 ae.setValue(annotation.annotations[a].value);
2331 if (annotation.annotations[a].secondaryStructure > ' ')
2333 ae.setSecondaryStructure(
2334 annotation.annotations[a].secondaryStructure + "");
2337 if (annotation.annotations[a].colour != null
2338 && annotation.annotations[a].colour != java.awt.Color.black)
2340 ae.setColour(annotation.annotations[a].colour.getRGB());
2343 // an.addAnnotationElement(ae);
2344 an.getAnnotationElement().add(ae);
2345 if (annotation.autoCalculated)
2347 // only write one non-null entry into the annotation row -
2348 // sufficient to get the visualization attributes necessary to
2356 an.setScoreOnly(true);
2358 if (!storeDS || (storeDS && !annotation.autoCalculated))
2360 // skip autocalculated annotation - these are only provided for
2362 // vamsasSet.addAnnotation(an);
2363 vamsasSet.getAnnotation().add(an);
2369 private CalcIdParam createCalcIdParam(String calcId, AlignViewport av)
2371 AutoCalcSetting settings = av.getCalcIdSettingsFor(calcId);
2372 if (settings != null)
2374 CalcIdParam vCalcIdParam = new CalcIdParam();
2375 vCalcIdParam.setCalcId(calcId);
2376 // vCalcIdParam.addServiceURL(settings.getServiceURI());
2377 vCalcIdParam.getServiceURL().add(settings.getServiceURI());
2378 // generic URI allowing a third party to resolve another instance of the
2379 // service used for this calculation
2380 for (String url : settings.getServiceURLs())
2382 // vCalcIdParam.addServiceURL(urls);
2383 vCalcIdParam.getServiceURL().add(url);
2385 vCalcIdParam.setVersion("1.0");
2386 if (settings.getPreset() != null)
2388 WsParamSetI setting = settings.getPreset();
2389 vCalcIdParam.setName(setting.getName());
2390 vCalcIdParam.setDescription(setting.getDescription());
2394 vCalcIdParam.setName("");
2395 vCalcIdParam.setDescription("Last used parameters");
2397 // need to be able to recover 1) settings 2) user-defined presets or
2398 // recreate settings from preset 3) predefined settings provided by
2399 // service - or settings that can be transferred (or discarded)
2400 vCalcIdParam.setParameters(
2401 settings.getWsParamFile().replace("\n", "|\\n|"));
2402 vCalcIdParam.setAutoUpdate(settings.isAutoUpdate());
2403 // todo - decide if updateImmediately is needed for any projects.
2405 return vCalcIdParam;
2410 private boolean recoverCalcIdParam(CalcIdParam calcIdParam,
2413 if (calcIdParam.getVersion().equals("1.0"))
2415 final String[] calcIds = calcIdParam.getServiceURL().toArray(new String[0]);
2416 ServiceWithParameters service = PreferredServiceRegistry.getRegistry()
2417 .getPreferredServiceFor(calcIds);
2418 if (service != null)
2420 WsParamSetI parmSet = null;
2423 parmSet = service.getParamStore().parseServiceParameterFile(
2424 calcIdParam.getName(), calcIdParam.getDescription(),
2426 calcIdParam.getParameters().replace("|\\n|", "\n"));
2427 } catch (IOException x)
2429 warn("Couldn't parse parameter data for "
2430 + calcIdParam.getCalcId(), x);
2433 List<ArgumentI> argList = null;
2434 if (calcIdParam.getName().length() > 0)
2436 parmSet = service.getParamStore()
2437 .getPreset(calcIdParam.getName());
2438 if (parmSet != null)
2440 // TODO : check we have a good match with settings in AACon -
2441 // otherwise we'll need to create a new preset
2446 argList = parmSet.getArguments();
2449 AutoCalcSetting settings = new AAConSettings(
2450 calcIdParam.isAutoUpdate(), service, parmSet, argList);
2451 av.setCalcIdSettingsFor(calcIdParam.getCalcId(), settings,
2452 calcIdParam.isNeedsUpdate());
2457 warn("Cannot resolve a service for the parameters used in this project. Try configuring a server in the Web Services preferences tab.");
2461 throw new Error(MessageManager.formatMessage(
2462 "error.unsupported_version_calcIdparam", new Object[]
2463 { calcIdParam.toString() }));
2467 * External mapping between jalview objects and objects yielding a valid and
2468 * unique object ID string. This is null for normal Jalview project IO, but
2469 * non-null when a jalview project is being read or written as part of a
2472 IdentityHashMap jv2vobj = null;
2475 * Construct a unique ID for jvobj using either existing bindings or if none
2476 * exist, the result of the hashcode call for the object.
2479 * jalview data object
2480 * @return unique ID for referring to jvobj
2482 private String makeHashCode(Object jvobj, String altCode)
2484 if (jv2vobj != null)
2486 Object id = jv2vobj.get(jvobj);
2489 return id.toString();
2491 // check string ID mappings
2492 if (jvids2vobj != null && jvobj instanceof String)
2494 id = jvids2vobj.get(jvobj);
2498 return id.toString();
2500 // give up and warn that something has gone wrong
2501 warn("Cannot find ID for object in external mapping : " + jvobj);
2507 * return local jalview object mapped to ID, if it exists
2511 * @return null or object bound to idcode
2513 private Object retrieveExistingObj(String idcode)
2515 if (idcode != null && vobj2jv != null)
2517 return vobj2jv.get(idcode);
2523 * binding from ID strings from external mapping table to jalview data model
2526 private Hashtable vobj2jv;
2528 private Sequence createVamsasSequence(String id, SequenceI jds)
2530 return createVamsasSequence(true, id, jds, null);
2533 private Sequence createVamsasSequence(boolean recurse, String id,
2534 SequenceI jds, SequenceI parentseq)
2536 Sequence vamsasSeq = new Sequence();
2537 vamsasSeq.setId(id);
2538 vamsasSeq.setName(jds.getName());
2539 vamsasSeq.setSequence(jds.getSequenceAsString());
2540 vamsasSeq.setDescription(jds.getDescription());
2541 jalview.datamodel.DBRefEntry[] dbrefs = null;
2542 if (jds.getDatasetSequence() != null)
2544 vamsasSeq.setDsseqid(seqHash(jds.getDatasetSequence()));
2548 // seqId==dsseqid so we can tell which sequences really are
2549 // dataset sequences only
2550 vamsasSeq.setDsseqid(id);
2551 dbrefs = jds.getDBRefs();
2552 if (parentseq == null)
2559 * save any dbrefs; special subclass GeneLocus is flagged as 'locus'
2563 for (int d = 0; d < dbrefs.length; d++)
2565 DBRef dbref = new DBRef();
2566 DBRefEntry dbRefEntry = dbrefs[d];
2567 dbref.setSource(dbRefEntry.getSource());
2568 dbref.setVersion(dbRefEntry.getVersion());
2569 dbref.setAccessionId(dbRefEntry.getAccessionId());
2570 if (dbRefEntry instanceof GeneLocus)
2572 dbref.setLocus(true);
2574 if (dbRefEntry.hasMap())
2576 Mapping mp = createVamsasMapping(dbRefEntry.getMap(), parentseq,
2578 dbref.setMapping(mp);
2580 vamsasSeq.getDBRef().add(dbref);
2586 private Mapping createVamsasMapping(jalview.datamodel.Mapping jmp,
2587 SequenceI parentseq, SequenceI jds, boolean recurse)
2590 if (jmp.getMap() != null)
2594 jalview.util.MapList mlst = jmp.getMap();
2595 List<int[]> r = mlst.getFromRanges();
2596 for (int[] range : r)
2598 MapListFrom mfrom = new MapListFrom();
2599 mfrom.setStart(range[0]);
2600 mfrom.setEnd(range[1]);
2601 // mp.addMapListFrom(mfrom);
2602 mp.getMapListFrom().add(mfrom);
2604 r = mlst.getToRanges();
2605 for (int[] range : r)
2607 MapListTo mto = new MapListTo();
2608 mto.setStart(range[0]);
2609 mto.setEnd(range[1]);
2610 // mp.addMapListTo(mto);
2611 mp.getMapListTo().add(mto);
2613 mp.setMapFromUnit(BigInteger.valueOf(mlst.getFromRatio()));
2614 mp.setMapToUnit(BigInteger.valueOf(mlst.getToRatio()));
2615 if (jmp.getTo() != null)
2617 // MappingChoice mpc = new MappingChoice();
2619 // check/create ID for the sequence referenced by getTo()
2622 SequenceI ps = null;
2623 if (parentseq != jmp.getTo()
2624 && parentseq.getDatasetSequence() != jmp.getTo())
2626 // chaining dbref rather than a handshaking one
2627 jmpid = seqHash(ps = jmp.getTo());
2631 jmpid = seqHash(ps = parentseq);
2633 // mpc.setDseqFor(jmpid);
2634 mp.setDseqFor(jmpid);
2635 if (!seqRefIds.containsKey(jmpid))
2637 jalview.bin.Cache.log.debug("creatign new DseqFor ID");
2638 seqRefIds.put(jmpid, ps);
2642 jalview.bin.Cache.log.debug("reusing DseqFor ID");
2645 // mp.setMappingChoice(mpc);
2651 String setUserColourScheme(jalview.schemes.ColourSchemeI cs,
2652 List<UserColourScheme> userColours, JalviewModel jm)
2655 jalview.schemes.UserColourScheme ucs = (jalview.schemes.UserColourScheme) cs;
2656 boolean newucs = false;
2657 if (!userColours.contains(ucs))
2659 userColours.add(ucs);
2662 id = "ucs" + userColours.indexOf(ucs);
2665 // actually create the scheme's entry in the XML model
2666 java.awt.Color[] colours = ucs.getColours();
2667 UserColours uc = new UserColours();
2668 // UserColourScheme jbucs = new UserColourScheme();
2669 JalviewUserColours jbucs = new JalviewUserColours();
2671 for (int i = 0; i < colours.length; i++)
2673 Colour col = new Colour();
2674 col.setName(ResidueProperties.aa[i]);
2675 col.setRGB(jalview.util.Format.getHexString(colours[i]));
2676 // jbucs.addColour(col);
2677 jbucs.getColour().add(col);
2679 if (ucs.getLowerCaseColours() != null)
2681 colours = ucs.getLowerCaseColours();
2682 for (int i = 0; i < colours.length; i++)
2684 Colour col = new Colour();
2685 col.setName(ResidueProperties.aa[i].toLowerCase());
2686 col.setRGB(jalview.util.Format.getHexString(colours[i]));
2687 // jbucs.addColour(col);
2688 jbucs.getColour().add(col);
2693 uc.setUserColourScheme(jbucs);
2694 // jm.addUserColours(uc);
2695 jm.getUserColours().add(uc);
2701 jalview.schemes.UserColourScheme getUserColourScheme(
2702 JalviewModel jm, String id)
2704 List<UserColours> uc = jm.getUserColours();
2705 UserColours colours = null;
2707 for (int i = 0; i < uc.length; i++)
2709 if (uc[i].getId().equals(id))
2716 for (UserColours c : uc)
2718 if (c.getId().equals(id))
2725 java.awt.Color[] newColours = new java.awt.Color[24];
2727 for (int i = 0; i < 24; i++)
2729 newColours[i] = new java.awt.Color(Integer.parseInt(
2730 // colours.getUserColourScheme().getColour(i).getRGB(), 16));
2731 colours.getUserColourScheme().getColour().get(i).getRGB(),
2735 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(
2738 if (colours.getUserColourScheme().getColour().size()/*Count()*/ > 24)
2740 newColours = new java.awt.Color[23];
2741 for (int i = 0; i < 23; i++)
2743 newColours[i] = new java.awt.Color(Integer.parseInt(
2744 colours.getUserColourScheme().getColour().get(i + 24)
2748 ucs.setLowerCaseColours(newColours);
2755 * contains last error message (if any) encountered by XML loader.
2757 String errorMessage = null;
2760 * flag to control whether the Jalview2XML_V1 parser should be deferred to if
2761 * exceptions are raised during project XML parsing
2763 public boolean attemptversion1parse = false;
2766 * Load a jalview project archive from a jar file
2769 * - HTTP URL or filename
2771 public AlignFrame loadJalviewAlign(final String file)
2774 jalview.gui.AlignFrame af = null;
2778 // create list to store references for any new Jmol viewers created
2779 newStructureViewers = new Vector<>();
2780 // UNMARSHALLER SEEMS TO CLOSE JARINPUTSTREAM, MOST ANNOYING
2781 // Workaround is to make sure caller implements the JarInputStreamProvider
2783 // so we can re-open the jar input stream for each entry.
2785 jarInputStreamProvider jprovider = createjarInputStreamProvider(file);
2786 af = loadJalviewAlign(jprovider);
2789 af.setMenusForViewport();
2791 } catch (MalformedURLException e)
2793 errorMessage = "Invalid URL format for '" + file + "'";
2799 SwingUtilities.invokeAndWait(new Runnable()
2804 setLoadingFinishedForNewStructureViewers();
2807 } catch (Exception x)
2809 System.err.println("Error loading alignment: " + x.getMessage());
2815 private jarInputStreamProvider createjarInputStreamProvider(
2816 final String file) throws MalformedURLException
2819 errorMessage = null;
2820 uniqueSetSuffix = null;
2822 viewportsAdded.clear();
2823 frefedSequence = null;
2825 if (file.startsWith("http://"))
2827 url = new URL(file);
2829 final URL _url = url;
2830 return new jarInputStreamProvider()
2834 public JarInputStream getJarInputStream() throws IOException
2838 return new JarInputStream(_url.openStream());
2842 return new JarInputStream(new FileInputStream(file));
2847 public String getFilename()
2855 * Recover jalview session from a jalview project archive. Caller may
2856 * initialise uniqueSetSuffix, seqRefIds, viewportsAdded and frefedSequence
2857 * themselves. Any null fields will be initialised with default values,
2858 * non-null fields are left alone.
2863 public AlignFrame loadJalviewAlign(final jarInputStreamProvider jprovider)
2865 errorMessage = null;
2866 if (uniqueSetSuffix == null)
2868 uniqueSetSuffix = System.currentTimeMillis() % 100000 + "";
2870 if (seqRefIds == null)
2874 AlignFrame af = null, _af = null;
2875 IdentityHashMap<AlignmentI, AlignmentI> importedDatasets = new IdentityHashMap<>();
2876 Map<String, AlignFrame> gatherToThisFrame = new HashMap<>();
2877 final String file = jprovider.getFilename();
2880 JarInputStream jin = null;
2881 JarEntry jarentry = null;
2886 jin = jprovider.getJarInputStream();
2887 for (int i = 0; i < entryCount; i++)
2889 jarentry = jin.getNextJarEntry();
2892 if (jarentry != null && jarentry.getName().endsWith(".xml"))
2894 InputStreamReader in = new InputStreamReader(jin, UTF_8);
2895 // JalviewModel object = new JalviewModel();
2897 JAXBContext jc = JAXBContext
2898 .newInstance("jalview.xml.binding.jalview");
2899 XMLStreamReader streamReader = XMLInputFactory.newInstance()
2900 .createXMLStreamReader(jin);
2901 javax.xml.bind.Unmarshaller um = jc.createUnmarshaller();
2902 JAXBElement<JalviewModel> jbe = um
2903 .unmarshal(streamReader, JalviewModel.class);
2904 JalviewModel object = jbe.getValue();
2907 Unmarshaller unmar = new Unmarshaller(object);
2908 unmar.setValidation(false);
2909 object = (JalviewModel) unmar.unmarshal(in);
2911 if (true) // !skipViewport(object))
2913 _af = loadFromObject(object, file, true, jprovider);
2914 if (_af != null && object.getViewport().size() > 0)
2915 // getJalviewModelSequence().getViewportCount() > 0)
2919 // store a reference to the first view
2922 if (_af.getViewport().isGatherViewsHere())
2924 // if this is a gathered view, keep its reference since
2925 // after gathering views, only this frame will remain
2927 gatherToThisFrame.put(_af.getViewport().getSequenceSetId(),
2930 // Save dataset to register mappings once all resolved
2931 importedDatasets.put(
2932 af.getViewport().getAlignment().getDataset(),
2933 af.getViewport().getAlignment().getDataset());
2938 else if (jarentry != null)
2940 // Some other file here.
2943 } while (jarentry != null);
2944 resolveFrefedSequences();
2945 } catch (IOException ex)
2947 ex.printStackTrace();
2948 errorMessage = "Couldn't locate Jalview XML file : " + file;
2950 "Exception whilst loading jalview XML file : " + ex + "\n");
2951 } catch (Exception ex)
2953 System.err.println("Parsing as Jalview Version 2 file failed.");
2954 ex.printStackTrace(System.err);
2955 if (attemptversion1parse)
2957 // used to attempt to parse as V1 castor-generated xml
2959 if (Desktop.instance != null)
2961 Desktop.instance.stopLoading();
2965 System.out.println("Successfully loaded archive file");
2968 ex.printStackTrace();
2971 "Exception whilst loading jalview XML file : " + ex + "\n");
2972 } catch (OutOfMemoryError e)
2974 // Don't use the OOM Window here
2975 errorMessage = "Out of memory loading jalview XML file";
2976 System.err.println("Out of memory whilst loading jalview XML file");
2977 e.printStackTrace();
2981 * Regather multiple views (with the same sequence set id) to the frame (if
2982 * any) that is flagged as the one to gather to, i.e. convert them to tabbed
2983 * views instead of separate frames. Note this doesn't restore a state where
2984 * some expanded views in turn have tabbed views - the last "first tab" read
2985 * in will play the role of gatherer for all.
2987 for (AlignFrame fr : gatherToThisFrame.values())
2989 Desktop.instance.gatherViews(fr);
2992 restoreSplitFrames();
2993 for (AlignmentI ds : importedDatasets.keySet())
2995 if (ds.getCodonFrames() != null)
2997 StructureSelectionManager
2998 .getStructureSelectionManager(Desktop.instance)
2999 .registerMappings(ds.getCodonFrames());
3002 if (errorMessage != null)
3007 if (Desktop.instance != null)
3009 Desktop.instance.stopLoading();
3016 * Try to reconstruct and display SplitFrame windows, where each contains
3017 * complementary dna and protein alignments. Done by pairing up AlignFrame
3018 * objects (created earlier) which have complementary viewport ids associated.
3020 protected void restoreSplitFrames()
3022 List<SplitFrame> gatherTo = new ArrayList<>();
3023 List<AlignFrame> addedToSplitFrames = new ArrayList<>();
3024 Map<String, AlignFrame> dna = new HashMap<>();
3027 * Identify the DNA alignments
3029 for (Entry<Viewport, AlignFrame> candidate : splitFrameCandidates
3032 AlignFrame af = candidate.getValue();
3033 if (af.getViewport().getAlignment().isNucleotide())
3035 dna.put(candidate.getKey().getId(), af);
3040 * Try to match up the protein complements
3042 for (Entry<Viewport, AlignFrame> candidate : splitFrameCandidates
3045 AlignFrame af = candidate.getValue();
3046 if (!af.getViewport().getAlignment().isNucleotide())
3048 String complementId = candidate.getKey().getComplementId();
3049 // only non-null complements should be in the Map
3050 if (complementId != null && dna.containsKey(complementId))
3052 final AlignFrame dnaFrame = dna.get(complementId);
3053 SplitFrame sf = createSplitFrame(dnaFrame, af);
3054 addedToSplitFrames.add(dnaFrame);
3055 addedToSplitFrames.add(af);
3056 dnaFrame.setMenusForViewport();
3057 af.setMenusForViewport();
3058 if (af.getViewport().isGatherViewsHere())
3067 * Open any that we failed to pair up (which shouldn't happen!) as
3068 * standalone AlignFrame's.
3070 for (Entry<Viewport, AlignFrame> candidate : splitFrameCandidates
3073 AlignFrame af = candidate.getValue();
3074 if (!addedToSplitFrames.contains(af))
3076 Viewport view = candidate.getKey();
3077 Desktop.addInternalFrame(af, view.getTitle(),
3078 safeInt(view.getWidth()), safeInt(view.getHeight()));
3079 af.setMenusForViewport();
3080 System.err.println("Failed to restore view " + view.getTitle()
3081 + " to split frame");
3086 * Gather back into tabbed views as flagged.
3088 for (SplitFrame sf : gatherTo)
3090 Desktop.instance.gatherViews(sf);
3093 splitFrameCandidates.clear();
3097 * Construct and display one SplitFrame holding DNA and protein alignments.
3100 * @param proteinFrame
3103 protected SplitFrame createSplitFrame(AlignFrame dnaFrame,
3104 AlignFrame proteinFrame)
3106 SplitFrame splitFrame = new SplitFrame(dnaFrame, proteinFrame);
3107 String title = MessageManager.getString("label.linked_view_title");
3108 int width = (int) dnaFrame.getBounds().getWidth();
3109 int height = (int) (dnaFrame.getBounds().getHeight()
3110 + proteinFrame.getBounds().getHeight() + 50);
3113 * SplitFrame location is saved to both enclosed frames
3115 splitFrame.setLocation(dnaFrame.getX(), dnaFrame.getY());
3116 Desktop.addInternalFrame(splitFrame, title, width, height);
3119 * And compute cDNA consensus (couldn't do earlier with consensus as
3120 * mappings were not yet present)
3122 proteinFrame.getViewport().alignmentChanged(proteinFrame.alignPanel);
3128 * check errorMessage for a valid error message and raise an error box in the
3129 * GUI or write the current errorMessage to stderr and then clear the error
3132 protected void reportErrors()
3134 reportErrors(false);
3137 protected void reportErrors(final boolean saving)
3139 if (errorMessage != null)
3141 final String finalErrorMessage = errorMessage;
3144 javax.swing.SwingUtilities.invokeLater(new Runnable()
3149 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
3151 "Error " + (saving ? "saving" : "loading")
3153 JvOptionPane.WARNING_MESSAGE);
3159 System.err.println("Problem loading Jalview file: " + errorMessage);
3162 errorMessage = null;
3165 Map<String, String> alreadyLoadedPDB = new HashMap<>();
3168 * when set, local views will be updated from view stored in JalviewXML
3169 * Currently (28th Sep 2008) things will go horribly wrong in vamsas document
3170 * sync if this is set to true.
3172 private final boolean updateLocalViews = false;
3175 * Returns the path to a temporary file holding the PDB file for the given PDB
3176 * id. The first time of asking, searches for a file of that name in the
3177 * Jalview project jar, and copies it to a new temporary file. Any repeat
3178 * requests just return the path to the file previously created.
3184 String loadPDBFile(jarInputStreamProvider jprovider, String pdbId,
3187 if (alreadyLoadedPDB.containsKey(pdbId))
3189 return alreadyLoadedPDB.get(pdbId).toString();
3192 String tempFile = copyJarEntry(jprovider, pdbId, "jalview_pdb",
3194 if (tempFile != null)
3196 alreadyLoadedPDB.put(pdbId, tempFile);
3202 * Copies the jar entry of given name to a new temporary file and returns the
3203 * path to the file, or null if the entry is not found.
3206 * @param jarEntryName
3208 * a prefix for the temporary file name, must be at least three
3210 * @param suffixModel
3211 * null or original file - so new file can be given the same suffix
3215 protected String copyJarEntry(jarInputStreamProvider jprovider,
3216 String jarEntryName, String prefix, String suffixModel)
3218 BufferedReader in = null;
3219 PrintWriter out = null;
3220 String suffix = ".tmp";
3221 if (suffixModel == null)
3223 suffixModel = jarEntryName;
3225 int sfpos = suffixModel.lastIndexOf(".");
3226 if (sfpos > -1 && sfpos < (suffixModel.length() - 1))
3228 suffix = "." + suffixModel.substring(sfpos + 1);
3232 JarInputStream jin = jprovider.getJarInputStream();
3234 * if (jprovider.startsWith("http://")) { jin = new JarInputStream(new
3235 * URL(jprovider).openStream()); } else { jin = new JarInputStream(new
3236 * FileInputStream(jprovider)); }
3239 JarEntry entry = null;
3242 entry = jin.getNextJarEntry();
3243 } while (entry != null && !entry.getName().equals(jarEntryName));
3246 in = new BufferedReader(new InputStreamReader(jin, UTF_8));
3247 File outFile = File.createTempFile(prefix, suffix);
3248 outFile.deleteOnExit();
3249 out = new PrintWriter(new FileOutputStream(outFile));
3252 while ((data = in.readLine()) != null)
3257 String t = outFile.getAbsolutePath();
3262 warn("Couldn't find entry in Jalview Jar for " + jarEntryName);
3264 } catch (Exception ex)
3266 ex.printStackTrace();
3274 } catch (IOException e)
3288 private class JvAnnotRow
3290 public JvAnnotRow(int i, AlignmentAnnotation jaa)
3297 * persisted version of annotation row from which to take vis properties
3299 public jalview.datamodel.AlignmentAnnotation template;
3302 * original position of the annotation row in the alignment
3308 * Load alignment frame from jalview XML DOM object
3310 * @param jalviewModel
3313 * filename source string
3314 * @param loadTreesAndStructures
3315 * when false only create Viewport
3317 * data source provider
3318 * @return alignment frame created from view stored in DOM
3320 AlignFrame loadFromObject(JalviewModel jalviewModel, String file,
3321 boolean loadTreesAndStructures, jarInputStreamProvider jprovider)
3323 SequenceSet vamsasSet = jalviewModel.getVamsasModel().getSequenceSet().get(0);
3324 List<Sequence> vamsasSeqs = vamsasSet.getSequence();
3326 // JalviewModelSequence jms = object.getJalviewModelSequence();
3328 // Viewport view = (jms.getViewportCount() > 0) ? jms.getViewport(0)
3330 Viewport view = (jalviewModel.getViewport().size() > 0)
3331 ? jalviewModel.getViewport().get(0)
3334 // ////////////////////////////////
3335 // INITIALISE ALIGNMENT SEQUENCESETID AND VIEWID
3338 // If we just load in the same jar file again, the sequenceSetId
3339 // will be the same, and we end up with multiple references
3340 // to the same sequenceSet. We must modify this id on load
3341 // so that each load of the file gives a unique id
3344 * used to resolve correct alignment dataset for alignments with multiple
3347 String uniqueSeqSetId = null;
3348 String viewId = null;
3351 uniqueSeqSetId = view.getSequenceSetId() + uniqueSetSuffix;
3352 viewId = (view.getId() == null ? null
3353 : view.getId() + uniqueSetSuffix);
3356 // ////////////////////////////////
3359 List<SequenceI> hiddenSeqs = null;
3361 List<SequenceI> tmpseqs = new ArrayList<>();
3363 boolean multipleView = false;
3364 SequenceI referenceseqForView = null;
3365 // JSeq[] jseqs = object.getJalviewModelSequence().getJSeq();
3366 List<JSeq> jseqs = jalviewModel.getJSeq();
3367 int vi = 0; // counter in vamsasSeq array
3368 for (int i = 0; i < jseqs.size(); i++)
3370 JSeq jseq = jseqs.get(i);
3371 String seqId = jseq.getId();
3373 SequenceI tmpSeq = seqRefIds.get(seqId);
3376 if (!incompleteSeqs.containsKey(seqId))
3378 // may not need this check, but keep it for at least 2.9,1 release
3379 if (tmpSeq.getStart() != jseq.getStart()
3380 || tmpSeq.getEnd() != jseq.getEnd())
3383 "Warning JAL-2154 regression: updating start/end for sequence "
3384 + tmpSeq.toString() + " to " + jseq);
3389 incompleteSeqs.remove(seqId);
3391 if (vamsasSeqs.size() > vi
3392 && vamsasSeqs.get(vi).getId().equals(seqId))
3394 // most likely we are reading a dataset XML document so
3395 // update from vamsasSeq section of XML for this sequence
3396 tmpSeq.setName(vamsasSeqs.get(vi).getName());
3397 tmpSeq.setDescription(vamsasSeqs.get(vi).getDescription());
3398 tmpSeq.setSequence(vamsasSeqs.get(vi).getSequence());
3403 // reading multiple views, so vamsasSeq set is a subset of JSeq
3404 multipleView = true;
3406 tmpSeq.setStart(jseq.getStart());
3407 tmpSeq.setEnd(jseq.getEnd());
3408 tmpseqs.add(tmpSeq);
3412 Sequence vamsasSeq = vamsasSeqs.get(vi);
3413 tmpSeq = new jalview.datamodel.Sequence(vamsasSeq.getName(),
3414 vamsasSeq.getSequence());
3415 tmpSeq.setDescription(vamsasSeq.getDescription());
3416 tmpSeq.setStart(jseq.getStart());
3417 tmpSeq.setEnd(jseq.getEnd());
3418 tmpSeq.setVamsasId(uniqueSetSuffix + seqId);
3419 seqRefIds.put(vamsasSeq.getId(), tmpSeq);
3420 tmpseqs.add(tmpSeq);
3424 if (safeBoolean(jseq.isViewreference()))
3426 referenceseqForView = tmpseqs.get(tmpseqs.size() - 1);
3429 if (jseq.isHidden() != null && jseq.isHidden().booleanValue())
3431 if (hiddenSeqs == null)
3433 hiddenSeqs = new ArrayList<>();
3436 hiddenSeqs.add(tmpSeq);
3441 // Create the alignment object from the sequence set
3442 // ///////////////////////////////
3443 SequenceI[] orderedSeqs = tmpseqs
3444 .toArray(new SequenceI[tmpseqs.size()]);
3446 AlignmentI al = null;
3447 // so we must create or recover the dataset alignment before going further
3448 // ///////////////////////////////
3449 if (vamsasSet.getDatasetId() == null || vamsasSet.getDatasetId() == "")
3451 // older jalview projects do not have a dataset - so creat alignment and
3453 al = new Alignment(orderedSeqs);
3454 al.setDataset(null);
3458 boolean isdsal = jalviewModel.getViewport().isEmpty();
3461 // we are importing a dataset record, so
3462 // recover reference to an alignment already materialsed as dataset
3463 al = getDatasetFor(vamsasSet.getDatasetId());
3467 // materialse the alignment
3468 al = new Alignment(orderedSeqs);
3472 addDatasetRef(vamsasSet.getDatasetId(), al);
3475 // finally, verify all data in vamsasSet is actually present in al
3476 // passing on flag indicating if it is actually a stored dataset
3477 recoverDatasetFor(vamsasSet, al, isdsal, uniqueSeqSetId);
3480 if (referenceseqForView != null)
3482 al.setSeqrep(referenceseqForView);
3484 // / Add the alignment properties
3485 for (int i = 0; i < vamsasSet.getSequenceSetProperties().size(); i++)
3487 SequenceSetProperties ssp = vamsasSet.getSequenceSetProperties()
3489 al.setProperty(ssp.getKey(), ssp.getValue());
3492 // ///////////////////////////////
3494 Hashtable pdbloaded = new Hashtable(); // TODO nothing writes to this??
3497 // load sequence features, database references and any associated PDB
3498 // structures for the alignment
3500 // prior to 2.10, this part would only be executed the first time a
3501 // sequence was encountered, but not afterwards.
3502 // now, for 2.10 projects, this is also done if the xml doc includes
3503 // dataset sequences not actually present in any particular view.
3505 for (int i = 0; i < vamsasSeqs.size(); i++)
3507 JSeq jseq = jseqs.get(i);
3508 if (jseq.getFeatures().size() > 0)
3510 List<Feature> features = jseq.getFeatures();
3511 for (int f = 0; f < features.size(); f++)
3513 Feature feat = features.get(f);
3514 SequenceFeature sf = new SequenceFeature(feat.getType(),
3515 feat.getDescription(), feat.getBegin(), feat.getEnd(),
3516 safeFloat(feat.getScore()), feat.getFeatureGroup());
3517 sf.setStatus(feat.getStatus());
3520 * load any feature attributes - include map-valued attributes
3522 Map<String, Map<String, String>> mapAttributes = new HashMap<>();
3523 for (int od = 0; od < feat.getOtherData().size(); od++)
3525 OtherData keyValue = feat.getOtherData().get(od);
3526 String attributeName = keyValue.getKey();
3527 String attributeValue = keyValue.getValue();
3528 if (attributeName.startsWith("LINK"))
3530 sf.addLink(attributeValue);
3534 String subAttribute = keyValue.getKey2();
3535 if (subAttribute == null)
3537 // simple string-valued attribute
3538 sf.setValue(attributeName, attributeValue);
3542 // attribute 'key' has sub-attribute 'key2'
3543 if (!mapAttributes.containsKey(attributeName))
3545 mapAttributes.put(attributeName, new HashMap<>());
3547 mapAttributes.get(attributeName).put(subAttribute,
3552 for (Entry<String, Map<String, String>> mapAttribute : mapAttributes
3555 sf.setValue(mapAttribute.getKey(), mapAttribute.getValue());
3558 // adds feature to datasequence's feature set (since Jalview 2.10)
3559 al.getSequenceAt(i).addSequenceFeature(sf);
3562 if (vamsasSeqs.get(i).getDBRef().size() > 0)
3564 // adds dbrefs to datasequence's set (since Jalview 2.10)
3566 al.getSequenceAt(i).getDatasetSequence() == null
3567 ? al.getSequenceAt(i)
3568 : al.getSequenceAt(i).getDatasetSequence(),
3571 if (jseq.getPdbids().size() > 0)
3573 List<Pdbids> ids = jseq.getPdbids();
3574 for (int p = 0; p < ids.size(); p++)
3576 Pdbids pdbid = ids.get(p);
3577 jalview.datamodel.PDBEntry entry = new jalview.datamodel.PDBEntry();
3578 entry.setId(pdbid.getId());
3579 if (pdbid.getType() != null)
3581 if (PDBEntry.Type.getType(pdbid.getType()) != null)
3583 entry.setType(PDBEntry.Type.getType(pdbid.getType()));
3587 entry.setType(PDBEntry.Type.FILE);
3590 // jprovider is null when executing 'New View'
3591 if (pdbid.getFile() != null && jprovider != null)
3593 if (!pdbloaded.containsKey(pdbid.getFile()))
3595 entry.setFile(loadPDBFile(jprovider, pdbid.getId(),
3600 entry.setFile(pdbloaded.get(pdbid.getId()).toString());
3604 if (pdbid.getPdbentryItem() != null)
3606 for (PdbentryItem item : pdbid.getPdbentryItem())
3608 for (Property pr : item.getProperty())
3610 entry.setProperty(pr.getName(), pr.getValue());
3615 for (Property prop : pdbid.getProperty())
3617 entry.setProperty(prop.getName(), prop.getValue());
3619 StructureSelectionManager
3620 .getStructureSelectionManager(Desktop.instance)
3621 .registerPDBEntry(entry);
3622 // adds PDBEntry to datasequence's set (since Jalview 2.10)
3623 if (al.getSequenceAt(i).getDatasetSequence() != null)
3625 al.getSequenceAt(i).getDatasetSequence().addPDBId(entry);
3629 al.getSequenceAt(i).addPDBId(entry);
3635 * load any HMMER profile
3639 String hmmJarFile = jseqs.get(i).getHmmerProfile();
3640 if (hmmJarFile != null && jprovider != null)
3642 loadHmmerProfile(jprovider, hmmJarFile, al.getSequenceAt(i));
3646 } // end !multipleview
3648 // ///////////////////////////////
3649 // LOAD SEQUENCE MAPPINGS
3651 if (vamsasSet.getAlcodonFrame().size() > 0)
3653 // TODO Potentially this should only be done once for all views of an
3655 List<AlcodonFrame> alc = vamsasSet.getAlcodonFrame();
3656 for (int i = 0; i < alc.size(); i++)
3658 AlignedCodonFrame cf = new AlignedCodonFrame();
3659 if (alc.get(i).getAlcodMap().size() > 0)
3661 List<AlcodMap> maps = alc.get(i).getAlcodMap();
3662 for (int m = 0; m < maps.size(); m++)
3664 AlcodMap map = maps.get(m);
3665 SequenceI dnaseq = seqRefIds.get(map.getDnasq());
3667 jalview.datamodel.Mapping mapping = null;
3668 // attach to dna sequence reference.
3669 if (map.getMapping() != null)
3671 mapping = addMapping(map.getMapping());
3672 if (dnaseq != null && mapping.getTo() != null)
3674 cf.addMap(dnaseq, mapping.getTo(), mapping.getMap());
3680 newAlcodMapRef(map.getDnasq(), cf, mapping));
3684 al.addCodonFrame(cf);
3689 // ////////////////////////////////
3691 List<JvAnnotRow> autoAlan = new ArrayList<>();
3694 * store any annotations which forward reference a group's ID
3696 Map<String, List<AlignmentAnnotation>> groupAnnotRefs = new Hashtable<>();
3698 if (vamsasSet.getAnnotation().size()/*Count()*/ > 0)
3700 List<Annotation> an = vamsasSet.getAnnotation();
3702 for (int i = 0; i < an.size(); i++)
3704 Annotation annotation = an.get(i);
3707 * test if annotation is automatically calculated for this view only
3709 boolean autoForView = false;
3710 if (annotation.getLabel().equals("Quality")
3711 || annotation.getLabel().equals("Conservation")
3712 || annotation.getLabel().equals("Consensus"))
3714 // Kludge for pre 2.5 projects which lacked the autocalculated flag
3716 // JAXB has no has() test; schema defaults value to false
3717 // if (!annotation.hasAutoCalculated())
3719 // annotation.setAutoCalculated(true);
3722 if (autoForView || annotation.isAutoCalculated())
3724 // remove ID - we don't recover annotation from other views for
3725 // view-specific annotation
3726 annotation.setId(null);
3729 // set visibility for other annotation in this view
3730 String annotationId = annotation.getId();
3731 if (annotationId != null && annotationIds.containsKey(annotationId))
3733 AlignmentAnnotation jda = annotationIds.get(annotationId);
3734 // in principle Visible should always be true for annotation displayed
3735 // in multiple views
3736 if (annotation.isVisible() != null)
3738 jda.visible = annotation.isVisible();
3741 al.addAnnotation(jda);
3745 // Construct new annotation from model.
3746 List<AnnotationElement> ae = annotation.getAnnotationElement();
3747 jalview.datamodel.Annotation[] anot = null;
3748 java.awt.Color firstColour = null;
3750 if (!annotation.isScoreOnly())
3752 anot = new jalview.datamodel.Annotation[al.getWidth()];
3753 for (int aa = 0; aa < ae.size() && aa < anot.length; aa++)
3755 AnnotationElement annElement = ae.get(aa);
3756 anpos = annElement.getPosition();
3758 if (anpos >= anot.length)
3763 float value = safeFloat(annElement.getValue());
3764 anot[anpos] = new jalview.datamodel.Annotation(
3765 annElement.getDisplayCharacter(),
3766 annElement.getDescription(),
3767 (annElement.getSecondaryStructure() == null
3768 || annElement.getSecondaryStructure()
3772 .getSecondaryStructure()
3775 anot[anpos].colour = new Color(safeInt(annElement.getColour()));
3776 if (firstColour == null)
3778 firstColour = anot[anpos].colour;
3782 jalview.datamodel.AlignmentAnnotation jaa = null;
3784 if (annotation.isGraph())
3786 float llim = 0, hlim = 0;
3787 // if (autoForView || an[i].isAutoCalculated()) {
3790 jaa = new jalview.datamodel.AlignmentAnnotation(
3791 annotation.getLabel(), annotation.getDescription(), anot,
3792 llim, hlim, safeInt(annotation.getGraphType()));
3794 jaa.graphGroup = safeInt(annotation.getGraphGroup());
3795 jaa._linecolour = firstColour;
3796 if (annotation.getThresholdLine() != null)
3798 jaa.setThreshold(new jalview.datamodel.GraphLine(
3799 safeFloat(annotation.getThresholdLine().getValue()),
3800 annotation.getThresholdLine().getLabel(),
3801 new java.awt.Color(safeInt(
3802 annotation.getThresholdLine().getColour()))));
3804 if (autoForView || annotation.isAutoCalculated())
3806 // Hardwire the symbol display line to ensure that labels for
3807 // histograms are displayed
3813 jaa = new jalview.datamodel.AlignmentAnnotation(
3814 annotation.getLabel(), annotation.getDescription(), anot);
3815 jaa._linecolour = firstColour;
3817 // register new annotation
3818 if (annotation.getId() != null)
3820 annotationIds.put(annotation.getId(), jaa);
3821 jaa.annotationId = annotation.getId();
3823 // recover sequence association
3824 String sequenceRef = annotation.getSequenceRef();
3825 if (sequenceRef != null)
3827 // from 2.9 sequenceRef is to sequence id (JAL-1781)
3828 SequenceI sequence = seqRefIds.get(sequenceRef);
3829 if (sequence == null)
3831 // in pre-2.9 projects sequence ref is to sequence name
3832 sequence = al.findName(sequenceRef);
3834 if (sequence != null)
3836 jaa.createSequenceMapping(sequence, 1, true);
3837 sequence.addAlignmentAnnotation(jaa);
3840 // and make a note of any group association
3841 if (annotation.getGroupRef() != null
3842 && annotation.getGroupRef().length() > 0)
3844 List<jalview.datamodel.AlignmentAnnotation> aal = groupAnnotRefs
3845 .get(annotation.getGroupRef());
3848 aal = new ArrayList<>();
3849 groupAnnotRefs.put(annotation.getGroupRef(), aal);
3854 if (annotation.getScore() != null)
3856 jaa.setScore(annotation.getScore().doubleValue());
3858 if (annotation.isVisible() != null)
3860 jaa.visible = annotation.isVisible().booleanValue();
3863 if (annotation.isCentreColLabels() != null)
3865 jaa.centreColLabels = annotation.isCentreColLabels()
3869 if (annotation.isScaleColLabels() != null)
3871 jaa.scaleColLabel = annotation.isScaleColLabels().booleanValue();
3873 if (annotation.isAutoCalculated())
3875 // newer files have an 'autoCalculated' flag and store calculation
3876 // state in viewport properties
3877 jaa.autoCalculated = true; // means annotation will be marked for
3878 // update at end of load.
3880 if (annotation.getGraphHeight() != null)
3882 jaa.graphHeight = annotation.getGraphHeight().intValue();
3884 jaa.belowAlignment = annotation.isBelowAlignment();
3885 jaa.setCalcId(annotation.getCalcId());
3886 if (annotation.getProperty().size() > 0)
3888 for (Annotation.Property prop : annotation
3891 jaa.setProperty(prop.getName(), prop.getValue());
3894 if (jaa.autoCalculated)
3896 autoAlan.add(new JvAnnotRow(i, jaa));
3899 // if (!autoForView)
3901 // add autocalculated group annotation and any user created annotation
3903 al.addAnnotation(jaa);
3907 // ///////////////////////
3909 // Create alignment markup and styles for this view
3910 if (jalviewModel.getJGroup().size() > 0)
3912 List<JGroup> groups = jalviewModel.getJGroup();
3913 boolean addAnnotSchemeGroup = false;
3914 for (int i = 0; i < groups.size(); i++)
3916 JGroup jGroup = groups.get(i);
3917 ColourSchemeI cs = null;
3918 if (jGroup.getColour() != null)
3920 if (jGroup.getColour().startsWith("ucs"))
3922 cs = getUserColourScheme(jalviewModel, jGroup.getColour());
3924 else if (jGroup.getColour().equals("AnnotationColourGradient")
3925 && jGroup.getAnnotationColours() != null)
3927 addAnnotSchemeGroup = true;
3931 cs = ColourSchemeProperty.getColourScheme(null, al,
3932 jGroup.getColour());
3935 int pidThreshold = safeInt(jGroup.getPidThreshold());
3937 Vector<SequenceI> seqs = new Vector<>();
3939 for (int s = 0; s < jGroup.getSeq().size(); s++)
3941 String seqId = jGroup.getSeq().get(s);
3942 SequenceI ts = seqRefIds.get(seqId);
3946 seqs.addElement(ts);
3950 if (seqs.size() < 1)
3955 SequenceGroup sg = new SequenceGroup(seqs, jGroup.getName(), cs,
3956 safeBoolean(jGroup.isDisplayBoxes()),
3957 safeBoolean(jGroup.isDisplayText()),
3958 safeBoolean(jGroup.isColourText()),
3959 safeInt(jGroup.getStart()), safeInt(jGroup.getEnd()));
3960 sg.getGroupColourScheme().setThreshold(pidThreshold, true);
3961 sg.getGroupColourScheme()
3962 .setConservationInc(safeInt(jGroup.getConsThreshold()));
3963 sg.setOutlineColour(new Color(safeInt(jGroup.getOutlineColour())));
3965 sg.textColour = new Color(safeInt(jGroup.getTextCol1()));
3966 sg.textColour2 = new Color(safeInt(jGroup.getTextCol2()));
3967 sg.setShowNonconserved(safeBoolean(jGroup.isShowUnconserved()));
3968 sg.thresholdTextColour = safeInt(jGroup.getTextColThreshold());
3969 // attributes with a default in the schema are never null
3970 sg.setShowConsensusHistogram(jGroup.isShowConsensusHistogram());
3971 sg.setshowSequenceLogo(jGroup.isShowSequenceLogo());
3972 sg.setNormaliseSequenceLogo(jGroup.isNormaliseSequenceLogo());
3973 sg.setIgnoreGapsConsensus(jGroup.isIgnoreGapsinConsensus());
3974 if (jGroup.getConsThreshold() != null
3975 && jGroup.getConsThreshold().intValue() != 0)
3977 Conservation c = new Conservation("All", sg.getSequences(null), 0,
3980 c.verdict(false, 25);
3981 sg.cs.setConservation(c);
3984 if (jGroup.getId() != null && groupAnnotRefs.size() > 0)
3986 // re-instate unique group/annotation row reference
3987 List<AlignmentAnnotation> jaal = groupAnnotRefs
3988 .get(jGroup.getId());
3991 for (AlignmentAnnotation jaa : jaal)
3994 if (jaa.autoCalculated)
3996 // match up and try to set group autocalc alignment row for this
3998 if (jaa.label.startsWith("Consensus for "))
4000 sg.setConsensus(jaa);
4002 // match up and try to set group autocalc alignment row for this
4004 if (jaa.label.startsWith("Conservation for "))
4006 sg.setConservationRow(jaa);
4013 if (addAnnotSchemeGroup)
4015 // reconstruct the annotation colourscheme
4016 sg.setColourScheme(constructAnnotationColour(
4017 jGroup.getAnnotationColours(), null, al, jalviewModel, false));
4023 // only dataset in this model, so just return.
4026 // ///////////////////////////////
4029 AlignFrame af = null;
4030 AlignViewport av = null;
4031 // now check to see if we really need to create a new viewport.
4032 if (multipleView && viewportsAdded.size() == 0)
4034 // We recovered an alignment for which a viewport already exists.
4035 // TODO: fix up any settings necessary for overlaying stored state onto
4036 // state recovered from another document. (may not be necessary).
4037 // we may need a binding from a viewport in memory to one recovered from
4039 // and then recover its containing af to allow the settings to be applied.
4040 // TODO: fix for vamsas demo
4042 "About to recover a viewport for existing alignment: Sequence set ID is "
4044 Object seqsetobj = retrieveExistingObj(uniqueSeqSetId);
4045 if (seqsetobj != null)
4047 if (seqsetobj instanceof String)
4049 uniqueSeqSetId = (String) seqsetobj;
4051 "Recovered extant sequence set ID mapping for ID : New Sequence set ID is "
4057 "Warning : Collision between sequence set ID string and existing jalview object mapping.");
4063 * indicate that annotation colours are applied across all groups (pre
4064 * Jalview 2.8.1 behaviour)
4066 boolean doGroupAnnColour = Jalview2XML.isVersionStringLaterThan("2.8.1",
4067 jalviewModel.getVersion());
4069 AlignmentPanel ap = null;
4070 boolean isnewview = true;
4073 // Check to see if this alignment already has a view id == viewId
4074 jalview.gui.AlignmentPanel views[] = Desktop
4075 .getAlignmentPanels(uniqueSeqSetId);
4076 if (views != null && views.length > 0)
4078 for (int v = 0; v < views.length; v++)
4080 if (views[v].av.getViewId().equalsIgnoreCase(viewId))
4082 // recover the existing alignpanel, alignframe, viewport
4083 af = views[v].alignFrame;
4086 // TODO: could even skip resetting view settings if we don't want to
4087 // change the local settings from other jalview processes
4096 af = loadViewport(file, jseqs, hiddenSeqs, al, jalviewModel, view,
4097 uniqueSeqSetId, viewId, autoAlan);
4098 av = af.getViewport();
4103 * Load any trees, PDB structures and viewers
4105 * Not done if flag is false (when this method is used for New View)
4107 if (loadTreesAndStructures)
4109 loadTrees(jalviewModel, view, af, av, ap);
4110 loadPCAViewers(jalviewModel, ap);
4111 loadPDBStructures(jprovider, jseqs, af, ap);
4112 loadRnaViewers(jprovider, jseqs, ap);
4114 // and finally return.
4119 * Loads a HMMER profile from a file stored in the project, and associates it
4120 * with the specified sequence
4126 protected void loadHmmerProfile(jarInputStreamProvider jprovider,
4127 String hmmJarFile, SequenceI seq)
4131 String hmmFile = copyJarEntry(jprovider, hmmJarFile, "hmm", null);
4132 HMMFile parser = new HMMFile(hmmFile, DataSourceType.FILE);
4133 HiddenMarkovModel hmmModel = parser.getHMM();
4134 hmmModel = new HiddenMarkovModel(hmmModel, seq);
4135 seq.setHMM(hmmModel);
4136 } catch (IOException e)
4138 warn("Error loading HMM profile for " + seq.getName() + ": "
4144 * Instantiate and link any saved RNA (Varna) viewers. The state of the Varna
4145 * panel is restored from separate jar entries, two (gapped and trimmed) per
4146 * sequence and secondary structure.
4148 * Currently each viewer shows just one sequence and structure (gapped and
4149 * trimmed), however this method is designed to support multiple sequences or
4150 * structures in viewers if wanted in future.
4156 private void loadRnaViewers(jarInputStreamProvider jprovider,
4157 List<JSeq> jseqs, AlignmentPanel ap)
4160 * scan the sequences for references to viewers; create each one the first
4161 * time it is referenced, add Rna models to existing viewers
4163 for (JSeq jseq : jseqs)
4165 for (int i = 0; i < jseq.getRnaViewer().size(); i++)
4167 RnaViewer viewer = jseq.getRnaViewer().get(i);
4168 AppVarna appVarna = findOrCreateVarnaViewer(viewer, uniqueSetSuffix,
4171 for (int j = 0; j < viewer.getSecondaryStructure().size(); j++)
4173 SecondaryStructure ss = viewer.getSecondaryStructure().get(j);
4174 SequenceI seq = seqRefIds.get(jseq.getId());
4175 AlignmentAnnotation ann = this.annotationIds
4176 .get(ss.getAnnotationId());
4179 * add the structure to the Varna display (with session state copied
4180 * from the jar to a temporary file)
4182 boolean gapped = safeBoolean(ss.isGapped());
4183 String rnaTitle = ss.getTitle();
4184 String sessionState = ss.getViewerState();
4185 String tempStateFile = copyJarEntry(jprovider, sessionState,
4187 RnaModel rna = new RnaModel(rnaTitle, ann, seq, null, gapped);
4188 appVarna.addModelSession(rna, rnaTitle, tempStateFile);
4190 appVarna.setInitialSelection(safeInt(viewer.getSelectedRna()));
4196 * Locate and return an already instantiated matching AppVarna, or create one
4200 * @param viewIdSuffix
4204 protected AppVarna findOrCreateVarnaViewer(RnaViewer viewer,
4205 String viewIdSuffix, AlignmentPanel ap)
4208 * on each load a suffix is appended to the saved viewId, to avoid conflicts
4209 * if load is repeated
4211 String postLoadId = viewer.getViewId() + viewIdSuffix;
4212 for (JInternalFrame frame : getAllFrames())
4214 if (frame instanceof AppVarna)
4216 AppVarna varna = (AppVarna) frame;
4217 if (postLoadId.equals(varna.getViewId()))
4219 // this viewer is already instantiated
4220 // could in future here add ap as another 'parent' of the
4221 // AppVarna window; currently just 1-to-many
4228 * viewer not found - make it
4230 RnaViewerModel model = new RnaViewerModel(postLoadId, viewer.getTitle(),
4231 safeInt(viewer.getXpos()), safeInt(viewer.getYpos()),
4232 safeInt(viewer.getWidth()), safeInt(viewer.getHeight()),
4233 safeInt(viewer.getDividerLocation()));
4234 AppVarna varna = new AppVarna(model, ap);
4240 * Load any saved trees
4248 protected void loadTrees(JalviewModel jm, Viewport view,
4249 AlignFrame af, AlignViewport av, AlignmentPanel ap)
4251 // TODO result of automated refactoring - are all these parameters needed?
4254 for (int t = 0; t < jm.getTree().size(); t++)
4257 Tree tree = jm.getTree().get(t);
4259 TreePanel tp = (TreePanel) retrieveExistingObj(tree.getId());
4262 tp = af.showNewickTree(new NewickFile(tree.getNewick()),
4263 tree.getTitle(), safeInt(tree.getWidth()),
4264 safeInt(tree.getHeight()), safeInt(tree.getXpos()),
4265 safeInt(tree.getYpos()));
4266 if (tree.getId() != null)
4268 // perhaps bind the tree id to something ?
4273 // update local tree attributes ?
4274 // TODO: should check if tp has been manipulated by user - if so its
4275 // settings shouldn't be modified
4276 tp.setTitle(tree.getTitle());
4277 tp.setBounds(new Rectangle(safeInt(tree.getXpos()),
4278 safeInt(tree.getYpos()), safeInt(tree.getWidth()),
4279 safeInt(tree.getHeight())));
4280 tp.setViewport(av); // af.viewport;
4281 // TODO: verify 'associate with all views' works still
4282 tp.getTreeCanvas().setViewport(av); // af.viewport;
4283 tp.getTreeCanvas().setAssociatedPanel(ap); // af.alignPanel;
4285 tp.getTreeCanvas().setApplyToAllViews(tree.isLinkToAllViews());
4288 warn("There was a problem recovering stored Newick tree: \n"
4289 + tree.getNewick());
4293 tp.fitToWindow.setState(safeBoolean(tree.isFitToWindow()));
4294 tp.fitToWindow_actionPerformed(null);
4296 if (tree.getFontName() != null)
4299 new Font(tree.getFontName(), safeInt(tree.getFontStyle()),
4300 safeInt(tree.getFontSize())));
4305 new Font(view.getFontName(), safeInt(view.getFontStyle()),
4306 safeInt(view.getFontSize())));
4309 tp.showPlaceholders(safeBoolean(tree.isMarkUnlinked()));
4310 tp.showBootstrap(safeBoolean(tree.isShowBootstrap()));
4311 tp.showDistances(safeBoolean(tree.isShowDistances()));
4313 tp.getTreeCanvas().setThreshold(safeFloat(tree.getThreshold()));
4315 if (safeBoolean(tree.isCurrentTree()))
4317 af.getViewport().setCurrentTree(tp.getTree());
4321 } catch (Exception ex)
4323 ex.printStackTrace();
4328 * Load and link any saved structure viewers.
4335 protected void loadPDBStructures(jarInputStreamProvider jprovider,
4336 List<JSeq> jseqs, AlignFrame af, AlignmentPanel ap)
4339 * Run through all PDB ids on the alignment, and collect mappings between
4340 * distinct view ids and all sequences referring to that view.
4342 Map<String, StructureViewerModel> structureViewers = new LinkedHashMap<>();
4344 for (int i = 0; i < jseqs.size(); i++)
4346 JSeq jseq = jseqs.get(i);
4347 if (jseq.getPdbids().size() > 0)
4349 List<Pdbids> ids = jseq.getPdbids();
4350 for (int p = 0; p < ids.size(); p++)
4352 Pdbids pdbid = ids.get(p);
4353 final int structureStateCount = pdbid.getStructureState().size();
4354 for (int s = 0; s < structureStateCount; s++)
4356 // check to see if we haven't already created this structure view
4357 final StructureState structureState = pdbid
4358 .getStructureState().get(s);
4359 String sviewid = (structureState.getViewId() == null) ? null
4360 : structureState.getViewId() + uniqueSetSuffix;
4361 jalview.datamodel.PDBEntry jpdb = new jalview.datamodel.PDBEntry();
4362 // Originally : pdbid.getFile()
4363 // : TODO: verify external PDB file recovery still works in normal
4364 // jalview project load
4366 loadPDBFile(jprovider, pdbid.getId(), pdbid.getFile()));
4367 jpdb.setId(pdbid.getId());
4369 int x = safeInt(structureState.getXpos());
4370 int y = safeInt(structureState.getYpos());
4371 int width = safeInt(structureState.getWidth());
4372 int height = safeInt(structureState.getHeight());
4374 // Probably don't need to do this anymore...
4375 // Desktop.desktop.getComponentAt(x, y);
4376 // TODO: NOW: check that this recovers the PDB file correctly.
4377 String pdbFile = loadPDBFile(jprovider, pdbid.getId(),
4379 jalview.datamodel.SequenceI seq = seqRefIds
4380 .get(jseq.getId() + "");
4381 if (sviewid == null)
4383 sviewid = "_jalview_pre2_4_" + x + "," + y + "," + width + ","
4386 if (!structureViewers.containsKey(sviewid))
4388 structureViewers.put(sviewid,
4389 new StructureViewerModel(x, y, width, height, false,
4390 false, true, structureState.getViewId(),
4391 structureState.getType()));
4392 // Legacy pre-2.7 conversion JAL-823 :
4393 // do not assume any view has to be linked for colour by
4397 // assemble String[] { pdb files }, String[] { id for each
4398 // file }, orig_fileloc, SequenceI[][] {{ seqs_file 1 }, {
4399 // seqs_file 2}, boolean[] {
4400 // linkAlignPanel,superposeWithAlignpanel}} from hash
4401 StructureViewerModel jmoldat = structureViewers.get(sviewid);
4402 jmoldat.setAlignWithPanel(jmoldat.isAlignWithPanel()
4403 || structureState.isAlignwithAlignPanel());
4406 * Default colour by linked panel to false if not specified (e.g.
4407 * for pre-2.7 projects)
4409 boolean colourWithAlignPanel = jmoldat.isColourWithAlignPanel();
4410 colourWithAlignPanel |= structureState.isColourwithAlignPanel();
4411 jmoldat.setColourWithAlignPanel(colourWithAlignPanel);
4414 * Default colour by viewer to true if not specified (e.g. for
4417 boolean colourByViewer = jmoldat.isColourByViewer();
4418 colourByViewer &= structureState.isColourByJmol();
4419 jmoldat.setColourByViewer(colourByViewer);
4421 if (jmoldat.getStateData().length() < structureState
4422 .getValue()/*Content()*/.length())
4424 jmoldat.setStateData(structureState.getValue());// Content());
4426 if (pdbid.getFile() != null)
4428 File mapkey = new File(pdbid.getFile());
4429 StructureData seqstrmaps = jmoldat.getFileData().get(mapkey);
4430 if (seqstrmaps == null)
4432 jmoldat.getFileData().put(mapkey,
4433 seqstrmaps = jmoldat.new StructureData(pdbFile,
4436 if (!seqstrmaps.getSeqList().contains(seq))
4438 seqstrmaps.getSeqList().add(seq);
4444 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");
4451 // Instantiate the associated structure views
4452 for (Entry<String, StructureViewerModel> entry : structureViewers
4457 createOrLinkStructureViewer(entry, af, ap, jprovider);
4458 } catch (Exception e)
4461 "Error loading structure viewer: " + e.getMessage());
4462 // failed - try the next one
4474 protected void createOrLinkStructureViewer(
4475 Entry<String, StructureViewerModel> viewerData, AlignFrame af,
4476 AlignmentPanel ap, jarInputStreamProvider jprovider)
4478 final StructureViewerModel stateData = viewerData.getValue();
4481 * Search for any viewer windows already open from other alignment views
4482 * that exactly match the stored structure state
4484 StructureViewerBase comp = findMatchingViewer(viewerData);
4488 linkStructureViewer(ap, comp, stateData);
4493 * From 2.9: stateData.type contains JMOL or CHIMERA, data is in jar entry
4494 * "viewer_"+stateData.viewId
4496 if (ViewerType.CHIMERA.toString().equals(stateData.getType()))
4498 createChimeraViewer(viewerData, af, jprovider);
4503 * else Jmol (if pre-2.9, stateData contains JMOL state string)
4505 createJmolViewer(viewerData, af, jprovider);
4510 * Create a new Chimera viewer.
4516 protected void createChimeraViewer(
4517 Entry<String, StructureViewerModel> viewerData, AlignFrame af,
4518 jarInputStreamProvider jprovider)
4520 StructureViewerModel data = viewerData.getValue();
4521 String chimeraSessionFile = data.getStateData();
4524 * Copy Chimera session from jar entry "viewer_"+viewId to a temporary file
4526 * NB this is the 'saved' viewId as in the project file XML, _not_ the
4527 * 'uniquified' sviewid used to reconstruct the viewer here
4529 String viewerJarEntryName = getViewerJarEntryName(data.getViewId());
4530 chimeraSessionFile = copyJarEntry(jprovider, viewerJarEntryName,
4533 Set<Entry<File, StructureData>> fileData = data.getFileData()
4535 List<PDBEntry> pdbs = new ArrayList<>();
4536 List<SequenceI[]> allseqs = new ArrayList<>();
4537 for (Entry<File, StructureData> pdb : fileData)
4539 String filePath = pdb.getValue().getFilePath();
4540 String pdbId = pdb.getValue().getPdbId();
4541 // pdbs.add(new PDBEntry(filePath, pdbId));
4542 pdbs.add(new PDBEntry(pdbId, null, PDBEntry.Type.PDB, filePath));
4543 final List<SequenceI> seqList = pdb.getValue().getSeqList();
4544 SequenceI[] seqs = seqList.toArray(new SequenceI[seqList.size()]);
4548 boolean colourByChimera = data.isColourByViewer();
4549 boolean colourBySequence = data.isColourWithAlignPanel();
4551 // TODO use StructureViewer as a factory here, see JAL-1761
4552 final PDBEntry[] pdbArray = pdbs.toArray(new PDBEntry[pdbs.size()]);
4553 final SequenceI[][] seqsArray = allseqs
4554 .toArray(new SequenceI[allseqs.size()][]);
4555 String newViewId = viewerData.getKey();
4557 ChimeraViewFrame cvf = new ChimeraViewFrame(chimeraSessionFile,
4558 af.alignPanel, pdbArray, seqsArray, colourByChimera,
4559 colourBySequence, newViewId);
4560 cvf.setSize(data.getWidth(), data.getHeight());
4561 cvf.setLocation(data.getX(), data.getY());
4565 * Create a new Jmol window. First parse the Jmol state to translate filenames
4566 * loaded into the view, and record the order in which files are shown in the
4567 * Jmol view, so we can add the sequence mappings in same order.
4573 protected void createJmolViewer(
4574 final Entry<String, StructureViewerModel> viewerData,
4575 AlignFrame af, jarInputStreamProvider jprovider)
4577 final StructureViewerModel svattrib = viewerData.getValue();
4578 String state = svattrib.getStateData();
4581 * Pre-2.9: state element value is the Jmol state string
4583 * 2.9+: @type is "JMOL", state data is in a Jar file member named "viewer_"
4586 if (ViewerType.JMOL.toString().equals(svattrib.getType()))
4588 state = readJarEntry(jprovider,
4589 getViewerJarEntryName(svattrib.getViewId()));
4592 List<String> pdbfilenames = new ArrayList<>();
4593 List<SequenceI[]> seqmaps = new ArrayList<>();
4594 List<String> pdbids = new ArrayList<>();
4595 StringBuilder newFileLoc = new StringBuilder(64);
4596 int cp = 0, ncp, ecp;
4597 Map<File, StructureData> oldFiles = svattrib.getFileData();
4598 while ((ncp = state.indexOf("load ", cp)) > -1)
4602 // look for next filename in load statement
4603 newFileLoc.append(state.substring(cp,
4604 ncp = (state.indexOf("\"", ncp + 1) + 1)));
4605 String oldfilenam = state.substring(ncp,
4606 ecp = state.indexOf("\"", ncp));
4607 // recover the new mapping data for this old filename
4608 // have to normalize filename - since Jmol and jalview do
4610 // translation differently.
4611 StructureData filedat = oldFiles.get(new File(oldfilenam));
4612 if (filedat == null)
4614 String reformatedOldFilename = oldfilenam.replaceAll("/", "\\\\");
4615 filedat = oldFiles.get(new File(reformatedOldFilename));
4617 newFileLoc.append(Platform.escapeBackslashes(filedat.getFilePath()));
4618 pdbfilenames.add(filedat.getFilePath());
4619 pdbids.add(filedat.getPdbId());
4620 seqmaps.add(filedat.getSeqList().toArray(new SequenceI[0]));
4621 newFileLoc.append("\"");
4622 cp = ecp + 1; // advance beyond last \" and set cursor so we can
4623 // look for next file statement.
4624 } while ((ncp = state.indexOf("/*file*/", cp)) > -1);
4628 // just append rest of state
4629 newFileLoc.append(state.substring(cp));
4633 System.err.print("Ignoring incomplete Jmol state for PDB ids: ");
4634 newFileLoc = new StringBuilder(state);
4635 newFileLoc.append("; load append ");
4636 for (File id : oldFiles.keySet())
4638 // add this and any other pdb files that should be present in
4640 StructureData filedat = oldFiles.get(id);
4641 newFileLoc.append(filedat.getFilePath());
4642 pdbfilenames.add(filedat.getFilePath());
4643 pdbids.add(filedat.getPdbId());
4644 seqmaps.add(filedat.getSeqList().toArray(new SequenceI[0]));
4645 newFileLoc.append(" \"");
4646 newFileLoc.append(filedat.getFilePath());
4647 newFileLoc.append("\"");
4650 newFileLoc.append(";");
4653 if (newFileLoc.length() == 0)
4657 int histbug = newFileLoc.indexOf("history = ");
4661 * change "history = [true|false];" to "history = [1|0];"
4664 int diff = histbug == -1 ? -1 : newFileLoc.indexOf(";", histbug);
4665 String val = (diff == -1) ? null
4666 : newFileLoc.substring(histbug, diff);
4667 if (val != null && val.length() >= 4)
4669 if (val.contains("e")) // eh? what can it be?
4671 if (val.trim().equals("true"))
4679 newFileLoc.replace(histbug, diff, val);
4684 final String[] pdbf = pdbfilenames
4685 .toArray(new String[pdbfilenames.size()]);
4686 final String[] id = pdbids.toArray(new String[pdbids.size()]);
4687 final SequenceI[][] sq = seqmaps
4688 .toArray(new SequenceI[seqmaps.size()][]);
4689 final String fileloc = newFileLoc.toString();
4690 final String sviewid = viewerData.getKey();
4691 final AlignFrame alf = af;
4692 final Rectangle rect = new Rectangle(svattrib.getX(), svattrib.getY(),
4693 svattrib.getWidth(), svattrib.getHeight());
4696 javax.swing.SwingUtilities.invokeAndWait(new Runnable()
4701 JalviewStructureDisplayI sview = null;
4704 sview = new StructureViewer(
4705 alf.alignPanel.getStructureSelectionManager())
4706 .createView(StructureViewer.ViewerType.JMOL,
4707 pdbf, id, sq, alf.alignPanel, svattrib,
4708 fileloc, rect, sviewid);
4709 addNewStructureViewer(sview);
4710 } catch (OutOfMemoryError ex)
4712 new OOMWarning("restoring structure view for PDB id " + id,
4713 (OutOfMemoryError) ex.getCause());
4714 if (sview != null && sview.isVisible())
4716 sview.closeViewer(false);
4717 sview.setVisible(false);
4723 } catch (InvocationTargetException ex)
4725 warn("Unexpected error when opening Jmol view.", ex);
4727 } catch (InterruptedException e)
4729 // e.printStackTrace();
4735 * Generates a name for the entry in the project jar file to hold state
4736 * information for a structure viewer
4741 protected String getViewerJarEntryName(String viewId)
4743 return VIEWER_PREFIX + viewId;
4747 * Returns any open frame that matches given structure viewer data. The match
4748 * is based on the unique viewId, or (for older project versions) the frame's
4754 protected StructureViewerBase findMatchingViewer(
4755 Entry<String, StructureViewerModel> viewerData)
4757 final String sviewid = viewerData.getKey();
4758 final StructureViewerModel svattrib = viewerData.getValue();
4759 StructureViewerBase comp = null;
4760 JInternalFrame[] frames = getAllFrames();
4761 for (JInternalFrame frame : frames)
4763 if (frame instanceof StructureViewerBase)
4766 * Post jalview 2.4 schema includes structure view id
4768 if (sviewid != null && ((StructureViewerBase) frame).getViewId()
4771 comp = (StructureViewerBase) frame;
4772 break; // break added in 2.9
4775 * Otherwise test for matching position and size of viewer frame
4777 else if (frame.getX() == svattrib.getX()
4778 && frame.getY() == svattrib.getY()
4779 && frame.getHeight() == svattrib.getHeight()
4780 && frame.getWidth() == svattrib.getWidth())
4782 comp = (StructureViewerBase) frame;
4783 // no break in faint hope of an exact match on viewId
4791 * Link an AlignmentPanel to an existing structure viewer.
4796 * @param useinViewerSuperpos
4797 * @param usetoColourbyseq
4798 * @param viewerColouring
4800 protected void linkStructureViewer(AlignmentPanel ap,
4801 StructureViewerBase viewer, StructureViewerModel stateData)
4803 // NOTE: if the jalview project is part of a shared session then
4804 // view synchronization should/could be done here.
4806 final boolean useinViewerSuperpos = stateData.isAlignWithPanel();
4807 final boolean usetoColourbyseq = stateData.isColourWithAlignPanel();
4808 final boolean viewerColouring = stateData.isColourByViewer();
4809 Map<File, StructureData> oldFiles = stateData.getFileData();
4812 * Add mapping for sequences in this view to an already open viewer
4814 final AAStructureBindingModel binding = viewer.getBinding();
4815 for (File id : oldFiles.keySet())
4817 // add this and any other pdb files that should be present in the
4819 StructureData filedat = oldFiles.get(id);
4820 String pdbFile = filedat.getFilePath();
4821 SequenceI[] seq = filedat.getSeqList().toArray(new SequenceI[0]);
4822 binding.getSsm().setMapping(seq, null, pdbFile, DataSourceType.FILE,
4824 binding.addSequenceForStructFile(pdbFile, seq);
4826 // and add the AlignmentPanel's reference to the view panel
4827 viewer.addAlignmentPanel(ap);
4828 if (useinViewerSuperpos)
4830 viewer.useAlignmentPanelForSuperposition(ap);
4834 viewer.excludeAlignmentPanelForSuperposition(ap);
4836 if (usetoColourbyseq)
4838 viewer.useAlignmentPanelForColourbyseq(ap, !viewerColouring);
4842 viewer.excludeAlignmentPanelForColourbyseq(ap);
4847 * Get all frames within the Desktop.
4851 protected JInternalFrame[] getAllFrames()
4853 JInternalFrame[] frames = null;
4854 // TODO is this necessary - is it safe - risk of hanging?
4859 frames = Desktop.desktop.getAllFrames();
4860 } catch (ArrayIndexOutOfBoundsException e)
4862 // occasional No such child exceptions are thrown here...
4866 } catch (InterruptedException f)
4870 } while (frames == null);
4875 * Answers true if 'version' is equal to or later than 'supported', where each
4876 * is formatted as major/minor versions like "2.8.3" or "2.3.4b1" for bugfix
4877 * changes. Development and test values for 'version' are leniently treated
4881 * - minimum version we are comparing against
4883 * - version of data being processsed
4886 public static boolean isVersionStringLaterThan(String supported,
4889 if (supported == null || version == null
4890 || version.equalsIgnoreCase("DEVELOPMENT BUILD")
4891 || version.equalsIgnoreCase("Test")
4892 || version.equalsIgnoreCase("AUTOMATED BUILD"))
4894 System.err.println("Assuming project file with "
4895 + (version == null ? "null" : version)
4896 + " is compatible with Jalview version " + supported);
4901 return StringUtils.compareVersions(version, supported, "b") >= 0;
4905 Vector<JalviewStructureDisplayI> newStructureViewers = null;
4907 protected void addNewStructureViewer(JalviewStructureDisplayI sview)
4909 if (newStructureViewers != null)
4911 sview.getBinding().setFinishedLoadingFromArchive(false);
4912 newStructureViewers.add(sview);
4916 protected void setLoadingFinishedForNewStructureViewers()
4918 if (newStructureViewers != null)
4920 for (JalviewStructureDisplayI sview : newStructureViewers)
4922 sview.getBinding().setFinishedLoadingFromArchive(true);
4924 newStructureViewers.clear();
4925 newStructureViewers = null;
4929 AlignFrame loadViewport(String file, List<JSeq> JSEQ,
4930 List<SequenceI> hiddenSeqs, AlignmentI al,
4931 JalviewModel jm, Viewport view, String uniqueSeqSetId,
4932 String viewId, List<JvAnnotRow> autoAlan)
4934 AlignFrame af = null;
4935 af = new AlignFrame(al, safeInt(view.getWidth()),
4936 safeInt(view.getHeight()), uniqueSeqSetId, viewId);
4938 af.setFileName(file, FileFormat.Jalview);
4940 final AlignViewport viewport = af.getViewport();
4941 for (int i = 0; i < JSEQ.size(); i++)
4943 int colour = safeInt(JSEQ.get(i).getColour());
4944 viewport.setSequenceColour(viewport.getAlignment().getSequenceAt(i),
4950 viewport.setColourByReferenceSeq(true);
4951 viewport.setDisplayReferenceSeq(true);
4954 viewport.setGatherViewsHere(safeBoolean(view.isGatheredViews()));
4956 if (view.getSequenceSetId() != null)
4958 AlignmentViewport av = viewportsAdded.get(uniqueSeqSetId);
4960 viewport.setSequenceSetId(uniqueSeqSetId);
4963 // propagate shared settings to this new view
4964 viewport.setHistoryList(av.getHistoryList());
4965 viewport.setRedoList(av.getRedoList());
4969 viewportsAdded.put(uniqueSeqSetId, viewport);
4971 // TODO: check if this method can be called repeatedly without
4972 // side-effects if alignpanel already registered.
4973 PaintRefresher.Register(af.alignPanel, uniqueSeqSetId);
4975 // apply Hidden regions to view.
4976 if (hiddenSeqs != null)
4978 for (int s = 0; s < JSEQ.size(); s++)
4980 SequenceGroup hidden = new SequenceGroup();
4981 boolean isRepresentative = false;
4982 for (int r = 0; r < JSEQ.get(s).getHiddenSequences().size(); r++)
4984 isRepresentative = true;
4985 SequenceI sequenceToHide = al
4986 .getSequenceAt(JSEQ.get(s).getHiddenSequences().get(r));
4987 hidden.addSequence(sequenceToHide, false);
4988 // remove from hiddenSeqs list so we don't try to hide it twice
4989 hiddenSeqs.remove(sequenceToHide);
4991 if (isRepresentative)
4993 SequenceI representativeSequence = al.getSequenceAt(s);
4994 hidden.addSequence(representativeSequence, false);
4995 viewport.hideRepSequences(representativeSequence, hidden);
4999 SequenceI[] hseqs = hiddenSeqs
5000 .toArray(new SequenceI[hiddenSeqs.size()]);
5001 viewport.hideSequence(hseqs);
5004 // recover view properties and display parameters
5006 viewport.setShowAnnotation(safeBoolean(view.isShowAnnotation()));
5007 viewport.setAbovePIDThreshold(safeBoolean(view.isPidSelected()));
5008 final int pidThreshold = safeInt(view.getPidThreshold());
5009 viewport.setThreshold(pidThreshold);
5011 viewport.setColourText(safeBoolean(view.isShowColourText()));
5014 .setConservationSelected(
5015 safeBoolean(view.isConservationSelected()));
5016 viewport.setIncrement(safeInt(view.getConsThreshold()));
5017 viewport.setShowJVSuffix(safeBoolean(view.isShowFullId()));
5018 viewport.setRightAlignIds(safeBoolean(view.isRightAlignIds()));
5019 viewport.setFont(new Font(view.getFontName(),
5020 safeInt(view.getFontStyle()), safeInt(view.getFontSize())),
5022 ViewStyleI vs = viewport.getViewStyle();
5023 vs.setScaleProteinAsCdna(view.isScaleProteinAsCdna());
5024 viewport.setViewStyle(vs);
5025 // TODO: allow custom charWidth/Heights to be restored by updating them
5026 // after setting font - which means set above to false
5027 viewport.setRenderGaps(safeBoolean(view.isRenderGaps()));
5028 viewport.setWrapAlignment(safeBoolean(view.isWrapAlignment()));
5029 viewport.setShowAnnotation(safeBoolean(view.isShowAnnotation()));
5031 viewport.setShowBoxes(safeBoolean(view.isShowBoxes()));
5033 viewport.setShowText(safeBoolean(view.isShowText()));
5035 viewport.setTextColour(new Color(safeInt(view.getTextCol1())));
5036 viewport.setTextColour2(new Color(safeInt(view.getTextCol2())));
5037 viewport.setThresholdTextColour(safeInt(view.getTextColThreshold()));
5038 viewport.setShowUnconserved(view.isShowUnconserved());
5039 viewport.getRanges().setStartRes(safeInt(view.getStartRes()));
5041 if (view.getViewName() != null)
5043 viewport.setViewName(view.getViewName());
5044 af.setInitialTabVisible();
5046 af.setBounds(safeInt(view.getXpos()), safeInt(view.getYpos()),
5047 safeInt(view.getWidth()), safeInt(view.getHeight()));
5048 // startSeq set in af.alignPanel.updateLayout below
5049 af.alignPanel.updateLayout();
5050 ColourSchemeI cs = null;
5051 // apply colourschemes
5052 if (view.getBgColour() != null)
5054 if (view.getBgColour().startsWith("ucs"))
5056 cs = getUserColourScheme(jm, view.getBgColour());
5058 else if (view.getBgColour().startsWith("Annotation"))
5060 AnnotationColourScheme viewAnnColour = view.getAnnotationColours();
5061 cs = constructAnnotationColour(viewAnnColour, af, al, jm, true);
5068 cs = ColourSchemeProperty.getColourScheme(af.getViewport(), al,
5069 view.getBgColour());
5074 * turn off 'alignment colour applies to all groups'
5075 * while restoring global colour scheme
5077 viewport.setColourAppliesToAllGroups(false);
5078 viewport.setGlobalColourScheme(cs);
5079 viewport.getResidueShading().setThreshold(pidThreshold,
5080 view.isIgnoreGapsinConsensus());
5081 viewport.getResidueShading()
5082 .setConsensus(viewport.getSequenceConsensusHash());
5083 if (safeBoolean(view.isConservationSelected()) && cs != null)
5085 viewport.getResidueShading()
5086 .setConservationInc(safeInt(view.getConsThreshold()));
5088 af.changeColour(cs);
5089 viewport.setColourAppliesToAllGroups(true);
5092 .setShowSequenceFeatures(
5093 safeBoolean(view.isShowSequenceFeatures()));
5095 viewport.setCentreColumnLabels(view.isCentreColumnLabels());
5096 viewport.setIgnoreGapsConsensus(view.isIgnoreGapsinConsensus(), null);
5097 viewport.setFollowHighlight(view.isFollowHighlight());
5098 viewport.followSelection = view.isFollowSelection();
5099 viewport.setShowConsensusHistogram(view.isShowConsensusHistogram());
5100 viewport.setShowSequenceLogo(view.isShowSequenceLogo());
5101 viewport.setNormaliseSequenceLogo(view.isNormaliseSequenceLogo());
5102 viewport.setShowDBRefs(safeBoolean(view.isShowDbRefTooltip()));
5103 viewport.setShowNPFeats(safeBoolean(view.isShowNPfeatureTooltip()));
5104 viewport.setShowGroupConsensus(view.isShowGroupConsensus());
5105 viewport.setShowGroupConservation(view.isShowGroupConservation());
5106 viewport.setShowComplementFeatures(view.isShowComplementFeatures());
5107 viewport.setShowComplementFeaturesOnTop(
5108 view.isShowComplementFeaturesOnTop());
5110 // recover feature settings
5111 if (jm.getFeatureSettings() != null)
5113 FeatureRendererModel fr = af.alignPanel.getSeqPanel().seqCanvas
5114 .getFeatureRenderer();
5115 FeaturesDisplayed fdi;
5116 viewport.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
5117 String[] renderOrder = new String[jm.getFeatureSettings()
5118 .getSetting().size()];
5119 Map<String, FeatureColourI> featureColours = new Hashtable<>();
5120 Map<String, Float> featureOrder = new Hashtable<>();
5122 for (int fs = 0; fs < jm.getFeatureSettings()
5123 .getSetting().size(); fs++)
5125 Setting setting = jm.getFeatureSettings().getSetting().get(fs);
5126 String featureType = setting.getType();
5129 * restore feature filters (if any)
5131 jalview.xml.binding.jalview.FeatureMatcherSet filters = setting
5133 if (filters != null)
5135 FeatureMatcherSetI filter = Jalview2XML
5136 .parseFilter(featureType, filters);
5137 if (!filter.isEmpty())
5139 fr.setFeatureFilter(featureType, filter);
5144 * restore feature colour scheme
5146 Color maxColour = new Color(setting.getColour());
5147 if (setting.getMincolour() != null)
5150 * minColour is always set unless a simple colour
5151 * (including for colour by label though it doesn't use it)
5153 Color minColour = new Color(setting.getMincolour().intValue());
5154 Color noValueColour = minColour;
5155 NoValueColour noColour = setting.getNoValueColour();
5156 if (noColour == NoValueColour.NONE)
5158 noValueColour = null;
5160 else if (noColour == NoValueColour.MAX)
5162 noValueColour = maxColour;
5164 float min = safeFloat(safeFloat(setting.getMin()));
5165 float max = setting.getMax() == null ? 1f
5166 : setting.getMax().floatValue();
5167 FeatureColourI gc = new FeatureColour(maxColour, minColour,
5169 noValueColour, min, max);
5170 if (setting.getAttributeName().size() > 0)
5172 gc.setAttributeName(setting.getAttributeName().toArray(
5173 new String[setting.getAttributeName().size()]));
5175 if (setting.getThreshold() != null)
5177 gc.setThreshold(setting.getThreshold().floatValue());
5178 int threshstate = safeInt(setting.getThreshstate());
5179 // -1 = None, 0 = Below, 1 = Above threshold
5180 if (threshstate == 0)
5182 gc.setBelowThreshold(true);
5184 else if (threshstate == 1)
5186 gc.setAboveThreshold(true);
5189 gc.setAutoScaled(true); // default
5190 if (setting.isAutoScale() != null)
5192 gc.setAutoScaled(setting.isAutoScale());
5194 if (setting.isColourByLabel() != null)
5196 gc.setColourByLabel(setting.isColourByLabel());
5198 // and put in the feature colour table.
5199 featureColours.put(featureType, gc);
5203 featureColours.put(featureType,
5204 new FeatureColour(maxColour));
5206 renderOrder[fs] = featureType;
5207 if (setting.getOrder() != null)
5209 featureOrder.put(featureType, setting.getOrder().floatValue());
5213 featureOrder.put(featureType, Float.valueOf(
5214 fs / jm.getFeatureSettings().getSetting().size()));
5216 if (safeBoolean(setting.isDisplay()))
5218 fdi.setVisible(featureType);
5221 Map<String, Boolean> fgtable = new Hashtable<>();
5222 for (int gs = 0; gs < jm.getFeatureSettings().getGroup().size(); gs++)
5224 Group grp = jm.getFeatureSettings().getGroup().get(gs);
5225 fgtable.put(grp.getName(), Boolean.valueOf(grp.isDisplay()));
5227 // FeatureRendererSettings frs = new FeatureRendererSettings(renderOrder,
5228 // fgtable, featureColours, jms.getFeatureSettings().hasTransparency() ?
5229 // jms.getFeatureSettings().getTransparency() : 0.0, featureOrder);
5230 FeatureRendererSettings frs = new FeatureRendererSettings(renderOrder,
5231 fgtable, featureColours, 1.0f, featureOrder);
5232 fr.transferSettings(frs);
5235 if (view.getHiddenColumns().size() > 0)
5237 for (int c = 0; c < view.getHiddenColumns().size(); c++)
5239 final HiddenColumns hc = view.getHiddenColumns().get(c);
5240 viewport.hideColumns(safeInt(hc.getStart()),
5241 safeInt(hc.getEnd()) /* +1 */);
5244 if (view.getCalcIdParam() != null)
5246 for (CalcIdParam calcIdParam : view.getCalcIdParam())
5248 if (calcIdParam != null)
5250 if (recoverCalcIdParam(calcIdParam, viewport))
5255 warn("Couldn't recover parameters for "
5256 + calcIdParam.getCalcId());
5261 af.setMenusFromViewport(viewport);
5262 af.setTitle(view.getTitle());
5263 // TODO: we don't need to do this if the viewport is aready visible.
5265 * Add the AlignFrame to the desktop (it may be 'gathered' later), unless it
5266 * has a 'cdna/protein complement' view, in which case save it in order to
5267 * populate a SplitFrame once all views have been read in.
5269 String complementaryViewId = view.getComplementId();
5270 if (complementaryViewId == null)
5272 Desktop.addInternalFrame(af, view.getTitle(),
5273 safeInt(view.getWidth()), safeInt(view.getHeight()));
5274 // recompute any autoannotation
5275 af.alignPanel.updateAnnotation(false, true);
5276 reorderAutoannotation(af, al, autoAlan);
5277 af.alignPanel.alignmentChanged();
5281 splitFrameCandidates.put(view, af);
5287 * Reads saved data to restore Colour by Annotation settings
5289 * @param viewAnnColour
5293 * @param checkGroupAnnColour
5296 private ColourSchemeI constructAnnotationColour(
5297 AnnotationColourScheme viewAnnColour, AlignFrame af,
5298 AlignmentI al, JalviewModel model, boolean checkGroupAnnColour)
5300 boolean propagateAnnColour = false;
5301 AlignmentI annAlignment = af != null ? af.getViewport().getAlignment()
5303 if (checkGroupAnnColour && al.getGroups() != null
5304 && al.getGroups().size() > 0)
5306 // pre 2.8.1 behaviour
5307 // check to see if we should transfer annotation colours
5308 propagateAnnColour = true;
5309 for (SequenceGroup sg : al.getGroups())
5311 if (sg.getColourScheme() instanceof AnnotationColourGradient)
5313 propagateAnnColour = false;
5319 * 2.10.2- : saved annotationId is AlignmentAnnotation.annotationId
5321 String annotationId = viewAnnColour.getAnnotation();
5322 AlignmentAnnotation matchedAnnotation = annotationIds.get(annotationId);
5325 * pre 2.10.2: saved annotationId is AlignmentAnnotation.label
5327 if (matchedAnnotation == null
5328 && annAlignment.getAlignmentAnnotation() != null)
5330 for (int i = 0; i < annAlignment.getAlignmentAnnotation().length; i++)
5333 .equals(annAlignment.getAlignmentAnnotation()[i].label))
5335 matchedAnnotation = annAlignment.getAlignmentAnnotation()[i];
5340 if (matchedAnnotation == null)
5342 System.err.println("Failed to match annotation colour scheme for "
5346 if (matchedAnnotation.getThreshold() == null)
5348 matchedAnnotation.setThreshold(
5349 new GraphLine(safeFloat(viewAnnColour.getThreshold()),
5350 "Threshold", Color.black));
5353 AnnotationColourGradient cs = null;
5354 if (viewAnnColour.getColourScheme().equals("None"))
5356 cs = new AnnotationColourGradient(matchedAnnotation,
5357 new Color(safeInt(viewAnnColour.getMinColour())),
5358 new Color(safeInt(viewAnnColour.getMaxColour())),
5359 safeInt(viewAnnColour.getAboveThreshold()));
5361 else if (viewAnnColour.getColourScheme().startsWith("ucs"))
5363 cs = new AnnotationColourGradient(matchedAnnotation,
5364 getUserColourScheme(model, viewAnnColour.getColourScheme()),
5365 safeInt(viewAnnColour.getAboveThreshold()));
5369 cs = new AnnotationColourGradient(matchedAnnotation,
5370 ColourSchemeProperty.getColourScheme(af.getViewport(), al,
5371 viewAnnColour.getColourScheme()),
5372 safeInt(viewAnnColour.getAboveThreshold()));
5375 boolean perSequenceOnly = safeBoolean(viewAnnColour.isPerSequence());
5376 boolean useOriginalColours = safeBoolean(
5377 viewAnnColour.isPredefinedColours());
5378 cs.setSeqAssociated(perSequenceOnly);
5379 cs.setPredefinedColours(useOriginalColours);
5381 if (propagateAnnColour && al.getGroups() != null)
5383 // Also use these settings for all the groups
5384 for (int g = 0; g < al.getGroups().size(); g++)
5386 SequenceGroup sg = al.getGroups().get(g);
5387 if (sg.getGroupColourScheme() == null)
5392 AnnotationColourGradient groupScheme = new AnnotationColourGradient(
5393 matchedAnnotation, sg.getColourScheme(),
5394 safeInt(viewAnnColour.getAboveThreshold()));
5395 sg.setColourScheme(groupScheme);
5396 groupScheme.setSeqAssociated(perSequenceOnly);
5397 groupScheme.setPredefinedColours(useOriginalColours);
5403 private void reorderAutoannotation(AlignFrame af, AlignmentI al,
5404 List<JvAnnotRow> autoAlan)
5406 // copy over visualization settings for autocalculated annotation in the
5408 if (al.getAlignmentAnnotation() != null)
5411 * Kludge for magic autoannotation names (see JAL-811)
5413 String[] magicNames = new String[] { "Consensus", "Quality",
5415 JvAnnotRow nullAnnot = new JvAnnotRow(-1, null);
5416 Hashtable<String, JvAnnotRow> visan = new Hashtable<>();
5417 for (String nm : magicNames)
5419 visan.put(nm, nullAnnot);
5421 for (JvAnnotRow auan : autoAlan)
5423 visan.put(auan.template.label
5424 + (auan.template.getCalcId() == null ? ""
5425 : "\t" + auan.template.getCalcId()),
5428 int hSize = al.getAlignmentAnnotation().length;
5429 List<JvAnnotRow> reorder = new ArrayList<>();
5430 // work through any autoCalculated annotation already on the view
5431 // removing it if it should be placed in a different location on the
5432 // annotation panel.
5433 List<String> remains = new ArrayList<>(visan.keySet());
5434 for (int h = 0; h < hSize; h++)
5436 jalview.datamodel.AlignmentAnnotation jalan = al
5437 .getAlignmentAnnotation()[h];
5438 if (jalan.autoCalculated)
5441 JvAnnotRow valan = visan.get(k = jalan.label);
5442 if (jalan.getCalcId() != null)
5444 valan = visan.get(k = jalan.label + "\t" + jalan.getCalcId());
5449 // delete the auto calculated row from the alignment
5450 al.deleteAnnotation(jalan, false);
5454 if (valan != nullAnnot)
5456 if (jalan != valan.template)
5458 // newly created autoannotation row instance
5459 // so keep a reference to the visible annotation row
5460 // and copy over all relevant attributes
5461 if (valan.template.graphHeight >= 0)
5464 jalan.graphHeight = valan.template.graphHeight;
5466 jalan.visible = valan.template.visible;
5468 reorder.add(new JvAnnotRow(valan.order, jalan));
5473 // Add any (possibly stale) autocalculated rows that were not appended to
5474 // the view during construction
5475 for (String other : remains)
5477 JvAnnotRow othera = visan.get(other);
5478 if (othera != nullAnnot && othera.template.getCalcId() != null
5479 && othera.template.getCalcId().length() > 0)
5481 reorder.add(othera);
5484 // now put the automatic annotation in its correct place
5485 int s = 0, srt[] = new int[reorder.size()];
5486 JvAnnotRow[] rws = new JvAnnotRow[reorder.size()];
5487 for (JvAnnotRow jvar : reorder)
5490 srt[s++] = jvar.order;
5493 jalview.util.QuickSort.sort(srt, rws);
5494 // and re-insert the annotation at its correct position
5495 for (JvAnnotRow jvar : rws)
5497 al.addAnnotation(jvar.template, jvar.order);
5499 af.alignPanel.adjustAnnotationHeight();
5503 Hashtable skipList = null;
5506 * TODO remove this method
5509 * @return AlignFrame bound to sequenceSetId from view, if one exists. private
5510 * AlignFrame getSkippedFrame(Viewport view) { if (skipList==null) {
5511 * throw new Error("Implementation Error. No skipList defined for this
5512 * Jalview2XML instance."); } return (AlignFrame)
5513 * skipList.get(view.getSequenceSetId()); }
5517 * Check if the Jalview view contained in object should be skipped or not.
5520 * @return true if view's sequenceSetId is a key in skipList
5522 private boolean skipViewport(JalviewModel object)
5524 if (skipList == null)
5528 String id = object.getViewport().get(0).getSequenceSetId();
5529 if (skipList.containsKey(id))
5531 if (Cache.log != null && Cache.log.isDebugEnabled())
5533 Cache.log.debug("Skipping seuqence set id " + id);
5540 public void addToSkipList(AlignFrame af)
5542 if (skipList == null)
5544 skipList = new Hashtable();
5546 skipList.put(af.getViewport().getSequenceSetId(), af);
5549 public void clearSkipList()
5551 if (skipList != null)
5558 private void recoverDatasetFor(SequenceSet vamsasSet, AlignmentI al,
5559 boolean ignoreUnrefed, String uniqueSeqSetId)
5561 jalview.datamodel.AlignmentI ds = getDatasetFor(
5562 vamsasSet.getDatasetId());
5563 AlignmentI xtant_ds = ds;
5564 if (xtant_ds == null)
5566 // good chance we are about to create a new dataset, but check if we've
5567 // seen some of the dataset sequence IDs before.
5568 // TODO: skip this check if we are working with project generated by
5569 // version 2.11 or later
5570 xtant_ds = checkIfHasDataset(vamsasSet.getSequence());
5571 if (xtant_ds != null)
5574 addDatasetRef(vamsasSet.getDatasetId(), ds);
5577 Vector dseqs = null;
5580 // recovering an alignment View
5581 AlignmentI seqSetDS = getDatasetFor(UNIQSEQSETID + uniqueSeqSetId);
5582 if (seqSetDS != null)
5584 if (ds != null && ds != seqSetDS)
5586 warn("JAL-3171 regression: Overwriting a dataset reference for an alignment"
5587 + " - CDS/Protein crossreference data may be lost");
5588 if (xtant_ds != null)
5590 // This can only happen if the unique sequence set ID was bound to a
5591 // dataset that did not contain any of the sequences in the view
5592 // currently being restored.
5593 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.");
5597 addDatasetRef(vamsasSet.getDatasetId(), ds);
5602 // try even harder to restore dataset
5603 AlignmentI xtantDS = checkIfHasDataset(vamsasSet.getSequence());
5604 // create a list of new dataset sequences
5605 dseqs = new Vector();
5607 for (int i = 0, iSize = vamsasSet.getSequence().size(); i < iSize; i++)
5609 Sequence vamsasSeq = vamsasSet.getSequence().get(i);
5610 ensureJalviewDatasetSequence(vamsasSeq, ds, dseqs, ignoreUnrefed, i);
5612 // create a new dataset
5615 SequenceI[] dsseqs = new SequenceI[dseqs.size()];
5616 dseqs.copyInto(dsseqs);
5617 ds = new jalview.datamodel.Alignment(dsseqs);
5618 debug("Created new dataset " + vamsasSet.getDatasetId()
5619 + " for alignment " + System.identityHashCode(al));
5620 addDatasetRef(vamsasSet.getDatasetId(), ds);
5622 // set the dataset for the newly imported alignment.
5623 if (al.getDataset() == null && !ignoreUnrefed)
5626 // register dataset for the alignment's uniqueSeqSetId for legacy projects
5627 addDatasetRef(UNIQSEQSETID + uniqueSeqSetId, ds);
5629 updateSeqDatasetBinding(vamsasSet.getSequence(), ds);
5633 * XML dataset sequence ID to materialised dataset reference
5635 HashMap<String, AlignmentI> seqToDataset = new HashMap<>();
5638 * @return the first materialised dataset reference containing a dataset
5639 * sequence referenced in the given view
5641 * - sequences from the view
5643 AlignmentI checkIfHasDataset(List<Sequence> list)
5645 for (Sequence restoredSeq : list)
5647 AlignmentI datasetFor = seqToDataset.get(restoredSeq.getDsseqid());
5648 if (datasetFor != null)
5657 * Register ds as the containing dataset for the dataset sequences referenced
5658 * by sequences in list
5661 * - sequences in a view
5664 void updateSeqDatasetBinding(List<Sequence> list, AlignmentI ds)
5666 for (Sequence restoredSeq : list)
5668 AlignmentI prevDS = seqToDataset.put(restoredSeq.getDsseqid(), ds);
5669 if (prevDS != null && prevDS != ds)
5671 warn("Dataset sequence appears in many datasets: "
5672 + restoredSeq.getDsseqid());
5673 // TODO: try to merge!
5680 * sequence definition to create/merge dataset sequence for
5684 * vector to add new dataset sequence to
5685 * @param ignoreUnrefed
5686 * - when true, don't create new sequences from vamsasSeq if it's id
5687 * doesn't already have an asssociated Jalview sequence.
5689 * - used to reorder the sequence in the alignment according to the
5690 * vamsasSeq array ordering, to preserve ordering of dataset
5692 private void ensureJalviewDatasetSequence(Sequence vamsasSeq,
5693 AlignmentI ds, Vector dseqs, boolean ignoreUnrefed, int vseqpos)
5695 // JBP TODO: Check this is called for AlCodonFrames to support recovery of
5697 SequenceI sq = seqRefIds.get(vamsasSeq.getId());
5698 boolean reorder = false;
5699 SequenceI dsq = null;
5700 if (sq != null && sq.getDatasetSequence() != null)
5702 dsq = sq.getDatasetSequence();
5708 if (sq == null && ignoreUnrefed)
5712 String sqid = vamsasSeq.getDsseqid();
5715 // need to create or add a new dataset sequence reference to this sequence
5718 dsq = seqRefIds.get(sqid);
5723 // make a new dataset sequence
5724 dsq = sq.createDatasetSequence();
5727 // make up a new dataset reference for this sequence
5728 sqid = seqHash(dsq);
5730 dsq.setVamsasId(uniqueSetSuffix + sqid);
5731 seqRefIds.put(sqid, dsq);
5736 dseqs.addElement(dsq);
5741 ds.addSequence(dsq);
5747 { // make this dataset sequence sq's dataset sequence
5748 sq.setDatasetSequence(dsq);
5749 // and update the current dataset alignment
5754 if (!dseqs.contains(dsq))
5761 if (ds.findIndex(dsq) < 0)
5763 ds.addSequence(dsq);
5770 // TODO: refactor this as a merge dataset sequence function
5771 // now check that sq (the dataset sequence) sequence really is the union of
5772 // all references to it
5773 // boolean pre = sq.getStart() < dsq.getStart();
5774 // boolean post = sq.getEnd() > dsq.getEnd();
5778 // StringBuffer sb = new StringBuffer();
5779 String newres = jalview.analysis.AlignSeq.extractGaps(
5780 jalview.util.Comparison.GapChars, sq.getSequenceAsString());
5781 if (!newres.equalsIgnoreCase(dsq.getSequenceAsString())
5782 && newres.length() > dsq.getLength())
5784 // Update with the longer sequence.
5788 * if (pre) { sb.insert(0, newres .substring(0, dsq.getStart() -
5789 * sq.getStart())); dsq.setStart(sq.getStart()); } if (post) {
5790 * sb.append(newres.substring(newres.length() - sq.getEnd() -
5791 * dsq.getEnd())); dsq.setEnd(sq.getEnd()); }
5793 dsq.setSequence(newres);
5795 // TODO: merges will never happen if we 'know' we have the real dataset
5796 // sequence - this should be detected when id==dssid
5798 "DEBUG Notice: Merged dataset sequence (if you see this often, post at http://issues.jalview.org/browse/JAL-1474)"); // ("
5799 // + (pre ? "prepended" : "") + " "
5800 // + (post ? "appended" : ""));
5805 // sequence refs are identical. We may need to update the existing dataset
5806 // alignment with this one, though.
5807 if (ds != null && dseqs == null)
5809 int opos = ds.findIndex(dsq);
5810 SequenceI tseq = null;
5811 if (opos != -1 && vseqpos != opos)
5813 // remove from old position
5814 ds.deleteSequence(dsq);
5816 if (vseqpos < ds.getHeight())
5818 if (vseqpos != opos)
5820 // save sequence at destination position
5821 tseq = ds.getSequenceAt(vseqpos);
5822 ds.replaceSequenceAt(vseqpos, dsq);
5823 ds.addSequence(tseq);
5828 ds.addSequence(dsq);
5835 * TODO use AlignmentI here and in related methods - needs
5836 * AlignmentI.getDataset() changed to return AlignmentI instead of Alignment
5838 Hashtable<String, AlignmentI> datasetIds = null;
5840 IdentityHashMap<AlignmentI, String> dataset2Ids = null;
5842 private AlignmentI getDatasetFor(String datasetId)
5844 if (datasetIds == null)
5846 datasetIds = new Hashtable<>();
5849 if (datasetIds.containsKey(datasetId))
5851 return datasetIds.get(datasetId);
5856 private void addDatasetRef(String datasetId, AlignmentI dataset)
5858 if (datasetIds == null)
5860 datasetIds = new Hashtable<>();
5862 datasetIds.put(datasetId, dataset);
5866 * make a new dataset ID for this jalview dataset alignment
5871 private String getDatasetIdRef(AlignmentI dataset)
5873 if (dataset.getDataset() != null)
5875 warn("Serious issue! Dataset Object passed to getDatasetIdRef is not a Jalview DATASET alignment...");
5877 String datasetId = makeHashCode(dataset, null);
5878 if (datasetId == null)
5880 // make a new datasetId and record it
5881 if (dataset2Ids == null)
5883 dataset2Ids = new IdentityHashMap<>();
5887 datasetId = dataset2Ids.get(dataset);
5889 if (datasetId == null)
5891 datasetId = "ds" + dataset2Ids.size() + 1;
5892 dataset2Ids.put(dataset, datasetId);
5899 * Add any saved DBRefEntry's to the sequence. An entry flagged as 'locus' is
5900 * constructed as a special subclass GeneLocus.
5902 * @param datasetSequence
5905 private void addDBRefs(SequenceI datasetSequence, Sequence sequence)
5907 for (int d = 0; d < sequence.getDBRef().size(); d++)
5909 DBRef dr = sequence.getDBRef().get(d);
5913 entry = new GeneLocus(dr.getSource(), dr.getVersion(),
5914 dr.getAccessionId());
5918 entry = new DBRefEntry(dr.getSource(), dr.getVersion(),
5919 dr.getAccessionId());
5921 if (dr.getMapping() != null)
5923 entry.setMap(addMapping(dr.getMapping()));
5925 datasetSequence.addDBRef(entry);
5929 private jalview.datamodel.Mapping addMapping(Mapping m)
5931 SequenceI dsto = null;
5932 // Mapping m = dr.getMapping();
5933 int fr[] = new int[m.getMapListFrom().size() * 2];
5934 Iterator<MapListFrom> from = m.getMapListFrom().iterator();// enumerateMapListFrom();
5935 for (int _i = 0; from.hasNext(); _i += 2)
5937 MapListFrom mf = from.next();
5938 fr[_i] = mf.getStart();
5939 fr[_i + 1] = mf.getEnd();
5941 int fto[] = new int[m.getMapListTo().size() * 2];
5942 Iterator<MapListTo> to = m.getMapListTo().iterator();// enumerateMapListTo();
5943 for (int _i = 0; to.hasNext(); _i += 2)
5945 MapListTo mf = to.next();
5946 fto[_i] = mf.getStart();
5947 fto[_i + 1] = mf.getEnd();
5949 jalview.datamodel.Mapping jmap = new jalview.datamodel.Mapping(dsto, fr,
5950 fto, m.getMapFromUnit().intValue(),
5951 m.getMapToUnit().intValue());
5954 * (optional) choice of dseqFor or Sequence
5956 if (m.getDseqFor() != null)
5958 String dsfor = m.getDseqFor();
5959 if (seqRefIds.containsKey(dsfor))
5964 jmap.setTo(seqRefIds.get(dsfor));
5968 frefedSequence.add(newMappingRef(dsfor, jmap));
5971 else if (m.getSequence() != null)
5974 * local sequence definition
5976 Sequence ms = m.getSequence();
5977 SequenceI djs = null;
5978 String sqid = ms.getDsseqid();
5979 if (sqid != null && sqid.length() > 0)
5982 * recover dataset sequence
5984 djs = seqRefIds.get(sqid);
5989 "Warning - making up dataset sequence id for DbRef sequence map reference");
5990 sqid = ((Object) ms).toString(); // make up a new hascode for
5991 // undefined dataset sequence hash
5992 // (unlikely to happen)
5998 * make a new dataset sequence and add it to refIds hash
6000 djs = new jalview.datamodel.Sequence(ms.getName(),
6002 djs.setStart(jmap.getMap().getToLowest());
6003 djs.setEnd(jmap.getMap().getToHighest());
6004 djs.setVamsasId(uniqueSetSuffix + sqid);
6006 incompleteSeqs.put(sqid, djs);
6007 seqRefIds.put(sqid, djs);
6009 jalview.bin.Cache.log.debug("about to recurse on addDBRefs.");
6018 * Provides a 'copy' of an alignment view (on action New View) by 'saving' the
6019 * view as XML (but not to file), and then reloading it
6024 public AlignmentPanel copyAlignPanel(AlignmentPanel ap)
6027 JalviewModel jm = saveState(ap, null, null, null);
6030 jm.getVamsasModel().getSequenceSet().get(0).getDatasetId(),
6031 ap.getAlignment().getDataset());
6033 uniqueSetSuffix = "";
6034 // jm.getJalviewModelSequence().getViewport(0).setId(null);
6035 jm.getViewport().get(0).setId(null);
6036 // we don't overwrite the view we just copied
6038 if (this.frefedSequence == null)
6040 frefedSequence = new Vector<>();
6043 viewportsAdded.clear();
6045 AlignFrame af = loadFromObject(jm, null, false, null);
6046 af.getAlignPanels().clear();
6047 af.closeMenuItem_actionPerformed(true);
6050 * if(ap.av.getAlignment().getAlignmentAnnotation()!=null) { for(int i=0;
6051 * i<ap.av.getAlignment().getAlignmentAnnotation().length; i++) {
6052 * if(!ap.av.getAlignment().getAlignmentAnnotation()[i].autoCalculated) {
6053 * af.alignPanel.av.getAlignment().getAlignmentAnnotation()[i] =
6054 * ap.av.getAlignment().getAlignmentAnnotation()[i]; } } }
6057 return af.alignPanel;
6060 private Hashtable jvids2vobj;
6062 private void warn(String msg)
6067 private void warn(String msg, Exception e)
6069 if (Cache.log != null)
6073 Cache.log.warn(msg, e);
6077 Cache.log.warn(msg);
6082 System.err.println("Warning: " + msg);
6085 e.printStackTrace();
6090 private void debug(String string)
6092 debug(string, null);
6095 private void debug(String msg, Exception e)
6097 if (Cache.log != null)
6101 Cache.log.debug(msg, e);
6105 Cache.log.debug(msg);
6110 System.err.println("Warning: " + msg);
6113 e.printStackTrace();
6119 * set the object to ID mapping tables used to write/recover objects and XML
6120 * ID strings for the jalview project. If external tables are provided then
6121 * finalize and clearSeqRefs will not clear the tables when the Jalview2XML
6122 * object goes out of scope. - also populates the datasetIds hashtable with
6123 * alignment objects containing dataset sequences
6126 * Map from ID strings to jalview datamodel
6128 * Map from jalview datamodel to ID strings
6132 public void setObjectMappingTables(Hashtable vobj2jv,
6133 IdentityHashMap jv2vobj)
6135 this.jv2vobj = jv2vobj;
6136 this.vobj2jv = vobj2jv;
6137 Iterator ds = jv2vobj.keySet().iterator();
6139 while (ds.hasNext())
6141 Object jvobj = ds.next();
6142 id = jv2vobj.get(jvobj).toString();
6143 if (jvobj instanceof jalview.datamodel.Alignment)
6145 if (((jalview.datamodel.Alignment) jvobj).getDataset() == null)
6147 addDatasetRef(id, (jalview.datamodel.Alignment) jvobj);
6150 else if (jvobj instanceof jalview.datamodel.Sequence)
6152 // register sequence object so the XML parser can recover it.
6153 if (seqRefIds == null)
6155 seqRefIds = new HashMap<>();
6157 if (seqsToIds == null)
6159 seqsToIds = new IdentityHashMap<>();
6161 seqRefIds.put(jv2vobj.get(jvobj).toString(), (SequenceI) jvobj);
6162 seqsToIds.put((SequenceI) jvobj, id);
6164 else if (jvobj instanceof jalview.datamodel.AlignmentAnnotation)
6167 AlignmentAnnotation jvann = (AlignmentAnnotation) jvobj;
6168 annotationIds.put(anid = jv2vobj.get(jvobj).toString(), jvann);
6169 if (jvann.annotationId == null)
6171 jvann.annotationId = anid;
6173 if (!jvann.annotationId.equals(anid))
6175 // TODO verify that this is the correct behaviour
6176 this.warn("Overriding Annotation ID for " + anid
6177 + " from different id : " + jvann.annotationId);
6178 jvann.annotationId = anid;
6181 else if (jvobj instanceof String)
6183 if (jvids2vobj == null)
6185 jvids2vobj = new Hashtable();
6186 jvids2vobj.put(jvobj, jv2vobj.get(jvobj).toString());
6191 Cache.log.debug("Ignoring " + jvobj.getClass() + " (ID = " + id);
6197 * set the uniqueSetSuffix used to prefix/suffix object IDs for jalview
6198 * objects created from the project archive. If string is null (default for
6199 * construction) then suffix will be set automatically.
6203 public void setUniqueSetSuffix(String string)
6205 uniqueSetSuffix = string;
6210 * uses skipList2 as the skipList for skipping views on sequence sets
6211 * associated with keys in the skipList
6215 public void setSkipList(Hashtable skipList2)
6217 skipList = skipList2;
6221 * Reads the jar entry of given name and returns its contents, or null if the
6222 * entry is not found.
6225 * @param jarEntryName
6228 protected String readJarEntry(jarInputStreamProvider jprovider,
6229 String jarEntryName)
6231 String result = null;
6232 BufferedReader in = null;
6237 * Reopen the jar input stream and traverse its entries to find a matching
6240 JarInputStream jin = jprovider.getJarInputStream();
6241 JarEntry entry = null;
6244 entry = jin.getNextJarEntry();
6245 } while (entry != null && !entry.getName().equals(jarEntryName));
6249 StringBuilder out = new StringBuilder(256);
6250 in = new BufferedReader(new InputStreamReader(jin, UTF_8));
6253 while ((data = in.readLine()) != null)
6257 result = out.toString();
6261 warn("Couldn't find entry in Jalview Jar for " + jarEntryName);
6263 } catch (Exception ex)
6265 ex.printStackTrace();
6273 } catch (IOException e)
6284 * Returns an incrementing counter (0, 1, 2...)
6288 private synchronized int nextCounter()
6294 * Loads any saved PCA viewers
6299 protected void loadPCAViewers(JalviewModel model, AlignmentPanel ap)
6303 List<PcaViewer> pcaviewers = model.getPcaViewer();
6304 for (PcaViewer viewer : pcaviewers)
6306 String modelName = viewer.getScoreModelName();
6307 SimilarityParamsI params = new SimilarityParams(
6308 viewer.isIncludeGappedColumns(), viewer.isMatchGaps(),
6309 viewer.isIncludeGaps(),
6310 viewer.isDenominateByShortestLength());
6313 * create the panel (without computing the PCA)
6315 PCAPanel panel = new PCAPanel(ap, modelName, params);
6317 panel.setTitle(viewer.getTitle());
6318 panel.setBounds(new Rectangle(viewer.getXpos(), viewer.getYpos(),
6319 viewer.getWidth(), viewer.getHeight()));
6321 boolean showLabels = viewer.isShowLabels();
6322 panel.setShowLabels(showLabels);
6323 panel.getRotatableCanvas().setShowLabels(showLabels);
6324 panel.getRotatableCanvas()
6325 .setBgColour(new Color(viewer.getBgColour()));
6326 panel.getRotatableCanvas()
6327 .setApplyToAllViews(viewer.isLinkToAllViews());
6330 * load PCA output data
6332 ScoreModelI scoreModel = ScoreModels.getInstance()
6333 .getScoreModel(modelName, ap);
6334 PCA pca = new PCA(null, scoreModel, params);
6335 PcaDataType pcaData = viewer.getPcaData();
6337 MatrixI pairwise = loadDoubleMatrix(pcaData.getPairwiseMatrix());
6338 pca.setPairwiseScores(pairwise);
6340 MatrixI triDiag = loadDoubleMatrix(pcaData.getTridiagonalMatrix());
6341 pca.setTridiagonal(triDiag);
6343 MatrixI result = loadDoubleMatrix(pcaData.getEigenMatrix());
6344 pca.setEigenmatrix(result);
6346 panel.getPcaModel().setPCA(pca);
6349 * we haven't saved the input data! (JAL-2647 to do)
6351 panel.setInputData(null);
6354 * add the sequence points for the PCA display
6356 List<jalview.datamodel.SequencePoint> seqPoints = new ArrayList<>();
6357 for (SequencePoint sp : viewer.getSequencePoint())
6359 String seqId = sp.getSequenceRef();
6360 SequenceI seq = seqRefIds.get(seqId);
6363 throw new IllegalStateException(
6364 "Unmatched seqref for PCA: " + seqId);
6366 Point pt = new Point(sp.getXPos(), sp.getYPos(), sp.getZPos());
6367 jalview.datamodel.SequencePoint seqPoint = new jalview.datamodel.SequencePoint(
6369 seqPoints.add(seqPoint);
6371 panel.getRotatableCanvas().setPoints(seqPoints, seqPoints.size());
6374 * set min-max ranges and scale after setPoints (which recomputes them)
6376 panel.getRotatableCanvas().setScaleFactor(viewer.getScaleFactor());
6377 SeqPointMin spMin = viewer.getSeqPointMin();
6378 float[] min = new float[] { spMin.getXPos(), spMin.getYPos(),
6380 SeqPointMax spMax = viewer.getSeqPointMax();
6381 float[] max = new float[] { spMax.getXPos(), spMax.getYPos(),
6383 panel.getRotatableCanvas().setSeqMinMax(min, max);
6385 // todo: hold points list in PCAModel only
6386 panel.getPcaModel().setSequencePoints(seqPoints);
6388 panel.setSelectedDimensionIndex(viewer.getXDim(), X);
6389 panel.setSelectedDimensionIndex(viewer.getYDim(), Y);
6390 panel.setSelectedDimensionIndex(viewer.getZDim(), Z);
6392 // is this duplication needed?
6393 panel.setTop(seqPoints.size() - 1);
6394 panel.getPcaModel().setTop(seqPoints.size() - 1);
6397 * add the axes' end points for the display
6399 for (int i = 0; i < 3; i++)
6401 Axis axis = viewer.getAxis().get(i);
6402 panel.getRotatableCanvas().getAxisEndPoints()[i] = new Point(
6403 axis.getXPos(), axis.getYPos(), axis.getZPos());
6406 Desktop.addInternalFrame(panel, MessageManager.formatMessage(
6407 "label.calc_title", "PCA", modelName), 475, 450);
6409 } catch (Exception ex)
6411 Cache.log.error("Error loading PCA: " + ex.toString());
6416 * Populates an XML model of the feature colour scheme for one feature type
6418 * @param featureType
6422 public static Colour marshalColour(
6423 String featureType, FeatureColourI fcol)
6425 Colour col = new Colour();
6426 if (fcol.isSimpleColour())
6428 col.setRGB(Format.getHexString(fcol.getColour()));
6432 col.setRGB(Format.getHexString(fcol.getMaxColour()));
6433 col.setMin(fcol.getMin());
6434 col.setMax(fcol.getMax());
6435 col.setMinRGB(jalview.util.Format.getHexString(fcol.getMinColour()));
6436 col.setAutoScale(fcol.isAutoScaled());
6437 col.setThreshold(fcol.getThreshold());
6438 col.setColourByLabel(fcol.isColourByLabel());
6439 col.setThreshType(fcol.isAboveThreshold() ? ThresholdType.ABOVE
6440 : (fcol.isBelowThreshold() ? ThresholdType.BELOW
6441 : ThresholdType.NONE));
6442 if (fcol.isColourByAttribute())
6444 final String[] attName = fcol.getAttributeName();
6445 col.getAttributeName().add(attName[0]);
6446 if (attName.length > 1)
6448 col.getAttributeName().add(attName[1]);
6451 Color noColour = fcol.getNoColour();
6452 if (noColour == null)
6454 col.setNoValueColour(NoValueColour.NONE);
6456 else if (noColour == fcol.getMaxColour())
6458 col.setNoValueColour(NoValueColour.MAX);
6462 col.setNoValueColour(NoValueColour.MIN);
6465 col.setName(featureType);
6470 * Populates an XML model of the feature filter(s) for one feature type
6472 * @param firstMatcher
6473 * the first (or only) match condition)
6475 * remaining match conditions (if any)
6477 * if true, conditions are and-ed, else or-ed
6479 public static jalview.xml.binding.jalview.FeatureMatcherSet marshalFilter(
6480 FeatureMatcherI firstMatcher, Iterator<FeatureMatcherI> filters,
6483 jalview.xml.binding.jalview.FeatureMatcherSet result = new jalview.xml.binding.jalview.FeatureMatcherSet();
6485 if (filters.hasNext())
6490 CompoundMatcher compound = new CompoundMatcher();
6491 compound.setAnd(and);
6492 jalview.xml.binding.jalview.FeatureMatcherSet matcher1 = marshalFilter(
6493 firstMatcher, Collections.emptyIterator(), and);
6494 // compound.addMatcherSet(matcher1);
6495 compound.getMatcherSet().add(matcher1);
6496 FeatureMatcherI nextMatcher = filters.next();
6497 jalview.xml.binding.jalview.FeatureMatcherSet matcher2 = marshalFilter(
6498 nextMatcher, filters, and);
6499 // compound.addMatcherSet(matcher2);
6500 compound.getMatcherSet().add(matcher2);
6501 result.setCompoundMatcher(compound);
6506 * single condition matcher
6508 // MatchCondition matcherModel = new MatchCondition();
6509 jalview.xml.binding.jalview.FeatureMatcher matcherModel = new jalview.xml.binding.jalview.FeatureMatcher();
6510 matcherModel.setCondition(
6511 firstMatcher.getMatcher().getCondition().getStableName());
6512 matcherModel.setValue(firstMatcher.getMatcher().getPattern());
6513 if (firstMatcher.isByAttribute())
6515 matcherModel.setBy(FilterBy.BY_ATTRIBUTE);
6516 // matcherModel.setAttributeName(firstMatcher.getAttribute());
6517 String[] attName = firstMatcher.getAttribute();
6518 matcherModel.getAttributeName().add(attName[0]); // attribute
6519 if (attName.length > 1)
6521 matcherModel.getAttributeName().add(attName[1]); // sub-attribute
6524 else if (firstMatcher.isByLabel())
6526 matcherModel.setBy(FilterBy.BY_LABEL);
6528 else if (firstMatcher.isByScore())
6530 matcherModel.setBy(FilterBy.BY_SCORE);
6532 result.setMatchCondition(matcherModel);
6539 * Loads one XML model of a feature filter to a Jalview object
6541 * @param featureType
6542 * @param matcherSetModel
6545 public static FeatureMatcherSetI parseFilter(
6547 jalview.xml.binding.jalview.FeatureMatcherSet matcherSetModel)
6549 FeatureMatcherSetI result = new FeatureMatcherSet();
6552 parseFilterConditions(result, matcherSetModel, true);
6553 } catch (IllegalStateException e)
6555 // mixing AND and OR conditions perhaps
6557 String.format("Error reading filter conditions for '%s': %s",
6558 featureType, e.getMessage()));
6559 // return as much as was parsed up to the error
6566 * Adds feature match conditions to matcherSet as unmarshalled from XML
6567 * (possibly recursively for compound conditions)
6570 * @param matcherSetModel
6572 * if true, multiple conditions are AND-ed, else they are OR-ed
6573 * @throws IllegalStateException
6574 * if AND and OR conditions are mixed
6576 protected static void parseFilterConditions(
6577 FeatureMatcherSetI matcherSet,
6578 jalview.xml.binding.jalview.FeatureMatcherSet matcherSetModel,
6581 jalview.xml.binding.jalview.FeatureMatcher mc = matcherSetModel
6582 .getMatchCondition();
6588 FilterBy filterBy = mc.getBy();
6589 Condition cond = Condition.fromString(mc.getCondition());
6590 String pattern = mc.getValue();
6591 FeatureMatcherI matchCondition = null;
6592 if (filterBy == FilterBy.BY_LABEL)
6594 matchCondition = FeatureMatcher.byLabel(cond, pattern);
6596 else if (filterBy == FilterBy.BY_SCORE)
6598 matchCondition = FeatureMatcher.byScore(cond, pattern);
6601 else if (filterBy == FilterBy.BY_ATTRIBUTE)
6603 final List<String> attributeName = mc.getAttributeName();
6604 String[] attNames = attributeName
6605 .toArray(new String[attributeName.size()]);
6606 matchCondition = FeatureMatcher.byAttribute(cond, pattern,
6611 * note this throws IllegalStateException if AND-ing to a
6612 * previously OR-ed compound condition, or vice versa
6616 matcherSet.and(matchCondition);
6620 matcherSet.or(matchCondition);
6626 * compound condition
6628 List<jalview.xml.binding.jalview.FeatureMatcherSet> matchers = matcherSetModel
6629 .getCompoundMatcher().getMatcherSet();
6630 boolean anded = matcherSetModel.getCompoundMatcher().isAnd();
6631 if (matchers.size() == 2)
6633 parseFilterConditions(matcherSet, matchers.get(0), anded);
6634 parseFilterConditions(matcherSet, matchers.get(1), anded);
6638 System.err.println("Malformed compound filter condition");
6644 * Loads one XML model of a feature colour to a Jalview object
6646 * @param colourModel
6649 public static FeatureColourI parseColour(Colour colourModel)
6651 FeatureColourI colour = null;
6653 if (colourModel.getMax() != null)
6655 Color mincol = null;
6656 Color maxcol = null;
6657 Color noValueColour = null;
6661 mincol = new Color(Integer.parseInt(colourModel.getMinRGB(), 16));
6662 maxcol = new Color(Integer.parseInt(colourModel.getRGB(), 16));
6663 } catch (Exception e)
6665 Cache.log.warn("Couldn't parse out graduated feature color.", e);
6668 NoValueColour noCol = colourModel.getNoValueColour();
6669 if (noCol == NoValueColour.MIN)
6671 noValueColour = mincol;
6673 else if (noCol == NoValueColour.MAX)
6675 noValueColour = maxcol;
6678 colour = new FeatureColour(maxcol, mincol, maxcol, noValueColour,
6679 safeFloat(colourModel.getMin()),
6680 safeFloat(colourModel.getMax()));
6681 final List<String> attributeName = colourModel.getAttributeName();
6682 String[] attributes = attributeName
6683 .toArray(new String[attributeName.size()]);
6684 if (attributes != null && attributes.length > 0)
6686 colour.setAttributeName(attributes);
6688 if (colourModel.isAutoScale() != null)
6690 colour.setAutoScaled(colourModel.isAutoScale().booleanValue());
6692 if (colourModel.isColourByLabel() != null)
6694 colour.setColourByLabel(
6695 colourModel.isColourByLabel().booleanValue());
6697 if (colourModel.getThreshold() != null)
6699 colour.setThreshold(colourModel.getThreshold().floatValue());
6701 ThresholdType ttyp = colourModel.getThreshType();
6702 if (ttyp == ThresholdType.ABOVE)
6704 colour.setAboveThreshold(true);
6706 else if (ttyp == ThresholdType.BELOW)
6708 colour.setBelowThreshold(true);
6713 Color color = new Color(Integer.parseInt(colourModel.getRGB(), 16));
6714 colour = new FeatureColour(color);