+ /*
+ * 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, svattrib);
+ return;
+ }
+
+ /*
+ * Pending an XML element for ViewerType, just check if stateData contains
+ * "chimera" (part of the chimera session filename).
+ */
+ if (svattrib.getStateData().indexOf("chimera") > -1)
+ {
+ createChimeraViewer(viewerData, af);
+ }
+ else
+ {
+ createJmolViewer(viewerData, af);
+ }
+ }
+
+ /**
+ * Create a new Chimera viewer.
+ *
+ * @param viewerData
+ * @param af
+ */
+ protected void createChimeraViewer(Entry<String, StructureViewerModel> viewerData,
+ AlignFrame af)
+ {
+ final StructureViewerModel data = viewerData.getValue();
+ String chimeraSession = data.getStateData();
+
+ if (new File(chimeraSession).exists())
+ {
+ 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));
+ 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 can/should this be done via StructureViewer (like Jmol)?
+ final PDBEntry[] pdbArray = pdbs.toArray(new PDBEntry[pdbs
+ .size()]);
+ final SequenceI[][] seqsArray = allseqs.toArray(new SequenceI[allseqs.size()][]);
+ new ChimeraViewFrame(chimeraSession, af.alignPanel, pdbArray,
+ seqsArray,
+ colourByChimera, colourBySequence);
+ }
+ else
+ {
+ Cache.log.error("Chimera session file " + chimeraSession
+ + " not found");
+ }
+ }
+
+ /**
+ * 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
+ */
+ protected void createJmolViewer(
+ final Entry<String, StructureViewerModel> viewerData, AlignFrame af)
+ {
+ final StructureViewerModel svattrib = viewerData.getValue();
+ String state = svattrib.getStateData();
+ 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("\"");