+ else
+ {
+ jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(),
+ an[i].getDescription(), anot);
+ jaa._linecolour = firstColour;
+ }
+ // register new annotation
+ if (an[i].getId() != null)
+ {
+ annotationIds.put(an[i].getId(), jaa);
+ jaa.annotationId = an[i].getId();
+ }
+ // recover sequence association
+ String sequenceRef = an[i].getSequenceRef();
+ if (sequenceRef != null)
+ {
+ // from 2.9 sequenceRef is to sequence id (JAL-1781)
+ SequenceI sequence = seqRefIds.get(sequenceRef);
+ if (sequence == null)
+ {
+ // in pre-2.9 projects sequence ref is to sequence name
+ sequence = al.findName(sequenceRef);
+ }
+ if (sequence != null)
+ {
+ jaa.createSequenceMapping(sequence, 1, true);
+ sequence.addAlignmentAnnotation(jaa);
+ }
+ }
+ // and make a note of any group association
+ if (an[i].getGroupRef() != null && an[i].getGroupRef().length() > 0)
+ {
+ List<jalview.datamodel.AlignmentAnnotation> aal = groupAnnotRefs
+ .get(an[i].getGroupRef());
+ if (aal == null)
+ {
+ aal = new ArrayList<jalview.datamodel.AlignmentAnnotation>();
+ groupAnnotRefs.put(an[i].getGroupRef(), aal);
+ }
+ aal.add(jaa);
+ }
+
+ if (an[i].hasScore())
+ {
+ jaa.setScore(an[i].getScore());
+ }
+ if (an[i].hasVisible())
+ {
+ jaa.visible = an[i].getVisible();
+ }
+
+ if (an[i].hasCentreColLabels())
+ {
+ jaa.centreColLabels = an[i].getCentreColLabels();
+ }
+
+ if (an[i].hasScaleColLabels())
+ {
+ jaa.scaleColLabel = an[i].getScaleColLabels();
+ }
+ if (an[i].hasAutoCalculated() && an[i].isAutoCalculated())
+ {
+ // newer files have an 'autoCalculated' flag and store calculation
+ // state in viewport properties
+ jaa.autoCalculated = true; // means annotation will be marked for
+ // update at end of load.
+ }
+ if (an[i].hasGraphHeight())
+ {
+ jaa.graphHeight = an[i].getGraphHeight();
+ }
+ if (an[i].hasBelowAlignment())
+ {
+ jaa.belowAlignment = an[i].isBelowAlignment();
+ }
+ jaa.setCalcId(an[i].getCalcId());
+ if (an[i].getPropertyCount() > 0)
+ {
+ for (jalview.schemabinding.version2.Property prop : an[i]
+ .getProperty())
+ {
+ jaa.setProperty(prop.getName(), prop.getValue());
+ }
+ }
+ if (jaa.autoCalculated)
+ {
+ autoAlan.add(new JvAnnotRow(i, jaa));
+ }
+ else
+ // if (!autoForView)
+ {
+ // add autocalculated group annotation and any user created annotation
+ // for the view
+ al.addAnnotation(jaa);
+ }
+ }
+ }
+ // ///////////////////////
+ // LOAD GROUPS
+ // Create alignment markup and styles for this view
+ if (jms.getJGroupCount() > 0)
+ {
+ JGroup[] groups = jms.getJGroup();
+ boolean addAnnotSchemeGroup = false;
+ for (int i = 0; i < groups.length; i++)
+ {
+ JGroup jGroup = groups[i];
+ ColourSchemeI cs = null;
+ if (jGroup.getColour() != null)
+ {
+ if (jGroup.getColour().startsWith("ucs"))
+ {
+ cs = getUserColourScheme(jms, jGroup.getColour());
+ }
+ else if (jGroup.getColour().equals("AnnotationColourGradient")
+ && jGroup.getAnnotationColours() != null)
+ {
+ addAnnotSchemeGroup = true;
+ }
+ else
+ {
+ cs = ColourSchemeProperty.getColourScheme(al, jGroup.getColour());
+ }
+ }
+ int pidThreshold = jGroup.getPidThreshold();
+
+ Vector<SequenceI> seqs = new Vector<SequenceI>();
+
+ for (int s = 0; s < jGroup.getSeqCount(); s++)
+ {
+ String seqId = jGroup.getSeq(s) + "";
+ SequenceI ts = seqRefIds.get(seqId);
+
+ if (ts != null)
+ {
+ seqs.addElement(ts);
+ }
+ }
+
+ if (seqs.size() < 1)
+ {
+ continue;
+ }
+
+ SequenceGroup sg = new SequenceGroup(seqs, jGroup.getName(), cs,
+ jGroup.getDisplayBoxes(), jGroup.getDisplayText(),
+ jGroup.getColourText(), jGroup.getStart(), jGroup.getEnd());
+ sg.getGroupColourScheme().setThreshold(pidThreshold, true);
+ sg.getGroupColourScheme().setConservationInc(jGroup.getConsThreshold());
+ sg.setOutlineColour(new java.awt.Color(jGroup.getOutlineColour()));
+
+ sg.textColour = new java.awt.Color(jGroup.getTextCol1());
+ sg.textColour2 = new java.awt.Color(jGroup.getTextCol2());
+ sg.setShowNonconserved(jGroup.hasShowUnconserved() ? jGroup
+ .isShowUnconserved() : false);
+ sg.thresholdTextColour = jGroup.getTextColThreshold();
+ if (jGroup.hasShowConsensusHistogram())
+ {
+ sg.setShowConsensusHistogram(jGroup.isShowConsensusHistogram());
+ }
+ ;
+ if (jGroup.hasShowSequenceLogo())
+ {
+ sg.setshowSequenceLogo(jGroup.isShowSequenceLogo());
+ }
+ if (jGroup.hasNormaliseSequenceLogo())
+ {
+ sg.setNormaliseSequenceLogo(jGroup.isNormaliseSequenceLogo());
+ }
+ if (jGroup.hasIgnoreGapsinConsensus())
+ {
+ sg.setIgnoreGapsConsensus(jGroup.getIgnoreGapsinConsensus());
+ }
+ if (jGroup.getConsThreshold() != 0)
+ {
+ Conservation c = new Conservation("All", sg.getSequences(null),
+ 0, sg.getWidth() - 1);
+ c.calculate();
+ c.verdict(false, 25);
+ sg.cs.setConservation(c);
+ }
+
+ if (jGroup.getId() != null && groupAnnotRefs.size() > 0)
+ {
+ // re-instate unique group/annotation row reference
+ List<AlignmentAnnotation> jaal = groupAnnotRefs.get(jGroup
+ .getId());
+ if (jaal != null)
+ {
+ for (AlignmentAnnotation jaa : jaal)
+ {
+ jaa.groupRef = sg;
+ if (jaa.autoCalculated)
+ {
+ // match up and try to set group autocalc alignment row for this
+ // annotation
+ if (jaa.label.startsWith("Consensus for "))
+ {
+ sg.setConsensus(jaa);
+ }
+ // match up and try to set group autocalc alignment row for this
+ // annotation
+ if (jaa.label.startsWith("Conservation for "))
+ {
+ sg.setConservationRow(jaa);
+ }
+ }
+ }
+ }
+ }
+ al.addGroup(sg);
+ if (addAnnotSchemeGroup)
+ {
+ // reconstruct the annotation colourscheme
+ sg.setColourScheme(constructAnnotationColour(
+ jGroup.getAnnotationColours(), null, al, jms, false));
+ }
+ }
+ }
+ if (view == null)
+ {
+ // only dataset in this model, so just return.
+ return null;
+ }
+ // ///////////////////////////////
+ // LOAD VIEWPORT
+
+ // If we just load in the same jar file again, the sequenceSetId
+ // will be the same, and we end up with multiple references
+ // to the same sequenceSet. We must modify this id on load
+ // so that each load of the file gives a unique id
+ String uniqueSeqSetId = view.getSequenceSetId() + uniqueSetSuffix;
+ String viewId = (view.getId() == null ? null : view.getId()
+ + uniqueSetSuffix);
+ AlignFrame af = null;
+ AlignViewport av = null;
+ // now check to see if we really need to create a new viewport.
+ if (multipleView && viewportsAdded.size() == 0)
+ {
+ // We recovered an alignment for which a viewport already exists.
+ // TODO: fix up any settings necessary for overlaying stored state onto
+ // state recovered from another document. (may not be necessary).
+ // we may need a binding from a viewport in memory to one recovered from
+ // XML.
+ // and then recover its containing af to allow the settings to be applied.
+ // TODO: fix for vamsas demo
+ System.err
+ .println("About to recover a viewport for existing alignment: Sequence set ID is "
+ + uniqueSeqSetId);
+ Object seqsetobj = retrieveExistingObj(uniqueSeqSetId);
+ if (seqsetobj != null)
+ {
+ if (seqsetobj instanceof String)
+ {
+ uniqueSeqSetId = (String) seqsetobj;
+ System.err
+ .println("Recovered extant sequence set ID mapping for ID : New Sequence set ID is "
+ + uniqueSeqSetId);
+ }
+ else
+ {
+ System.err
+ .println("Warning : Collision between sequence set ID string and existing jalview object mapping.");
+ }
+
+ }
+ }
+ /**
+ * indicate that annotation colours are applied across all groups (pre
+ * Jalview 2.8.1 behaviour)
+ */
+ boolean doGroupAnnColour = Jalview2XML.isVersionStringLaterThan(
+ "2.8.1", object.getVersion());
+
+ AlignmentPanel ap = null;
+ boolean isnewview = true;
+ if (viewId != null)
+ {
+ // Check to see if this alignment already has a view id == viewId
+ jalview.gui.AlignmentPanel views[] = Desktop
+ .getAlignmentPanels(uniqueSeqSetId);
+ if (views != null && views.length > 0)
+ {
+ for (int v = 0; v < views.length; v++)
+ {
+ if (views[v].av.getViewId().equalsIgnoreCase(viewId))
+ {
+ // recover the existing alignpanel, alignframe, viewport
+ af = views[v].alignFrame;
+ av = views[v].av;
+ ap = views[v];
+ // TODO: could even skip resetting view settings if we don't want to
+ // change the local settings from other jalview processes
+ isnewview = false;
+ }
+ }
+ }
+ }
+
+ if (isnewview)
+ {
+ af = loadViewport(file, jseqs, hiddenSeqs, al, jms, view,
+ uniqueSeqSetId, viewId, autoAlan);
+ av = af.viewport;
+ ap = af.alignPanel;
+ }
+
+ /*
+ * Load any trees, PDB structures and viewers
+ *
+ * Not done if flag is false (when this method is used for New View)
+ */
+ if (loadTreesAndStructures)
+ {
+ loadTrees(jms, view, af, av, ap);
+ loadPDBStructures(jprovider, jseqs, af, ap);
+ loadRnaViewers(jprovider, jseqs, ap);
+ }
+ // and finally return.
+ return af;
+ }
+
+ /**
+ * Instantiate and link any saved RNA (Varna) viewers. The state of the Varna
+ * panel is restored from separate jar entries, two (gapped and trimmed) per
+ * sequence and secondary structure.
+ *
+ * Currently each viewer shows just one sequence and structure (gapped and
+ * trimmed), however this method is designed to support multiple sequences or
+ * structures in viewers if wanted in future.
+ *
+ * @param jprovider
+ * @param jseqs
+ * @param ap
+ */
+ private void loadRnaViewers(jarInputStreamProvider jprovider,
+ JSeq[] jseqs, AlignmentPanel ap)
+ {
+ /*
+ * scan the sequences for references to viewers; create each one the first
+ * time it is referenced, add Rna models to existing viewers
+ */
+ for (JSeq jseq : jseqs)
+ {
+ for (int i = 0; i < jseq.getRnaViewerCount(); i++)
+ {
+ RnaViewer viewer = jseq.getRnaViewer(i);
+ AppVarna appVarna = findOrCreateVarnaViewer(viewer,
+ uniqueSetSuffix, ap);
+
+ for (int j = 0; j < viewer.getSecondaryStructureCount(); j++)
+ {
+ SecondaryStructure ss = viewer.getSecondaryStructure(j);
+ SequenceI seq = seqRefIds.get(jseq.getId());
+ AlignmentAnnotation ann = this.annotationIds.get(ss
+ .getAnnotationId());
+
+ /*
+ * add the structure to the Varna display (with session state copied
+ * from the jar to a temporary file)
+ */
+ boolean gapped = ss.isGapped();
+ String rnaTitle = ss.getTitle();
+ String sessionState = ss.getViewerState();
+ String tempStateFile = copyJarEntry(jprovider, sessionState,
+ "varna", null);
+ RnaModel rna = new RnaModel(rnaTitle, ann, seq, null, gapped);
+ appVarna.addModelSession(rna, rnaTitle, tempStateFile);
+ }
+ appVarna.setInitialSelection(viewer.getSelectedRna());
+ }
+ }
+ }
+
+ /**
+ * Locate and return an already instantiated matching AppVarna, or create one
+ * if not found
+ *
+ * @param viewer
+ * @param viewIdSuffix
+ * @param ap
+ * @return
+ */
+ protected AppVarna findOrCreateVarnaViewer(RnaViewer viewer,
+ String viewIdSuffix, AlignmentPanel ap)
+ {
+ /*
+ * on each load a suffix is appended to the saved viewId, to avoid conflicts
+ * if load is repeated
+ */
+ String postLoadId = viewer.getViewId() + viewIdSuffix;
+ for (JInternalFrame frame : getAllFrames())
+ {
+ if (frame instanceof AppVarna)
+ {
+ AppVarna varna = (AppVarna) frame;
+ if (postLoadId.equals(varna.getViewId()))
+ {
+ // this viewer is already instantiated
+ // could in future here add ap as another 'parent' of the
+ // AppVarna window; currently just 1-to-many
+ return varna;
+ }
+ }
+ }
+
+ /*
+ * viewer not found - make it
+ */
+ RnaViewerModel model = new RnaViewerModel(postLoadId,
+ viewer.getTitle(), viewer.getXpos(), viewer.getYpos(),
+ viewer.getWidth(), viewer.getHeight(),
+ viewer.getDividerLocation());
+ AppVarna varna = new AppVarna(model, ap);
+
+ return varna;
+ }
+
+ /**
+ * Load any saved trees
+ *
+ * @param jms
+ * @param view
+ * @param af
+ * @param av
+ * @param ap
+ */
+ protected void loadTrees(JalviewModelSequence jms, Viewport view,
+ AlignFrame af, AlignViewport av, AlignmentPanel ap)
+ {
+ // TODO result of automated refactoring - are all these parameters needed?
+ try
+ {
+ for (int t = 0; t < jms.getTreeCount(); t++)
+ {
+
+ Tree tree = jms.getTree(t);
+
+ TreePanel tp = (TreePanel) retrieveExistingObj(tree.getId());
+ if (tp == null)
+ {
+ tp = af.showNewickTree(
+ new jalview.io.NewickFile(tree.getNewick()),
+ tree.getTitle(), tree.getWidth(), tree.getHeight(),
+ tree.getXpos(), tree.getYpos());
+ if (tree.getId() != null)
+ {
+ // perhaps bind the tree id to something ?
+ }
+ }
+ else
+ {
+ // update local tree attributes ?
+ // TODO: should check if tp has been manipulated by user - if so its
+ // settings shouldn't be modified
+ tp.setTitle(tree.getTitle());
+ tp.setBounds(new Rectangle(tree.getXpos(), tree.getYpos(), tree
+ .getWidth(), tree.getHeight()));
+ tp.av = av; // af.viewport; // TODO: verify 'associate with all
+ // views'
+ // works still
+ tp.treeCanvas.av = av; // af.viewport;
+ tp.treeCanvas.ap = ap; // af.alignPanel;
+
+ }
+ if (tp == null)
+ {
+ warn("There was a problem recovering stored Newick tree: \n"
+ + tree.getNewick());
+ continue;
+ }
+
+ tp.fitToWindow.setState(tree.getFitToWindow());
+ tp.fitToWindow_actionPerformed(null);
+
+ if (tree.getFontName() != null)
+ {
+ tp.setTreeFont(new java.awt.Font(tree.getFontName(), tree
+ .getFontStyle(), tree.getFontSize()));
+ }
+ else
+ {
+ tp.setTreeFont(new java.awt.Font(view.getFontName(), view
+ .getFontStyle(), tree.getFontSize()));
+ }
+
+ tp.showPlaceholders(tree.getMarkUnlinked());
+ tp.showBootstrap(tree.getShowBootstrap());
+ tp.showDistances(tree.getShowDistances());
+
+ tp.treeCanvas.threshold = tree.getThreshold();
+
+ if (tree.getCurrentTree())
+ {
+ af.viewport.setCurrentTree(tp.getTree());
+ }
+ }
+
+ } catch (Exception ex)
+ {
+ ex.printStackTrace();
+ }
+ }
+
+ /**
+ * Load and link any saved structure viewers.
+ *
+ * @param jprovider
+ * @param jseqs
+ * @param af
+ * @param ap
+ */
+ protected void loadPDBStructures(jarInputStreamProvider jprovider,
+ JSeq[] jseqs, AlignFrame af, AlignmentPanel ap)
+ {
+ /*
+ * Run through all PDB ids on the alignment, and collect mappings between
+ * distinct view ids and all sequences referring to that view.
+ */
+ Map<String, StructureViewerModel> structureViewers = new LinkedHashMap<String, StructureViewerModel>();
+
+ for (int i = 0; i < jseqs.length; i++)
+ {
+ if (jseqs[i].getPdbidsCount() > 0)
+ {
+ Pdbids[] ids = jseqs[i].getPdbids();
+ for (int p = 0; p < ids.length; p++)
+ {
+ final int structureStateCount = ids[p].getStructureStateCount();
+ for (int s = 0; s < structureStateCount; s++)
+ {
+ // check to see if we haven't already created this structure view
+ final StructureState structureState = ids[p]
+ .getStructureState(s);
+ String sviewid = (structureState.getViewId() == null) ? null
+ : structureState.getViewId() + uniqueSetSuffix;
+ jalview.datamodel.PDBEntry jpdb = new jalview.datamodel.PDBEntry();
+ // Originally : ids[p].getFile()
+ // : TODO: verify external PDB file recovery still works in normal
+ // jalview project load
+ jpdb.setFile(loadPDBFile(jprovider, ids[p].getId(),
+ ids[p].getFile()));
+ jpdb.setId(ids[p].getId());
+
+ int x = structureState.getXpos();
+ int y = structureState.getYpos();
+ int width = structureState.getWidth();
+ int height = structureState.getHeight();
+
+ // Probably don't need to do this anymore...
+ // Desktop.desktop.getComponentAt(x, y);
+ // TODO: NOW: check that this recovers the PDB file correctly.
+ String pdbFile = loadPDBFile(jprovider, ids[p].getId(),
+ ids[p].getFile());
+ jalview.datamodel.SequenceI seq = seqRefIds.get(jseqs[i]
+ .getId() + "");
+ if (sviewid == null)
+ {
+ sviewid = "_jalview_pre2_4_" + x + "," + y + "," + width
+ + "," + height;
+ }
+ if (!structureViewers.containsKey(sviewid))
+ {
+ structureViewers.put(sviewid,
+ new StructureViewerModel(x, y, width, height, false,
+ false, true, structureState.getViewId(),
+ structureState.getType()));
+ // Legacy pre-2.7 conversion JAL-823 :
+ // do not assume any view has to be linked for colour by
+ // sequence
+ }
+
+ // assemble String[] { pdb files }, String[] { id for each
+ // file }, orig_fileloc, SequenceI[][] {{ seqs_file 1 }, {
+ // seqs_file 2}, boolean[] {
+ // linkAlignPanel,superposeWithAlignpanel}} from hash
+ StructureViewerModel jmoldat = structureViewers.get(sviewid);
+ jmoldat.setAlignWithPanel(jmoldat.isAlignWithPanel()
+ | (structureState.hasAlignwithAlignPanel() ? structureState
+ .getAlignwithAlignPanel() : false));
+
+ /*
+ * Default colour by linked panel to false if not specified (e.g.
+ * for pre-2.7 projects)
+ */
+ boolean colourWithAlignPanel = jmoldat.isColourWithAlignPanel();
+ colourWithAlignPanel |= (structureState
+ .hasColourwithAlignPanel() ? structureState
+ .getColourwithAlignPanel() : false);
+ jmoldat.setColourWithAlignPanel(colourWithAlignPanel);
+
+ /*
+ * Default colour by viewer to true if not specified (e.g. for
+ * pre-2.7 projects)
+ */
+ boolean colourByViewer = jmoldat.isColourByViewer();
+ colourByViewer &= structureState.hasColourByJmol() ? structureState
+ .getColourByJmol() : true;
+ jmoldat.setColourByViewer(colourByViewer);
+
+ if (jmoldat.getStateData().length() < structureState
+ .getContent().length())
+ {
+ {
+ jmoldat.setStateData(structureState.getContent());
+ }
+ }
+ if (ids[p].getFile() != null)
+ {
+ File mapkey = new File(ids[p].getFile());
+ StructureData seqstrmaps = jmoldat.getFileData().get(mapkey);
+ if (seqstrmaps == null)
+ {
+ jmoldat.getFileData().put(
+ mapkey,
+ seqstrmaps = jmoldat.new StructureData(pdbFile,
+ ids[p].getId()));
+ }
+ if (!seqstrmaps.getSeqList().contains(seq))
+ {
+ seqstrmaps.getSeqList().add(seq);
+ // TODO and chains?
+ }
+ }
+ else
+ {
+ 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");
+ warn(errorMessage);
+ }
+ }
+ }
+ }
+ }
+ // Instantiate the associated structure views
+ for (Entry<String, StructureViewerModel> entry : structureViewers
+ .entrySet())
+ {
+ try
+ {
+ createOrLinkStructureViewer(entry, af, ap, jprovider);
+ } catch (Exception e)
+ {
+ System.err.println("Error loading structure viewer: "
+ + e.getMessage());
+ // failed - try the next one
+ }
+ }
+ }
+
+ /**
+ *
+ * @param viewerData
+ * @param af
+ * @param ap
+ * @param jprovider
+ */
+ protected void createOrLinkStructureViewer(
+ Entry<String, StructureViewerModel> viewerData, AlignFrame af,
+ AlignmentPanel ap, jarInputStreamProvider jprovider)
+ {
+ final StructureViewerModel stateData = viewerData.getValue();
+
+ /*
+ * Search for any viewer windows already open from other alignment views
+ * that exactly match the stored structure state
+ */
+ StructureViewerBase comp = findMatchingViewer(viewerData);
+
+ if (comp != null)
+ {
+ linkStructureViewer(ap, comp, stateData);
+ return;
+ }
+
+ /*
+ * From 2.9: stateData.type contains JMOL or CHIMERA, data is in jar entry
+ * "viewer_"+stateData.viewId
+ */
+ if (ViewerType.CHIMERA.toString().equals(stateData.getType()))
+ {
+ createChimeraViewer(viewerData, af, jprovider);
+ }
+ else
+ {
+ /*
+ * else Jmol (if pre-2.9, stateData contains JMOL state string)
+ */
+ createJmolViewer(viewerData, af, jprovider);
+ }
+ }
+
+ /**
+ * Create a new Chimera viewer.
+ *
+ * @param data
+ * @param af
+ * @param jprovider
+ */
+ protected void createChimeraViewer(
+ Entry<String, StructureViewerModel> viewerData, AlignFrame af,
+ jarInputStreamProvider jprovider)
+ {
+ StructureViewerModel data = viewerData.getValue();
+ String chimeraSessionFile = data.getStateData();
+
+ /*
+ * Copy Chimera session from jar entry "viewer_"+viewId to a temporary file
+ *
+ * NB this is the 'saved' viewId as in the project file XML, _not_ the
+ * 'uniquified' sviewid used to reconstruct the viewer here
+ */
+ String viewerJarEntryName = getViewerJarEntryName(data.getViewId());
+ chimeraSessionFile = copyJarEntry(jprovider, viewerJarEntryName,
+ "chimera", null);
+
+ Set<Entry<File, StructureData>> fileData = data.getFileData()
+ .entrySet();
+ List<PDBEntry> pdbs = new ArrayList<PDBEntry>();
+ List<SequenceI[]> allseqs = new ArrayList<SequenceI[]>();
+ for (Entry<File, StructureData> pdb : fileData)
+ {
+ String filePath = pdb.getValue().getFilePath();
+ String pdbId = pdb.getValue().getPdbId();
+ // pdbs.add(new PDBEntry(filePath, pdbId));
+ pdbs.add(new PDBEntry(pdbId, null, PDBEntry.Type.PDB, filePath));
+ final List<SequenceI> seqList = pdb.getValue().getSeqList();
+ SequenceI[] seqs = seqList.toArray(new SequenceI[seqList.size()]);
+ allseqs.add(seqs);
+ }
+
+ boolean colourByChimera = data.isColourByViewer();
+ boolean colourBySequence = data.isColourWithAlignPanel();
+
+ // TODO use StructureViewer as a factory here, see JAL-1761
+ final PDBEntry[] pdbArray = pdbs.toArray(new PDBEntry[pdbs.size()]);
+ final SequenceI[][] seqsArray = allseqs.toArray(new SequenceI[allseqs
+ .size()][]);
+ String newViewId = viewerData.getKey();
+
+ ChimeraViewFrame cvf = new ChimeraViewFrame(chimeraSessionFile,
+ af.alignPanel, pdbArray, seqsArray, colourByChimera,
+ colourBySequence, newViewId);
+ cvf.setSize(data.getWidth(), data.getHeight());
+ cvf.setLocation(data.getX(), data.getY());
+ }
+
+ /**
+ * Create a new Jmol window. First parse the Jmol state to translate filenames
+ * loaded into the view, and record the order in which files are shown in the
+ * Jmol view, so we can add the sequence mappings in same order.
+ *
+ * @param viewerData
+ * @param af
+ * @param jprovider
+ */
+ protected void createJmolViewer(
+ final Entry<String, StructureViewerModel> viewerData,
+ AlignFrame af, jarInputStreamProvider jprovider)
+ {
+ final StructureViewerModel svattrib = viewerData.getValue();
+ String state = svattrib.getStateData();
+
+ /*
+ * Pre-2.9: state element value is the Jmol state string
+ *
+ * 2.9+: @type is "JMOL", state data is in a Jar file member named "viewer_"
+ * + viewId
+ */
+ if (ViewerType.JMOL.toString().equals(svattrib.getType()))
+ {
+ state = readJarEntry(jprovider,
+ getViewerJarEntryName(svattrib.getViewId()));
+ }
+
+ List<String> pdbfilenames = new ArrayList<String>();
+ List<SequenceI[]> seqmaps = new ArrayList<SequenceI[]>();
+ List<String> pdbids = new ArrayList<String>();
+ StringBuilder newFileLoc = new StringBuilder(64);
+ int cp = 0, ncp, ecp;
+ Map<File, StructureData> oldFiles = svattrib.getFileData();
+ while ((ncp = state.indexOf("load ", cp)) > -1)
+ {
+ do
+ {
+ // look for next filename in load statement
+ newFileLoc.append(state.substring(cp,
+ ncp = (state.indexOf("\"", ncp + 1) + 1)));
+ String oldfilenam = state.substring(ncp,
+ ecp = state.indexOf("\"", ncp));
+ // recover the new mapping data for this old filename
+ // have to normalize filename - since Jmol and jalview do
+ // filename
+ // translation differently.
+ StructureData filedat = oldFiles.get(new File(oldfilenam));
+ if (filedat == null)
+ {
+ String reformatedOldFilename = oldfilenam.replaceAll("/", "\\\\");
+ filedat = oldFiles.get(new File(reformatedOldFilename));
+ }
+ newFileLoc.append(Platform.escapeString(filedat.getFilePath()));
+ pdbfilenames.add(filedat.getFilePath());
+ pdbids.add(filedat.getPdbId());
+ seqmaps.add(filedat.getSeqList().toArray(new SequenceI[0]));
+ newFileLoc.append("\"");
+ cp = ecp + 1; // advance beyond last \" and set cursor so we can
+ // look for next file statement.
+ } while ((ncp = state.indexOf("/*file*/", cp)) > -1);
+ }
+ if (cp > 0)
+ {
+ // just append rest of state
+ newFileLoc.append(state.substring(cp));
+ }
+ else
+ {
+ System.err.print("Ignoring incomplete Jmol state for PDB ids: ");
+ newFileLoc = new StringBuilder(state);
+ newFileLoc.append("; load append ");
+ for (File id : oldFiles.keySet())
+ {
+ // add this and any other pdb files that should be present in
+ // the viewer
+ StructureData filedat = oldFiles.get(id);
+ newFileLoc.append(filedat.getFilePath());
+ pdbfilenames.add(filedat.getFilePath());
+ pdbids.add(filedat.getPdbId());
+ seqmaps.add(filedat.getSeqList().toArray(new SequenceI[0]));
+ newFileLoc.append(" \"");
+ newFileLoc.append(filedat.getFilePath());
+ newFileLoc.append("\"");
+
+ }
+ newFileLoc.append(";");
+ }
+
+ if (newFileLoc.length() == 0)
+ {
+ return;
+ }
+ int histbug = newFileLoc.indexOf("history = ");
+ if (histbug > -1)
+ {
+ /*
+ * change "history = [true|false];" to "history = [1|0];"
+ */
+ histbug += 10;
+ int diff = histbug == -1 ? -1 : newFileLoc.indexOf(";", histbug);
+ String val = (diff == -1) ? null : newFileLoc
+ .substring(histbug, diff);
+ if (val != null && val.length() >= 4)
+ {
+ if (val.contains("e")) // eh? what can it be?
+ {
+ if (val.trim().equals("true"))
+ {
+ val = "1";
+ }
+ else
+ {
+ val = "0";
+ }
+ newFileLoc.replace(histbug, diff, val);
+ }
+ }
+ }
+
+ final String[] pdbf = pdbfilenames.toArray(new String[pdbfilenames
+ .size()]);
+ final String[] id = pdbids.toArray(new String[pdbids.size()]);
+ final SequenceI[][] sq = seqmaps
+ .toArray(new SequenceI[seqmaps.size()][]);
+ final String fileloc = newFileLoc.toString();
+ final String sviewid = viewerData.getKey();
+ final AlignFrame alf = af;
+ final Rectangle rect = new Rectangle(svattrib.getX(), svattrib.getY(),
+ svattrib.getWidth(), svattrib.getHeight());
+ try
+ {
+ javax.swing.SwingUtilities.invokeAndWait(new Runnable()
+ {
+ @Override
+ public void run()
+ {
+ JalviewStructureDisplayI sview = null;
+ try
+ {
+ sview = new StructureViewer(alf.alignPanel
+ .getStructureSelectionManager()).createView(
+ StructureViewer.ViewerType.JMOL, pdbf, id, sq,
+ alf.alignPanel, svattrib, fileloc, rect, sviewid);
+ addNewStructureViewer(sview);
+ } catch (OutOfMemoryError ex)
+ {
+ new OOMWarning("restoring structure view for PDB id " + id,
+ (OutOfMemoryError) ex.getCause());
+ if (sview != null && sview.isVisible())
+ {
+ sview.closeViewer(false);
+ sview.setVisible(false);
+ sview.dispose();
+ }
+ }
+ }
+ });
+ } catch (InvocationTargetException ex)
+ {
+ warn("Unexpected error when opening Jmol view.", ex);
+
+ } catch (InterruptedException e)
+ {
+ // e.printStackTrace();
+ }
+
+ }
+
+ /**
+ * Generates a name for the entry in the project jar file to hold state
+ * information for a structure viewer
+ *
+ * @param viewId
+ * @return
+ */
+ protected String getViewerJarEntryName(String viewId)
+ {
+ return VIEWER_PREFIX + viewId;
+ }
+
+ /**
+ * Returns any open frame that matches given structure viewer data. The match
+ * is based on the unique viewId, or (for older project versions) the frame's
+ * geometry.
+ *
+ * @param viewerData
+ * @return
+ */
+ protected StructureViewerBase findMatchingViewer(
+ Entry<String, StructureViewerModel> viewerData)
+ {
+ final String sviewid = viewerData.getKey();
+ final StructureViewerModel svattrib = viewerData.getValue();
+ StructureViewerBase comp = null;
+ JInternalFrame[] frames = getAllFrames();
+ for (JInternalFrame frame : frames)
+ {
+ if (frame instanceof StructureViewerBase)
+ {
+ /*
+ * Post jalview 2.4 schema includes structure view id
+ */
+ if (sviewid != null
+ && ((StructureViewerBase) frame).getViewId()
+ .equals(sviewid))
+ {
+ comp = (StructureViewerBase) frame;
+ break; // break added in 2.9
+ }
+ /*
+ * Otherwise test for matching position and size of viewer frame
+ */
+ else if (frame.getX() == svattrib.getX()
+ && frame.getY() == svattrib.getY()
+ && frame.getHeight() == svattrib.getHeight()
+ && frame.getWidth() == svattrib.getWidth())
+ {
+ comp = (StructureViewerBase) frame;
+ // no break in faint hope of an exact match on viewId
+ }
+ }
+ }
+ return comp;
+ }
+
+ /**
+ * Link an AlignmentPanel to an existing structure viewer.
+ *
+ * @param ap
+ * @param viewer
+ * @param oldFiles
+ * @param useinViewerSuperpos
+ * @param usetoColourbyseq
+ * @param viewerColouring
+ */
+ protected void linkStructureViewer(AlignmentPanel ap,
+ StructureViewerBase viewer, StructureViewerModel stateData)
+ {
+ // NOTE: if the jalview project is part of a shared session then
+ // view synchronization should/could be done here.
+
+ final boolean useinViewerSuperpos = stateData.isAlignWithPanel();
+ final boolean usetoColourbyseq = stateData.isColourWithAlignPanel();
+ final boolean viewerColouring = stateData.isColourByViewer();
+ Map<File, StructureData> oldFiles = stateData.getFileData();
+
+ /*
+ * Add mapping for sequences in this view to an already open viewer
+ */
+ final AAStructureBindingModel binding = viewer.getBinding();
+ for (File id : oldFiles.keySet())
+ {
+ // add this and any other pdb files that should be present in the
+ // viewer
+ StructureData filedat = oldFiles.get(id);
+ String pdbFile = filedat.getFilePath();
+ SequenceI[] seq = filedat.getSeqList().toArray(new SequenceI[0]);
+ binding.getSsm().setMapping(seq, null, pdbFile, DataSourceType.FILE);
+ binding.addSequenceForStructFile(pdbFile, seq);
+ }
+ // and add the AlignmentPanel's reference to the view panel
+ viewer.addAlignmentPanel(ap);
+ if (useinViewerSuperpos)
+ {
+ viewer.useAlignmentPanelForSuperposition(ap);
+ }
+ else
+ {
+ viewer.excludeAlignmentPanelForSuperposition(ap);
+ }
+ if (usetoColourbyseq)
+ {
+ viewer.useAlignmentPanelForColourbyseq(ap, !viewerColouring);
+ }
+ else
+ {
+ viewer.excludeAlignmentPanelForColourbyseq(ap);
+ }
+ }
+
+ /**
+ * Get all frames within the Desktop.
+ *
+ * @return
+ */
+ protected JInternalFrame[] getAllFrames()
+ {
+ JInternalFrame[] frames = null;
+ // TODO is this necessary - is it safe - risk of hanging?
+ do
+ {
+ try
+ {
+ frames = Desktop.desktop.getAllFrames();
+ } catch (ArrayIndexOutOfBoundsException e)
+ {
+ // occasional No such child exceptions are thrown here...
+ try
+ {
+ Thread.sleep(10);
+ } catch (InterruptedException f)
+ {
+ }
+ }
+ } while (frames == null);
+ return frames;
+ }
+
+ /**
+ * Answers true if 'version' is equal to or later than 'supported', where each
+ * is formatted as major/minor versions like "2.8.3" or "2.3.4b1" for bugfix
+ * changes. Development and test values for 'version' are leniently treated
+ * i.e. answer true.
+ *
+ * @param supported
+ * - minimum version we are comparing against
+ * @param version
+ * - version of data being processsed
+ * @return
+ */
+ public static boolean isVersionStringLaterThan(String supported,
+ String version)
+ {
+ if (supported == null || version == null
+ || version.equalsIgnoreCase("DEVELOPMENT BUILD")
+ || version.equalsIgnoreCase("Test")
+ || version.equalsIgnoreCase("AUTOMATED BUILD"))
+ {
+ System.err.println("Assuming project file with "
+ + (version == null ? "null" : version)
+ + " is compatible with Jalview version " + supported);
+ return true;
+ }
+ else
+ {
+ return StringUtils.compareVersions(version, supported, "b") >= 0;
+ }
+ }
+
+ Vector<JalviewStructureDisplayI> newStructureViewers = null;
+
+ protected void addNewStructureViewer(JalviewStructureDisplayI sview)
+ {
+ if (newStructureViewers != null)
+ {
+ sview.getBinding().setFinishedLoadingFromArchive(false);
+ newStructureViewers.add(sview);
+ }
+ }
+
+ protected void setLoadingFinishedForNewStructureViewers()
+ {
+ if (newStructureViewers != null)
+ {
+ for (JalviewStructureDisplayI sview : newStructureViewers)
+ {
+ sview.getBinding().setFinishedLoadingFromArchive(true);
+ }
+ newStructureViewers.clear();
+ newStructureViewers = null;
+ }
+ }
+
+ AlignFrame loadViewport(String file, JSeq[] JSEQ,
+ List<SequenceI> hiddenSeqs, AlignmentI al,
+ JalviewModelSequence jms, Viewport view, String uniqueSeqSetId,
+ String viewId, List<JvAnnotRow> autoAlan)
+ {
+ AlignFrame af = null;
+ af = new AlignFrame(al, view.getWidth(), view.getHeight(),
+ uniqueSeqSetId, viewId);
+
+ af.setFileName(file, FileFormat.Jalview);
+
+ for (int i = 0; i < JSEQ.length; i++)
+ {
+ af.viewport.setSequenceColour(af.viewport.getAlignment()
+ .getSequenceAt(i), new java.awt.Color(JSEQ[i].getColour()));
+ }
+
+ if (al.hasSeqrep())
+ {
+ af.getViewport().setColourByReferenceSeq(true);
+ af.getViewport().setDisplayReferenceSeq(true);
+ }
+
+ af.viewport.setGatherViewsHere(view.getGatheredViews());
+
+ if (view.getSequenceSetId() != null)
+ {
+ AlignmentViewport av = viewportsAdded.get(uniqueSeqSetId);
+
+ af.viewport.setSequenceSetId(uniqueSeqSetId);
+ if (av != null)
+ {
+ // propagate shared settings to this new view
+ af.viewport.setHistoryList(av.getHistoryList());
+ af.viewport.setRedoList(av.getRedoList());
+ }
+ else
+ {
+ viewportsAdded.put(uniqueSeqSetId, af.viewport);
+ }
+ // TODO: check if this method can be called repeatedly without
+ // side-effects if alignpanel already registered.
+ PaintRefresher.Register(af.alignPanel, uniqueSeqSetId);
+ }
+ // apply Hidden regions to view.
+ if (hiddenSeqs != null)
+ {
+ for (int s = 0; s < JSEQ.length; s++)
+ {
+ SequenceGroup hidden = new SequenceGroup();
+ boolean isRepresentative = false;
+ for (int r = 0; r < JSEQ[s].getHiddenSequencesCount(); r++)
+ {
+ isRepresentative = true;
+ SequenceI sequenceToHide = al.getSequenceAt(JSEQ[s]
+ .getHiddenSequences(r));
+ hidden.addSequence(sequenceToHide, false);
+ // remove from hiddenSeqs list so we don't try to hide it twice
+ hiddenSeqs.remove(sequenceToHide);
+ }
+ if (isRepresentative)
+ {
+ SequenceI representativeSequence = al.getSequenceAt(s);
+ hidden.addSequence(representativeSequence, false);
+ af.viewport.hideRepSequences(representativeSequence, hidden);
+ }
+ }
+
+ SequenceI[] hseqs = hiddenSeqs.toArray(new SequenceI[hiddenSeqs
+ .size()]);
+ af.viewport.hideSequence(hseqs);
+
+ }
+ // recover view properties and display parameters
+ if (view.getViewName() != null)
+ {
+ af.viewport.viewName = view.getViewName();
+ af.setInitialTabVisible();
+ }
+ af.setBounds(view.getXpos(), view.getYpos(), view.getWidth(),
+ view.getHeight());
+
+ af.viewport.setShowAnnotation(view.getShowAnnotation());
+ af.viewport.setAbovePIDThreshold(view.getPidSelected());
+ af.viewport.setThreshold(view.getPidThreshold());
+
+ af.viewport.setColourText(view.getShowColourText());
+
+ af.viewport.setConservationSelected(view.getConservationSelected());
+ af.viewport.setIncrement(view.getConsThreshold());
+ af.viewport.setShowJVSuffix(view.getShowFullId());
+ af.viewport.setRightAlignIds(view.getRightAlignIds());
+ af.viewport.setFont(
+ new java.awt.Font(view.getFontName(), view.getFontStyle(), view
+ .getFontSize()), true);
+ ViewStyleI vs = af.viewport.getViewStyle();
+ vs.setScaleProteinAsCdna(view.isScaleProteinAsCdna());
+ af.viewport.setViewStyle(vs);
+ // TODO: allow custom charWidth/Heights to be restored by updating them
+ // after setting font - which means set above to false
+ af.viewport.setRenderGaps(view.getRenderGaps());
+ af.viewport.setWrapAlignment(view.getWrapAlignment());
+ af.viewport.setShowAnnotation(view.getShowAnnotation());
+
+ af.viewport.setShowBoxes(view.getShowBoxes());
+
+ af.viewport.setShowText(view.getShowText());
+
+ af.viewport.setTextColour(new java.awt.Color(view.getTextCol1()));
+ af.viewport.setTextColour2(new java.awt.Color(view.getTextCol2()));
+ af.viewport.setThresholdTextColour(view.getTextColThreshold());
+ af.viewport.setShowUnconserved(view.hasShowUnconserved() ? view
+ .isShowUnconserved() : false);
+ af.viewport.getRanges().setStartRes(view.getStartRes());
+ af.viewport.getRanges().setStartSeq(view.getStartSeq());
+ af.alignPanel.updateLayout();
+ ColourSchemeI cs = null;
+ // apply colourschemes
+ if (view.getBgColour() != null)
+ {
+ if (view.getBgColour().startsWith("ucs"))
+ {
+ cs = getUserColourScheme(jms, view.getBgColour());
+ }
+ else if (view.getBgColour().startsWith("Annotation"))
+ {
+ AnnotationColours viewAnnColour = view.getAnnotationColours();
+ cs = constructAnnotationColour(viewAnnColour, af, al, jms, true);
+
+ // annpos
+
+ }
+ else
+ {
+ cs = ColourSchemeProperty.getColourScheme(al, view.getBgColour());
+ }
+ }
+
+ af.viewport.setGlobalColourScheme(cs);
+ af.viewport.getResidueShading().setThreshold(
+ view.getPidThreshold(), true);
+ af.viewport.getResidueShading().setConsensus(
+ af.viewport.getSequenceConsensusHash());
+ af.viewport.setColourAppliesToAllGroups(false);
+
+ if (view.getConservationSelected() && cs != null)
+ {
+ af.viewport.getResidueShading().setConservationInc(
+ view.getConsThreshold());
+ }
+
+ af.changeColour(cs);
+
+ af.viewport.setColourAppliesToAllGroups(true);
+
+ af.viewport.setShowSequenceFeatures(view.getShowSequenceFeatures());
+
+ if (view.hasCentreColumnLabels())
+ {
+ af.viewport.setCentreColumnLabels(view.getCentreColumnLabels());
+ }
+ if (view.hasIgnoreGapsinConsensus())
+ {
+ af.viewport.setIgnoreGapsConsensus(view.getIgnoreGapsinConsensus(),
+ null);
+ }
+ if (view.hasFollowHighlight())
+ {
+ af.viewport.setFollowHighlight(view.getFollowHighlight());
+ }
+ if (view.hasFollowSelection())
+ {
+ af.viewport.followSelection = view.getFollowSelection();
+ }
+ if (view.hasShowConsensusHistogram())
+ {
+ af.viewport.setShowConsensusHistogram(view
+ .getShowConsensusHistogram());
+ }
+ else
+ {
+ af.viewport.setShowConsensusHistogram(true);
+ }
+ if (view.hasShowSequenceLogo())
+ {
+ af.viewport.setShowSequenceLogo(view.getShowSequenceLogo());
+ }
+ else
+ {
+ af.viewport.setShowSequenceLogo(false);
+ }
+ if (view.hasNormaliseSequenceLogo())
+ {
+ af.viewport.setNormaliseSequenceLogo(view.getNormaliseSequenceLogo());
+ }
+ if (view.hasShowDbRefTooltip())
+ {
+ af.viewport.setShowDBRefs(view.getShowDbRefTooltip());
+ }
+ if (view.hasShowNPfeatureTooltip())
+ {
+ af.viewport.setShowNPFeats(view.hasShowNPfeatureTooltip());
+ }
+ if (view.hasShowGroupConsensus())
+ {
+ af.viewport.setShowGroupConsensus(view.getShowGroupConsensus());
+ }
+ else
+ {
+ af.viewport.setShowGroupConsensus(false);
+ }
+ if (view.hasShowGroupConservation())
+ {
+ af.viewport.setShowGroupConservation(view.getShowGroupConservation());
+ }
+ else
+ {
+ af.viewport.setShowGroupConservation(false);
+ }
+
+ // recover featre settings
+ if (jms.getFeatureSettings() != null)
+ {
+ FeaturesDisplayed fdi;
+ af.viewport.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
+ String[] renderOrder = new String[jms.getFeatureSettings()
+ .getSettingCount()];
+ Map<String, FeatureColourI> featureColours = new Hashtable<String, FeatureColourI>();
+ Map<String, Float> featureOrder = new Hashtable<String, Float>();
+
+ for (int fs = 0; fs < jms.getFeatureSettings().getSettingCount(); fs++)
+ {
+ Setting setting = jms.getFeatureSettings().getSetting(fs);
+ if (setting.hasMincolour())
+ {
+ FeatureColourI gc = setting.hasMin() ? new FeatureColour(
+ new Color(setting.getMincolour()), new Color(
+ setting.getColour()), setting.getMin(),
+ setting.getMax()) : new FeatureColour(new Color(
+ setting.getMincolour()), new Color(setting.getColour()),
+ 0, 1);
+ if (setting.hasThreshold())
+ {
+ gc.setThreshold(setting.getThreshold());
+ int threshstate = setting.getThreshstate();
+ // -1 = None, 0 = Below, 1 = Above threshold
+ if (threshstate == 0)
+ {
+ gc.setBelowThreshold(true);
+ }
+ else if (threshstate == 1)
+ {
+ gc.setAboveThreshold(true);
+ }
+ }
+ gc.setAutoScaled(true); // default
+ if (setting.hasAutoScale())
+ {
+ gc.setAutoScaled(setting.getAutoScale());
+ }
+ if (setting.hasColourByLabel())
+ {
+ gc.setColourByLabel(setting.getColourByLabel());
+ }
+ // and put in the feature colour table.
+ featureColours.put(setting.getType(), gc);
+ }
+ else
+ {
+ featureColours.put(setting.getType(), new FeatureColour(
+ new Color(setting.getColour())));
+ }
+ renderOrder[fs] = setting.getType();
+ if (setting.hasOrder())
+ {
+ featureOrder.put(setting.getType(), setting.getOrder());
+ }
+ else
+ {
+ featureOrder.put(setting.getType(), new Float(fs
+ / jms.getFeatureSettings().getSettingCount()));
+ }
+ if (setting.getDisplay())
+ {
+ fdi.setVisible(setting.getType());
+ }
+ }
+ Map<String, Boolean> fgtable = new Hashtable<String, Boolean>();
+ for (int gs = 0; gs < jms.getFeatureSettings().getGroupCount(); gs++)
+ {
+ Group grp = jms.getFeatureSettings().getGroup(gs);
+ fgtable.put(grp.getName(), new Boolean(grp.getDisplay()));
+ }
+ // FeatureRendererSettings frs = new FeatureRendererSettings(renderOrder,
+ // fgtable, featureColours, jms.getFeatureSettings().hasTransparency() ?
+ // jms.getFeatureSettings().getTransparency() : 0.0, featureOrder);
+ FeatureRendererSettings frs = new FeatureRendererSettings(
+ renderOrder, fgtable, featureColours, 1.0f, featureOrder);
+ af.alignPanel.getSeqPanel().seqCanvas.getFeatureRenderer()
+ .transferSettings(frs);
+
+ }
+
+ if (view.getHiddenColumnsCount() > 0)
+ {
+ for (int c = 0; c < view.getHiddenColumnsCount(); c++)
+ {
+ af.viewport.hideColumns(view.getHiddenColumns(c).getStart(), view
+ .getHiddenColumns(c).getEnd() // +1
+ );
+ }
+ }
+ if (view.getCalcIdParam() != null)
+ {
+ for (CalcIdParam calcIdParam : view.getCalcIdParam())
+ {
+ if (calcIdParam != null)
+ {
+ if (recoverCalcIdParam(calcIdParam, af.viewport))
+ {
+ }
+ else
+ {
+ warn("Couldn't recover parameters for "
+ + calcIdParam.getCalcId());
+ }
+ }
+ }
+ }
+ af.setMenusFromViewport(af.viewport);
+ af.setTitle(view.getTitle());
+ // TODO: we don't need to do this if the viewport is aready visible.
+ /*
+ * Add the AlignFrame to the desktop (it may be 'gathered' later), unless it
+ * has a 'cdna/protein complement' view, in which case save it in order to
+ * populate a SplitFrame once all views have been read in.
+ */
+ String complementaryViewId = view.getComplementId();
+ if (complementaryViewId == null)
+ {
+ Desktop.addInternalFrame(af, view.getTitle(), view.getWidth(),
+ view.getHeight());
+ // recompute any autoannotation
+ af.alignPanel.updateAnnotation(false, true);
+ reorderAutoannotation(af, al, autoAlan);
+ af.alignPanel.alignmentChanged();
+ }
+ else
+ {
+ splitFrameCandidates.put(view, af);
+ }
+ return af;
+ }
+
+ /**
+ * Reads saved data to restore Colour by Annotation settings
+ *
+ * @param viewAnnColour
+ * @param af
+ * @param al
+ * @param jms
+ * @param checkGroupAnnColour
+ * @return
+ */
+ private ColourSchemeI constructAnnotationColour(
+ AnnotationColours viewAnnColour, AlignFrame af, AlignmentI al,
+ JalviewModelSequence jms, boolean checkGroupAnnColour)
+ {
+ boolean propagateAnnColour = false;
+ AlignmentI annAlignment = af != null ? af.viewport.getAlignment() : al;
+ if (checkGroupAnnColour && al.getGroups() != null
+ && al.getGroups().size() > 0)
+ {
+ // pre 2.8.1 behaviour
+ // check to see if we should transfer annotation colours
+ propagateAnnColour = true;
+ for (SequenceGroup sg : al.getGroups())
+ {
+ if (sg.getColourScheme() instanceof AnnotationColourGradient)
+ {
+ propagateAnnColour = false;
+ }
+ }
+ }
+
+ /*
+ * 2.10.2- : saved annotationId is AlignmentAnnotation.annotationId
+ */
+ String annotationId = viewAnnColour.getAnnotation();
+ AlignmentAnnotation matchedAnnotation = annotationIds.get(annotationId);
+
+ /*
+ * pre 2.10.2: saved annotationId is AlignmentAnnotation.label
+ */
+ if (matchedAnnotation == null && annAlignment.getAlignmentAnnotation() != null)
+ {
+ for (int i = 0; i < annAlignment.getAlignmentAnnotation().length; i++)
+ {
+ if (annotationId
+ .equals(annAlignment.getAlignmentAnnotation()[i].label))
+ {
+ matchedAnnotation = annAlignment.getAlignmentAnnotation()[i];
+ break;
+ }
+ }
+ }
+ if (matchedAnnotation == null)
+ {
+ System.err.println("Failed to match annotation colour scheme for "
+ + annotationId);
+ return null;
+ }
+ if (matchedAnnotation.getThreshold() == null)
+ {
+ matchedAnnotation.setThreshold(new GraphLine(viewAnnColour.getThreshold(),
+ "Threshold", Color.black));
+ }
+
+ AnnotationColourGradient cs = null;
+ if (viewAnnColour.getColourScheme().equals("None"))
+ {
+ cs = new AnnotationColourGradient(matchedAnnotation, new Color(
+ viewAnnColour.getMinColour()), new Color(
+ viewAnnColour.getMaxColour()),
+ viewAnnColour.getAboveThreshold());
+ }
+ else if (viewAnnColour.getColourScheme().startsWith("ucs"))
+ {
+ cs = new AnnotationColourGradient(matchedAnnotation, getUserColourScheme(
+ jms, viewAnnColour.getColourScheme()),
+ viewAnnColour.getAboveThreshold());
+ }
+ else
+ {
+ cs = new AnnotationColourGradient(matchedAnnotation,
+ ColourSchemeProperty.getColourScheme(al,
+ viewAnnColour.getColourScheme()),
+ viewAnnColour.getAboveThreshold());
+ }
+
+ boolean perSequenceOnly = viewAnnColour.isPerSequence();
+ boolean useOriginalColours = viewAnnColour.isPredefinedColours();
+ cs.setSeqAssociated(perSequenceOnly);
+ cs.setPredefinedColours(useOriginalColours);
+
+ if (propagateAnnColour && al.getGroups() != null)
+ {
+ // Also use these settings for all the groups
+ for (int g = 0; g < al.getGroups().size(); g++)
+ {
+ SequenceGroup sg = al.getGroups().get(g);
+ if (sg.getGroupColourScheme() == null)
+ {
+ continue;
+ }
+
+ AnnotationColourGradient groupScheme = new AnnotationColourGradient(
+ matchedAnnotation, sg.getColourScheme(),
+ viewAnnColour.getAboveThreshold());
+ sg.setColourScheme(groupScheme);
+ groupScheme.setSeqAssociated(perSequenceOnly);
+ groupScheme.setPredefinedColours(useOriginalColours);
+ }
+ }
+ return cs;
+ }
+
+ private void reorderAutoannotation(AlignFrame af, AlignmentI al,
+ List<JvAnnotRow> autoAlan)
+ {
+ // copy over visualization settings for autocalculated annotation in the
+ // view
+ if (al.getAlignmentAnnotation() != null)
+ {
+ /**
+ * Kludge for magic autoannotation names (see JAL-811)
+ */
+ String[] magicNames = new String[] { "Consensus", "Quality",
+ "Conservation" };
+ JvAnnotRow nullAnnot = new JvAnnotRow(-1, null);
+ Hashtable<String, JvAnnotRow> visan = new Hashtable<String, JvAnnotRow>();
+ for (String nm : magicNames)
+ {
+ visan.put(nm, nullAnnot);
+ }
+ for (JvAnnotRow auan : autoAlan)
+ {
+ visan.put(auan.template.label
+ + (auan.template.getCalcId() == null ? "" : "\t"
+ + auan.template.getCalcId()), auan);
+ }
+ int hSize = al.getAlignmentAnnotation().length;
+ List<JvAnnotRow> reorder = new ArrayList<JvAnnotRow>();
+ // work through any autoCalculated annotation already on the view
+ // removing it if it should be placed in a different location on the
+ // annotation panel.
+ List<String> remains = new ArrayList<String>(visan.keySet());
+ for (int h = 0; h < hSize; h++)
+ {
+ jalview.datamodel.AlignmentAnnotation jalan = al
+ .getAlignmentAnnotation()[h];
+ if (jalan.autoCalculated)
+ {
+ String k;
+ JvAnnotRow valan = visan.get(k = jalan.label);
+ if (jalan.getCalcId() != null)
+ {
+ valan = visan.get(k = jalan.label + "\t" + jalan.getCalcId());
+ }
+
+ if (valan != null)
+ {
+ // delete the auto calculated row from the alignment
+ al.deleteAnnotation(jalan, false);
+ remains.remove(k);
+ hSize--;
+ h--;
+ if (valan != nullAnnot)
+ {
+ if (jalan != valan.template)
+ {
+ // newly created autoannotation row instance
+ // so keep a reference to the visible annotation row
+ // and copy over all relevant attributes
+ if (valan.template.graphHeight >= 0)
+
+ {
+ jalan.graphHeight = valan.template.graphHeight;
+ }
+ jalan.visible = valan.template.visible;
+ }
+ reorder.add(new JvAnnotRow(valan.order, jalan));
+ }
+ }
+ }
+ }
+ // Add any (possibly stale) autocalculated rows that were not appended to
+ // the view during construction
+ for (String other : remains)
+ {
+ JvAnnotRow othera = visan.get(other);
+ if (othera != nullAnnot && othera.template.getCalcId() != null
+ && othera.template.getCalcId().length() > 0)
+ {
+ reorder.add(othera);
+ }
+ }
+ // now put the automatic annotation in its correct place
+ int s = 0, srt[] = new int[reorder.size()];
+ JvAnnotRow[] rws = new JvAnnotRow[reorder.size()];
+ for (JvAnnotRow jvar : reorder)
+ {
+ rws[s] = jvar;
+ srt[s++] = jvar.order;
+ }
+ reorder.clear();
+ jalview.util.QuickSort.sort(srt, rws);
+ // and re-insert the annotation at its correct position
+ for (JvAnnotRow jvar : rws)
+ {
+ al.addAnnotation(jvar.template, jvar.order);
+ }
+ af.alignPanel.adjustAnnotationHeight();
+ }
+ }
+
+ Hashtable skipList = null;
+
+ /**
+ * TODO remove this method
+ *
+ * @param view
+ * @return AlignFrame bound to sequenceSetId from view, if one exists. private
+ * AlignFrame getSkippedFrame(Viewport view) { if (skipList==null) {
+ * throw new Error("Implementation Error. No skipList defined for this
+ * Jalview2XML instance."); } return (AlignFrame)
+ * skipList.get(view.getSequenceSetId()); }
+ */
+
+ /**
+ * Check if the Jalview view contained in object should be skipped or not.
+ *
+ * @param object
+ * @return true if view's sequenceSetId is a key in skipList
+ */
+ private boolean skipViewport(JalviewModel object)
+ {
+ if (skipList == null)
+ {
+ return false;
+ }
+ String id;
+ if (skipList.containsKey(id = object.getJalviewModelSequence()
+ .getViewport()[0].getSequenceSetId()))
+ {
+ if (Cache.log != null && Cache.log.isDebugEnabled())
+ {
+ Cache.log.debug("Skipping seuqence set id " + id);
+ }
+ return true;
+ }
+ return false;
+ }
+
+ public void addToSkipList(AlignFrame af)
+ {
+ if (skipList == null)
+ {
+ skipList = new Hashtable();
+ }
+ skipList.put(af.getViewport().getSequenceSetId(), af);
+ }
+
+ public void clearSkipList()
+ {
+ if (skipList != null)
+ {
+ skipList.clear();
+ skipList = null;
+ }
+ }
+
+ private void recoverDatasetFor(SequenceSet vamsasSet, AlignmentI al,
+ boolean ignoreUnrefed)
+ {
+ jalview.datamodel.AlignmentI ds = getDatasetFor(vamsasSet
+ .getDatasetId());
+ Vector dseqs = null;
+ if (ds == null)
+ {
+ // create a list of new dataset sequences
+ dseqs = new Vector();
+ }
+ for (int i = 0, iSize = vamsasSet.getSequenceCount(); i < iSize; i++)
+ {
+ Sequence vamsasSeq = vamsasSet.getSequence(i);
+ ensureJalviewDatasetSequence(vamsasSeq, ds, dseqs, ignoreUnrefed, i);
+ }
+ // create a new dataset
+ if (ds == null)
+ {
+ SequenceI[] dsseqs = new SequenceI[dseqs.size()];
+ dseqs.copyInto(dsseqs);
+ ds = new jalview.datamodel.Alignment(dsseqs);
+ debug("Created new dataset " + vamsasSet.getDatasetId()
+ + " for alignment " + System.identityHashCode(al));
+ addDatasetRef(vamsasSet.getDatasetId(), ds);
+ }
+ // set the dataset for the newly imported alignment.
+ if (al.getDataset() == null && !ignoreUnrefed)
+ {
+ al.setDataset(ds);
+ }
+ }
+
+ /**
+ *
+ * @param vamsasSeq
+ * sequence definition to create/merge dataset sequence for
+ * @param ds
+ * dataset alignment
+ * @param dseqs
+ * vector to add new dataset sequence to
+ * @param ignoreUnrefed
+ * - when true, don't create new sequences from vamsasSeq if it's id
+ * doesn't already have an asssociated Jalview sequence.
+ * @param vseqpos
+ * - used to reorder the sequence in the alignment according to the
+ * vamsasSeq array ordering, to preserve ordering of dataset
+ */
+ private void ensureJalviewDatasetSequence(Sequence vamsasSeq,
+ AlignmentI ds, Vector dseqs, boolean ignoreUnrefed, int vseqpos)
+ {
+ // JBP TODO: Check this is called for AlCodonFrames to support recovery of
+ // xRef Codon Maps
+ SequenceI sq = seqRefIds.get(vamsasSeq.getId());
+ boolean reorder = false;
+ SequenceI dsq = null;
+ if (sq != null && sq.getDatasetSequence() != null)
+ {
+ dsq = sq.getDatasetSequence();
+ }
+ else
+ {
+ reorder = true;
+ }
+ if (sq == null && ignoreUnrefed)
+ {
+ return;
+ }
+ String sqid = vamsasSeq.getDsseqid();
+ if (dsq == null)
+ {
+ // need to create or add a new dataset sequence reference to this sequence
+ if (sqid != null)
+ {
+ dsq = seqRefIds.get(sqid);
+ }
+ // check again
+ if (dsq == null)
+ {
+ // make a new dataset sequence
+ dsq = sq.createDatasetSequence();
+ if (sqid == null)
+ {
+ // make up a new dataset reference for this sequence
+ sqid = seqHash(dsq);
+ }
+ dsq.setVamsasId(uniqueSetSuffix + sqid);
+ seqRefIds.put(sqid, dsq);
+ if (ds == null)
+ {
+ if (dseqs != null)
+ {
+ dseqs.addElement(dsq);
+ }
+ }
+ else
+ {
+ ds.addSequence(dsq);
+ }
+ }
+ else
+ {
+ if (sq != dsq)
+ { // make this dataset sequence sq's dataset sequence
+ sq.setDatasetSequence(dsq);
+ // and update the current dataset alignment
+ if (ds == null)
+ {
+ if (dseqs != null)
+ {
+ if (!dseqs.contains(dsq))
+ {
+ dseqs.add(dsq);
+ }
+ }
+ else
+ {
+ if (ds.findIndex(dsq) < 0)
+ {
+ ds.addSequence(dsq);
+ }
+ }
+ }
+ }
+ }
+ }
+ // TODO: refactor this as a merge dataset sequence function
+ // now check that sq (the dataset sequence) sequence really is the union of
+ // all references to it
+ // boolean pre = sq.getStart() < dsq.getStart();
+ // boolean post = sq.getEnd() > dsq.getEnd();
+ // if (pre || post)
+ if (sq != dsq)
+ {
+ // StringBuffer sb = new StringBuffer();
+ String newres = jalview.analysis.AlignSeq.extractGaps(
+ jalview.util.Comparison.GapChars, sq.getSequenceAsString());
+ if (!newres.equalsIgnoreCase(dsq.getSequenceAsString())
+ && newres.length() > dsq.getLength())
+ {
+ // Update with the longer sequence.
+ synchronized (dsq)
+ {
+ /*
+ * if (pre) { sb.insert(0, newres .substring(0, dsq.getStart() -
+ * sq.getStart())); dsq.setStart(sq.getStart()); } if (post) {
+ * sb.append(newres.substring(newres.length() - sq.getEnd() -
+ * dsq.getEnd())); dsq.setEnd(sq.getEnd()); }
+ */
+ dsq.setSequence(newres);
+ }
+ // TODO: merges will never happen if we 'know' we have the real dataset
+ // sequence - this should be detected when id==dssid
+ System.err
+ .println("DEBUG Notice: Merged dataset sequence (if you see this often, post at http://issues.jalview.org/browse/JAL-1474)"); // ("
+ // + (pre ? "prepended" : "") + " "
+ // + (post ? "appended" : ""));
+ }
+ }
+ else
+ {
+ // sequence refs are identical. We may need to update the existing dataset
+ // alignment with this one, though.
+ if (ds != null && dseqs == null)
+ {
+ int opos = ds.findIndex(dsq);
+ SequenceI tseq = null;
+ if (opos != -1 && vseqpos != opos)
+ {
+ // remove from old position
+ ds.deleteSequence(dsq);
+ }
+ if (vseqpos < ds.getHeight())
+ {
+ if (vseqpos != opos)
+ {
+ // save sequence at destination position
+ tseq = ds.getSequenceAt(vseqpos);
+ ds.replaceSequenceAt(vseqpos, dsq);
+ ds.addSequence(tseq);
+ }
+ }
+ else
+ {
+ ds.addSequence(dsq);
+ }
+ }
+ }
+ }
+
+ /*
+ * TODO use AlignmentI here and in related methods - needs
+ * AlignmentI.getDataset() changed to return AlignmentI instead of Alignment
+ */
+ Hashtable<String, AlignmentI> datasetIds = null;
+
+ IdentityHashMap<AlignmentI, String> dataset2Ids = null;
+
+ private AlignmentI getDatasetFor(String datasetId)
+ {
+ if (datasetIds == null)
+ {
+ datasetIds = new Hashtable<String, AlignmentI>();
+ return null;
+ }
+ if (datasetIds.containsKey(datasetId))
+ {
+ return datasetIds.get(datasetId);
+ }
+ return null;
+ }
+
+ private void addDatasetRef(String datasetId, AlignmentI dataset)
+ {
+ if (datasetIds == null)
+ {
+ datasetIds = new Hashtable<String, AlignmentI>();
+ }
+ datasetIds.put(datasetId, dataset);
+ }
+
+ /**
+ * make a new dataset ID for this jalview dataset alignment
+ *
+ * @param dataset
+ * @return
+ */
+ private String getDatasetIdRef(AlignmentI dataset)
+ {
+ if (dataset.getDataset() != null)
+ {
+ warn("Serious issue! Dataset Object passed to getDatasetIdRef is not a Jalview DATASET alignment...");
+ }
+ String datasetId = makeHashCode(dataset, null);
+ if (datasetId == null)
+ {
+ // make a new datasetId and record it
+ if (dataset2Ids == null)
+ {
+ dataset2Ids = new IdentityHashMap<AlignmentI, String>();
+ }
+ else
+ {
+ datasetId = dataset2Ids.get(dataset);
+ }
+ if (datasetId == null)
+ {
+ datasetId = "ds" + dataset2Ids.size() + 1;
+ dataset2Ids.put(dataset, datasetId);
+ }
+ }
+ return datasetId;
+ }
+
+ private void addDBRefs(SequenceI datasetSequence, Sequence sequence)
+ {
+ for (int d = 0; d < sequence.getDBRefCount(); d++)
+ {
+ DBRef dr = sequence.getDBRef(d);
+ jalview.datamodel.DBRefEntry entry = new jalview.datamodel.DBRefEntry(
+ sequence.getDBRef(d).getSource(), sequence.getDBRef(d)
+ .getVersion(), sequence.getDBRef(d).getAccessionId());
+ if (dr.getMapping() != null)
+ {
+ entry.setMap(addMapping(dr.getMapping()));
+ }
+ datasetSequence.addDBRef(entry);
+ }
+ }
+
+ private jalview.datamodel.Mapping addMapping(Mapping m)
+ {
+ SequenceI dsto = null;
+ // Mapping m = dr.getMapping();
+ int fr[] = new int[m.getMapListFromCount() * 2];
+ Enumeration f = m.enumerateMapListFrom();
+ for (int _i = 0; f.hasMoreElements(); _i += 2)
+ {
+ MapListFrom mf = (MapListFrom) f.nextElement();
+ fr[_i] = mf.getStart();
+ fr[_i + 1] = mf.getEnd();
+ }
+ int fto[] = new int[m.getMapListToCount() * 2];
+ f = m.enumerateMapListTo();
+ for (int _i = 0; f.hasMoreElements(); _i += 2)
+ {
+ MapListTo mf = (MapListTo) f.nextElement();
+ fto[_i] = mf.getStart();
+ fto[_i + 1] = mf.getEnd();
+ }
+ jalview.datamodel.Mapping jmap = new jalview.datamodel.Mapping(dsto,
+ fr, fto, (int) m.getMapFromUnit(), (int) m.getMapToUnit());
+ if (m.getMappingChoice() != null)
+ {
+ MappingChoice mc = m.getMappingChoice();
+ if (mc.getDseqFor() != null)
+ {
+ String dsfor = "" + mc.getDseqFor();
+ if (seqRefIds.containsKey(dsfor))
+ {
+ /**
+ * recover from hash
+ */
+ jmap.setTo(seqRefIds.get(dsfor));
+ }
+ else
+ {
+ frefedSequence.add(newMappingRef(dsfor, jmap));
+ }
+ }
+ else
+ {
+ /**
+ * local sequence definition
+ */
+ Sequence ms = mc.getSequence();
+ SequenceI djs = null;
+ String sqid = ms.getDsseqid();
+ if (sqid != null && sqid.length() > 0)
+ {
+ /*
+ * recover dataset sequence
+ */
+ djs = seqRefIds.get(sqid);
+ }
+ else
+ {
+ System.err
+ .println("Warning - making up dataset sequence id for DbRef sequence map reference");
+ sqid = ((Object) ms).toString(); // make up a new hascode for
+ // undefined dataset sequence hash
+ // (unlikely to happen)
+ }
+
+ if (djs == null)
+ {
+ /**
+ * make a new dataset sequence and add it to refIds hash
+ */
+ djs = new jalview.datamodel.Sequence(ms.getName(),
+ ms.getSequence());
+ djs.setStart(jmap.getMap().getToLowest());
+ djs.setEnd(jmap.getMap().getToHighest());
+ djs.setVamsasId(uniqueSetSuffix + sqid);
+ jmap.setTo(djs);
+ incompleteSeqs.put(sqid, djs);
+ seqRefIds.put(sqid, djs);
+
+ }
+ jalview.bin.Cache.log.debug("about to recurse on addDBRefs.");
+ addDBRefs(djs, ms);
+
+ }
+ }
+ return (jmap);
+
+ }
+
+ public jalview.gui.AlignmentPanel copyAlignPanel(AlignmentPanel ap,
+ boolean keepSeqRefs)
+ {
+ initSeqRefs();
+ JalviewModel jm = saveState(ap, null, null, null);
+
+ if (!keepSeqRefs)
+ {
+ clearSeqRefs();
+ jm.getJalviewModelSequence().getViewport(0).setSequenceSetId(null);
+ }
+ else
+ {
+ uniqueSetSuffix = "";
+ jm.getJalviewModelSequence().getViewport(0).setId(null); // we don't
+ // overwrite the
+ // view we just
+ // copied
+ }
+ if (this.frefedSequence == null)
+ {
+ frefedSequence = new Vector();
+ }
+
+ viewportsAdded.clear();
+
+ AlignFrame af = loadFromObject(jm, null, false, null);
+ af.alignPanels.clear();
+ af.closeMenuItem_actionPerformed(true);
+
+ /*
+ * if(ap.av.getAlignment().getAlignmentAnnotation()!=null) { for(int i=0;
+ * i<ap.av.getAlignment().getAlignmentAnnotation().length; i++) {
+ * if(!ap.av.getAlignment().getAlignmentAnnotation()[i].autoCalculated) {
+ * af.alignPanel.av.getAlignment().getAlignmentAnnotation()[i] =
+ * ap.av.getAlignment().getAlignmentAnnotation()[i]; } } }
+ */
+
+ return af.alignPanel;
+ }
+
+ /**
+ * flag indicating if hashtables should be cleared on finalization TODO this
+ * flag may not be necessary
+ */
+ private final boolean _cleartables = true;
+
+ private Hashtable jvids2vobj;
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#finalize()
+ */
+ @Override
+ protected void finalize() throws Throwable
+ {
+ // really make sure we have no buried refs left.
+ if (_cleartables)
+ {
+ clearSeqRefs();
+ }
+ this.seqRefIds = null;
+ this.seqsToIds = null;
+ super.finalize();
+ }
+
+ private void warn(String msg)
+ {
+ warn(msg, null);
+ }
+
+ private void warn(String msg, Exception e)
+ {
+ if (Cache.log != null)
+ {
+ if (e != null)
+ {
+ Cache.log.warn(msg, e);
+ }
+ else
+ {
+ Cache.log.warn(msg);
+ }
+ }
+ else
+ {
+ System.err.println("Warning: " + msg);
+ if (e != null)
+ {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ private void debug(String string)
+ {
+ debug(string, null);
+ }
+
+ private void debug(String msg, Exception e)
+ {
+ if (Cache.log != null)
+ {
+ if (e != null)
+ {
+ Cache.log.debug(msg, e);
+ }
+ else
+ {
+ Cache.log.debug(msg);
+ }
+ }
+ else
+ {
+ System.err.println("Warning: " + msg);
+ if (e != null)
+ {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ /**
+ * set the object to ID mapping tables used to write/recover objects and XML
+ * ID strings for the jalview project. If external tables are provided then
+ * finalize and clearSeqRefs will not clear the tables when the Jalview2XML
+ * object goes out of scope. - also populates the datasetIds hashtable with
+ * alignment objects containing dataset sequences
+ *
+ * @param vobj2jv
+ * Map from ID strings to jalview datamodel
+ * @param jv2vobj
+ * Map from jalview datamodel to ID strings
+ *
+ *
+ */
+ public void setObjectMappingTables(Hashtable vobj2jv,
+ IdentityHashMap jv2vobj)
+ {
+ this.jv2vobj = jv2vobj;
+ this.vobj2jv = vobj2jv;
+ Iterator ds = jv2vobj.keySet().iterator();
+ String id;
+ while (ds.hasNext())
+ {
+ Object jvobj = ds.next();
+ id = jv2vobj.get(jvobj).toString();
+ if (jvobj instanceof jalview.datamodel.Alignment)
+ {
+ if (((jalview.datamodel.Alignment) jvobj).getDataset() == null)
+ {
+ addDatasetRef(id, (jalview.datamodel.Alignment) jvobj);
+ }
+ }
+ else if (jvobj instanceof jalview.datamodel.Sequence)
+ {
+ // register sequence object so the XML parser can recover it.
+ if (seqRefIds == null)
+ {
+ seqRefIds = new HashMap<String, SequenceI>();
+ }
+ if (seqsToIds == null)
+ {
+ seqsToIds = new IdentityHashMap<SequenceI, String>();
+ }
+ seqRefIds.put(jv2vobj.get(jvobj).toString(), (SequenceI) jvobj);
+ seqsToIds.put((SequenceI) jvobj, id);
+ }
+ else if (jvobj instanceof jalview.datamodel.AlignmentAnnotation)
+ {
+ String anid;
+ AlignmentAnnotation jvann = (AlignmentAnnotation) jvobj;
+ annotationIds.put(anid = jv2vobj.get(jvobj).toString(), jvann);
+ if (jvann.annotationId == null)
+ {
+ jvann.annotationId = anid;
+ }
+ if (!jvann.annotationId.equals(anid))
+ {
+ // TODO verify that this is the correct behaviour
+ this.warn("Overriding Annotation ID for " + anid
+ + " from different id : " + jvann.annotationId);
+ jvann.annotationId = anid;
+ }
+ }
+ else if (jvobj instanceof String)
+ {
+ if (jvids2vobj == null)
+ {
+ jvids2vobj = new Hashtable();
+ jvids2vobj.put(jvobj, jv2vobj.get(jvobj).toString());
+ }
+ }
+ else
+ {
+ Cache.log.debug("Ignoring " + jvobj.getClass() + " (ID = " + id);
+ }
+ }
+ }
+
+ /**
+ * set the uniqueSetSuffix used to prefix/suffix object IDs for jalview
+ * objects created from the project archive. If string is null (default for
+ * construction) then suffix will be set automatically.
+ *
+ * @param string
+ */
+ public void setUniqueSetSuffix(String string)
+ {
+ uniqueSetSuffix = string;
+
+ }
+
+ /**
+ * uses skipList2 as the skipList for skipping views on sequence sets
+ * associated with keys in the skipList
+ *
+ * @param skipList2
+ */
+ public void setSkipList(Hashtable skipList2)
+ {
+ skipList = skipList2;
+ }
+
+ /**
+ * Reads the jar entry of given name and returns its contents, or null if the
+ * entry is not found.
+ *
+ * @param jprovider
+ * @param jarEntryName
+ * @return
+ */
+ protected String readJarEntry(jarInputStreamProvider jprovider,
+ String jarEntryName)
+ {
+ String result = null;
+ BufferedReader in = null;
+
+ try
+ {
+ /*
+ * Reopen the jar input stream and traverse its entries to find a matching
+ * name
+ */
+ JarInputStream jin = jprovider.getJarInputStream();
+ JarEntry entry = null;
+ do
+ {
+ entry = jin.getNextJarEntry();
+ } while (entry != null && !entry.getName().equals(jarEntryName));
+
+ if (entry != null)
+ {
+ StringBuilder out = new StringBuilder(256);
+ in = new BufferedReader(new InputStreamReader(jin, UTF_8));
+ String data;
+
+ while ((data = in.readLine()) != null)
+ {
+ out.append(data);
+ }
+ result = out.toString();
+ }
+ else
+ {
+ warn("Couldn't find entry in Jalview Jar for " + jarEntryName);
+ }
+ } catch (Exception ex)
+ {
+ ex.printStackTrace();
+ } finally
+ {
+ if (in != null)
+ {
+ try
+ {
+ in.close();
+ } catch (IOException e)
+ {
+ // ignore
+ }
+ }
+ }
+
+ return result;
+ }
+
+ /**
+ * Returns an incrementing counter (0, 1, 2...)
+ *
+ * @return
+ */
+ private synchronized int nextCounter()
+ {
+ return counter++;
+ }
+}