+ 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");
+ 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()));
+ 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());
+ 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");
+
+ 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));
+ 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)