+ MapListFrom mfrom = new MapListFrom();
+ mfrom.setStart(range[0]);
+ mfrom.setEnd(range[1]);
+ mp.addMapListFrom(mfrom);
+ }
+ r = mlst.getToRanges();
+ for (int[] range : r)
+ {
+ MapListTo mto = new MapListTo();
+ mto.setStart(range[0]);
+ mto.setEnd(range[1]);
+ mp.addMapListTo(mto);
+ }
+ mp.setMapFromUnit(mlst.getFromRatio());
+ mp.setMapToUnit(mlst.getToRatio());
+ if (jmp.getTo() != null)
+ {
+ MappingChoice mpc = new MappingChoice();
+ if (recurse
+ && (parentseq != jmp.getTo() || parentseq
+ .getDatasetSequence() != jmp.getTo()))
+ {
+ mpc.setSequence(createVamsasSequence(false, seqHash(jmp.getTo()),
+ jmp.getTo(), jds));
+ }
+ else
+ {
+ String jmpid = "";
+ SequenceI ps = null;
+ if (parentseq != jmp.getTo()
+ && parentseq.getDatasetSequence() != jmp.getTo())
+ {
+ // chaining dbref rather than a handshaking one
+ jmpid = seqHash(ps = jmp.getTo());
+ }
+ else
+ {
+ jmpid = seqHash(ps = parentseq);
+ }
+ mpc.setDseqFor(jmpid);
+ if (!seqRefIds.containsKey(mpc.getDseqFor()))
+ {
+ jalview.bin.Cache.log.debug("creatign new DseqFor ID");
+ seqRefIds.put(mpc.getDseqFor(), ps);
+ }
+ else
+ {
+ jalview.bin.Cache.log.debug("reusing DseqFor ID");
+ }
+ }
+ mp.setMappingChoice(mpc);
+ }
+ }
+ return mp;
+ }
+
+ String setUserColourScheme(jalview.schemes.ColourSchemeI cs,
+ List<UserColourScheme> userColours, JalviewModelSequence jms)
+ {
+ String id = null;
+ jalview.schemes.UserColourScheme ucs = (jalview.schemes.UserColourScheme) cs;
+ boolean newucs = false;
+ if (!userColours.contains(ucs))
+ {
+ userColours.add(ucs);
+ newucs = true;
+ }
+ id = "ucs" + userColours.indexOf(ucs);
+ if (newucs)
+ {
+ // actually create the scheme's entry in the XML model
+ java.awt.Color[] colours = ucs.getColours();
+ jalview.schemabinding.version2.UserColours uc = new jalview.schemabinding.version2.UserColours();
+ jalview.schemabinding.version2.UserColourScheme jbucs = new jalview.schemabinding.version2.UserColourScheme();
+
+ for (int i = 0; i < colours.length; i++)
+ {
+ jalview.schemabinding.version2.Colour col = new jalview.schemabinding.version2.Colour();
+ col.setName(ResidueProperties.aa[i]);
+ col.setRGB(jalview.util.Format.getHexString(colours[i]));
+ jbucs.addColour(col);
+ }
+ if (ucs.getLowerCaseColours() != null)
+ {
+ colours = ucs.getLowerCaseColours();
+ for (int i = 0; i < colours.length; i++)
+ {
+ jalview.schemabinding.version2.Colour col = new jalview.schemabinding.version2.Colour();
+ col.setName(ResidueProperties.aa[i].toLowerCase());
+ col.setRGB(jalview.util.Format.getHexString(colours[i]));
+ jbucs.addColour(col);
+ }
+ }
+
+ uc.setId(id);
+ uc.setUserColourScheme(jbucs);
+ jms.addUserColours(uc);
+ }
+
+ return id;
+ }
+
+ jalview.schemes.UserColourScheme getUserColourScheme(
+ JalviewModelSequence jms, String id)
+ {
+ UserColours[] uc = jms.getUserColours();
+ UserColours colours = null;
+
+ for (int i = 0; i < uc.length; i++)
+ {
+ if (uc[i].getId().equals(id))
+ {
+ colours = uc[i];
+
+ break;
+ }
+ }
+
+ java.awt.Color[] newColours = new java.awt.Color[24];
+
+ for (int i = 0; i < 24; i++)
+ {
+ newColours[i] = new java.awt.Color(Integer.parseInt(colours
+ .getUserColourScheme().getColour(i).getRGB(), 16));
+ }
+
+ jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(
+ newColours);
+
+ if (colours.getUserColourScheme().getColourCount() > 24)
+ {
+ newColours = new java.awt.Color[23];
+ for (int i = 0; i < 23; i++)
+ {
+ newColours[i] = new java.awt.Color(Integer.parseInt(colours
+ .getUserColourScheme().getColour(i + 24).getRGB(), 16));
+ }
+ ucs.setLowerCaseColours(newColours);
+ }
+
+ return ucs;
+ }
+
+ /**
+ * contains last error message (if any) encountered by XML loader.
+ */
+ String errorMessage = null;
+
+ /**
+ * flag to control whether the Jalview2XML_V1 parser should be deferred to if
+ * exceptions are raised during project XML parsing
+ */
+ public boolean attemptversion1parse = true;
+
+ /**
+ * Load a jalview project archive from a jar file
+ *
+ * @param file
+ * - HTTP URL or filename
+ */
+ public AlignFrame loadJalviewAlign(final String file)
+ {
+
+ jalview.gui.AlignFrame af = null;
+
+ try
+ {
+ // create list to store references for any new Jmol viewers created
+ newStructureViewers = new Vector<JalviewStructureDisplayI>();
+ // UNMARSHALLER SEEMS TO CLOSE JARINPUTSTREAM, MOST ANNOYING
+ // Workaround is to make sure caller implements the JarInputStreamProvider
+ // interface
+ // so we can re-open the jar input stream for each entry.
+
+ jarInputStreamProvider jprovider = createjarInputStreamProvider(file);
+ af = loadJalviewAlign(jprovider);
+
+ } catch (MalformedURLException e)
+ {
+ errorMessage = "Invalid URL format for '" + file + "'";
+ reportErrors();
+ } finally
+ {
+ try
+ {
+ SwingUtilities.invokeAndWait(new Runnable()
+ {
+ @Override
+ public void run()
+ {
+ setLoadingFinishedForNewStructureViewers();
+ };
+ });
+ } catch (Exception x)
+ {
+ System.err.println("Error loading alignment: " + x.getMessage());
+ }
+ }
+ return af;
+ }
+
+ private jarInputStreamProvider createjarInputStreamProvider(
+ final String file) throws MalformedURLException
+ {
+ URL url = null;
+ errorMessage = null;
+ uniqueSetSuffix = null;
+ seqRefIds = null;
+ viewportsAdded.clear();
+ frefedSequence = null;
+
+ if (file.startsWith("http://"))
+ {
+ url = new URL(file);
+ }
+ final URL _url = url;
+ return new jarInputStreamProvider()
+ {
+
+ @Override
+ public JarInputStream getJarInputStream() throws IOException
+ {
+ if (_url != null)
+ {
+ return new JarInputStream(_url.openStream());
+ }
+ else
+ {
+ return new JarInputStream(new FileInputStream(file));
+ }
+ }
+
+ @Override
+ public String getFilename()
+ {
+ return file;
+ }
+ };
+ }
+
+ /**
+ * Recover jalview session from a jalview project archive. Caller may
+ * initialise uniqueSetSuffix, seqRefIds, viewportsAdded and frefedSequence
+ * themselves. Any null fields will be initialised with default values,
+ * non-null fields are left alone.
+ *
+ * @param jprovider
+ * @return
+ */
+ public AlignFrame loadJalviewAlign(final jarInputStreamProvider jprovider)
+ {
+ errorMessage = null;
+ if (uniqueSetSuffix == null)
+ {
+ uniqueSetSuffix = System.currentTimeMillis() % 100000 + "";
+ }
+ if (seqRefIds == null)
+ {
+ seqRefIds = new HashMap<String, SequenceI>();
+ }
+ if (frefedSequence == null)
+ {
+ frefedSequence = new Vector<Object[]>();
+ }
+
+ AlignFrame af = null, _af = null;
+ Map<String, AlignFrame> gatherToThisFrame = new HashMap<String, AlignFrame>();
+ final String file = jprovider.getFilename();
+ try
+ {
+ JarInputStream jin = null;
+ JarEntry jarentry = null;
+ int entryCount = 1;
+
+ do
+ {
+ jin = jprovider.getJarInputStream();
+ for (int i = 0; i < entryCount; i++)
+ {
+ jarentry = jin.getNextJarEntry();
+ }
+
+ if (jarentry != null && jarentry.getName().endsWith(".xml"))
+ {
+ InputStreamReader in = new InputStreamReader(jin, UTF_8);
+ JalviewModel object = new JalviewModel();
+
+ Unmarshaller unmar = new Unmarshaller(object);
+ unmar.setValidation(false);
+ object = (JalviewModel) unmar.unmarshal(in);
+ if (true) // !skipViewport(object))
+ {
+ _af = loadFromObject(object, file, true, jprovider);
+ if (object.getJalviewModelSequence().getViewportCount() > 0)
+ {
+ af = _af;
+ if (af.viewport.isGatherViewsHere())
+ {
+ gatherToThisFrame.put(af.viewport.getSequenceSetId(), af);
+ }
+ }
+ }
+ entryCount++;
+ }
+ else if (jarentry != null)
+ {
+ // Some other file here.
+ entryCount++;
+ }
+ } while (jarentry != null);
+ resolveFrefedSequences();
+ } catch (IOException ex)
+ {
+ ex.printStackTrace();
+ errorMessage = "Couldn't locate Jalview XML file : " + file;
+ System.err.println("Exception whilst loading jalview XML file : "
+ + ex + "\n");
+ } catch (Exception ex)
+ {
+ System.err.println("Parsing as Jalview Version 2 file failed.");
+ ex.printStackTrace(System.err);
+ if (attemptversion1parse)
+ {
+ // Is Version 1 Jar file?
+ try
+ {
+ af = new Jalview2XML_V1(raiseGUI).LoadJalviewAlign(jprovider);
+ } catch (Exception ex2)
+ {
+ System.err.println("Exception whilst loading as jalviewXMLV1:");
+ ex2.printStackTrace();
+ af = null;
+ }
+ }
+ if (Desktop.instance != null)
+ {
+ Desktop.instance.stopLoading();
+ }
+ if (af != null)
+ {
+ System.out.println("Successfully loaded archive file");
+ return af;
+ }
+ ex.printStackTrace();
+
+ System.err.println("Exception whilst loading jalview XML file : "
+ + ex + "\n");
+ } catch (OutOfMemoryError e)
+ {
+ // Don't use the OOM Window here
+ errorMessage = "Out of memory loading jalview XML file";
+ System.err.println("Out of memory whilst loading jalview XML file");
+ e.printStackTrace();
+ }
+
+ if (Desktop.instance != null)
+ {
+ Desktop.instance.stopLoading();
+ }
+
+ /*
+ * Regather multiple views (with the same sequence set id) to the frame (if
+ * any) that is flagged as the one to gather to, i.e. convert them to tabbed
+ * views instead of separate frames. Note this doesn't restore a state where
+ * some expanded views in turn have tabbed views - the last "first tab" read
+ * in will play the role of gatherer for all.
+ */
+ for (AlignFrame fr : gatherToThisFrame.values())
+ {
+ Desktop.instance.gatherViews(fr);
+ }
+
+ restoreSplitFrames();
+
+ if (errorMessage != null)
+ {
+ reportErrors();
+ }
+ return af;
+ }
+
+ /**
+ * Try to reconstruct and display SplitFrame windows, where each contains
+ * complementary dna and protein alignments. Done by pairing up AlignFrame
+ * objects (created earlier) which have complementary viewport ids associated.
+ */
+ protected void restoreSplitFrames()
+ {
+ List<SplitFrame> gatherTo = new ArrayList<SplitFrame>();
+ List<AlignFrame> addedToSplitFrames = new ArrayList<AlignFrame>();
+ Map<String, AlignFrame> dna = new HashMap<String, AlignFrame>();
+
+ /*
+ * Identify the DNA alignments
+ */
+ for (Entry<Viewport, AlignFrame> candidate : splitFrameCandidates
+ .entrySet())
+ {
+ AlignFrame af = candidate.getValue();
+ if (af.getViewport().getAlignment().isNucleotide())
+ {
+ dna.put(candidate.getKey().getId(), af);
+ }
+ }
+
+ /*
+ * Try to match up the protein complements
+ */
+ for (Entry<Viewport, AlignFrame> candidate : splitFrameCandidates
+ .entrySet())
+ {
+ AlignFrame af = candidate.getValue();
+ if (!af.getViewport().getAlignment().isNucleotide())
+ {
+ String complementId = candidate.getKey().getComplementId();
+ // only non-null complements should be in the Map
+ if (complementId != null && dna.containsKey(complementId))
+ {
+ final AlignFrame dnaFrame = dna.get(complementId);
+ SplitFrame sf = createSplitFrame(dnaFrame, af);
+ addedToSplitFrames.add(dnaFrame);
+ addedToSplitFrames.add(af);
+ if (af.viewport.isGatherViewsHere())
+ {
+ gatherTo.add(sf);
+ }
+ }
+ }
+ }
+
+ /*
+ * Open any that we failed to pair up (which shouldn't happen!) as
+ * standalone AlignFrame's.
+ */
+ for (Entry<Viewport, AlignFrame> candidate : splitFrameCandidates
+ .entrySet())
+ {
+ AlignFrame af = candidate.getValue();
+ if (!addedToSplitFrames.contains(af))
+ {
+ Viewport view = candidate.getKey();
+ Desktop.addInternalFrame(af, view.getTitle(), view.getWidth(),
+ view.getHeight());
+ System.err.println("Failed to restore view " + view.getTitle()
+ + " to split frame");
+ }
+ }
+
+ /*
+ * Gather back into tabbed views as flagged.
+ */
+ for (SplitFrame sf : gatherTo)
+ {
+ Desktop.instance.gatherViews(sf);
+ }
+
+ splitFrameCandidates.clear();
+ }
+
+ /**
+ * Construct and display one SplitFrame holding DNA and protein alignments.
+ *
+ * @param dnaFrame
+ * @param proteinFrame
+ * @return
+ */
+ protected SplitFrame createSplitFrame(AlignFrame dnaFrame,
+ AlignFrame proteinFrame)
+ {
+ SplitFrame splitFrame = new SplitFrame(dnaFrame, proteinFrame);
+ String title = MessageManager.getString("label.linked_view_title");
+ int width = (int) dnaFrame.getBounds().getWidth();
+ int height = (int) (dnaFrame.getBounds().getHeight()
+ + proteinFrame.getBounds().getHeight() + 50);
+
+ /*
+ * SplitFrame location is saved to both enclosed frames
+ */
+ splitFrame.setLocation(dnaFrame.getX(), dnaFrame.getY());
+ Desktop.addInternalFrame(splitFrame, title, width, height);
+
+ /*
+ * And compute cDNA consensus (couldn't do earlier with consensus as
+ * mappings were not yet present)
+ */
+ proteinFrame.viewport.alignmentChanged(proteinFrame.alignPanel);
+
+ return splitFrame;
+ }
+
+ /**
+ * check errorMessage for a valid error message and raise an error box in the
+ * GUI or write the current errorMessage to stderr and then clear the error
+ * state.
+ */
+ protected void reportErrors()
+ {
+ reportErrors(false);
+ }
+
+ protected void reportErrors(final boolean saving)
+ {
+ if (errorMessage != null)
+ {
+ final String finalErrorMessage = errorMessage;
+ if (raiseGUI)
+ {
+ javax.swing.SwingUtilities.invokeLater(new Runnable()
+ {
+ @Override
+ public void run()
+ {
+ JOptionPane.showInternalMessageDialog(Desktop.desktop,
+ finalErrorMessage, "Error "
+ + (saving ? "saving" : "loading")
+ + " Jalview file", JOptionPane.WARNING_MESSAGE);
+ }
+ });
+ }
+ else
+ {
+ System.err.println("Problem loading Jalview file: " + errorMessage);
+ }
+ }
+ errorMessage = null;
+ }
+
+ Map<String, String> alreadyLoadedPDB = new HashMap<String, String>();
+
+ /**
+ * when set, local views will be updated from view stored in JalviewXML
+ * Currently (28th Sep 2008) things will go horribly wrong in vamsas document
+ * sync if this is set to true.
+ */
+ private final boolean updateLocalViews = false;
+
+ /**
+ * Returns the path to a temporary file holding the PDB file for the given PDB
+ * id. The first time of asking, searches for a file of that name in the
+ * Jalview project jar, and copies it to a new temporary file. Any repeat
+ * requests just return the path to the file previously created.
+ *
+ * @param jprovider
+ * @param pdbId
+ * @return
+ */
+ String loadPDBFile(jarInputStreamProvider jprovider, String pdbId)
+ {
+ if (alreadyLoadedPDB.containsKey(pdbId))
+ {
+ return alreadyLoadedPDB.get(pdbId).toString();
+ }
+
+ String tempFile = copyJarEntry(jprovider, pdbId, "jalview_pdb");
+ if (tempFile != null)
+ {
+ alreadyLoadedPDB.put(pdbId, tempFile);
+ }
+ return tempFile;
+ }
+
+ /**
+ * Copies the jar entry of given name to a new temporary file and returns the
+ * path to the file, or null if the entry is not found.
+ *
+ * @param jprovider
+ * @param jarEntryName
+ * @param prefix
+ * a prefix for the temporary file name, must be at least three
+ * characters long
+ * @return
+ */
+ protected String copyJarEntry(jarInputStreamProvider jprovider,
+ String jarEntryName, String prefix)
+ {
+ BufferedReader in = null;
+ PrintWriter out = null;
+
+ try
+ {
+ JarInputStream jin = jprovider.getJarInputStream();
+ /*
+ * if (jprovider.startsWith("http://")) { jin = new JarInputStream(new
+ * URL(jprovider).openStream()); } else { jin = new JarInputStream(new
+ * FileInputStream(jprovider)); }
+ */
+
+ JarEntry entry = null;
+ do
+ {
+ entry = jin.getNextJarEntry();
+ } while (entry != null && !entry.getName().equals(jarEntryName));
+ if (entry != null)
+ {
+ in = new BufferedReader(new InputStreamReader(jin, UTF_8));
+ File outFile = File.createTempFile(prefix, ".tmp");
+ outFile.deleteOnExit();
+ out = new PrintWriter(new FileOutputStream(outFile));
+ String data;
+
+ while ((data = in.readLine()) != null)
+ {
+ out.println(data);
+ }
+ out.flush();
+ String t = outFile.getAbsolutePath();
+ return t;
+ }
+ 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
+ }
+ }
+ if (out != null)
+ {
+ out.close();
+ }
+ }
+
+ return null;
+ }
+
+ private class JvAnnotRow
+ {
+ public JvAnnotRow(int i, AlignmentAnnotation jaa)
+ {
+ order = i;
+ template = jaa;
+ }
+
+ /**
+ * persisted version of annotation row from which to take vis properties
+ */
+ public jalview.datamodel.AlignmentAnnotation template;
+
+ /**
+ * original position of the annotation row in the alignment
+ */
+ public int order;
+ }
+
+ /**
+ * Load alignment frame from jalview XML DOM object
+ *
+ * @param object
+ * DOM
+ * @param file
+ * filename source string
+ * @param loadTreesAndStructures
+ * when false only create Viewport
+ * @param jprovider
+ * data source provider
+ * @return alignment frame created from view stored in DOM
+ */
+ AlignFrame loadFromObject(JalviewModel object, String file,
+ boolean loadTreesAndStructures, jarInputStreamProvider jprovider)
+ {
+ SequenceSet vamsasSet = object.getVamsasModel().getSequenceSet(0);
+ Sequence[] vamsasSeq = vamsasSet.getSequence();
+
+ JalviewModelSequence jms = object.getJalviewModelSequence();
+
+ Viewport view = (jms.getViewportCount() > 0) ? jms.getViewport(0)
+ : null;
+
+ // ////////////////////////////////
+ // LOAD SEQUENCES
+
+ List<SequenceI> hiddenSeqs = null;
+ jalview.datamodel.Sequence jseq;
+
+ List<SequenceI> tmpseqs = new ArrayList<SequenceI>();
+
+ boolean multipleView = false;
+
+ JSeq[] jseqs = object.getJalviewModelSequence().getJSeq();
+ int vi = 0; // counter in vamsasSeq array
+ for (int i = 0; i < jseqs.length; i++)
+ {
+ String seqId = jseqs[i].getId();
+
+ if (seqRefIds.get(seqId) != null)
+ {
+ tmpseqs.add(seqRefIds.get(seqId));
+ multipleView = true;
+ }
+ else
+ {
+ jseq = new jalview.datamodel.Sequence(vamsasSeq[vi].getName(),
+ vamsasSeq[vi].getSequence());
+ jseq.setDescription(vamsasSeq[vi].getDescription());
+ jseq.setStart(jseqs[i].getStart());
+ jseq.setEnd(jseqs[i].getEnd());
+ jseq.setVamsasId(uniqueSetSuffix + seqId);
+ seqRefIds.put(vamsasSeq[vi].getId(), jseq);
+ tmpseqs.add(jseq);
+ vi++;
+ }
+
+ if (jseqs[i].getHidden())
+ {
+ if (hiddenSeqs == null)
+ {
+ hiddenSeqs = new ArrayList<SequenceI>();
+ }
+
+ hiddenSeqs.add(seqRefIds.get(seqId));
+ }
+
+ }
+
+ // /
+ // Create the alignment object from the sequence set
+ // ///////////////////////////////
+ SequenceI[] orderedSeqs = tmpseqs
+ .toArray(new SequenceI[tmpseqs.size()]);
+
+ Alignment al = new Alignment(orderedSeqs);
+
+ // / Add the alignment properties
+ for (int i = 0; i < vamsasSet.getSequenceSetPropertiesCount(); i++)
+ {
+ SequenceSetProperties ssp = vamsasSet.getSequenceSetProperties(i);
+ al.setProperty(ssp.getKey(), ssp.getValue());
+ }
+
+ // /
+ // SequenceFeatures are added to the DatasetSequence,
+ // so we must create or recover the dataset before loading features
+ // ///////////////////////////////
+ if (vamsasSet.getDatasetId() == null || vamsasSet.getDatasetId() == "")
+ {
+ // older jalview projects do not have a dataset id.
+ al.setDataset(null);
+ }
+ else
+ {
+ // recover dataset - passing on flag indicating if this a 'viewless'
+ // sequence set (a.k.a. a stored dataset for the project)
+ recoverDatasetFor(vamsasSet, al, object.getJalviewModelSequence()
+ .getViewportCount() == 0);
+ }
+ // ///////////////////////////////
+
+ Hashtable pdbloaded = new Hashtable(); // TODO nothing writes to this??
+ if (!multipleView)
+ {
+ // load sequence features, database references and any associated PDB
+ // structures for the alignment
+ for (int i = 0; i < vamsasSeq.length; i++)
+ {
+ if (jseqs[i].getFeaturesCount() > 0)
+ {
+ Features[] features = jseqs[i].getFeatures();
+ for (int f = 0; f < features.length; f++)
+ {
+ jalview.datamodel.SequenceFeature sf = new jalview.datamodel.SequenceFeature(
+ features[f].getType(), features[f].getDescription(),
+ features[f].getStatus(), features[f].getBegin(),
+ features[f].getEnd(), features[f].getFeatureGroup());
+
+ sf.setScore(features[f].getScore());