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.
23 import jalview.analysis.Conservation;
24 import jalview.api.FeatureColourI;
25 import jalview.api.ViewStyleI;
26 import jalview.api.structures.JalviewStructureDisplayI;
27 import jalview.bin.Cache;
28 import jalview.datamodel.AlignedCodonFrame;
29 import jalview.datamodel.Alignment;
30 import jalview.datamodel.AlignmentAnnotation;
31 import jalview.datamodel.AlignmentI;
32 import jalview.datamodel.GraphLine;
33 import jalview.datamodel.PDBEntry;
34 import jalview.datamodel.RnaViewerModel;
35 import jalview.datamodel.SequenceGroup;
36 import jalview.datamodel.SequenceI;
37 import jalview.datamodel.StructureViewerModel;
38 import jalview.datamodel.StructureViewerModel.StructureData;
39 import jalview.ext.varna.RnaModel;
40 import jalview.gui.StructureViewer.ViewerType;
41 import jalview.io.DataSourceType;
42 import jalview.io.FileFormat;
43 import jalview.renderer.ResidueShaderI;
44 import jalview.schemabinding.version2.AlcodMap;
45 import jalview.schemabinding.version2.AlcodonFrame;
46 import jalview.schemabinding.version2.Annotation;
47 import jalview.schemabinding.version2.AnnotationColours;
48 import jalview.schemabinding.version2.AnnotationElement;
49 import jalview.schemabinding.version2.CalcIdParam;
50 import jalview.schemabinding.version2.DBRef;
51 import jalview.schemabinding.version2.Features;
52 import jalview.schemabinding.version2.Group;
53 import jalview.schemabinding.version2.HiddenColumns;
54 import jalview.schemabinding.version2.JGroup;
55 import jalview.schemabinding.version2.JSeq;
56 import jalview.schemabinding.version2.JalviewModel;
57 import jalview.schemabinding.version2.JalviewModelSequence;
58 import jalview.schemabinding.version2.MapListFrom;
59 import jalview.schemabinding.version2.MapListTo;
60 import jalview.schemabinding.version2.Mapping;
61 import jalview.schemabinding.version2.MappingChoice;
62 import jalview.schemabinding.version2.OtherData;
63 import jalview.schemabinding.version2.PdbentryItem;
64 import jalview.schemabinding.version2.Pdbids;
65 import jalview.schemabinding.version2.Property;
66 import jalview.schemabinding.version2.RnaViewer;
67 import jalview.schemabinding.version2.SecondaryStructure;
68 import jalview.schemabinding.version2.Sequence;
69 import jalview.schemabinding.version2.SequenceSet;
70 import jalview.schemabinding.version2.SequenceSetProperties;
71 import jalview.schemabinding.version2.Setting;
72 import jalview.schemabinding.version2.StructureState;
73 import jalview.schemabinding.version2.ThresholdLine;
74 import jalview.schemabinding.version2.Tree;
75 import jalview.schemabinding.version2.UserColours;
76 import jalview.schemabinding.version2.Viewport;
77 import jalview.schemes.AnnotationColourGradient;
78 import jalview.schemes.ColourSchemeI;
79 import jalview.schemes.ColourSchemeProperty;
80 import jalview.schemes.FeatureColour;
81 import jalview.schemes.ResidueProperties;
82 import jalview.schemes.UserColourScheme;
83 import jalview.structure.StructureSelectionManager;
84 import jalview.structures.models.AAStructureBindingModel;
85 import jalview.util.MessageManager;
86 import jalview.util.Platform;
87 import jalview.util.StringUtils;
88 import jalview.util.jarInputStreamProvider;
89 import jalview.viewmodel.AlignmentViewport;
90 import jalview.viewmodel.ViewportRanges;
91 import jalview.viewmodel.seqfeatures.FeatureRendererSettings;
92 import jalview.viewmodel.seqfeatures.FeaturesDisplayed;
93 import jalview.ws.jws2.Jws2Discoverer;
94 import jalview.ws.jws2.dm.AAConSettings;
95 import jalview.ws.jws2.jabaws2.Jws2Instance;
96 import jalview.ws.params.ArgumentI;
97 import jalview.ws.params.AutoCalcSetting;
98 import jalview.ws.params.WsParamSetI;
100 import java.awt.Color;
101 import java.awt.Rectangle;
102 import java.io.BufferedReader;
103 import java.io.DataInputStream;
104 import java.io.DataOutputStream;
106 import java.io.FileInputStream;
107 import java.io.FileOutputStream;
108 import java.io.IOException;
109 import java.io.InputStreamReader;
110 import java.io.OutputStreamWriter;
111 import java.io.PrintWriter;
112 import java.lang.reflect.InvocationTargetException;
113 import java.net.MalformedURLException;
115 import java.util.ArrayList;
116 import java.util.Arrays;
117 import java.util.Enumeration;
118 import java.util.HashMap;
119 import java.util.HashSet;
120 import java.util.Hashtable;
121 import java.util.IdentityHashMap;
122 import java.util.Iterator;
123 import java.util.LinkedHashMap;
124 import java.util.List;
125 import java.util.Map;
126 import java.util.Map.Entry;
127 import java.util.Set;
128 import java.util.Vector;
129 import java.util.jar.JarEntry;
130 import java.util.jar.JarInputStream;
131 import java.util.jar.JarOutputStream;
133 import javax.swing.JInternalFrame;
134 import javax.swing.SwingUtilities;
136 import org.exolab.castor.xml.Marshaller;
137 import org.exolab.castor.xml.Unmarshaller;
140 * Write out the current jalview desktop state as a Jalview XML stream.
142 * Note: the vamsas objects referred to here are primitive versions of the
143 * VAMSAS project schema elements - they are not the same and most likely never
147 * @version $Revision: 1.134 $
149 public class Jalview2XML
151 private static final String VIEWER_PREFIX = "viewer_";
153 private static final String RNA_PREFIX = "rna_";
155 private static final String UTF_8 = "UTF-8";
157 // use this with nextCounter() to make unique names for entities
158 private int counter = 0;
161 * SequenceI reference -> XML ID string in jalview XML. Populated as XML reps
162 * of sequence objects are created.
164 IdentityHashMap<SequenceI, String> seqsToIds = null;
167 * jalview XML Sequence ID to jalview sequence object reference (both dataset
168 * and alignment sequences. Populated as XML reps of sequence objects are
171 Map<String, SequenceI> seqRefIds = null;
173 Map<String, SequenceI> incompleteSeqs = null;
175 List<SeqFref> frefedSequence = null;
177 boolean raiseGUI = true; // whether errors are raised in dialog boxes or not
180 * Map of reconstructed AlignFrame objects that appear to have come from
181 * SplitFrame objects (have a dna/protein complement view).
183 private Map<Viewport, AlignFrame> splitFrameCandidates = new HashMap<Viewport, AlignFrame>();
186 * Map from displayed rna structure models to their saved session state jar
189 private Map<RnaModel, String> rnaSessions = new HashMap<RnaModel, String>();
192 * create/return unique hash string for sq
195 * @return new or existing unique string for sq
197 String seqHash(SequenceI sq)
199 if (seqsToIds == null)
203 if (seqsToIds.containsKey(sq))
205 return seqsToIds.get(sq);
209 // create sequential key
210 String key = "sq" + (seqsToIds.size() + 1);
211 key = makeHashCode(sq, key); // check we don't have an external reference
213 seqsToIds.put(sq, key);
222 if (seqRefIds != null)
226 if (seqsToIds != null)
230 if (incompleteSeqs != null)
232 incompleteSeqs.clear();
240 warn("clearSeqRefs called when _cleartables was not set. Doing nothing.");
241 // seqRefIds = new Hashtable();
242 // seqsToIds = new IdentityHashMap();
248 if (seqsToIds == null)
250 seqsToIds = new IdentityHashMap<SequenceI, String>();
252 if (seqRefIds == null)
254 seqRefIds = new HashMap<String, SequenceI>();
256 if (incompleteSeqs == null)
258 incompleteSeqs = new HashMap<String, SequenceI>();
260 if (frefedSequence == null)
262 frefedSequence = new ArrayList<SeqFref>();
270 public Jalview2XML(boolean raiseGUI)
272 this.raiseGUI = raiseGUI;
276 * base class for resolving forward references to sequences by their ID
281 abstract class SeqFref
287 public SeqFref(String _sref, String type)
293 public String getSref()
298 public SequenceI getSrefSeq()
300 return seqRefIds.get(sref);
303 public boolean isResolvable()
305 return seqRefIds.get(sref) != null;
308 public SequenceI getSrefDatasetSeq()
310 SequenceI sq = seqRefIds.get(sref);
313 while (sq.getDatasetSequence() != null)
315 sq = sq.getDatasetSequence();
322 * @return true if the forward reference was fully resolved
324 abstract boolean resolve();
327 public String toString()
329 return type + " reference to " + sref;
334 * create forward reference for a mapping
340 public SeqFref newMappingRef(final String sref,
341 final jalview.datamodel.Mapping _jmap)
343 SeqFref fref = new SeqFref(sref, "Mapping")
345 public jalview.datamodel.Mapping jmap = _jmap;
350 SequenceI seq = getSrefDatasetSeq();
362 public SeqFref newAlcodMapRef(final String sref,
363 final AlignedCodonFrame _cf, final jalview.datamodel.Mapping _jmap)
366 SeqFref fref = new SeqFref(sref, "Codon Frame")
368 AlignedCodonFrame cf = _cf;
370 public jalview.datamodel.Mapping mp = _jmap;
373 public boolean isResolvable()
375 return super.isResolvable() && mp.getTo() != null;
381 SequenceI seq = getSrefDatasetSeq();
386 cf.addMap(seq, mp.getTo(), mp.getMap());
393 public void resolveFrefedSequences()
395 Iterator<SeqFref> nextFref = frefedSequence.iterator();
396 int toresolve = frefedSequence.size();
397 int unresolved = 0, failedtoresolve = 0;
398 while (nextFref.hasNext())
400 SeqFref ref = nextFref.next();
401 if (ref.isResolvable())
413 } catch (Exception x)
416 .println("IMPLEMENTATION ERROR: Failed to resolve forward reference for sequence "
429 System.err.println("Jalview Project Import: There were " + unresolved
430 + " forward references left unresolved on the stack.");
432 if (failedtoresolve > 0)
434 System.err.println("SERIOUS! " + failedtoresolve
435 + " resolvable forward references failed to resolve.");
437 if (incompleteSeqs != null && incompleteSeqs.size() > 0)
439 System.err.println("Jalview Project Import: There are "
440 + incompleteSeqs.size()
441 + " sequences which may have incomplete metadata.");
442 if (incompleteSeqs.size() < 10)
444 for (SequenceI s : incompleteSeqs.values())
446 System.err.println(s.toString());
452 .println("Too many to report. Skipping output of incomplete sequences.");
458 * This maintains a map of viewports, the key being the seqSetId. Important to
459 * set historyItem and redoList for multiple views
461 Map<String, AlignViewport> viewportsAdded = new HashMap<String, AlignViewport>();
463 Map<String, AlignmentAnnotation> annotationIds = new HashMap<String, AlignmentAnnotation>();
465 String uniqueSetSuffix = "";
468 * List of pdbfiles added to Jar
470 List<String> pdbfiles = null;
472 // SAVES SEVERAL ALIGNMENT WINDOWS TO SAME JARFILE
473 public void saveState(File statefile)
475 FileOutputStream fos = null;
478 fos = new FileOutputStream(statefile);
479 JarOutputStream jout = new JarOutputStream(fos);
482 } catch (Exception e)
484 // TODO: inform user of the problem - they need to know if their data was
486 if (errorMessage == null)
488 errorMessage = "Couldn't write Jalview Archive to output file '"
489 + statefile + "' - See console error log for details";
493 errorMessage += "(output file was '" + statefile + "')";
503 } catch (IOException e)
513 * Writes a jalview project archive to the given Jar output stream.
517 public void saveState(JarOutputStream jout)
519 AlignFrame[] frames = Desktop.getAlignFrames();
525 saveAllFrames(Arrays.asList(frames), jout);
529 * core method for storing state for a set of AlignFrames.
532 * - frames involving all data to be exported (including containing
535 * - project output stream
537 private void saveAllFrames(List<AlignFrame> frames, JarOutputStream jout)
539 Hashtable<String, AlignFrame> dsses = new Hashtable<String, AlignFrame>();
542 * ensure cached data is clear before starting
544 // todo tidy up seqRefIds, seqsToIds initialisation / reset
546 splitFrameCandidates.clear();
551 // NOTE UTF-8 MUST BE USED FOR WRITING UNICODE CHARS
552 // //////////////////////////////////////////////////
554 List<String> shortNames = new ArrayList<String>();
555 List<String> viewIds = new ArrayList<String>();
558 for (int i = frames.size() - 1; i > -1; i--)
560 AlignFrame af = frames.get(i);
564 .containsKey(af.getViewport().getSequenceSetId()))
569 String shortName = makeFilename(af, shortNames);
571 int ap, apSize = af.alignPanels.size();
573 for (ap = 0; ap < apSize; ap++)
575 AlignmentPanel apanel = af.alignPanels.get(ap);
576 String fileName = apSize == 1 ? shortName : ap + shortName;
577 if (!fileName.endsWith(".xml"))
579 fileName = fileName + ".xml";
582 saveState(apanel, fileName, jout, viewIds);
584 String dssid = getDatasetIdRef(af.getViewport().getAlignment()
586 if (!dsses.containsKey(dssid))
588 dsses.put(dssid, af);
593 writeDatasetFor(dsses, "" + jout.hashCode() + " " + uniqueSetSuffix,
599 } catch (Exception foo)
604 } catch (Exception ex)
606 // TODO: inform user of the problem - they need to know if their data was
608 if (errorMessage == null)
610 errorMessage = "Couldn't write Jalview Archive - see error output for details";
612 ex.printStackTrace();
617 * Generates a distinct file name, based on the title of the AlignFrame, by
618 * appending _n for increasing n until an unused name is generated. The new
619 * name (without its extension) is added to the list.
623 * @return the generated name, with .xml extension
625 protected String makeFilename(AlignFrame af, List<String> namesUsed)
627 String shortName = af.getTitle();
629 if (shortName.indexOf(File.separatorChar) > -1)
631 shortName = shortName.substring(shortName
632 .lastIndexOf(File.separatorChar) + 1);
637 while (namesUsed.contains(shortName))
639 if (shortName.endsWith("_" + (count - 1)))
641 shortName = shortName.substring(0, shortName.lastIndexOf("_"));
644 shortName = shortName.concat("_" + count);
648 namesUsed.add(shortName);
650 if (!shortName.endsWith(".xml"))
652 shortName = shortName + ".xml";
657 // USE THIS METHOD TO SAVE A SINGLE ALIGNMENT WINDOW
658 public boolean saveAlignment(AlignFrame af, String jarFile,
663 FileOutputStream fos = new FileOutputStream(jarFile);
664 JarOutputStream jout = new JarOutputStream(fos);
665 List<AlignFrame> frames = new ArrayList<AlignFrame>();
667 // resolve splitframes
668 if (af.getViewport().getCodingComplement() != null)
670 frames = ((SplitFrame) af.getSplitViewContainer()).getAlignFrames();
676 saveAllFrames(frames, jout);
680 } catch (Exception foo)
686 } catch (Exception ex)
688 errorMessage = "Couldn't Write alignment view to Jalview Archive - see error output for details";
689 ex.printStackTrace();
694 private void writeDatasetFor(Hashtable<String, AlignFrame> dsses,
695 String fileName, JarOutputStream jout)
698 for (String dssids : dsses.keySet())
700 AlignFrame _af = dsses.get(dssids);
701 String jfileName = fileName + " Dataset for " + _af.getTitle();
702 if (!jfileName.endsWith(".xml"))
704 jfileName = jfileName + ".xml";
706 saveState(_af.alignPanel, jfileName, true, jout, null);
711 * create a JalviewModel from an alignment view and marshall it to a
715 * panel to create jalview model for
717 * name of alignment panel written to output stream
724 public JalviewModel saveState(AlignmentPanel ap, String fileName,
725 JarOutputStream jout, List<String> viewIds)
727 return saveState(ap, fileName, false, jout, viewIds);
731 * create a JalviewModel from an alignment view and marshall it to a
735 * panel to create jalview model for
737 * name of alignment panel written to output stream
739 * when true, only write the dataset for the alignment, not the data
740 * associated with the view.
746 public JalviewModel saveState(AlignmentPanel ap, String fileName,
747 boolean storeDS, JarOutputStream jout, List<String> viewIds)
751 viewIds = new ArrayList<String>();
756 List<UserColourScheme> userColours = new ArrayList<UserColourScheme>();
758 AlignViewport av = ap.av;
759 ViewportRanges vpRanges = av.getRanges();
761 JalviewModel object = new JalviewModel();
762 object.setVamsasModel(new jalview.schemabinding.version2.VamsasModel());
764 object.setCreationDate(new java.util.Date(System.currentTimeMillis()));
765 object.setVersion(jalview.bin.Cache.getDefault("VERSION",
766 "Development Build"));
769 * rjal is full height alignment, jal is actual alignment with full metadata
770 * but excludes hidden sequences.
772 jalview.datamodel.AlignmentI rjal = av.getAlignment(), jal = rjal;
774 if (av.hasHiddenRows())
776 rjal = jal.getHiddenSequences().getFullAlignment();
779 SequenceSet vamsasSet = new SequenceSet();
781 JalviewModelSequence jms = new JalviewModelSequence();
783 vamsasSet.setGapChar(jal.getGapCharacter() + "");
785 if (jal.getDataset() != null)
787 // dataset id is the dataset's hashcode
788 vamsasSet.setDatasetId(getDatasetIdRef(jal.getDataset()));
791 // switch jal and the dataset
792 jal = jal.getDataset();
796 if (jal.getProperties() != null)
798 Enumeration en = jal.getProperties().keys();
799 while (en.hasMoreElements())
801 String key = en.nextElement().toString();
802 SequenceSetProperties ssp = new SequenceSetProperties();
804 ssp.setValue(jal.getProperties().get(key).toString());
805 vamsasSet.addSequenceSetProperties(ssp);
810 Set<String> calcIdSet = new HashSet<String>();
811 // record the set of vamsas sequence XML POJO we create.
812 HashMap<String, Sequence> vamsasSetIds = new HashMap<String, Sequence>();
814 for (final SequenceI jds : rjal.getSequences())
816 final SequenceI jdatasq = jds.getDatasetSequence() == null ? jds
817 : jds.getDatasetSequence();
818 String id = seqHash(jds);
819 if (vamsasSetIds.get(id) == null)
821 if (seqRefIds.get(id) != null && !storeDS)
823 // This happens for two reasons: 1. multiple views are being
825 // 2. the hashCode has collided with another sequence's code. This
827 // HAPPEN! (PF00072.15.stk does this)
828 // JBPNote: Uncomment to debug writing out of files that do not read
829 // back in due to ArrayOutOfBoundExceptions.
830 // System.err.println("vamsasSeq backref: "+id+"");
831 // System.err.println(jds.getName()+"
832 // "+jds.getStart()+"-"+jds.getEnd()+" "+jds.getSequenceAsString());
833 // System.err.println("Hashcode: "+seqHash(jds));
834 // SequenceI rsq = (SequenceI) seqRefIds.get(id + "");
835 // System.err.println(rsq.getName()+"
836 // "+rsq.getStart()+"-"+rsq.getEnd()+" "+rsq.getSequenceAsString());
837 // System.err.println("Hashcode: "+seqHash(rsq));
841 vamsasSeq = createVamsasSequence(id, jds);
842 vamsasSet.addSequence(vamsasSeq);
843 vamsasSetIds.put(id, vamsasSeq);
844 seqRefIds.put(id, jds);
848 jseq.setStart(jds.getStart());
849 jseq.setEnd(jds.getEnd());
850 jseq.setColour(av.getSequenceColour(jds).getRGB());
852 jseq.setId(id); // jseq id should be a string not a number
855 // Store any sequences this sequence represents
856 if (av.hasHiddenRows())
858 // use rjal, contains the full height alignment
859 jseq.setHidden(av.getAlignment().getHiddenSequences()
862 if (av.isHiddenRepSequence(jds))
864 jalview.datamodel.SequenceI[] reps = av
865 .getRepresentedSequences(jds).getSequencesInOrder(rjal);
867 for (int h = 0; h < reps.length; h++)
871 jseq.addHiddenSequences(rjal.findIndex(reps[h]));
876 // mark sequence as reference - if it is the reference for this view
879 jseq.setViewreference(jds == jal.getSeqrep());
883 // TODO: omit sequence features from each alignment view's XML dump if we
884 // are storing dataset
885 if (jds.getSequenceFeatures() != null)
887 jalview.datamodel.SequenceFeature[] sf = jds.getSequenceFeatures();
889 while (index < sf.length)
891 Features features = new Features();
893 features.setBegin(sf[index].getBegin());
894 features.setEnd(sf[index].getEnd());
895 features.setDescription(sf[index].getDescription());
896 features.setType(sf[index].getType());
897 features.setFeatureGroup(sf[index].getFeatureGroup());
898 features.setScore(sf[index].getScore());
899 if (sf[index].links != null)
901 for (int l = 0; l < sf[index].links.size(); l++)
903 OtherData keyValue = new OtherData();
904 keyValue.setKey("LINK_" + l);
905 keyValue.setValue(sf[index].links.elementAt(l).toString());
906 features.addOtherData(keyValue);
909 if (sf[index].otherDetails != null)
912 Iterator<String> keys = sf[index].otherDetails.keySet()
914 while (keys.hasNext())
917 OtherData keyValue = new OtherData();
918 keyValue.setKey(key);
919 keyValue.setValue(sf[index].otherDetails.get(key).toString());
920 features.addOtherData(keyValue);
924 jseq.addFeatures(features);
929 if (jdatasq.getAllPDBEntries() != null)
931 Enumeration en = jdatasq.getAllPDBEntries().elements();
932 while (en.hasMoreElements())
934 Pdbids pdb = new Pdbids();
935 jalview.datamodel.PDBEntry entry = (jalview.datamodel.PDBEntry) en
938 String pdbId = entry.getId();
940 pdb.setType(entry.getType());
943 * Store any structure views associated with this sequence. This
944 * section copes with duplicate entries in the project, so a dataset
945 * only view *should* be coped with sensibly.
947 // This must have been loaded, is it still visible?
948 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
949 String matchedFile = null;
950 for (int f = frames.length - 1; f > -1; f--)
952 if (frames[f] instanceof StructureViewerBase)
954 StructureViewerBase viewFrame = (StructureViewerBase) frames[f];
955 matchedFile = saveStructureState(ap, jds, pdb, entry,
956 viewIds, matchedFile, viewFrame);
958 * Only store each structure viewer's state once in the project
959 * jar. First time through only (storeDS==false)
961 String viewId = viewFrame.getViewId();
962 if (!storeDS && !viewIds.contains(viewId))
967 String viewerState = viewFrame.getStateInfo();
968 writeJarEntry(jout, getViewerJarEntryName(viewId),
969 viewerState.getBytes());
970 } catch (IOException e)
972 System.err.println("Error saving viewer state: "
979 if (matchedFile != null || entry.getFile() != null)
981 if (entry.getFile() != null)
984 matchedFile = entry.getFile();
986 pdb.setFile(matchedFile); // entry.getFile());
987 if (pdbfiles == null)
989 pdbfiles = new ArrayList<String>();
992 if (!pdbfiles.contains(pdbId))
995 copyFileToJar(jout, matchedFile, pdbId);
999 Enumeration<String> props = entry.getProperties();
1000 if (props.hasMoreElements())
1002 PdbentryItem item = new PdbentryItem();
1003 while (props.hasMoreElements())
1005 Property prop = new Property();
1006 String key = props.nextElement();
1008 prop.setValue(entry.getProperty(key).toString());
1009 item.addProperty(prop);
1011 pdb.addPdbentryItem(item);
1014 jseq.addPdbids(pdb);
1018 saveRnaViewers(jout, jseq, jds, viewIds, ap, storeDS);
1023 if (!storeDS && av.hasHiddenRows())
1025 jal = av.getAlignment();
1029 if (storeDS && jal.getCodonFrames() != null)
1031 List<AlignedCodonFrame> jac = jal.getCodonFrames();
1032 for (AlignedCodonFrame acf : jac)
1034 AlcodonFrame alc = new AlcodonFrame();
1035 if (acf.getProtMappings() != null
1036 && acf.getProtMappings().length > 0)
1038 boolean hasMap = false;
1039 SequenceI[] dnas = acf.getdnaSeqs();
1040 jalview.datamodel.Mapping[] pmaps = acf.getProtMappings();
1041 for (int m = 0; m < pmaps.length; m++)
1043 AlcodMap alcmap = new AlcodMap();
1044 alcmap.setDnasq(seqHash(dnas[m]));
1045 alcmap.setMapping(createVamsasMapping(pmaps[m], dnas[m], null,
1047 alc.addAlcodMap(alcmap);
1052 vamsasSet.addAlcodonFrame(alc);
1055 // TODO: delete this ? dead code from 2.8.3->2.9 ?
1057 // AlcodonFrame alc = new AlcodonFrame();
1058 // vamsasSet.addAlcodonFrame(alc);
1059 // for (int p = 0; p < acf.aaWidth; p++)
1061 // Alcodon cmap = new Alcodon();
1062 // if (acf.codons[p] != null)
1064 // // Null codons indicate a gapped column in the translated peptide
1066 // cmap.setPos1(acf.codons[p][0]);
1067 // cmap.setPos2(acf.codons[p][1]);
1068 // cmap.setPos3(acf.codons[p][2]);
1070 // alc.addAlcodon(cmap);
1072 // if (acf.getProtMappings() != null
1073 // && acf.getProtMappings().length > 0)
1075 // SequenceI[] dnas = acf.getdnaSeqs();
1076 // jalview.datamodel.Mapping[] pmaps = acf.getProtMappings();
1077 // for (int m = 0; m < pmaps.length; m++)
1079 // AlcodMap alcmap = new AlcodMap();
1080 // alcmap.setDnasq(seqHash(dnas[m]));
1081 // alcmap.setMapping(createVamsasMapping(pmaps[m], dnas[m], null,
1083 // alc.addAlcodMap(alcmap);
1090 // /////////////////////////////////
1091 if (!storeDS && av.currentTree != null)
1093 // FIND ANY ASSOCIATED TREES
1094 // NOT IMPLEMENTED FOR HEADLESS STATE AT PRESENT
1095 if (Desktop.desktop != null)
1097 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
1099 for (int t = 0; t < frames.length; t++)
1101 if (frames[t] instanceof TreePanel)
1103 TreePanel tp = (TreePanel) frames[t];
1105 if (tp.treeCanvas.av.getAlignment() == jal)
1107 Tree tree = new Tree();
1108 tree.setTitle(tp.getTitle());
1109 tree.setCurrentTree((av.currentTree == tp.getTree()));
1110 tree.setNewick(tp.getTree().print());
1111 tree.setThreshold(tp.treeCanvas.threshold);
1113 tree.setFitToWindow(tp.fitToWindow.getState());
1114 tree.setFontName(tp.getTreeFont().getName());
1115 tree.setFontSize(tp.getTreeFont().getSize());
1116 tree.setFontStyle(tp.getTreeFont().getStyle());
1117 tree.setMarkUnlinked(tp.placeholdersMenu.getState());
1119 tree.setShowBootstrap(tp.bootstrapMenu.getState());
1120 tree.setShowDistances(tp.distanceMenu.getState());
1122 tree.setHeight(tp.getHeight());
1123 tree.setWidth(tp.getWidth());
1124 tree.setXpos(tp.getX());
1125 tree.setYpos(tp.getY());
1126 tree.setId(makeHashCode(tp, null));
1136 * store forward refs from an annotationRow to any groups
1138 IdentityHashMap<SequenceGroup, String> groupRefs = new IdentityHashMap<SequenceGroup, String>();
1141 for (SequenceI sq : jal.getSequences())
1143 // Store annotation on dataset sequences only
1144 AlignmentAnnotation[] aa = sq.getAnnotation();
1145 if (aa != null && aa.length > 0)
1147 storeAlignmentAnnotation(aa, groupRefs, av, calcIdSet, storeDS,
1154 if (jal.getAlignmentAnnotation() != null)
1156 // Store the annotation shown on the alignment.
1157 AlignmentAnnotation[] aa = jal.getAlignmentAnnotation();
1158 storeAlignmentAnnotation(aa, groupRefs, av, calcIdSet, storeDS,
1163 if (jal.getGroups() != null)
1165 JGroup[] groups = new JGroup[jal.getGroups().size()];
1167 for (jalview.datamodel.SequenceGroup sg : jal.getGroups())
1169 JGroup jGroup = new JGroup();
1170 groups[++i] = jGroup;
1172 jGroup.setStart(sg.getStartRes());
1173 jGroup.setEnd(sg.getEndRes());
1174 jGroup.setName(sg.getName());
1175 if (groupRefs.containsKey(sg))
1177 // group has references so set its ID field
1178 jGroup.setId(groupRefs.get(sg));
1180 ColourSchemeI colourScheme = sg.getColourScheme();
1181 if (colourScheme != null)
1183 ResidueShaderI groupColourScheme = sg
1184 .getGroupColourScheme();
1185 if (groupColourScheme.conservationApplied())
1187 jGroup.setConsThreshold(groupColourScheme.getConservationInc());
1189 if (colourScheme instanceof jalview.schemes.UserColourScheme)
1191 jGroup.setColour(setUserColourScheme(colourScheme,
1196 jGroup.setColour(colourScheme.getSchemeName());
1199 else if (colourScheme instanceof jalview.schemes.AnnotationColourGradient)
1201 jGroup.setColour("AnnotationColourGradient");
1202 jGroup.setAnnotationColours(constructAnnotationColours(
1203 (jalview.schemes.AnnotationColourGradient) colourScheme,
1206 else if (colourScheme instanceof jalview.schemes.UserColourScheme)
1208 jGroup.setColour(setUserColourScheme(colourScheme,
1213 jGroup.setColour(colourScheme.getSchemeName());
1216 jGroup.setPidThreshold(groupColourScheme.getThreshold());
1219 jGroup.setOutlineColour(sg.getOutlineColour().getRGB());
1220 jGroup.setDisplayBoxes(sg.getDisplayBoxes());
1221 jGroup.setDisplayText(sg.getDisplayText());
1222 jGroup.setColourText(sg.getColourText());
1223 jGroup.setTextCol1(sg.textColour.getRGB());
1224 jGroup.setTextCol2(sg.textColour2.getRGB());
1225 jGroup.setTextColThreshold(sg.thresholdTextColour);
1226 jGroup.setShowUnconserved(sg.getShowNonconserved());
1227 jGroup.setIgnoreGapsinConsensus(sg.getIgnoreGapsConsensus());
1228 jGroup.setShowConsensusHistogram(sg.isShowConsensusHistogram());
1229 jGroup.setShowSequenceLogo(sg.isShowSequenceLogo());
1230 jGroup.setNormaliseSequenceLogo(sg.isNormaliseSequenceLogo());
1231 for (SequenceI seq : sg.getSequences())
1233 jGroup.addSeq(seqHash(seq));
1237 jms.setJGroup(groups);
1241 // /////////SAVE VIEWPORT
1242 Viewport view = new Viewport();
1243 view.setTitle(ap.alignFrame.getTitle());
1244 view.setSequenceSetId(makeHashCode(av.getSequenceSetId(),
1245 av.getSequenceSetId()));
1246 view.setId(av.getViewId());
1247 if (av.getCodingComplement() != null)
1249 view.setComplementId(av.getCodingComplement().getViewId());
1251 view.setViewName(av.viewName);
1252 view.setGatheredViews(av.isGatherViewsHere());
1254 Rectangle size = ap.av.getExplodedGeometry();
1255 Rectangle position = size;
1258 size = ap.alignFrame.getBounds();
1259 if (av.getCodingComplement() != null)
1261 position = ((SplitFrame) ap.alignFrame.getSplitViewContainer())
1269 view.setXpos(position.x);
1270 view.setYpos(position.y);
1272 view.setWidth(size.width);
1273 view.setHeight(size.height);
1275 view.setStartRes(vpRanges.getStartRes());
1276 view.setStartSeq(vpRanges.getStartSeq());
1278 if (av.getGlobalColourScheme() instanceof jalview.schemes.UserColourScheme)
1280 view.setBgColour(setUserColourScheme(av.getGlobalColourScheme(),
1283 else if (av.getGlobalColourScheme() instanceof jalview.schemes.AnnotationColourGradient)
1285 AnnotationColours ac = constructAnnotationColours(
1286 (jalview.schemes.AnnotationColourGradient) av
1287 .getGlobalColourScheme(),
1290 view.setAnnotationColours(ac);
1291 view.setBgColour("AnnotationColourGradient");
1295 view.setBgColour(ColourSchemeProperty.getColourName(av
1296 .getGlobalColourScheme()));
1299 ResidueShaderI vcs = av.getResidueShading();
1300 ColourSchemeI cs = av.getGlobalColourScheme();
1304 if (vcs.conservationApplied())
1306 view.setConsThreshold(vcs.getConservationInc());
1307 if (cs instanceof jalview.schemes.UserColourScheme)
1309 view.setBgColour(setUserColourScheme(cs, userColours, jms));
1312 view.setPidThreshold(vcs.getThreshold());
1315 view.setConservationSelected(av.getConservationSelected());
1316 view.setPidSelected(av.getAbovePIDThreshold());
1317 view.setFontName(av.font.getName());
1318 view.setFontSize(av.font.getSize());
1319 view.setFontStyle(av.font.getStyle());
1320 view.setScaleProteinAsCdna(av.getViewStyle().isScaleProteinAsCdna());
1321 view.setRenderGaps(av.isRenderGaps());
1322 view.setShowAnnotation(av.isShowAnnotation());
1323 view.setShowBoxes(av.getShowBoxes());
1324 view.setShowColourText(av.getColourText());
1325 view.setShowFullId(av.getShowJVSuffix());
1326 view.setRightAlignIds(av.isRightAlignIds());
1327 view.setShowSequenceFeatures(av.isShowSequenceFeatures());
1328 view.setShowText(av.getShowText());
1329 view.setShowUnconserved(av.getShowUnconserved());
1330 view.setWrapAlignment(av.getWrapAlignment());
1331 view.setTextCol1(av.getTextColour().getRGB());
1332 view.setTextCol2(av.getTextColour2().getRGB());
1333 view.setTextColThreshold(av.getThresholdTextColour());
1334 view.setShowConsensusHistogram(av.isShowConsensusHistogram());
1335 view.setShowSequenceLogo(av.isShowSequenceLogo());
1336 view.setNormaliseSequenceLogo(av.isNormaliseSequenceLogo());
1337 view.setShowGroupConsensus(av.isShowGroupConsensus());
1338 view.setShowGroupConservation(av.isShowGroupConservation());
1339 view.setShowNPfeatureTooltip(av.isShowNPFeats());
1340 view.setShowDbRefTooltip(av.isShowDBRefs());
1341 view.setFollowHighlight(av.isFollowHighlight());
1342 view.setFollowSelection(av.followSelection);
1343 view.setIgnoreGapsinConsensus(av.isIgnoreGapsConsensus());
1344 if (av.getFeaturesDisplayed() != null)
1346 jalview.schemabinding.version2.FeatureSettings fs = new jalview.schemabinding.version2.FeatureSettings();
1348 String[] renderOrder = ap.getSeqPanel().seqCanvas
1349 .getFeatureRenderer().getRenderOrder()
1350 .toArray(new String[0]);
1352 Vector<String> settingsAdded = new Vector<String>();
1353 if (renderOrder != null)
1355 for (String featureType : renderOrder)
1357 FeatureColourI fcol = ap.getSeqPanel().seqCanvas
1358 .getFeatureRenderer().getFeatureStyle(featureType);
1359 Setting setting = new Setting();
1360 setting.setType(featureType);
1361 if (!fcol.isSimpleColour())
1363 setting.setColour(fcol.getMaxColour().getRGB());
1364 setting.setMincolour(fcol.getMinColour().getRGB());
1365 setting.setMin(fcol.getMin());
1366 setting.setMax(fcol.getMax());
1367 setting.setColourByLabel(fcol.isColourByLabel());
1368 setting.setAutoScale(fcol.isAutoScaled());
1369 setting.setThreshold(fcol.getThreshold());
1370 // -1 = No threshold, 0 = Below, 1 = Above
1371 setting.setThreshstate(fcol.isAboveThreshold() ? 1 : (fcol
1372 .isBelowThreshold() ? 0 : -1));
1376 setting.setColour(fcol.getColour().getRGB());
1379 setting.setDisplay(av.getFeaturesDisplayed().isVisible(
1381 float rorder = ap.getSeqPanel().seqCanvas.getFeatureRenderer()
1382 .getOrder(featureType);
1385 setting.setOrder(rorder);
1387 fs.addSetting(setting);
1388 settingsAdded.addElement(featureType);
1392 // is groups actually supposed to be a map here ?
1393 Iterator<String> en = ap.getSeqPanel().seqCanvas
1394 .getFeatureRenderer().getFeatureGroups().iterator();
1395 Vector<String> groupsAdded = new Vector<String>();
1396 while (en.hasNext())
1398 String grp = en.next();
1399 if (groupsAdded.contains(grp))
1403 Group g = new Group();
1405 g.setDisplay(((Boolean) ap.getSeqPanel().seqCanvas
1406 .getFeatureRenderer().checkGroupVisibility(grp, false))
1409 groupsAdded.addElement(grp);
1411 jms.setFeatureSettings(fs);
1414 if (av.hasHiddenColumns())
1416 jalview.datamodel.HiddenColumns hidden = av.getAlignment()
1417 .getHiddenColumns();
1418 if (hidden == null || hidden.getHiddenRegions() == null)
1420 warn("REPORT BUG: avoided null columnselection bug (DMAM reported). Please contact Jim about this.");
1424 for (int c = 0; c < hidden.getHiddenRegions()
1427 int[] region = hidden.getHiddenRegions()
1429 HiddenColumns hc = new HiddenColumns();
1430 hc.setStart(region[0]);
1431 hc.setEnd(region[1]);
1432 view.addHiddenColumns(hc);
1436 if (calcIdSet.size() > 0)
1438 for (String calcId : calcIdSet)
1440 if (calcId.trim().length() > 0)
1442 CalcIdParam cidp = createCalcIdParam(calcId, av);
1443 // Some calcIds have no parameters.
1446 view.addCalcIdParam(cidp);
1452 jms.addViewport(view);
1454 object.setJalviewModelSequence(jms);
1455 object.getVamsasModel().addSequenceSet(vamsasSet);
1457 if (jout != null && fileName != null)
1459 // We may not want to write the object to disk,
1460 // eg we can copy the alignViewport to a new view object
1461 // using save and then load
1464 System.out.println("Writing jar entry " + fileName);
1465 JarEntry entry = new JarEntry(fileName);
1466 jout.putNextEntry(entry);
1467 PrintWriter pout = new PrintWriter(new OutputStreamWriter(jout,
1469 Marshaller marshaller = new Marshaller(pout);
1470 marshaller.marshal(object);
1473 } catch (Exception ex)
1475 // TODO: raise error in GUI if marshalling failed.
1476 ex.printStackTrace();
1483 * Save any Varna viewers linked to this sequence. Writes an rnaViewer element
1484 * for each viewer, with
1486 * <li>viewer geometry (position, size, split pane divider location)</li>
1487 * <li>index of the selected structure in the viewer (currently shows gapped
1489 * <li>the id of the annotation holding RNA secondary structure</li>
1490 * <li>(currently only one SS is shown per viewer, may be more in future)</li>
1492 * Varna viewer state is also written out (in native Varna XML) to separate
1493 * project jar entries. A separate entry is written for each RNA structure
1494 * displayed, with the naming convention
1496 * <li>rna_viewId_sequenceId_annotationId_[gapped|trimmed]</li>
1504 * @param storeDataset
1506 protected void saveRnaViewers(JarOutputStream jout, JSeq jseq,
1507 final SequenceI jds, List<String> viewIds, AlignmentPanel ap,
1508 boolean storeDataset)
1510 if (Desktop.desktop == null)
1514 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
1515 for (int f = frames.length - 1; f > -1; f--)
1517 if (frames[f] instanceof AppVarna)
1519 AppVarna varna = (AppVarna) frames[f];
1521 * link the sequence to every viewer that is showing it and is linked to
1522 * its alignment panel
1524 if (varna.isListeningFor(jds) && ap == varna.getAlignmentPanel())
1526 String viewId = varna.getViewId();
1527 RnaViewer rna = new RnaViewer();
1528 rna.setViewId(viewId);
1529 rna.setTitle(varna.getTitle());
1530 rna.setXpos(varna.getX());
1531 rna.setYpos(varna.getY());
1532 rna.setWidth(varna.getWidth());
1533 rna.setHeight(varna.getHeight());
1534 rna.setDividerLocation(varna.getDividerLocation());
1535 rna.setSelectedRna(varna.getSelectedIndex());
1536 jseq.addRnaViewer(rna);
1539 * Store each Varna panel's state once in the project per sequence.
1540 * First time through only (storeDataset==false)
1542 // boolean storeSessions = false;
1543 // String sequenceViewId = viewId + seqsToIds.get(jds);
1544 // if (!storeDataset && !viewIds.contains(sequenceViewId))
1546 // viewIds.add(sequenceViewId);
1547 // storeSessions = true;
1549 for (RnaModel model : varna.getModels())
1551 if (model.seq == jds)
1554 * VARNA saves each view (sequence or alignment secondary
1555 * structure, gapped or trimmed) as a separate XML file
1557 String jarEntryName = rnaSessions.get(model);
1558 if (jarEntryName == null)
1561 String varnaStateFile = varna.getStateInfo(model.rna);
1562 jarEntryName = RNA_PREFIX + viewId + "_" + nextCounter();
1563 copyFileToJar(jout, varnaStateFile, jarEntryName);
1564 rnaSessions.put(model, jarEntryName);
1566 SecondaryStructure ss = new SecondaryStructure();
1567 String annotationId = varna.getAnnotation(jds).annotationId;
1568 ss.setAnnotationId(annotationId);
1569 ss.setViewerState(jarEntryName);
1570 ss.setGapped(model.gapped);
1571 ss.setTitle(model.title);
1572 rna.addSecondaryStructure(ss);
1581 * Copy the contents of a file to a new entry added to the output jar
1585 * @param jarEntryName
1587 protected void copyFileToJar(JarOutputStream jout, String infilePath,
1588 String jarEntryName)
1590 DataInputStream dis = null;
1593 File file = new File(infilePath);
1594 if (file.exists() && jout != null)
1596 dis = new DataInputStream(new FileInputStream(file));
1597 byte[] data = new byte[(int) file.length()];
1598 dis.readFully(data);
1599 writeJarEntry(jout, jarEntryName, data);
1601 } catch (Exception ex)
1603 ex.printStackTrace();
1611 } catch (IOException e)
1620 * Write the data to a new entry of given name in the output jar file
1623 * @param jarEntryName
1625 * @throws IOException
1627 protected void writeJarEntry(JarOutputStream jout, String jarEntryName,
1628 byte[] data) throws IOException
1632 System.out.println("Writing jar entry " + jarEntryName);
1633 jout.putNextEntry(new JarEntry(jarEntryName));
1634 DataOutputStream dout = new DataOutputStream(jout);
1635 dout.write(data, 0, data.length);
1642 * Save the state of a structure viewer
1647 * the archive XML element under which to save the state
1650 * @param matchedFile
1654 protected String saveStructureState(AlignmentPanel ap, SequenceI jds,
1655 Pdbids pdb, PDBEntry entry, List<String> viewIds,
1656 String matchedFile, StructureViewerBase viewFrame)
1658 final AAStructureBindingModel bindingModel = viewFrame.getBinding();
1661 * Look for any bindings for this viewer to the PDB file of interest
1662 * (including part matches excluding chain id)
1664 for (int peid = 0; peid < bindingModel.getPdbCount(); peid++)
1666 final PDBEntry pdbentry = bindingModel.getPdbEntry(peid);
1667 final String pdbId = pdbentry.getId();
1668 if (!pdbId.equals(entry.getId())
1669 && !(entry.getId().length() > 4 && entry.getId()
1670 .toLowerCase().startsWith(pdbId.toLowerCase())))
1673 * not interested in a binding to a different PDB entry here
1677 if (matchedFile == null)
1679 matchedFile = pdbentry.getFile();
1681 else if (!matchedFile.equals(pdbentry.getFile()))
1684 .warn("Probably lost some PDB-Sequence mappings for this structure file (which apparently has same PDB Entry code): "
1685 + pdbentry.getFile());
1689 // can get at it if the ID
1690 // match is ambiguous (e.g.
1693 for (int smap = 0; smap < viewFrame.getBinding().getSequence()[peid].length; smap++)
1695 // if (jal.findIndex(jmol.jmb.sequence[peid][smap]) > -1)
1696 if (jds == viewFrame.getBinding().getSequence()[peid][smap])
1698 StructureState state = new StructureState();
1699 state.setVisible(true);
1700 state.setXpos(viewFrame.getX());
1701 state.setYpos(viewFrame.getY());
1702 state.setWidth(viewFrame.getWidth());
1703 state.setHeight(viewFrame.getHeight());
1704 final String viewId = viewFrame.getViewId();
1705 state.setViewId(viewId);
1706 state.setAlignwithAlignPanel(viewFrame.isUsedforaligment(ap));
1707 state.setColourwithAlignPanel(viewFrame.isUsedforcolourby(ap));
1708 state.setColourByJmol(viewFrame.isColouredByViewer());
1709 state.setType(viewFrame.getViewerType().toString());
1710 pdb.addStructureState(state);
1718 * Populates the AnnotationColours xml for save. This captures the settings of
1719 * the options in the 'Colour by Annotation' dialog.
1722 * @param userColours
1726 private AnnotationColours constructAnnotationColours(
1727 AnnotationColourGradient acg, List<UserColourScheme> userColours,
1728 JalviewModelSequence jms)
1730 AnnotationColours ac = new AnnotationColours();
1731 ac.setAboveThreshold(acg.getAboveThreshold());
1732 ac.setThreshold(acg.getAnnotationThreshold());
1733 // 2.10.2 save annotationId (unique) not annotation label
1734 ac.setAnnotation(acg.getAnnotation().annotationId);
1735 if (acg.getBaseColour() instanceof UserColourScheme)
1737 ac.setColourScheme(setUserColourScheme(acg.getBaseColour(),
1742 ac.setColourScheme(ColourSchemeProperty.getColourName(acg.getBaseColour()));
1745 ac.setMaxColour(acg.getMaxColour().getRGB());
1746 ac.setMinColour(acg.getMinColour().getRGB());
1747 ac.setPerSequence(acg.isSeqAssociated());
1748 ac.setPredefinedColours(acg.isPredefinedColours());
1752 private void storeAlignmentAnnotation(AlignmentAnnotation[] aa,
1753 IdentityHashMap<SequenceGroup, String> groupRefs,
1754 AlignmentViewport av, Set<String> calcIdSet, boolean storeDS,
1755 SequenceSet vamsasSet)
1758 for (int i = 0; i < aa.length; i++)
1760 Annotation an = new Annotation();
1762 AlignmentAnnotation annotation = aa[i];
1763 if (annotation.annotationId != null)
1765 annotationIds.put(annotation.annotationId, annotation);
1768 an.setId(annotation.annotationId);
1770 an.setVisible(annotation.visible);
1772 an.setDescription(annotation.description);
1774 if (annotation.sequenceRef != null)
1776 // 2.9 JAL-1781 xref on sequence id rather than name
1777 an.setSequenceRef(seqsToIds.get(annotation.sequenceRef));
1779 if (annotation.groupRef != null)
1781 String groupIdr = groupRefs.get(annotation.groupRef);
1782 if (groupIdr == null)
1784 // make a locally unique String
1786 annotation.groupRef,
1787 groupIdr = ("" + System.currentTimeMillis()
1788 + annotation.groupRef.getName() + groupRefs
1791 an.setGroupRef(groupIdr.toString());
1794 // store all visualization attributes for annotation
1795 an.setGraphHeight(annotation.graphHeight);
1796 an.setCentreColLabels(annotation.centreColLabels);
1797 an.setScaleColLabels(annotation.scaleColLabel);
1798 an.setShowAllColLabels(annotation.showAllColLabels);
1799 an.setBelowAlignment(annotation.belowAlignment);
1801 if (annotation.graph > 0)
1804 an.setGraphType(annotation.graph);
1805 an.setGraphGroup(annotation.graphGroup);
1806 if (annotation.getThreshold() != null)
1808 ThresholdLine line = new ThresholdLine();
1809 line.setLabel(annotation.getThreshold().label);
1810 line.setValue(annotation.getThreshold().value);
1811 line.setColour(annotation.getThreshold().colour.getRGB());
1812 an.setThresholdLine(line);
1820 an.setLabel(annotation.label);
1822 if (annotation == av.getAlignmentQualityAnnot()
1823 || annotation == av.getAlignmentConservationAnnotation()
1824 || annotation == av.getAlignmentConsensusAnnotation()
1825 || annotation.autoCalculated)
1827 // new way of indicating autocalculated annotation -
1828 an.setAutoCalculated(annotation.autoCalculated);
1830 if (annotation.hasScore())
1832 an.setScore(annotation.getScore());
1835 if (annotation.getCalcId() != null)
1837 calcIdSet.add(annotation.getCalcId());
1838 an.setCalcId(annotation.getCalcId());
1840 if (annotation.hasProperties())
1842 for (String pr : annotation.getProperties())
1844 Property prop = new Property();
1846 prop.setValue(annotation.getProperty(pr));
1847 an.addProperty(prop);
1851 AnnotationElement ae;
1852 if (annotation.annotations != null)
1854 an.setScoreOnly(false);
1855 for (int a = 0; a < annotation.annotations.length; a++)
1857 if ((annotation == null) || (annotation.annotations[a] == null))
1862 ae = new AnnotationElement();
1863 if (annotation.annotations[a].description != null)
1865 ae.setDescription(annotation.annotations[a].description);
1867 if (annotation.annotations[a].displayCharacter != null)
1869 ae.setDisplayCharacter(annotation.annotations[a].displayCharacter);
1872 if (!Float.isNaN(annotation.annotations[a].value))
1874 ae.setValue(annotation.annotations[a].value);
1878 if (annotation.annotations[a].secondaryStructure > ' ')
1880 ae.setSecondaryStructure(annotation.annotations[a].secondaryStructure
1884 if (annotation.annotations[a].colour != null
1885 && annotation.annotations[a].colour != java.awt.Color.black)
1887 ae.setColour(annotation.annotations[a].colour.getRGB());
1890 an.addAnnotationElement(ae);
1891 if (annotation.autoCalculated)
1893 // only write one non-null entry into the annotation row -
1894 // sufficient to get the visualization attributes necessary to
1902 an.setScoreOnly(true);
1904 if (!storeDS || (storeDS && !annotation.autoCalculated))
1906 // skip autocalculated annotation - these are only provided for
1908 vamsasSet.addAnnotation(an);
1914 private CalcIdParam createCalcIdParam(String calcId, AlignViewport av)
1916 AutoCalcSetting settings = av.getCalcIdSettingsFor(calcId);
1917 if (settings != null)
1919 CalcIdParam vCalcIdParam = new CalcIdParam();
1920 vCalcIdParam.setCalcId(calcId);
1921 vCalcIdParam.addServiceURL(settings.getServiceURI());
1922 // generic URI allowing a third party to resolve another instance of the
1923 // service used for this calculation
1924 for (String urls : settings.getServiceURLs())
1926 vCalcIdParam.addServiceURL(urls);
1928 vCalcIdParam.setVersion("1.0");
1929 if (settings.getPreset() != null)
1931 WsParamSetI setting = settings.getPreset();
1932 vCalcIdParam.setName(setting.getName());
1933 vCalcIdParam.setDescription(setting.getDescription());
1937 vCalcIdParam.setName("");
1938 vCalcIdParam.setDescription("Last used parameters");
1940 // need to be able to recover 1) settings 2) user-defined presets or
1941 // recreate settings from preset 3) predefined settings provided by
1942 // service - or settings that can be transferred (or discarded)
1943 vCalcIdParam.setParameters(settings.getWsParamFile().replace("\n",
1945 vCalcIdParam.setAutoUpdate(settings.isAutoUpdate());
1946 // todo - decide if updateImmediately is needed for any projects.
1948 return vCalcIdParam;
1953 private boolean recoverCalcIdParam(CalcIdParam calcIdParam,
1956 if (calcIdParam.getVersion().equals("1.0"))
1958 Jws2Instance service = Jws2Discoverer.getDiscoverer()
1959 .getPreferredServiceFor(calcIdParam.getServiceURL());
1960 if (service != null)
1962 WsParamSetI parmSet = null;
1965 parmSet = service.getParamStore().parseServiceParameterFile(
1966 calcIdParam.getName(), calcIdParam.getDescription(),
1967 calcIdParam.getServiceURL(),
1968 calcIdParam.getParameters().replace("|\\n|", "\n"));
1969 } catch (IOException x)
1971 warn("Couldn't parse parameter data for "
1972 + calcIdParam.getCalcId(), x);
1975 List<ArgumentI> argList = null;
1976 if (calcIdParam.getName().length() > 0)
1978 parmSet = service.getParamStore()
1979 .getPreset(calcIdParam.getName());
1980 if (parmSet != null)
1982 // TODO : check we have a good match with settings in AACon -
1983 // otherwise we'll need to create a new preset
1988 argList = parmSet.getArguments();
1991 AAConSettings settings = new AAConSettings(
1992 calcIdParam.isAutoUpdate(), service, parmSet, argList);
1993 av.setCalcIdSettingsFor(calcIdParam.getCalcId(), settings,
1994 calcIdParam.isNeedsUpdate());
1999 warn("Cannot resolve a service for the parameters used in this project. Try configuring a JABAWS server.");
2003 throw new Error(MessageManager.formatMessage(
2004 "error.unsupported_version_calcIdparam",
2005 new Object[] { calcIdParam.toString() }));
2009 * External mapping between jalview objects and objects yielding a valid and
2010 * unique object ID string. This is null for normal Jalview project IO, but
2011 * non-null when a jalview project is being read or written as part of a
2014 IdentityHashMap jv2vobj = null;
2017 * Construct a unique ID for jvobj using either existing bindings or if none
2018 * exist, the result of the hashcode call for the object.
2021 * jalview data object
2022 * @return unique ID for referring to jvobj
2024 private String makeHashCode(Object jvobj, String altCode)
2026 if (jv2vobj != null)
2028 Object id = jv2vobj.get(jvobj);
2031 return id.toString();
2033 // check string ID mappings
2034 if (jvids2vobj != null && jvobj instanceof String)
2036 id = jvids2vobj.get(jvobj);
2040 return id.toString();
2042 // give up and warn that something has gone wrong
2043 warn("Cannot find ID for object in external mapping : " + jvobj);
2049 * return local jalview object mapped to ID, if it exists
2053 * @return null or object bound to idcode
2055 private Object retrieveExistingObj(String idcode)
2057 if (idcode != null && vobj2jv != null)
2059 return vobj2jv.get(idcode);
2065 * binding from ID strings from external mapping table to jalview data model
2068 private Hashtable vobj2jv;
2070 private Sequence createVamsasSequence(String id, SequenceI jds)
2072 return createVamsasSequence(true, id, jds, null);
2075 private Sequence createVamsasSequence(boolean recurse, String id,
2076 SequenceI jds, SequenceI parentseq)
2078 Sequence vamsasSeq = new Sequence();
2079 vamsasSeq.setId(id);
2080 vamsasSeq.setName(jds.getName());
2081 vamsasSeq.setSequence(jds.getSequenceAsString());
2082 vamsasSeq.setDescription(jds.getDescription());
2083 jalview.datamodel.DBRefEntry[] dbrefs = null;
2084 if (jds.getDatasetSequence() != null)
2086 vamsasSeq.setDsseqid(seqHash(jds.getDatasetSequence()));
2090 // seqId==dsseqid so we can tell which sequences really are
2091 // dataset sequences only
2092 vamsasSeq.setDsseqid(id);
2093 dbrefs = jds.getDBRefs();
2094 if (parentseq == null)
2101 for (int d = 0; d < dbrefs.length; d++)
2103 DBRef dbref = new DBRef();
2104 dbref.setSource(dbrefs[d].getSource());
2105 dbref.setVersion(dbrefs[d].getVersion());
2106 dbref.setAccessionId(dbrefs[d].getAccessionId());
2107 if (dbrefs[d].hasMap())
2109 Mapping mp = createVamsasMapping(dbrefs[d].getMap(), parentseq,
2111 dbref.setMapping(mp);
2113 vamsasSeq.addDBRef(dbref);
2119 private Mapping createVamsasMapping(jalview.datamodel.Mapping jmp,
2120 SequenceI parentseq, SequenceI jds, boolean recurse)
2123 if (jmp.getMap() != null)
2127 jalview.util.MapList mlst = jmp.getMap();
2128 List<int[]> r = mlst.getFromRanges();
2129 for (int[] range : r)
2131 MapListFrom mfrom = new MapListFrom();
2132 mfrom.setStart(range[0]);
2133 mfrom.setEnd(range[1]);
2134 mp.addMapListFrom(mfrom);
2136 r = mlst.getToRanges();
2137 for (int[] range : r)
2139 MapListTo mto = new MapListTo();
2140 mto.setStart(range[0]);
2141 mto.setEnd(range[1]);
2142 mp.addMapListTo(mto);
2144 mp.setMapFromUnit(mlst.getFromRatio());
2145 mp.setMapToUnit(mlst.getToRatio());
2146 if (jmp.getTo() != null)
2148 MappingChoice mpc = new MappingChoice();
2150 // check/create ID for the sequence referenced by getTo()
2153 SequenceI ps = null;
2154 if (parentseq != jmp.getTo()
2155 && parentseq.getDatasetSequence() != jmp.getTo())
2157 // chaining dbref rather than a handshaking one
2158 jmpid = seqHash(ps = jmp.getTo());
2162 jmpid = seqHash(ps = parentseq);
2164 mpc.setDseqFor(jmpid);
2165 if (!seqRefIds.containsKey(mpc.getDseqFor()))
2167 jalview.bin.Cache.log.debug("creatign new DseqFor ID");
2168 seqRefIds.put(mpc.getDseqFor(), ps);
2172 jalview.bin.Cache.log.debug("reusing DseqFor ID");
2175 mp.setMappingChoice(mpc);
2181 String setUserColourScheme(jalview.schemes.ColourSchemeI cs,
2182 List<UserColourScheme> userColours, JalviewModelSequence jms)
2185 jalview.schemes.UserColourScheme ucs = (jalview.schemes.UserColourScheme) cs;
2186 boolean newucs = false;
2187 if (!userColours.contains(ucs))
2189 userColours.add(ucs);
2192 id = "ucs" + userColours.indexOf(ucs);
2195 // actually create the scheme's entry in the XML model
2196 java.awt.Color[] colours = ucs.getColours();
2197 jalview.schemabinding.version2.UserColours uc = new jalview.schemabinding.version2.UserColours();
2198 jalview.schemabinding.version2.UserColourScheme jbucs = new jalview.schemabinding.version2.UserColourScheme();
2200 for (int i = 0; i < colours.length; i++)
2202 jalview.schemabinding.version2.Colour col = new jalview.schemabinding.version2.Colour();
2203 col.setName(ResidueProperties.aa[i]);
2204 col.setRGB(jalview.util.Format.getHexString(colours[i]));
2205 jbucs.addColour(col);
2207 if (ucs.getLowerCaseColours() != null)
2209 colours = ucs.getLowerCaseColours();
2210 for (int i = 0; i < colours.length; i++)
2212 jalview.schemabinding.version2.Colour col = new jalview.schemabinding.version2.Colour();
2213 col.setName(ResidueProperties.aa[i].toLowerCase());
2214 col.setRGB(jalview.util.Format.getHexString(colours[i]));
2215 jbucs.addColour(col);
2220 uc.setUserColourScheme(jbucs);
2221 jms.addUserColours(uc);
2227 jalview.schemes.UserColourScheme getUserColourScheme(
2228 JalviewModelSequence jms, String id)
2230 UserColours[] uc = jms.getUserColours();
2231 UserColours colours = null;
2233 for (int i = 0; i < uc.length; i++)
2235 if (uc[i].getId().equals(id))
2243 java.awt.Color[] newColours = new java.awt.Color[24];
2245 for (int i = 0; i < 24; i++)
2247 newColours[i] = new java.awt.Color(Integer.parseInt(colours
2248 .getUserColourScheme().getColour(i).getRGB(), 16));
2251 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(
2254 if (colours.getUserColourScheme().getColourCount() > 24)
2256 newColours = new java.awt.Color[23];
2257 for (int i = 0; i < 23; i++)
2259 newColours[i] = new java.awt.Color(Integer.parseInt(colours
2260 .getUserColourScheme().getColour(i + 24).getRGB(), 16));
2262 ucs.setLowerCaseColours(newColours);
2269 * contains last error message (if any) encountered by XML loader.
2271 String errorMessage = null;
2274 * flag to control whether the Jalview2XML_V1 parser should be deferred to if
2275 * exceptions are raised during project XML parsing
2277 public boolean attemptversion1parse = true;
2280 * Load a jalview project archive from a jar file
2283 * - HTTP URL or filename
2285 public AlignFrame loadJalviewAlign(final String file)
2288 jalview.gui.AlignFrame af = null;
2292 // create list to store references for any new Jmol viewers created
2293 newStructureViewers = new Vector<JalviewStructureDisplayI>();
2294 // UNMARSHALLER SEEMS TO CLOSE JARINPUTSTREAM, MOST ANNOYING
2295 // Workaround is to make sure caller implements the JarInputStreamProvider
2297 // so we can re-open the jar input stream for each entry.
2299 jarInputStreamProvider jprovider = createjarInputStreamProvider(file);
2300 af = loadJalviewAlign(jprovider);
2302 } catch (MalformedURLException e)
2304 errorMessage = "Invalid URL format for '" + file + "'";
2310 SwingUtilities.invokeAndWait(new Runnable()
2315 setLoadingFinishedForNewStructureViewers();
2318 } catch (Exception x)
2320 System.err.println("Error loading alignment: " + x.getMessage());
2326 private jarInputStreamProvider createjarInputStreamProvider(
2327 final String file) throws MalformedURLException
2330 errorMessage = null;
2331 uniqueSetSuffix = null;
2333 viewportsAdded.clear();
2334 frefedSequence = null;
2336 if (file.startsWith("http://"))
2338 url = new URL(file);
2340 final URL _url = url;
2341 return new jarInputStreamProvider()
2345 public JarInputStream getJarInputStream() throws IOException
2349 return new JarInputStream(_url.openStream());
2353 return new JarInputStream(new FileInputStream(file));
2358 public String getFilename()
2366 * Recover jalview session from a jalview project archive. Caller may
2367 * initialise uniqueSetSuffix, seqRefIds, viewportsAdded and frefedSequence
2368 * themselves. Any null fields will be initialised with default values,
2369 * non-null fields are left alone.
2374 public AlignFrame loadJalviewAlign(final jarInputStreamProvider jprovider)
2376 errorMessage = null;
2377 if (uniqueSetSuffix == null)
2379 uniqueSetSuffix = System.currentTimeMillis() % 100000 + "";
2381 if (seqRefIds == null)
2385 AlignFrame af = null, _af = null;
2386 IdentityHashMap<AlignmentI, AlignmentI> importedDatasets = new IdentityHashMap<AlignmentI, AlignmentI>();
2387 Map<String, AlignFrame> gatherToThisFrame = new HashMap<String, AlignFrame>();
2388 final String file = jprovider.getFilename();
2391 JarInputStream jin = null;
2392 JarEntry jarentry = null;
2397 jin = jprovider.getJarInputStream();
2398 for (int i = 0; i < entryCount; i++)
2400 jarentry = jin.getNextJarEntry();
2403 if (jarentry != null && jarentry.getName().endsWith(".xml"))
2405 InputStreamReader in = new InputStreamReader(jin, UTF_8);
2406 JalviewModel object = new JalviewModel();
2408 Unmarshaller unmar = new Unmarshaller(object);
2409 unmar.setValidation(false);
2410 object = (JalviewModel) unmar.unmarshal(in);
2411 if (true) // !skipViewport(object))
2413 _af = loadFromObject(object, file, true, jprovider);
2415 && object.getJalviewModelSequence().getViewportCount() > 0)
2419 // store a reference to the first view
2422 if (_af.viewport.isGatherViewsHere())
2424 // if this is a gathered view, keep its reference since
2425 // after gathering views, only this frame will remain
2427 gatherToThisFrame.put(_af.viewport.getSequenceSetId(), _af);
2429 // Save dataset to register mappings once all resolved
2430 importedDatasets.put(af.viewport.getAlignment().getDataset(),
2431 af.viewport.getAlignment().getDataset());
2436 else if (jarentry != null)
2438 // Some other file here.
2441 } while (jarentry != null);
2442 resolveFrefedSequences();
2443 } catch (IOException ex)
2445 ex.printStackTrace();
2446 errorMessage = "Couldn't locate Jalview XML file : " + file;
2447 System.err.println("Exception whilst loading jalview XML file : "
2449 } catch (Exception ex)
2451 System.err.println("Parsing as Jalview Version 2 file failed.");
2452 ex.printStackTrace(System.err);
2453 if (attemptversion1parse)
2455 // Is Version 1 Jar file?
2458 af = new Jalview2XML_V1(raiseGUI).LoadJalviewAlign(jprovider);
2459 } catch (Exception ex2)
2461 System.err.println("Exception whilst loading as jalviewXMLV1:");
2462 ex2.printStackTrace();
2466 if (Desktop.instance != null)
2468 Desktop.instance.stopLoading();
2472 System.out.println("Successfully loaded archive file");
2475 ex.printStackTrace();
2477 System.err.println("Exception whilst loading jalview XML file : "
2479 } catch (OutOfMemoryError e)
2481 // Don't use the OOM Window here
2482 errorMessage = "Out of memory loading jalview XML file";
2483 System.err.println("Out of memory whilst loading jalview XML file");
2484 e.printStackTrace();
2488 * Regather multiple views (with the same sequence set id) to the frame (if
2489 * any) that is flagged as the one to gather to, i.e. convert them to tabbed
2490 * views instead of separate frames. Note this doesn't restore a state where
2491 * some expanded views in turn have tabbed views - the last "first tab" read
2492 * in will play the role of gatherer for all.
2494 for (AlignFrame fr : gatherToThisFrame.values())
2496 Desktop.instance.gatherViews(fr);
2499 restoreSplitFrames();
2500 for (AlignmentI ds : importedDatasets.keySet())
2502 if (ds.getCodonFrames() != null)
2504 StructureSelectionManager.getStructureSelectionManager(
2505 Desktop.instance).registerMappings(ds.getCodonFrames());
2508 if (errorMessage != null)
2513 if (Desktop.instance != null)
2515 Desktop.instance.stopLoading();
2522 * Try to reconstruct and display SplitFrame windows, where each contains
2523 * complementary dna and protein alignments. Done by pairing up AlignFrame
2524 * objects (created earlier) which have complementary viewport ids associated.
2526 protected void restoreSplitFrames()
2528 List<SplitFrame> gatherTo = new ArrayList<SplitFrame>();
2529 List<AlignFrame> addedToSplitFrames = new ArrayList<AlignFrame>();
2530 Map<String, AlignFrame> dna = new HashMap<String, AlignFrame>();
2533 * Identify the DNA alignments
2535 for (Entry<Viewport, AlignFrame> candidate : splitFrameCandidates
2538 AlignFrame af = candidate.getValue();
2539 if (af.getViewport().getAlignment().isNucleotide())
2541 dna.put(candidate.getKey().getId(), af);
2546 * Try to match up the protein complements
2548 for (Entry<Viewport, AlignFrame> candidate : splitFrameCandidates
2551 AlignFrame af = candidate.getValue();
2552 if (!af.getViewport().getAlignment().isNucleotide())
2554 String complementId = candidate.getKey().getComplementId();
2555 // only non-null complements should be in the Map
2556 if (complementId != null && dna.containsKey(complementId))
2558 final AlignFrame dnaFrame = dna.get(complementId);
2559 SplitFrame sf = createSplitFrame(dnaFrame, af);
2560 addedToSplitFrames.add(dnaFrame);
2561 addedToSplitFrames.add(af);
2562 dnaFrame.setMenusForViewport();
2563 af.setMenusForViewport();
2564 if (af.viewport.isGatherViewsHere())
2573 * Open any that we failed to pair up (which shouldn't happen!) as
2574 * standalone AlignFrame's.
2576 for (Entry<Viewport, AlignFrame> candidate : splitFrameCandidates
2579 AlignFrame af = candidate.getValue();
2580 if (!addedToSplitFrames.contains(af))
2582 Viewport view = candidate.getKey();
2583 Desktop.addInternalFrame(af, view.getTitle(), view.getWidth(),
2585 af.setMenusForViewport();
2586 System.err.println("Failed to restore view " + view.getTitle()
2587 + " to split frame");
2592 * Gather back into tabbed views as flagged.
2594 for (SplitFrame sf : gatherTo)
2596 Desktop.instance.gatherViews(sf);
2599 splitFrameCandidates.clear();
2603 * Construct and display one SplitFrame holding DNA and protein alignments.
2606 * @param proteinFrame
2609 protected SplitFrame createSplitFrame(AlignFrame dnaFrame,
2610 AlignFrame proteinFrame)
2612 SplitFrame splitFrame = new SplitFrame(dnaFrame, proteinFrame);
2613 String title = MessageManager.getString("label.linked_view_title");
2614 int width = (int) dnaFrame.getBounds().getWidth();
2615 int height = (int) (dnaFrame.getBounds().getHeight()
2616 + proteinFrame.getBounds().getHeight() + 50);
2619 * SplitFrame location is saved to both enclosed frames
2621 splitFrame.setLocation(dnaFrame.getX(), dnaFrame.getY());
2622 Desktop.addInternalFrame(splitFrame, title, width, height);
2625 * And compute cDNA consensus (couldn't do earlier with consensus as
2626 * mappings were not yet present)
2628 proteinFrame.viewport.alignmentChanged(proteinFrame.alignPanel);
2634 * check errorMessage for a valid error message and raise an error box in the
2635 * GUI or write the current errorMessage to stderr and then clear the error
2638 protected void reportErrors()
2640 reportErrors(false);
2643 protected void reportErrors(final boolean saving)
2645 if (errorMessage != null)
2647 final String finalErrorMessage = errorMessage;
2650 javax.swing.SwingUtilities.invokeLater(new Runnable()
2656 .showInternalMessageDialog(Desktop.desktop,
2657 finalErrorMessage, "Error "
2658 + (saving ? "saving" : "loading")
2660 JvOptionPane.WARNING_MESSAGE);
2666 System.err.println("Problem loading Jalview file: " + errorMessage);
2669 errorMessage = null;
2672 Map<String, String> alreadyLoadedPDB = new HashMap<String, String>();
2675 * when set, local views will be updated from view stored in JalviewXML
2676 * Currently (28th Sep 2008) things will go horribly wrong in vamsas document
2677 * sync if this is set to true.
2679 private final boolean updateLocalViews = false;
2682 * Returns the path to a temporary file holding the PDB file for the given PDB
2683 * id. The first time of asking, searches for a file of that name in the
2684 * Jalview project jar, and copies it to a new temporary file. Any repeat
2685 * requests just return the path to the file previously created.
2691 String loadPDBFile(jarInputStreamProvider jprovider, String pdbId,
2694 if (alreadyLoadedPDB.containsKey(pdbId))
2696 return alreadyLoadedPDB.get(pdbId).toString();
2699 String tempFile = copyJarEntry(jprovider, pdbId, "jalview_pdb",
2701 if (tempFile != null)
2703 alreadyLoadedPDB.put(pdbId, tempFile);
2709 * Copies the jar entry of given name to a new temporary file and returns the
2710 * path to the file, or null if the entry is not found.
2713 * @param jarEntryName
2715 * a prefix for the temporary file name, must be at least three
2718 * null or original file - so new file can be given the same suffix
2722 protected String copyJarEntry(jarInputStreamProvider jprovider,
2723 String jarEntryName, String prefix, String origFile)
2725 BufferedReader in = null;
2726 PrintWriter out = null;
2727 String suffix = ".tmp";
2728 if (origFile == null)
2730 origFile = jarEntryName;
2732 int sfpos = origFile.lastIndexOf(".");
2733 if (sfpos > -1 && sfpos < (origFile.length() - 3))
2735 suffix = "." + origFile.substring(sfpos + 1);
2739 JarInputStream jin = jprovider.getJarInputStream();
2741 * if (jprovider.startsWith("http://")) { jin = new JarInputStream(new
2742 * URL(jprovider).openStream()); } else { jin = new JarInputStream(new
2743 * FileInputStream(jprovider)); }
2746 JarEntry entry = null;
2749 entry = jin.getNextJarEntry();
2750 } while (entry != null && !entry.getName().equals(jarEntryName));
2753 in = new BufferedReader(new InputStreamReader(jin, UTF_8));
2754 File outFile = File.createTempFile(prefix, suffix);
2755 outFile.deleteOnExit();
2756 out = new PrintWriter(new FileOutputStream(outFile));
2759 while ((data = in.readLine()) != null)
2764 String t = outFile.getAbsolutePath();
2769 warn("Couldn't find entry in Jalview Jar for " + jarEntryName);
2771 } catch (Exception ex)
2773 ex.printStackTrace();
2781 } catch (IOException e)
2795 private class JvAnnotRow
2797 public JvAnnotRow(int i, AlignmentAnnotation jaa)
2804 * persisted version of annotation row from which to take vis properties
2806 public jalview.datamodel.AlignmentAnnotation template;
2809 * original position of the annotation row in the alignment
2815 * Load alignment frame from jalview XML DOM object
2820 * filename source string
2821 * @param loadTreesAndStructures
2822 * when false only create Viewport
2824 * data source provider
2825 * @return alignment frame created from view stored in DOM
2827 AlignFrame loadFromObject(JalviewModel object, String file,
2828 boolean loadTreesAndStructures, jarInputStreamProvider jprovider)
2830 SequenceSet vamsasSet = object.getVamsasModel().getSequenceSet(0);
2831 Sequence[] vamsasSeq = vamsasSet.getSequence();
2833 JalviewModelSequence jms = object.getJalviewModelSequence();
2835 Viewport view = (jms.getViewportCount() > 0) ? jms.getViewport(0)
2838 // ////////////////////////////////
2841 List<SequenceI> hiddenSeqs = null;
2843 List<SequenceI> tmpseqs = new ArrayList<SequenceI>();
2845 boolean multipleView = false;
2846 SequenceI referenceseqForView = null;
2847 JSeq[] jseqs = object.getJalviewModelSequence().getJSeq();
2848 int vi = 0; // counter in vamsasSeq array
2849 for (int i = 0; i < jseqs.length; i++)
2851 String seqId = jseqs[i].getId();
2853 SequenceI tmpSeq = seqRefIds.get(seqId);
2856 if (!incompleteSeqs.containsKey(seqId))
2858 // may not need this check, but keep it for at least 2.9,1 release
2859 if (tmpSeq.getStart() != jseqs[i].getStart()
2860 || tmpSeq.getEnd() != jseqs[i].getEnd())
2863 .println("Warning JAL-2154 regression: updating start/end for sequence "
2864 + tmpSeq.toString() + " to " + jseqs[i]);
2869 incompleteSeqs.remove(seqId);
2871 if (vamsasSeq.length > vi && vamsasSeq[vi].getId().equals(seqId))
2873 // most likely we are reading a dataset XML document so
2874 // update from vamsasSeq section of XML for this sequence
2875 tmpSeq.setName(vamsasSeq[vi].getName());
2876 tmpSeq.setDescription(vamsasSeq[vi].getDescription());
2877 tmpSeq.setSequence(vamsasSeq[vi].getSequence());
2882 // reading multiple views, so vamsasSeq set is a subset of JSeq
2883 multipleView = true;
2885 tmpSeq.setStart(jseqs[i].getStart());
2886 tmpSeq.setEnd(jseqs[i].getEnd());
2887 tmpseqs.add(tmpSeq);
2891 tmpSeq = new jalview.datamodel.Sequence(vamsasSeq[vi].getName(),
2892 vamsasSeq[vi].getSequence());
2893 tmpSeq.setDescription(vamsasSeq[vi].getDescription());
2894 tmpSeq.setStart(jseqs[i].getStart());
2895 tmpSeq.setEnd(jseqs[i].getEnd());
2896 tmpSeq.setVamsasId(uniqueSetSuffix + seqId);
2897 seqRefIds.put(vamsasSeq[vi].getId(), tmpSeq);
2898 tmpseqs.add(tmpSeq);
2902 if (jseqs[i].hasViewreference() && jseqs[i].getViewreference())
2904 referenceseqForView = tmpseqs.get(tmpseqs.size() - 1);
2907 if (jseqs[i].getHidden())
2909 if (hiddenSeqs == null)
2911 hiddenSeqs = new ArrayList<SequenceI>();
2914 hiddenSeqs.add(tmpSeq);
2919 // Create the alignment object from the sequence set
2920 // ///////////////////////////////
2921 SequenceI[] orderedSeqs = tmpseqs
2922 .toArray(new SequenceI[tmpseqs.size()]);
2924 AlignmentI al = null;
2925 // so we must create or recover the dataset alignment before going further
2926 // ///////////////////////////////
2927 if (vamsasSet.getDatasetId() == null || vamsasSet.getDatasetId() == "")
2929 // older jalview projects do not have a dataset - so creat alignment and
2931 al = new Alignment(orderedSeqs);
2932 al.setDataset(null);
2936 boolean isdsal = object.getJalviewModelSequence().getViewportCount() == 0;
2939 // we are importing a dataset record, so
2940 // recover reference to an alignment already materialsed as dataset
2941 al = getDatasetFor(vamsasSet.getDatasetId());
2945 // materialse the alignment
2946 al = new Alignment(orderedSeqs);
2950 addDatasetRef(vamsasSet.getDatasetId(), al);
2953 // finally, verify all data in vamsasSet is actually present in al
2954 // passing on flag indicating if it is actually a stored dataset
2955 recoverDatasetFor(vamsasSet, al, isdsal);
2958 if (referenceseqForView != null)
2960 al.setSeqrep(referenceseqForView);
2962 // / Add the alignment properties
2963 for (int i = 0; i < vamsasSet.getSequenceSetPropertiesCount(); i++)
2965 SequenceSetProperties ssp = vamsasSet.getSequenceSetProperties(i);
2966 al.setProperty(ssp.getKey(), ssp.getValue());
2969 // ///////////////////////////////
2971 Hashtable pdbloaded = new Hashtable(); // TODO nothing writes to this??
2974 // load sequence features, database references and any associated PDB
2975 // structures for the alignment
2977 // prior to 2.10, this part would only be executed the first time a
2978 // sequence was encountered, but not afterwards.
2979 // now, for 2.10 projects, this is also done if the xml doc includes
2980 // dataset sequences not actually present in any particular view.
2982 for (int i = 0; i < vamsasSeq.length; i++)
2984 if (jseqs[i].getFeaturesCount() > 0)
2986 Features[] features = jseqs[i].getFeatures();
2987 for (int f = 0; f < features.length; f++)
2989 jalview.datamodel.SequenceFeature sf = new jalview.datamodel.SequenceFeature(
2990 features[f].getType(), features[f].getDescription(),
2991 features[f].getStatus(), features[f].getBegin(),
2992 features[f].getEnd(), features[f].getFeatureGroup());
2994 sf.setScore(features[f].getScore());
2995 for (int od = 0; od < features[f].getOtherDataCount(); od++)
2997 OtherData keyValue = features[f].getOtherData(od);
2998 if (keyValue.getKey().startsWith("LINK"))
3000 sf.addLink(keyValue.getValue());
3004 sf.setValue(keyValue.getKey(), keyValue.getValue());
3008 // adds feature to datasequence's feature set (since Jalview 2.10)
3009 al.getSequenceAt(i).addSequenceFeature(sf);
3012 if (vamsasSeq[i].getDBRefCount() > 0)
3014 // adds dbrefs to datasequence's set (since Jalview 2.10)
3016 al.getSequenceAt(i).getDatasetSequence() == null ? al.getSequenceAt(i)
3017 : al.getSequenceAt(i).getDatasetSequence(),
3020 if (jseqs[i].getPdbidsCount() > 0)
3022 Pdbids[] ids = jseqs[i].getPdbids();
3023 for (int p = 0; p < ids.length; p++)
3025 jalview.datamodel.PDBEntry entry = new jalview.datamodel.PDBEntry();
3026 entry.setId(ids[p].getId());
3027 if (ids[p].getType() != null)
3029 if (PDBEntry.Type.getType(ids[p].getType()) != null)
3031 entry.setType(PDBEntry.Type.getType(ids[p].getType()));
3035 entry.setType(PDBEntry.Type.FILE);
3038 // jprovider is null when executing 'New View'
3039 if (ids[p].getFile() != null && jprovider != null)
3041 if (!pdbloaded.containsKey(ids[p].getFile()))
3043 entry.setFile(loadPDBFile(jprovider, ids[p].getId(),
3048 entry.setFile(pdbloaded.get(ids[p].getId()).toString());
3051 if (ids[p].getPdbentryItem() != null)
3053 for (PdbentryItem item : ids[p].getPdbentryItem())
3055 for (Property pr : item.getProperty())
3057 entry.setProperty(pr.getName(), pr.getValue());
3061 StructureSelectionManager.getStructureSelectionManager(
3062 Desktop.instance).registerPDBEntry(entry);
3063 // adds PDBEntry to datasequence's set (since Jalview 2.10)
3064 if (al.getSequenceAt(i).getDatasetSequence() != null)
3066 al.getSequenceAt(i).getDatasetSequence().addPDBId(entry);
3070 al.getSequenceAt(i).addPDBId(entry);
3075 } // end !multipleview
3077 // ///////////////////////////////
3078 // LOAD SEQUENCE MAPPINGS
3080 if (vamsasSet.getAlcodonFrameCount() > 0)
3082 // TODO Potentially this should only be done once for all views of an
3084 AlcodonFrame[] alc = vamsasSet.getAlcodonFrame();
3085 for (int i = 0; i < alc.length; i++)
3087 AlignedCodonFrame cf = new AlignedCodonFrame();
3088 if (alc[i].getAlcodMapCount() > 0)
3090 AlcodMap[] maps = alc[i].getAlcodMap();
3091 for (int m = 0; m < maps.length; m++)
3093 SequenceI dnaseq = seqRefIds.get(maps[m].getDnasq());
3095 jalview.datamodel.Mapping mapping = null;
3096 // attach to dna sequence reference.
3097 if (maps[m].getMapping() != null)
3099 mapping = addMapping(maps[m].getMapping());
3100 if (dnaseq != null && mapping.getTo() != null)
3102 cf.addMap(dnaseq, mapping.getTo(), mapping.getMap());
3107 frefedSequence.add(newAlcodMapRef(maps[m].getDnasq(), cf,
3112 al.addCodonFrame(cf);
3117 // ////////////////////////////////
3119 List<JvAnnotRow> autoAlan = new ArrayList<JvAnnotRow>();
3122 * store any annotations which forward reference a group's ID
3124 Map<String, List<AlignmentAnnotation>> groupAnnotRefs = new Hashtable<String, List<AlignmentAnnotation>>();
3126 if (vamsasSet.getAnnotationCount() > 0)
3128 Annotation[] an = vamsasSet.getAnnotation();
3130 for (int i = 0; i < an.length; i++)
3132 Annotation annotation = an[i];
3135 * test if annotation is automatically calculated for this view only
3137 boolean autoForView = false;
3138 if (annotation.getLabel().equals("Quality")
3139 || annotation.getLabel().equals("Conservation")
3140 || annotation.getLabel().equals("Consensus"))
3142 // Kludge for pre 2.5 projects which lacked the autocalculated flag
3144 if (!annotation.hasAutoCalculated())
3146 annotation.setAutoCalculated(true);
3150 || (annotation.hasAutoCalculated() && annotation
3151 .isAutoCalculated()))
3153 // remove ID - we don't recover annotation from other views for
3154 // view-specific annotation
3155 annotation.setId(null);
3158 // set visiblity for other annotation in this view
3159 String annotationId = annotation.getId();
3160 if (annotationId != null && annotationIds.containsKey(annotationId))
3162 AlignmentAnnotation jda = annotationIds.get(annotationId);
3163 // in principle Visible should always be true for annotation displayed
3164 // in multiple views
3165 if (annotation.hasVisible())
3167 jda.visible = annotation.getVisible();
3170 al.addAnnotation(jda);
3174 // Construct new annotation from model.
3175 AnnotationElement[] ae = annotation.getAnnotationElement();
3176 jalview.datamodel.Annotation[] anot = null;
3177 java.awt.Color firstColour = null;
3179 if (!annotation.getScoreOnly())
3181 anot = new jalview.datamodel.Annotation[al.getWidth()];
3182 for (int aa = 0; aa < ae.length && aa < anot.length; aa++)
3184 anpos = ae[aa].getPosition();
3186 if (anpos >= anot.length)
3191 anot[anpos] = new jalview.datamodel.Annotation(
3193 ae[aa].getDisplayCharacter(), ae[aa].getDescription(),
3194 (ae[aa].getSecondaryStructure() == null || ae[aa]
3195 .getSecondaryStructure().length() == 0) ? ' '
3196 : ae[aa].getSecondaryStructure().charAt(0),
3200 // JBPNote: Consider verifying dataflow for IO of secondary
3201 // structure annotation read from Stockholm files
3202 // this was added to try to ensure that
3203 // if (anot[ae[aa].getPosition()].secondaryStructure>' ')
3205 // anot[ae[aa].getPosition()].displayCharacter = "";
3207 anot[anpos].colour = new java.awt.Color(ae[aa].getColour());
3208 if (firstColour == null)
3210 firstColour = anot[anpos].colour;
3214 jalview.datamodel.AlignmentAnnotation jaa = null;
3216 if (annotation.getGraph())
3218 float llim = 0, hlim = 0;
3219 // if (autoForView || an[i].isAutoCalculated()) {
3222 jaa = new jalview.datamodel.AlignmentAnnotation(
3223 annotation.getLabel(), annotation.getDescription(), anot,
3224 llim, hlim, annotation.getGraphType());
3226 jaa.graphGroup = annotation.getGraphGroup();
3227 jaa._linecolour = firstColour;
3228 if (annotation.getThresholdLine() != null)
3230 jaa.setThreshold(new jalview.datamodel.GraphLine(annotation
3231 .getThresholdLine().getValue(), annotation
3232 .getThresholdLine().getLabel(), new java.awt.Color(
3233 annotation.getThresholdLine().getColour())));
3236 if (autoForView || annotation.isAutoCalculated())
3238 // Hardwire the symbol display line to ensure that labels for
3239 // histograms are displayed
3245 jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(),
3246 an[i].getDescription(), anot);
3247 jaa._linecolour = firstColour;
3249 // register new annotation
3250 if (an[i].getId() != null)
3252 annotationIds.put(an[i].getId(), jaa);
3253 jaa.annotationId = an[i].getId();
3255 // recover sequence association
3256 String sequenceRef = an[i].getSequenceRef();
3257 if (sequenceRef != null)
3259 // from 2.9 sequenceRef is to sequence id (JAL-1781)
3260 SequenceI sequence = seqRefIds.get(sequenceRef);
3261 if (sequence == null)
3263 // in pre-2.9 projects sequence ref is to sequence name
3264 sequence = al.findName(sequenceRef);
3266 if (sequence != null)
3268 jaa.createSequenceMapping(sequence, 1, true);
3269 sequence.addAlignmentAnnotation(jaa);
3272 // and make a note of any group association
3273 if (an[i].getGroupRef() != null && an[i].getGroupRef().length() > 0)
3275 List<jalview.datamodel.AlignmentAnnotation> aal = groupAnnotRefs
3276 .get(an[i].getGroupRef());
3279 aal = new ArrayList<jalview.datamodel.AlignmentAnnotation>();
3280 groupAnnotRefs.put(an[i].getGroupRef(), aal);
3285 if (an[i].hasScore())
3287 jaa.setScore(an[i].getScore());
3289 if (an[i].hasVisible())
3291 jaa.visible = an[i].getVisible();
3294 if (an[i].hasCentreColLabels())
3296 jaa.centreColLabels = an[i].getCentreColLabels();
3299 if (an[i].hasScaleColLabels())
3301 jaa.scaleColLabel = an[i].getScaleColLabels();
3303 if (an[i].hasAutoCalculated() && an[i].isAutoCalculated())
3305 // newer files have an 'autoCalculated' flag and store calculation
3306 // state in viewport properties
3307 jaa.autoCalculated = true; // means annotation will be marked for
3308 // update at end of load.
3310 if (an[i].hasGraphHeight())
3312 jaa.graphHeight = an[i].getGraphHeight();
3314 if (an[i].hasBelowAlignment())
3316 jaa.belowAlignment = an[i].isBelowAlignment();
3318 jaa.setCalcId(an[i].getCalcId());
3319 if (an[i].getPropertyCount() > 0)
3321 for (jalview.schemabinding.version2.Property prop : an[i]
3324 jaa.setProperty(prop.getName(), prop.getValue());
3327 if (jaa.autoCalculated)
3329 autoAlan.add(new JvAnnotRow(i, jaa));
3332 // if (!autoForView)
3334 // add autocalculated group annotation and any user created annotation
3336 al.addAnnotation(jaa);
3340 // ///////////////////////
3342 // Create alignment markup and styles for this view
3343 if (jms.getJGroupCount() > 0)
3345 JGroup[] groups = jms.getJGroup();
3346 boolean addAnnotSchemeGroup = false;
3347 for (int i = 0; i < groups.length; i++)
3349 JGroup jGroup = groups[i];
3350 ColourSchemeI cs = null;
3351 if (jGroup.getColour() != null)
3353 if (jGroup.getColour().startsWith("ucs"))
3355 cs = getUserColourScheme(jms, jGroup.getColour());
3357 else if (jGroup.getColour().equals("AnnotationColourGradient")
3358 && jGroup.getAnnotationColours() != null)
3360 addAnnotSchemeGroup = true;
3364 cs = ColourSchemeProperty.getColourScheme(al, jGroup.getColour());
3367 int pidThreshold = jGroup.getPidThreshold();
3369 Vector<SequenceI> seqs = new Vector<SequenceI>();
3371 for (int s = 0; s < jGroup.getSeqCount(); s++)
3373 String seqId = jGroup.getSeq(s) + "";
3374 SequenceI ts = seqRefIds.get(seqId);
3378 seqs.addElement(ts);
3382 if (seqs.size() < 1)
3387 SequenceGroup sg = new SequenceGroup(seqs, jGroup.getName(), cs,
3388 jGroup.getDisplayBoxes(), jGroup.getDisplayText(),
3389 jGroup.getColourText(), jGroup.getStart(), jGroup.getEnd());
3390 sg.getGroupColourScheme().setThreshold(pidThreshold, true);
3391 sg.getGroupColourScheme().setConservationInc(jGroup.getConsThreshold());
3392 sg.setOutlineColour(new java.awt.Color(jGroup.getOutlineColour()));
3394 sg.textColour = new java.awt.Color(jGroup.getTextCol1());
3395 sg.textColour2 = new java.awt.Color(jGroup.getTextCol2());
3396 sg.setShowNonconserved(jGroup.hasShowUnconserved() ? jGroup
3397 .isShowUnconserved() : false);
3398 sg.thresholdTextColour = jGroup.getTextColThreshold();
3399 if (jGroup.hasShowConsensusHistogram())
3401 sg.setShowConsensusHistogram(jGroup.isShowConsensusHistogram());
3404 if (jGroup.hasShowSequenceLogo())
3406 sg.setshowSequenceLogo(jGroup.isShowSequenceLogo());
3408 if (jGroup.hasNormaliseSequenceLogo())
3410 sg.setNormaliseSequenceLogo(jGroup.isNormaliseSequenceLogo());
3412 if (jGroup.hasIgnoreGapsinConsensus())
3414 sg.setIgnoreGapsConsensus(jGroup.getIgnoreGapsinConsensus());
3416 if (jGroup.getConsThreshold() != 0)
3418 Conservation c = new Conservation("All", sg.getSequences(null),
3419 0, sg.getWidth() - 1);
3421 c.verdict(false, 25);
3422 sg.cs.setConservation(c);
3425 if (jGroup.getId() != null && groupAnnotRefs.size() > 0)
3427 // re-instate unique group/annotation row reference
3428 List<AlignmentAnnotation> jaal = groupAnnotRefs.get(jGroup
3432 for (AlignmentAnnotation jaa : jaal)
3435 if (jaa.autoCalculated)
3437 // match up and try to set group autocalc alignment row for this
3439 if (jaa.label.startsWith("Consensus for "))
3441 sg.setConsensus(jaa);
3443 // match up and try to set group autocalc alignment row for this
3445 if (jaa.label.startsWith("Conservation for "))
3447 sg.setConservationRow(jaa);
3454 if (addAnnotSchemeGroup)
3456 // reconstruct the annotation colourscheme
3457 sg.setColourScheme(constructAnnotationColour(
3458 jGroup.getAnnotationColours(), null, al, jms, false));
3464 // only dataset in this model, so just return.
3467 // ///////////////////////////////
3470 // If we just load in the same jar file again, the sequenceSetId
3471 // will be the same, and we end up with multiple references
3472 // to the same sequenceSet. We must modify this id on load
3473 // so that each load of the file gives a unique id
3474 String uniqueSeqSetId = view.getSequenceSetId() + uniqueSetSuffix;
3475 String viewId = (view.getId() == null ? null : view.getId()
3477 AlignFrame af = null;
3478 AlignViewport av = null;
3479 // now check to see if we really need to create a new viewport.
3480 if (multipleView && viewportsAdded.size() == 0)
3482 // We recovered an alignment for which a viewport already exists.
3483 // TODO: fix up any settings necessary for overlaying stored state onto
3484 // state recovered from another document. (may not be necessary).
3485 // we may need a binding from a viewport in memory to one recovered from
3487 // and then recover its containing af to allow the settings to be applied.
3488 // TODO: fix for vamsas demo
3490 .println("About to recover a viewport for existing alignment: Sequence set ID is "
3492 Object seqsetobj = retrieveExistingObj(uniqueSeqSetId);
3493 if (seqsetobj != null)
3495 if (seqsetobj instanceof String)
3497 uniqueSeqSetId = (String) seqsetobj;
3499 .println("Recovered extant sequence set ID mapping for ID : New Sequence set ID is "
3505 .println("Warning : Collision between sequence set ID string and existing jalview object mapping.");
3511 * indicate that annotation colours are applied across all groups (pre
3512 * Jalview 2.8.1 behaviour)
3514 boolean doGroupAnnColour = Jalview2XML.isVersionStringLaterThan(
3515 "2.8.1", object.getVersion());
3517 AlignmentPanel ap = null;
3518 boolean isnewview = true;
3521 // Check to see if this alignment already has a view id == viewId
3522 jalview.gui.AlignmentPanel views[] = Desktop
3523 .getAlignmentPanels(uniqueSeqSetId);
3524 if (views != null && views.length > 0)
3526 for (int v = 0; v < views.length; v++)
3528 if (views[v].av.getViewId().equalsIgnoreCase(viewId))
3530 // recover the existing alignpanel, alignframe, viewport
3531 af = views[v].alignFrame;
3534 // TODO: could even skip resetting view settings if we don't want to
3535 // change the local settings from other jalview processes
3544 af = loadViewport(file, jseqs, hiddenSeqs, al, jms, view,
3545 uniqueSeqSetId, viewId, autoAlan);
3551 * Load any trees, PDB structures and viewers
3553 * Not done if flag is false (when this method is used for New View)
3555 if (loadTreesAndStructures)
3557 loadTrees(jms, view, af, av, ap);
3558 loadPDBStructures(jprovider, jseqs, af, ap);
3559 loadRnaViewers(jprovider, jseqs, ap);
3561 // and finally return.
3566 * Instantiate and link any saved RNA (Varna) viewers. The state of the Varna
3567 * panel is restored from separate jar entries, two (gapped and trimmed) per
3568 * sequence and secondary structure.
3570 * Currently each viewer shows just one sequence and structure (gapped and
3571 * trimmed), however this method is designed to support multiple sequences or
3572 * structures in viewers if wanted in future.
3578 private void loadRnaViewers(jarInputStreamProvider jprovider,
3579 JSeq[] jseqs, AlignmentPanel ap)
3582 * scan the sequences for references to viewers; create each one the first
3583 * time it is referenced, add Rna models to existing viewers
3585 for (JSeq jseq : jseqs)
3587 for (int i = 0; i < jseq.getRnaViewerCount(); i++)
3589 RnaViewer viewer = jseq.getRnaViewer(i);
3590 AppVarna appVarna = findOrCreateVarnaViewer(viewer,
3591 uniqueSetSuffix, ap);
3593 for (int j = 0; j < viewer.getSecondaryStructureCount(); j++)
3595 SecondaryStructure ss = viewer.getSecondaryStructure(j);
3596 SequenceI seq = seqRefIds.get(jseq.getId());
3597 AlignmentAnnotation ann = this.annotationIds.get(ss
3598 .getAnnotationId());
3601 * add the structure to the Varna display (with session state copied
3602 * from the jar to a temporary file)
3604 boolean gapped = ss.isGapped();
3605 String rnaTitle = ss.getTitle();
3606 String sessionState = ss.getViewerState();
3607 String tempStateFile = copyJarEntry(jprovider, sessionState,
3609 RnaModel rna = new RnaModel(rnaTitle, ann, seq, null, gapped);
3610 appVarna.addModelSession(rna, rnaTitle, tempStateFile);
3612 appVarna.setInitialSelection(viewer.getSelectedRna());
3618 * Locate and return an already instantiated matching AppVarna, or create one
3622 * @param viewIdSuffix
3626 protected AppVarna findOrCreateVarnaViewer(RnaViewer viewer,
3627 String viewIdSuffix, AlignmentPanel ap)
3630 * on each load a suffix is appended to the saved viewId, to avoid conflicts
3631 * if load is repeated
3633 String postLoadId = viewer.getViewId() + viewIdSuffix;
3634 for (JInternalFrame frame : getAllFrames())
3636 if (frame instanceof AppVarna)
3638 AppVarna varna = (AppVarna) frame;
3639 if (postLoadId.equals(varna.getViewId()))
3641 // this viewer is already instantiated
3642 // could in future here add ap as another 'parent' of the
3643 // AppVarna window; currently just 1-to-many
3650 * viewer not found - make it
3652 RnaViewerModel model = new RnaViewerModel(postLoadId,
3653 viewer.getTitle(), viewer.getXpos(), viewer.getYpos(),
3654 viewer.getWidth(), viewer.getHeight(),
3655 viewer.getDividerLocation());
3656 AppVarna varna = new AppVarna(model, ap);
3662 * Load any saved trees
3670 protected void loadTrees(JalviewModelSequence jms, Viewport view,
3671 AlignFrame af, AlignViewport av, AlignmentPanel ap)
3673 // TODO result of automated refactoring - are all these parameters needed?
3676 for (int t = 0; t < jms.getTreeCount(); t++)
3679 Tree tree = jms.getTree(t);
3681 TreePanel tp = (TreePanel) retrieveExistingObj(tree.getId());
3684 tp = af.showNewickTree(
3685 new jalview.io.NewickFile(tree.getNewick()),
3686 tree.getTitle(), tree.getWidth(), tree.getHeight(),
3687 tree.getXpos(), tree.getYpos());
3688 if (tree.getId() != null)
3690 // perhaps bind the tree id to something ?
3695 // update local tree attributes ?
3696 // TODO: should check if tp has been manipulated by user - if so its
3697 // settings shouldn't be modified
3698 tp.setTitle(tree.getTitle());
3699 tp.setBounds(new Rectangle(tree.getXpos(), tree.getYpos(), tree
3700 .getWidth(), tree.getHeight()));
3701 tp.av = av; // af.viewport; // TODO: verify 'associate with all
3704 tp.treeCanvas.av = av; // af.viewport;
3705 tp.treeCanvas.ap = ap; // af.alignPanel;
3710 warn("There was a problem recovering stored Newick tree: \n"
3711 + tree.getNewick());
3715 tp.fitToWindow.setState(tree.getFitToWindow());
3716 tp.fitToWindow_actionPerformed(null);
3718 if (tree.getFontName() != null)
3720 tp.setTreeFont(new java.awt.Font(tree.getFontName(), tree
3721 .getFontStyle(), tree.getFontSize()));
3725 tp.setTreeFont(new java.awt.Font(view.getFontName(), view
3726 .getFontStyle(), tree.getFontSize()));
3729 tp.showPlaceholders(tree.getMarkUnlinked());
3730 tp.showBootstrap(tree.getShowBootstrap());
3731 tp.showDistances(tree.getShowDistances());
3733 tp.treeCanvas.threshold = tree.getThreshold();
3735 if (tree.getCurrentTree())
3737 af.viewport.setCurrentTree(tp.getTree());
3741 } catch (Exception ex)
3743 ex.printStackTrace();
3748 * Load and link any saved structure viewers.
3755 protected void loadPDBStructures(jarInputStreamProvider jprovider,
3756 JSeq[] jseqs, AlignFrame af, AlignmentPanel ap)
3759 * Run through all PDB ids on the alignment, and collect mappings between
3760 * distinct view ids and all sequences referring to that view.
3762 Map<String, StructureViewerModel> structureViewers = new LinkedHashMap<String, StructureViewerModel>();
3764 for (int i = 0; i < jseqs.length; i++)
3766 if (jseqs[i].getPdbidsCount() > 0)
3768 Pdbids[] ids = jseqs[i].getPdbids();
3769 for (int p = 0; p < ids.length; p++)
3771 final int structureStateCount = ids[p].getStructureStateCount();
3772 for (int s = 0; s < structureStateCount; s++)
3774 // check to see if we haven't already created this structure view
3775 final StructureState structureState = ids[p]
3776 .getStructureState(s);
3777 String sviewid = (structureState.getViewId() == null) ? null
3778 : structureState.getViewId() + uniqueSetSuffix;
3779 jalview.datamodel.PDBEntry jpdb = new jalview.datamodel.PDBEntry();
3780 // Originally : ids[p].getFile()
3781 // : TODO: verify external PDB file recovery still works in normal
3782 // jalview project load
3783 jpdb.setFile(loadPDBFile(jprovider, ids[p].getId(),
3785 jpdb.setId(ids[p].getId());
3787 int x = structureState.getXpos();
3788 int y = structureState.getYpos();
3789 int width = structureState.getWidth();
3790 int height = structureState.getHeight();
3792 // Probably don't need to do this anymore...
3793 // Desktop.desktop.getComponentAt(x, y);
3794 // TODO: NOW: check that this recovers the PDB file correctly.
3795 String pdbFile = loadPDBFile(jprovider, ids[p].getId(),
3797 jalview.datamodel.SequenceI seq = seqRefIds.get(jseqs[i]
3799 if (sviewid == null)
3801 sviewid = "_jalview_pre2_4_" + x + "," + y + "," + width
3804 if (!structureViewers.containsKey(sviewid))
3806 structureViewers.put(sviewid,
3807 new StructureViewerModel(x, y, width, height, false,
3808 false, true, structureState.getViewId(),
3809 structureState.getType()));
3810 // Legacy pre-2.7 conversion JAL-823 :
3811 // do not assume any view has to be linked for colour by
3815 // assemble String[] { pdb files }, String[] { id for each
3816 // file }, orig_fileloc, SequenceI[][] {{ seqs_file 1 }, {
3817 // seqs_file 2}, boolean[] {
3818 // linkAlignPanel,superposeWithAlignpanel}} from hash
3819 StructureViewerModel jmoldat = structureViewers.get(sviewid);
3820 jmoldat.setAlignWithPanel(jmoldat.isAlignWithPanel()
3821 | (structureState.hasAlignwithAlignPanel() ? structureState
3822 .getAlignwithAlignPanel() : false));
3825 * Default colour by linked panel to false if not specified (e.g.
3826 * for pre-2.7 projects)
3828 boolean colourWithAlignPanel = jmoldat.isColourWithAlignPanel();
3829 colourWithAlignPanel |= (structureState
3830 .hasColourwithAlignPanel() ? structureState
3831 .getColourwithAlignPanel() : false);
3832 jmoldat.setColourWithAlignPanel(colourWithAlignPanel);
3835 * Default colour by viewer to true if not specified (e.g. for
3838 boolean colourByViewer = jmoldat.isColourByViewer();
3839 colourByViewer &= structureState.hasColourByJmol() ? structureState
3840 .getColourByJmol() : true;
3841 jmoldat.setColourByViewer(colourByViewer);
3843 if (jmoldat.getStateData().length() < structureState
3844 .getContent().length())
3847 jmoldat.setStateData(structureState.getContent());
3850 if (ids[p].getFile() != null)
3852 File mapkey = new File(ids[p].getFile());
3853 StructureData seqstrmaps = jmoldat.getFileData().get(mapkey);
3854 if (seqstrmaps == null)
3856 jmoldat.getFileData().put(
3858 seqstrmaps = jmoldat.new StructureData(pdbFile,
3861 if (!seqstrmaps.getSeqList().contains(seq))
3863 seqstrmaps.getSeqList().add(seq);
3869 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");
3876 // Instantiate the associated structure views
3877 for (Entry<String, StructureViewerModel> entry : structureViewers
3882 createOrLinkStructureViewer(entry, af, ap, jprovider);
3883 } catch (Exception e)
3885 System.err.println("Error loading structure viewer: "
3887 // failed - try the next one
3899 protected void createOrLinkStructureViewer(
3900 Entry<String, StructureViewerModel> viewerData, AlignFrame af,
3901 AlignmentPanel ap, jarInputStreamProvider jprovider)
3903 final StructureViewerModel stateData = viewerData.getValue();
3906 * Search for any viewer windows already open from other alignment views
3907 * that exactly match the stored structure state
3909 StructureViewerBase comp = findMatchingViewer(viewerData);
3913 linkStructureViewer(ap, comp, stateData);
3918 * From 2.9: stateData.type contains JMOL or CHIMERA, data is in jar entry
3919 * "viewer_"+stateData.viewId
3921 if (ViewerType.CHIMERA.toString().equals(stateData.getType()))
3923 createChimeraViewer(viewerData, af, jprovider);
3928 * else Jmol (if pre-2.9, stateData contains JMOL state string)
3930 createJmolViewer(viewerData, af, jprovider);
3935 * Create a new Chimera viewer.
3941 protected void createChimeraViewer(
3942 Entry<String, StructureViewerModel> viewerData, AlignFrame af,
3943 jarInputStreamProvider jprovider)
3945 StructureViewerModel data = viewerData.getValue();
3946 String chimeraSessionFile = data.getStateData();
3949 * Copy Chimera session from jar entry "viewer_"+viewId to a temporary file
3951 * NB this is the 'saved' viewId as in the project file XML, _not_ the
3952 * 'uniquified' sviewid used to reconstruct the viewer here
3954 String viewerJarEntryName = getViewerJarEntryName(data.getViewId());
3955 chimeraSessionFile = copyJarEntry(jprovider, viewerJarEntryName,
3958 Set<Entry<File, StructureData>> fileData = data.getFileData()
3960 List<PDBEntry> pdbs = new ArrayList<PDBEntry>();
3961 List<SequenceI[]> allseqs = new ArrayList<SequenceI[]>();
3962 for (Entry<File, StructureData> pdb : fileData)
3964 String filePath = pdb.getValue().getFilePath();
3965 String pdbId = pdb.getValue().getPdbId();
3966 // pdbs.add(new PDBEntry(filePath, pdbId));
3967 pdbs.add(new PDBEntry(pdbId, null, PDBEntry.Type.PDB, filePath));
3968 final List<SequenceI> seqList = pdb.getValue().getSeqList();
3969 SequenceI[] seqs = seqList.toArray(new SequenceI[seqList.size()]);
3973 boolean colourByChimera = data.isColourByViewer();
3974 boolean colourBySequence = data.isColourWithAlignPanel();
3976 // TODO use StructureViewer as a factory here, see JAL-1761
3977 final PDBEntry[] pdbArray = pdbs.toArray(new PDBEntry[pdbs.size()]);
3978 final SequenceI[][] seqsArray = allseqs.toArray(new SequenceI[allseqs
3980 String newViewId = viewerData.getKey();
3982 ChimeraViewFrame cvf = new ChimeraViewFrame(chimeraSessionFile,
3983 af.alignPanel, pdbArray, seqsArray, colourByChimera,
3984 colourBySequence, newViewId);
3985 cvf.setSize(data.getWidth(), data.getHeight());
3986 cvf.setLocation(data.getX(), data.getY());
3990 * Create a new Jmol window. First parse the Jmol state to translate filenames
3991 * loaded into the view, and record the order in which files are shown in the
3992 * Jmol view, so we can add the sequence mappings in same order.
3998 protected void createJmolViewer(
3999 final Entry<String, StructureViewerModel> viewerData,
4000 AlignFrame af, jarInputStreamProvider jprovider)
4002 final StructureViewerModel svattrib = viewerData.getValue();
4003 String state = svattrib.getStateData();
4006 * Pre-2.9: state element value is the Jmol state string
4008 * 2.9+: @type is "JMOL", state data is in a Jar file member named "viewer_"
4011 if (ViewerType.JMOL.toString().equals(svattrib.getType()))
4013 state = readJarEntry(jprovider,
4014 getViewerJarEntryName(svattrib.getViewId()));
4017 List<String> pdbfilenames = new ArrayList<String>();
4018 List<SequenceI[]> seqmaps = new ArrayList<SequenceI[]>();
4019 List<String> pdbids = new ArrayList<String>();
4020 StringBuilder newFileLoc = new StringBuilder(64);
4021 int cp = 0, ncp, ecp;
4022 Map<File, StructureData> oldFiles = svattrib.getFileData();
4023 while ((ncp = state.indexOf("load ", cp)) > -1)
4027 // look for next filename in load statement
4028 newFileLoc.append(state.substring(cp,
4029 ncp = (state.indexOf("\"", ncp + 1) + 1)));
4030 String oldfilenam = state.substring(ncp,
4031 ecp = state.indexOf("\"", ncp));
4032 // recover the new mapping data for this old filename
4033 // have to normalize filename - since Jmol and jalview do
4035 // translation differently.
4036 StructureData filedat = oldFiles.get(new File(oldfilenam));
4037 if (filedat == null)
4039 String reformatedOldFilename = oldfilenam.replaceAll("/", "\\\\");
4040 filedat = oldFiles.get(new File(reformatedOldFilename));
4042 newFileLoc.append(Platform.escapeString(filedat.getFilePath()));
4043 pdbfilenames.add(filedat.getFilePath());
4044 pdbids.add(filedat.getPdbId());
4045 seqmaps.add(filedat.getSeqList().toArray(new SequenceI[0]));
4046 newFileLoc.append("\"");
4047 cp = ecp + 1; // advance beyond last \" and set cursor so we can
4048 // look for next file statement.
4049 } while ((ncp = state.indexOf("/*file*/", cp)) > -1);
4053 // just append rest of state
4054 newFileLoc.append(state.substring(cp));
4058 System.err.print("Ignoring incomplete Jmol state for PDB ids: ");
4059 newFileLoc = new StringBuilder(state);
4060 newFileLoc.append("; load append ");
4061 for (File id : oldFiles.keySet())
4063 // add this and any other pdb files that should be present in
4065 StructureData filedat = oldFiles.get(id);
4066 newFileLoc.append(filedat.getFilePath());
4067 pdbfilenames.add(filedat.getFilePath());
4068 pdbids.add(filedat.getPdbId());
4069 seqmaps.add(filedat.getSeqList().toArray(new SequenceI[0]));
4070 newFileLoc.append(" \"");
4071 newFileLoc.append(filedat.getFilePath());
4072 newFileLoc.append("\"");
4075 newFileLoc.append(";");
4078 if (newFileLoc.length() == 0)
4082 int histbug = newFileLoc.indexOf("history = ");
4086 * change "history = [true|false];" to "history = [1|0];"
4089 int diff = histbug == -1 ? -1 : newFileLoc.indexOf(";", histbug);
4090 String val = (diff == -1) ? null : newFileLoc
4091 .substring(histbug, diff);
4092 if (val != null && val.length() >= 4)
4094 if (val.contains("e")) // eh? what can it be?
4096 if (val.trim().equals("true"))
4104 newFileLoc.replace(histbug, diff, val);
4109 final String[] pdbf = pdbfilenames.toArray(new String[pdbfilenames
4111 final String[] id = pdbids.toArray(new String[pdbids.size()]);
4112 final SequenceI[][] sq = seqmaps
4113 .toArray(new SequenceI[seqmaps.size()][]);
4114 final String fileloc = newFileLoc.toString();
4115 final String sviewid = viewerData.getKey();
4116 final AlignFrame alf = af;
4117 final Rectangle rect = new Rectangle(svattrib.getX(), svattrib.getY(),
4118 svattrib.getWidth(), svattrib.getHeight());
4121 javax.swing.SwingUtilities.invokeAndWait(new Runnable()
4126 JalviewStructureDisplayI sview = null;
4129 sview = new StructureViewer(alf.alignPanel
4130 .getStructureSelectionManager()).createView(
4131 StructureViewer.ViewerType.JMOL, pdbf, id, sq,
4132 alf.alignPanel, svattrib, fileloc, rect, sviewid);
4133 addNewStructureViewer(sview);
4134 } catch (OutOfMemoryError ex)
4136 new OOMWarning("restoring structure view for PDB id " + id,
4137 (OutOfMemoryError) ex.getCause());
4138 if (sview != null && sview.isVisible())
4140 sview.closeViewer(false);
4141 sview.setVisible(false);
4147 } catch (InvocationTargetException ex)
4149 warn("Unexpected error when opening Jmol view.", ex);
4151 } catch (InterruptedException e)
4153 // e.printStackTrace();
4159 * Generates a name for the entry in the project jar file to hold state
4160 * information for a structure viewer
4165 protected String getViewerJarEntryName(String viewId)
4167 return VIEWER_PREFIX + viewId;
4171 * Returns any open frame that matches given structure viewer data. The match
4172 * is based on the unique viewId, or (for older project versions) the frame's
4178 protected StructureViewerBase findMatchingViewer(
4179 Entry<String, StructureViewerModel> viewerData)
4181 final String sviewid = viewerData.getKey();
4182 final StructureViewerModel svattrib = viewerData.getValue();
4183 StructureViewerBase comp = null;
4184 JInternalFrame[] frames = getAllFrames();
4185 for (JInternalFrame frame : frames)
4187 if (frame instanceof StructureViewerBase)
4190 * Post jalview 2.4 schema includes structure view id
4193 && ((StructureViewerBase) frame).getViewId()
4196 comp = (StructureViewerBase) frame;
4197 break; // break added in 2.9
4200 * Otherwise test for matching position and size of viewer frame
4202 else if (frame.getX() == svattrib.getX()
4203 && frame.getY() == svattrib.getY()
4204 && frame.getHeight() == svattrib.getHeight()
4205 && frame.getWidth() == svattrib.getWidth())
4207 comp = (StructureViewerBase) frame;
4208 // no break in faint hope of an exact match on viewId
4216 * Link an AlignmentPanel to an existing structure viewer.
4221 * @param useinViewerSuperpos
4222 * @param usetoColourbyseq
4223 * @param viewerColouring
4225 protected void linkStructureViewer(AlignmentPanel ap,
4226 StructureViewerBase viewer, StructureViewerModel stateData)
4228 // NOTE: if the jalview project is part of a shared session then
4229 // view synchronization should/could be done here.
4231 final boolean useinViewerSuperpos = stateData.isAlignWithPanel();
4232 final boolean usetoColourbyseq = stateData.isColourWithAlignPanel();
4233 final boolean viewerColouring = stateData.isColourByViewer();
4234 Map<File, StructureData> oldFiles = stateData.getFileData();
4237 * Add mapping for sequences in this view to an already open viewer
4239 final AAStructureBindingModel binding = viewer.getBinding();
4240 for (File id : oldFiles.keySet())
4242 // add this and any other pdb files that should be present in the
4244 StructureData filedat = oldFiles.get(id);
4245 String pdbFile = filedat.getFilePath();
4246 SequenceI[] seq = filedat.getSeqList().toArray(new SequenceI[0]);
4247 binding.getSsm().setMapping(seq, null, pdbFile, DataSourceType.FILE);
4248 binding.addSequenceForStructFile(pdbFile, seq);
4250 // and add the AlignmentPanel's reference to the view panel
4251 viewer.addAlignmentPanel(ap);
4252 if (useinViewerSuperpos)
4254 viewer.useAlignmentPanelForSuperposition(ap);
4258 viewer.excludeAlignmentPanelForSuperposition(ap);
4260 if (usetoColourbyseq)
4262 viewer.useAlignmentPanelForColourbyseq(ap, !viewerColouring);
4266 viewer.excludeAlignmentPanelForColourbyseq(ap);
4271 * Get all frames within the Desktop.
4275 protected JInternalFrame[] getAllFrames()
4277 JInternalFrame[] frames = null;
4278 // TODO is this necessary - is it safe - risk of hanging?
4283 frames = Desktop.desktop.getAllFrames();
4284 } catch (ArrayIndexOutOfBoundsException e)
4286 // occasional No such child exceptions are thrown here...
4290 } catch (InterruptedException f)
4294 } while (frames == null);
4299 * Answers true if 'version' is equal to or later than 'supported', where each
4300 * is formatted as major/minor versions like "2.8.3" or "2.3.4b1" for bugfix
4301 * changes. Development and test values for 'version' are leniently treated
4305 * - minimum version we are comparing against
4307 * - version of data being processsed
4310 public static boolean isVersionStringLaterThan(String supported,
4313 if (supported == null || version == null
4314 || version.equalsIgnoreCase("DEVELOPMENT BUILD")
4315 || version.equalsIgnoreCase("Test")
4316 || version.equalsIgnoreCase("AUTOMATED BUILD"))
4318 System.err.println("Assuming project file with "
4319 + (version == null ? "null" : version)
4320 + " is compatible with Jalview version " + supported);
4325 return StringUtils.compareVersions(version, supported, "b") >= 0;
4329 Vector<JalviewStructureDisplayI> newStructureViewers = null;
4331 protected void addNewStructureViewer(JalviewStructureDisplayI sview)
4333 if (newStructureViewers != null)
4335 sview.getBinding().setFinishedLoadingFromArchive(false);
4336 newStructureViewers.add(sview);
4340 protected void setLoadingFinishedForNewStructureViewers()
4342 if (newStructureViewers != null)
4344 for (JalviewStructureDisplayI sview : newStructureViewers)
4346 sview.getBinding().setFinishedLoadingFromArchive(true);
4348 newStructureViewers.clear();
4349 newStructureViewers = null;
4353 AlignFrame loadViewport(String file, JSeq[] JSEQ,
4354 List<SequenceI> hiddenSeqs, AlignmentI al,
4355 JalviewModelSequence jms, Viewport view, String uniqueSeqSetId,
4356 String viewId, List<JvAnnotRow> autoAlan)
4358 AlignFrame af = null;
4359 af = new AlignFrame(al, view.getWidth(), view.getHeight(),
4360 uniqueSeqSetId, viewId);
4362 af.setFileName(file, FileFormat.Jalview);
4364 for (int i = 0; i < JSEQ.length; i++)
4366 af.viewport.setSequenceColour(af.viewport.getAlignment()
4367 .getSequenceAt(i), new java.awt.Color(JSEQ[i].getColour()));
4372 af.getViewport().setColourByReferenceSeq(true);
4373 af.getViewport().setDisplayReferenceSeq(true);
4376 af.viewport.setGatherViewsHere(view.getGatheredViews());
4378 if (view.getSequenceSetId() != null)
4380 AlignmentViewport av = viewportsAdded.get(uniqueSeqSetId);
4382 af.viewport.setSequenceSetId(uniqueSeqSetId);
4385 // propagate shared settings to this new view
4386 af.viewport.setHistoryList(av.getHistoryList());
4387 af.viewport.setRedoList(av.getRedoList());
4391 viewportsAdded.put(uniqueSeqSetId, af.viewport);
4393 // TODO: check if this method can be called repeatedly without
4394 // side-effects if alignpanel already registered.
4395 PaintRefresher.Register(af.alignPanel, uniqueSeqSetId);
4397 // apply Hidden regions to view.
4398 if (hiddenSeqs != null)
4400 for (int s = 0; s < JSEQ.length; s++)
4402 SequenceGroup hidden = new SequenceGroup();
4403 boolean isRepresentative = false;
4404 for (int r = 0; r < JSEQ[s].getHiddenSequencesCount(); r++)
4406 isRepresentative = true;
4407 SequenceI sequenceToHide = al.getSequenceAt(JSEQ[s]
4408 .getHiddenSequences(r));
4409 hidden.addSequence(sequenceToHide, false);
4410 // remove from hiddenSeqs list so we don't try to hide it twice
4411 hiddenSeqs.remove(sequenceToHide);
4413 if (isRepresentative)
4415 SequenceI representativeSequence = al.getSequenceAt(s);
4416 hidden.addSequence(representativeSequence, false);
4417 af.viewport.hideRepSequences(representativeSequence, hidden);
4421 SequenceI[] hseqs = hiddenSeqs.toArray(new SequenceI[hiddenSeqs
4423 af.viewport.hideSequence(hseqs);
4426 // recover view properties and display parameters
4427 if (view.getViewName() != null)
4429 af.viewport.viewName = view.getViewName();
4430 af.setInitialTabVisible();
4432 af.setBounds(view.getXpos(), view.getYpos(), view.getWidth(),
4435 af.viewport.setShowAnnotation(view.getShowAnnotation());
4436 af.viewport.setAbovePIDThreshold(view.getPidSelected());
4437 af.viewport.setThreshold(view.getPidThreshold());
4439 af.viewport.setColourText(view.getShowColourText());
4441 af.viewport.setConservationSelected(view.getConservationSelected());
4442 af.viewport.setIncrement(view.getConsThreshold());
4443 af.viewport.setShowJVSuffix(view.getShowFullId());
4444 af.viewport.setRightAlignIds(view.getRightAlignIds());
4445 af.viewport.setFont(
4446 new java.awt.Font(view.getFontName(), view.getFontStyle(), view
4447 .getFontSize()), true);
4448 ViewStyleI vs = af.viewport.getViewStyle();
4449 vs.setScaleProteinAsCdna(view.isScaleProteinAsCdna());
4450 af.viewport.setViewStyle(vs);
4451 // TODO: allow custom charWidth/Heights to be restored by updating them
4452 // after setting font - which means set above to false
4453 af.viewport.setRenderGaps(view.getRenderGaps());
4454 af.viewport.setWrapAlignment(view.getWrapAlignment());
4455 af.viewport.setShowAnnotation(view.getShowAnnotation());
4457 af.viewport.setShowBoxes(view.getShowBoxes());
4459 af.viewport.setShowText(view.getShowText());
4461 af.viewport.setTextColour(new java.awt.Color(view.getTextCol1()));
4462 af.viewport.setTextColour2(new java.awt.Color(view.getTextCol2()));
4463 af.viewport.setThresholdTextColour(view.getTextColThreshold());
4464 af.viewport.setShowUnconserved(view.hasShowUnconserved() ? view
4465 .isShowUnconserved() : false);
4466 af.viewport.getRanges().setStartRes(view.getStartRes());
4467 // startSeq set in af.alignPanel.updateLayout below
4468 af.alignPanel.updateLayout();
4469 ColourSchemeI cs = null;
4470 // apply colourschemes
4471 if (view.getBgColour() != null)
4473 if (view.getBgColour().startsWith("ucs"))
4475 cs = getUserColourScheme(jms, view.getBgColour());
4477 else if (view.getBgColour().startsWith("Annotation"))
4479 AnnotationColours viewAnnColour = view.getAnnotationColours();
4480 cs = constructAnnotationColour(viewAnnColour, af, al, jms, true);
4487 cs = ColourSchemeProperty.getColourScheme(al, view.getBgColour());
4491 af.viewport.setGlobalColourScheme(cs);
4492 af.viewport.getResidueShading().setThreshold(
4493 view.getPidThreshold(), true);
4494 af.viewport.getResidueShading().setConsensus(
4495 af.viewport.getSequenceConsensusHash());
4496 af.viewport.setColourAppliesToAllGroups(false);
4498 if (view.getConservationSelected() && cs != null)
4500 af.viewport.getResidueShading().setConservationInc(
4501 view.getConsThreshold());
4504 af.changeColour(cs);
4506 af.viewport.setColourAppliesToAllGroups(true);
4508 af.viewport.setShowSequenceFeatures(view.getShowSequenceFeatures());
4510 if (view.hasCentreColumnLabels())
4512 af.viewport.setCentreColumnLabels(view.getCentreColumnLabels());
4514 if (view.hasIgnoreGapsinConsensus())
4516 af.viewport.setIgnoreGapsConsensus(view.getIgnoreGapsinConsensus(),
4519 if (view.hasFollowHighlight())
4521 af.viewport.setFollowHighlight(view.getFollowHighlight());
4523 if (view.hasFollowSelection())
4525 af.viewport.followSelection = view.getFollowSelection();
4527 if (view.hasShowConsensusHistogram())
4529 af.viewport.setShowConsensusHistogram(view
4530 .getShowConsensusHistogram());
4534 af.viewport.setShowConsensusHistogram(true);
4536 if (view.hasShowSequenceLogo())
4538 af.viewport.setShowSequenceLogo(view.getShowSequenceLogo());
4542 af.viewport.setShowSequenceLogo(false);
4544 if (view.hasNormaliseSequenceLogo())
4546 af.viewport.setNormaliseSequenceLogo(view.getNormaliseSequenceLogo());
4548 if (view.hasShowDbRefTooltip())
4550 af.viewport.setShowDBRefs(view.getShowDbRefTooltip());
4552 if (view.hasShowNPfeatureTooltip())
4554 af.viewport.setShowNPFeats(view.hasShowNPfeatureTooltip());
4556 if (view.hasShowGroupConsensus())
4558 af.viewport.setShowGroupConsensus(view.getShowGroupConsensus());
4562 af.viewport.setShowGroupConsensus(false);
4564 if (view.hasShowGroupConservation())
4566 af.viewport.setShowGroupConservation(view.getShowGroupConservation());
4570 af.viewport.setShowGroupConservation(false);
4573 // recover featre settings
4574 if (jms.getFeatureSettings() != null)
4576 FeaturesDisplayed fdi;
4577 af.viewport.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
4578 String[] renderOrder = new String[jms.getFeatureSettings()
4579 .getSettingCount()];
4580 Map<String, FeatureColourI> featureColours = new Hashtable<String, FeatureColourI>();
4581 Map<String, Float> featureOrder = new Hashtable<String, Float>();
4583 for (int fs = 0; fs < jms.getFeatureSettings().getSettingCount(); fs++)
4585 Setting setting = jms.getFeatureSettings().getSetting(fs);
4586 if (setting.hasMincolour())
4588 FeatureColourI gc = setting.hasMin() ? new FeatureColour(
4589 new Color(setting.getMincolour()), new Color(
4590 setting.getColour()), setting.getMin(),
4591 setting.getMax()) : new FeatureColour(new Color(
4592 setting.getMincolour()), new Color(setting.getColour()),
4594 if (setting.hasThreshold())
4596 gc.setThreshold(setting.getThreshold());
4597 int threshstate = setting.getThreshstate();
4598 // -1 = None, 0 = Below, 1 = Above threshold
4599 if (threshstate == 0)
4601 gc.setBelowThreshold(true);
4603 else if (threshstate == 1)
4605 gc.setAboveThreshold(true);
4608 gc.setAutoScaled(true); // default
4609 if (setting.hasAutoScale())
4611 gc.setAutoScaled(setting.getAutoScale());
4613 if (setting.hasColourByLabel())
4615 gc.setColourByLabel(setting.getColourByLabel());
4617 // and put in the feature colour table.
4618 featureColours.put(setting.getType(), gc);
4622 featureColours.put(setting.getType(), new FeatureColour(
4623 new Color(setting.getColour())));
4625 renderOrder[fs] = setting.getType();
4626 if (setting.hasOrder())
4628 featureOrder.put(setting.getType(), setting.getOrder());
4632 featureOrder.put(setting.getType(), new Float(fs
4633 / jms.getFeatureSettings().getSettingCount()));
4635 if (setting.getDisplay())
4637 fdi.setVisible(setting.getType());
4640 Map<String, Boolean> fgtable = new Hashtable<String, Boolean>();
4641 for (int gs = 0; gs < jms.getFeatureSettings().getGroupCount(); gs++)
4643 Group grp = jms.getFeatureSettings().getGroup(gs);
4644 fgtable.put(grp.getName(), new Boolean(grp.getDisplay()));
4646 // FeatureRendererSettings frs = new FeatureRendererSettings(renderOrder,
4647 // fgtable, featureColours, jms.getFeatureSettings().hasTransparency() ?
4648 // jms.getFeatureSettings().getTransparency() : 0.0, featureOrder);
4649 FeatureRendererSettings frs = new FeatureRendererSettings(
4650 renderOrder, fgtable, featureColours, 1.0f, featureOrder);
4651 af.alignPanel.getSeqPanel().seqCanvas.getFeatureRenderer()
4652 .transferSettings(frs);
4656 if (view.getHiddenColumnsCount() > 0)
4658 for (int c = 0; c < view.getHiddenColumnsCount(); c++)
4660 af.viewport.hideColumns(view.getHiddenColumns(c).getStart(), view
4661 .getHiddenColumns(c).getEnd() // +1
4665 if (view.getCalcIdParam() != null)
4667 for (CalcIdParam calcIdParam : view.getCalcIdParam())
4669 if (calcIdParam != null)
4671 if (recoverCalcIdParam(calcIdParam, af.viewport))
4676 warn("Couldn't recover parameters for "
4677 + calcIdParam.getCalcId());
4682 af.setMenusFromViewport(af.viewport);
4683 af.setTitle(view.getTitle());
4684 // TODO: we don't need to do this if the viewport is aready visible.
4686 * Add the AlignFrame to the desktop (it may be 'gathered' later), unless it
4687 * has a 'cdna/protein complement' view, in which case save it in order to
4688 * populate a SplitFrame once all views have been read in.
4690 String complementaryViewId = view.getComplementId();
4691 if (complementaryViewId == null)
4693 Desktop.addInternalFrame(af, view.getTitle(), view.getWidth(),
4695 // recompute any autoannotation
4696 af.alignPanel.updateAnnotation(false, true);
4697 reorderAutoannotation(af, al, autoAlan);
4698 af.alignPanel.alignmentChanged();
4702 splitFrameCandidates.put(view, af);
4708 * Reads saved data to restore Colour by Annotation settings
4710 * @param viewAnnColour
4714 * @param checkGroupAnnColour
4717 private ColourSchemeI constructAnnotationColour(
4718 AnnotationColours viewAnnColour, AlignFrame af, AlignmentI al,
4719 JalviewModelSequence jms, boolean checkGroupAnnColour)
4721 boolean propagateAnnColour = false;
4722 AlignmentI annAlignment = af != null ? af.viewport.getAlignment() : al;
4723 if (checkGroupAnnColour && al.getGroups() != null
4724 && al.getGroups().size() > 0)
4726 // pre 2.8.1 behaviour
4727 // check to see if we should transfer annotation colours
4728 propagateAnnColour = true;
4729 for (SequenceGroup sg : al.getGroups())
4731 if (sg.getColourScheme() instanceof AnnotationColourGradient)
4733 propagateAnnColour = false;
4739 * 2.10.2- : saved annotationId is AlignmentAnnotation.annotationId
4741 String annotationId = viewAnnColour.getAnnotation();
4742 AlignmentAnnotation matchedAnnotation = annotationIds.get(annotationId);
4745 * pre 2.10.2: saved annotationId is AlignmentAnnotation.label
4747 if (matchedAnnotation == null && annAlignment.getAlignmentAnnotation() != null)
4749 for (int i = 0; i < annAlignment.getAlignmentAnnotation().length; i++)
4752 .equals(annAlignment.getAlignmentAnnotation()[i].label))
4754 matchedAnnotation = annAlignment.getAlignmentAnnotation()[i];
4759 if (matchedAnnotation == null)
4761 System.err.println("Failed to match annotation colour scheme for "
4765 if (matchedAnnotation.getThreshold() == null)
4767 matchedAnnotation.setThreshold(new GraphLine(viewAnnColour.getThreshold(),
4768 "Threshold", Color.black));
4771 AnnotationColourGradient cs = null;
4772 if (viewAnnColour.getColourScheme().equals("None"))
4774 cs = new AnnotationColourGradient(matchedAnnotation, new Color(
4775 viewAnnColour.getMinColour()), new Color(
4776 viewAnnColour.getMaxColour()),
4777 viewAnnColour.getAboveThreshold());
4779 else if (viewAnnColour.getColourScheme().startsWith("ucs"))
4781 cs = new AnnotationColourGradient(matchedAnnotation, getUserColourScheme(
4782 jms, viewAnnColour.getColourScheme()),
4783 viewAnnColour.getAboveThreshold());
4787 cs = new AnnotationColourGradient(matchedAnnotation,
4788 ColourSchemeProperty.getColourScheme(al,
4789 viewAnnColour.getColourScheme()),
4790 viewAnnColour.getAboveThreshold());
4793 boolean perSequenceOnly = viewAnnColour.isPerSequence();
4794 boolean useOriginalColours = viewAnnColour.isPredefinedColours();
4795 cs.setSeqAssociated(perSequenceOnly);
4796 cs.setPredefinedColours(useOriginalColours);
4798 if (propagateAnnColour && al.getGroups() != null)
4800 // Also use these settings for all the groups
4801 for (int g = 0; g < al.getGroups().size(); g++)
4803 SequenceGroup sg = al.getGroups().get(g);
4804 if (sg.getGroupColourScheme() == null)
4809 AnnotationColourGradient groupScheme = new AnnotationColourGradient(
4810 matchedAnnotation, sg.getColourScheme(),
4811 viewAnnColour.getAboveThreshold());
4812 sg.setColourScheme(groupScheme);
4813 groupScheme.setSeqAssociated(perSequenceOnly);
4814 groupScheme.setPredefinedColours(useOriginalColours);
4820 private void reorderAutoannotation(AlignFrame af, AlignmentI al,
4821 List<JvAnnotRow> autoAlan)
4823 // copy over visualization settings for autocalculated annotation in the
4825 if (al.getAlignmentAnnotation() != null)
4828 * Kludge for magic autoannotation names (see JAL-811)
4830 String[] magicNames = new String[] { "Consensus", "Quality",
4832 JvAnnotRow nullAnnot = new JvAnnotRow(-1, null);
4833 Hashtable<String, JvAnnotRow> visan = new Hashtable<String, JvAnnotRow>();
4834 for (String nm : magicNames)
4836 visan.put(nm, nullAnnot);
4838 for (JvAnnotRow auan : autoAlan)
4840 visan.put(auan.template.label
4841 + (auan.template.getCalcId() == null ? "" : "\t"
4842 + auan.template.getCalcId()), auan);
4844 int hSize = al.getAlignmentAnnotation().length;
4845 List<JvAnnotRow> reorder = new ArrayList<JvAnnotRow>();
4846 // work through any autoCalculated annotation already on the view
4847 // removing it if it should be placed in a different location on the
4848 // annotation panel.
4849 List<String> remains = new ArrayList<String>(visan.keySet());
4850 for (int h = 0; h < hSize; h++)
4852 jalview.datamodel.AlignmentAnnotation jalan = al
4853 .getAlignmentAnnotation()[h];
4854 if (jalan.autoCalculated)
4857 JvAnnotRow valan = visan.get(k = jalan.label);
4858 if (jalan.getCalcId() != null)
4860 valan = visan.get(k = jalan.label + "\t" + jalan.getCalcId());
4865 // delete the auto calculated row from the alignment
4866 al.deleteAnnotation(jalan, false);
4870 if (valan != nullAnnot)
4872 if (jalan != valan.template)
4874 // newly created autoannotation row instance
4875 // so keep a reference to the visible annotation row
4876 // and copy over all relevant attributes
4877 if (valan.template.graphHeight >= 0)
4880 jalan.graphHeight = valan.template.graphHeight;
4882 jalan.visible = valan.template.visible;
4884 reorder.add(new JvAnnotRow(valan.order, jalan));
4889 // Add any (possibly stale) autocalculated rows that were not appended to
4890 // the view during construction
4891 for (String other : remains)
4893 JvAnnotRow othera = visan.get(other);
4894 if (othera != nullAnnot && othera.template.getCalcId() != null
4895 && othera.template.getCalcId().length() > 0)
4897 reorder.add(othera);
4900 // now put the automatic annotation in its correct place
4901 int s = 0, srt[] = new int[reorder.size()];
4902 JvAnnotRow[] rws = new JvAnnotRow[reorder.size()];
4903 for (JvAnnotRow jvar : reorder)
4906 srt[s++] = jvar.order;
4909 jalview.util.QuickSort.sort(srt, rws);
4910 // and re-insert the annotation at its correct position
4911 for (JvAnnotRow jvar : rws)
4913 al.addAnnotation(jvar.template, jvar.order);
4915 af.alignPanel.adjustAnnotationHeight();
4919 Hashtable skipList = null;
4922 * TODO remove this method
4925 * @return AlignFrame bound to sequenceSetId from view, if one exists. private
4926 * AlignFrame getSkippedFrame(Viewport view) { if (skipList==null) {
4927 * throw new Error("Implementation Error. No skipList defined for this
4928 * Jalview2XML instance."); } return (AlignFrame)
4929 * skipList.get(view.getSequenceSetId()); }
4933 * Check if the Jalview view contained in object should be skipped or not.
4936 * @return true if view's sequenceSetId is a key in skipList
4938 private boolean skipViewport(JalviewModel object)
4940 if (skipList == null)
4945 if (skipList.containsKey(id = object.getJalviewModelSequence()
4946 .getViewport()[0].getSequenceSetId()))
4948 if (Cache.log != null && Cache.log.isDebugEnabled())
4950 Cache.log.debug("Skipping seuqence set id " + id);
4957 public void addToSkipList(AlignFrame af)
4959 if (skipList == null)
4961 skipList = new Hashtable();
4963 skipList.put(af.getViewport().getSequenceSetId(), af);
4966 public void clearSkipList()
4968 if (skipList != null)
4975 private void recoverDatasetFor(SequenceSet vamsasSet, AlignmentI al,
4976 boolean ignoreUnrefed)
4978 jalview.datamodel.AlignmentI ds = getDatasetFor(vamsasSet
4980 Vector dseqs = null;
4983 // create a list of new dataset sequences
4984 dseqs = new Vector();
4986 for (int i = 0, iSize = vamsasSet.getSequenceCount(); i < iSize; i++)
4988 Sequence vamsasSeq = vamsasSet.getSequence(i);
4989 ensureJalviewDatasetSequence(vamsasSeq, ds, dseqs, ignoreUnrefed, i);
4991 // create a new dataset
4994 SequenceI[] dsseqs = new SequenceI[dseqs.size()];
4995 dseqs.copyInto(dsseqs);
4996 ds = new jalview.datamodel.Alignment(dsseqs);
4997 debug("Created new dataset " + vamsasSet.getDatasetId()
4998 + " for alignment " + System.identityHashCode(al));
4999 addDatasetRef(vamsasSet.getDatasetId(), ds);
5001 // set the dataset for the newly imported alignment.
5002 if (al.getDataset() == null && !ignoreUnrefed)
5011 * sequence definition to create/merge dataset sequence for
5015 * vector to add new dataset sequence to
5016 * @param ignoreUnrefed
5017 * - when true, don't create new sequences from vamsasSeq if it's id
5018 * doesn't already have an asssociated Jalview sequence.
5020 * - used to reorder the sequence in the alignment according to the
5021 * vamsasSeq array ordering, to preserve ordering of dataset
5023 private void ensureJalviewDatasetSequence(Sequence vamsasSeq,
5024 AlignmentI ds, Vector dseqs, boolean ignoreUnrefed, int vseqpos)
5026 // JBP TODO: Check this is called for AlCodonFrames to support recovery of
5028 SequenceI sq = seqRefIds.get(vamsasSeq.getId());
5029 boolean reorder = false;
5030 SequenceI dsq = null;
5031 if (sq != null && sq.getDatasetSequence() != null)
5033 dsq = sq.getDatasetSequence();
5039 if (sq == null && ignoreUnrefed)
5043 String sqid = vamsasSeq.getDsseqid();
5046 // need to create or add a new dataset sequence reference to this sequence
5049 dsq = seqRefIds.get(sqid);
5054 // make a new dataset sequence
5055 dsq = sq.createDatasetSequence();
5058 // make up a new dataset reference for this sequence
5059 sqid = seqHash(dsq);
5061 dsq.setVamsasId(uniqueSetSuffix + sqid);
5062 seqRefIds.put(sqid, dsq);
5067 dseqs.addElement(dsq);
5072 ds.addSequence(dsq);
5078 { // make this dataset sequence sq's dataset sequence
5079 sq.setDatasetSequence(dsq);
5080 // and update the current dataset alignment
5085 if (!dseqs.contains(dsq))
5092 if (ds.findIndex(dsq) < 0)
5094 ds.addSequence(dsq);
5101 // TODO: refactor this as a merge dataset sequence function
5102 // now check that sq (the dataset sequence) sequence really is the union of
5103 // all references to it
5104 // boolean pre = sq.getStart() < dsq.getStart();
5105 // boolean post = sq.getEnd() > dsq.getEnd();
5109 // StringBuffer sb = new StringBuffer();
5110 String newres = jalview.analysis.AlignSeq.extractGaps(
5111 jalview.util.Comparison.GapChars, sq.getSequenceAsString());
5112 if (!newres.equalsIgnoreCase(dsq.getSequenceAsString())
5113 && newres.length() > dsq.getLength())
5115 // Update with the longer sequence.
5119 * if (pre) { sb.insert(0, newres .substring(0, dsq.getStart() -
5120 * sq.getStart())); dsq.setStart(sq.getStart()); } if (post) {
5121 * sb.append(newres.substring(newres.length() - sq.getEnd() -
5122 * dsq.getEnd())); dsq.setEnd(sq.getEnd()); }
5124 dsq.setSequence(newres);
5126 // TODO: merges will never happen if we 'know' we have the real dataset
5127 // sequence - this should be detected when id==dssid
5129 .println("DEBUG Notice: Merged dataset sequence (if you see this often, post at http://issues.jalview.org/browse/JAL-1474)"); // ("
5130 // + (pre ? "prepended" : "") + " "
5131 // + (post ? "appended" : ""));
5136 // sequence refs are identical. We may need to update the existing dataset
5137 // alignment with this one, though.
5138 if (ds != null && dseqs == null)
5140 int opos = ds.findIndex(dsq);
5141 SequenceI tseq = null;
5142 if (opos != -1 && vseqpos != opos)
5144 // remove from old position
5145 ds.deleteSequence(dsq);
5147 if (vseqpos < ds.getHeight())
5149 if (vseqpos != opos)
5151 // save sequence at destination position
5152 tseq = ds.getSequenceAt(vseqpos);
5153 ds.replaceSequenceAt(vseqpos, dsq);
5154 ds.addSequence(tseq);
5159 ds.addSequence(dsq);
5166 * TODO use AlignmentI here and in related methods - needs
5167 * AlignmentI.getDataset() changed to return AlignmentI instead of Alignment
5169 Hashtable<String, AlignmentI> datasetIds = null;
5171 IdentityHashMap<AlignmentI, String> dataset2Ids = null;
5173 private AlignmentI getDatasetFor(String datasetId)
5175 if (datasetIds == null)
5177 datasetIds = new Hashtable<String, AlignmentI>();
5180 if (datasetIds.containsKey(datasetId))
5182 return datasetIds.get(datasetId);
5187 private void addDatasetRef(String datasetId, AlignmentI dataset)
5189 if (datasetIds == null)
5191 datasetIds = new Hashtable<String, AlignmentI>();
5193 datasetIds.put(datasetId, dataset);
5197 * make a new dataset ID for this jalview dataset alignment
5202 private String getDatasetIdRef(AlignmentI dataset)
5204 if (dataset.getDataset() != null)
5206 warn("Serious issue! Dataset Object passed to getDatasetIdRef is not a Jalview DATASET alignment...");
5208 String datasetId = makeHashCode(dataset, null);
5209 if (datasetId == null)
5211 // make a new datasetId and record it
5212 if (dataset2Ids == null)
5214 dataset2Ids = new IdentityHashMap<AlignmentI, String>();
5218 datasetId = dataset2Ids.get(dataset);
5220 if (datasetId == null)
5222 datasetId = "ds" + dataset2Ids.size() + 1;
5223 dataset2Ids.put(dataset, datasetId);
5229 private void addDBRefs(SequenceI datasetSequence, Sequence sequence)
5231 for (int d = 0; d < sequence.getDBRefCount(); d++)
5233 DBRef dr = sequence.getDBRef(d);
5234 jalview.datamodel.DBRefEntry entry = new jalview.datamodel.DBRefEntry(
5235 sequence.getDBRef(d).getSource(), sequence.getDBRef(d)
5236 .getVersion(), sequence.getDBRef(d).getAccessionId());
5237 if (dr.getMapping() != null)
5239 entry.setMap(addMapping(dr.getMapping()));
5241 datasetSequence.addDBRef(entry);
5245 private jalview.datamodel.Mapping addMapping(Mapping m)
5247 SequenceI dsto = null;
5248 // Mapping m = dr.getMapping();
5249 int fr[] = new int[m.getMapListFromCount() * 2];
5250 Enumeration f = m.enumerateMapListFrom();
5251 for (int _i = 0; f.hasMoreElements(); _i += 2)
5253 MapListFrom mf = (MapListFrom) f.nextElement();
5254 fr[_i] = mf.getStart();
5255 fr[_i + 1] = mf.getEnd();
5257 int fto[] = new int[m.getMapListToCount() * 2];
5258 f = m.enumerateMapListTo();
5259 for (int _i = 0; f.hasMoreElements(); _i += 2)
5261 MapListTo mf = (MapListTo) f.nextElement();
5262 fto[_i] = mf.getStart();
5263 fto[_i + 1] = mf.getEnd();
5265 jalview.datamodel.Mapping jmap = new jalview.datamodel.Mapping(dsto,
5266 fr, fto, (int) m.getMapFromUnit(), (int) m.getMapToUnit());
5267 if (m.getMappingChoice() != null)
5269 MappingChoice mc = m.getMappingChoice();
5270 if (mc.getDseqFor() != null)
5272 String dsfor = "" + mc.getDseqFor();
5273 if (seqRefIds.containsKey(dsfor))
5278 jmap.setTo(seqRefIds.get(dsfor));
5282 frefedSequence.add(newMappingRef(dsfor, jmap));
5288 * local sequence definition
5290 Sequence ms = mc.getSequence();
5291 SequenceI djs = null;
5292 String sqid = ms.getDsseqid();
5293 if (sqid != null && sqid.length() > 0)
5296 * recover dataset sequence
5298 djs = seqRefIds.get(sqid);
5303 .println("Warning - making up dataset sequence id for DbRef sequence map reference");
5304 sqid = ((Object) ms).toString(); // make up a new hascode for
5305 // undefined dataset sequence hash
5306 // (unlikely to happen)
5312 * make a new dataset sequence and add it to refIds hash
5314 djs = new jalview.datamodel.Sequence(ms.getName(),
5316 djs.setStart(jmap.getMap().getToLowest());
5317 djs.setEnd(jmap.getMap().getToHighest());
5318 djs.setVamsasId(uniqueSetSuffix + sqid);
5320 incompleteSeqs.put(sqid, djs);
5321 seqRefIds.put(sqid, djs);
5324 jalview.bin.Cache.log.debug("about to recurse on addDBRefs.");
5333 public jalview.gui.AlignmentPanel copyAlignPanel(AlignmentPanel ap,
5334 boolean keepSeqRefs)
5337 JalviewModel jm = saveState(ap, null, null, null);
5342 jm.getJalviewModelSequence().getViewport(0).setSequenceSetId(null);
5346 uniqueSetSuffix = "";
5347 jm.getJalviewModelSequence().getViewport(0).setId(null); // we don't
5352 if (this.frefedSequence == null)
5354 frefedSequence = new Vector();
5357 viewportsAdded.clear();
5359 AlignFrame af = loadFromObject(jm, null, false, null);
5360 af.alignPanels.clear();
5361 af.closeMenuItem_actionPerformed(true);
5364 * if(ap.av.getAlignment().getAlignmentAnnotation()!=null) { for(int i=0;
5365 * i<ap.av.getAlignment().getAlignmentAnnotation().length; i++) {
5366 * if(!ap.av.getAlignment().getAlignmentAnnotation()[i].autoCalculated) {
5367 * af.alignPanel.av.getAlignment().getAlignmentAnnotation()[i] =
5368 * ap.av.getAlignment().getAlignmentAnnotation()[i]; } } }
5371 return af.alignPanel;
5375 * flag indicating if hashtables should be cleared on finalization TODO this
5376 * flag may not be necessary
5378 private final boolean _cleartables = true;
5380 private Hashtable jvids2vobj;
5385 * @see java.lang.Object#finalize()
5388 protected void finalize() throws Throwable
5390 // really make sure we have no buried refs left.
5395 this.seqRefIds = null;
5396 this.seqsToIds = null;
5400 private void warn(String msg)
5405 private void warn(String msg, Exception e)
5407 if (Cache.log != null)
5411 Cache.log.warn(msg, e);
5415 Cache.log.warn(msg);
5420 System.err.println("Warning: " + msg);
5423 e.printStackTrace();
5428 private void debug(String string)
5430 debug(string, null);
5433 private void debug(String msg, Exception e)
5435 if (Cache.log != null)
5439 Cache.log.debug(msg, e);
5443 Cache.log.debug(msg);
5448 System.err.println("Warning: " + msg);
5451 e.printStackTrace();
5457 * set the object to ID mapping tables used to write/recover objects and XML
5458 * ID strings for the jalview project. If external tables are provided then
5459 * finalize and clearSeqRefs will not clear the tables when the Jalview2XML
5460 * object goes out of scope. - also populates the datasetIds hashtable with
5461 * alignment objects containing dataset sequences
5464 * Map from ID strings to jalview datamodel
5466 * Map from jalview datamodel to ID strings
5470 public void setObjectMappingTables(Hashtable vobj2jv,
5471 IdentityHashMap jv2vobj)
5473 this.jv2vobj = jv2vobj;
5474 this.vobj2jv = vobj2jv;
5475 Iterator ds = jv2vobj.keySet().iterator();
5477 while (ds.hasNext())
5479 Object jvobj = ds.next();
5480 id = jv2vobj.get(jvobj).toString();
5481 if (jvobj instanceof jalview.datamodel.Alignment)
5483 if (((jalview.datamodel.Alignment) jvobj).getDataset() == null)
5485 addDatasetRef(id, (jalview.datamodel.Alignment) jvobj);
5488 else if (jvobj instanceof jalview.datamodel.Sequence)
5490 // register sequence object so the XML parser can recover it.
5491 if (seqRefIds == null)
5493 seqRefIds = new HashMap<String, SequenceI>();
5495 if (seqsToIds == null)
5497 seqsToIds = new IdentityHashMap<SequenceI, String>();
5499 seqRefIds.put(jv2vobj.get(jvobj).toString(), (SequenceI) jvobj);
5500 seqsToIds.put((SequenceI) jvobj, id);
5502 else if (jvobj instanceof jalview.datamodel.AlignmentAnnotation)
5505 AlignmentAnnotation jvann = (AlignmentAnnotation) jvobj;
5506 annotationIds.put(anid = jv2vobj.get(jvobj).toString(), jvann);
5507 if (jvann.annotationId == null)
5509 jvann.annotationId = anid;
5511 if (!jvann.annotationId.equals(anid))
5513 // TODO verify that this is the correct behaviour
5514 this.warn("Overriding Annotation ID for " + anid
5515 + " from different id : " + jvann.annotationId);
5516 jvann.annotationId = anid;
5519 else if (jvobj instanceof String)
5521 if (jvids2vobj == null)
5523 jvids2vobj = new Hashtable();
5524 jvids2vobj.put(jvobj, jv2vobj.get(jvobj).toString());
5529 Cache.log.debug("Ignoring " + jvobj.getClass() + " (ID = " + id);
5535 * set the uniqueSetSuffix used to prefix/suffix object IDs for jalview
5536 * objects created from the project archive. If string is null (default for
5537 * construction) then suffix will be set automatically.
5541 public void setUniqueSetSuffix(String string)
5543 uniqueSetSuffix = string;
5548 * uses skipList2 as the skipList for skipping views on sequence sets
5549 * associated with keys in the skipList
5553 public void setSkipList(Hashtable skipList2)
5555 skipList = skipList2;
5559 * Reads the jar entry of given name and returns its contents, or null if the
5560 * entry is not found.
5563 * @param jarEntryName
5566 protected String readJarEntry(jarInputStreamProvider jprovider,
5567 String jarEntryName)
5569 String result = null;
5570 BufferedReader in = null;
5575 * Reopen the jar input stream and traverse its entries to find a matching
5578 JarInputStream jin = jprovider.getJarInputStream();
5579 JarEntry entry = null;
5582 entry = jin.getNextJarEntry();
5583 } while (entry != null && !entry.getName().equals(jarEntryName));
5587 StringBuilder out = new StringBuilder(256);
5588 in = new BufferedReader(new InputStreamReader(jin, UTF_8));
5591 while ((data = in.readLine()) != null)
5595 result = out.toString();
5599 warn("Couldn't find entry in Jalview Jar for " + jarEntryName);
5601 } catch (Exception ex)
5603 ex.printStackTrace();
5611 } catch (IOException e)
5622 * Returns an incrementing counter (0, 1, 2...)
5626 private synchronized int nextCounter()