X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FJalview2XML.java;h=97dfefd969be3330ac3f3d13594016fa7b741b04;hb=b9d84d8a7aa2557c51307addf9e80139f07bb14b;hp=6652aea7fefe82ee1baf0c3617745eec1e648c24;hpb=0452450533728e478ef8c893ea2cb3483c740fb3;p=jalview.git diff --git a/src/jalview/gui/Jalview2XML.java b/src/jalview/gui/Jalview2XML.java index 6652aea..97dfefd 100755 --- a/src/jalview/gui/Jalview2XML.java +++ b/src/jalview/gui/Jalview2XML.java @@ -1,6 +1,6 @@ /* * Jalview - A Sequence Alignment Editor and Viewer - * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle + * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -18,36 +18,130 @@ */ package jalview.gui; - -import jalview.schemes.*; - import java.io.*; - import java.net.*; - import java.util.*; - import java.util.jar.*; import javax.swing.*; import org.exolab.castor.xml.*; +import uk.ac.vamsas.objects.utils.MapList; +import jalview.datamodel.Alignment; +import jalview.datamodel.AlignmentI; +import jalview.datamodel.SequenceI; import jalview.schemabinding.version2.*; - - - +import jalview.schemes.*; +import jalview.structure.StructureSelectionManager; /** - * DOCUMENT ME! + * Write out the current jalview desktop state + * as a Jalview XML stream. + * + * Note: the vamsas objects referred to here are primitive + * versions of the VAMSAS project schema elements - they are + * not the same and most likely never will be :) * * @author $author$ * @version $Revision$ */ public class Jalview2XML { - - Hashtable seqRefIds; + /** + * create/return unique hash string for sq + * @param sq + * @return new or existing unique string for sq + */ + String seqHash(SequenceI sq) + { + if (seqsToIds==null) + { + initSeqRefs(); + } + if (seqsToIds.containsKey(sq)) + { + return (String) seqsToIds.get(sq); + } else { + // create sequential key + String key = "sq"+(seqsToIds.size()+1); + seqsToIds.put(sq, key); + return key; + } + } + void clearSeqRefs() + { + seqRefIds.clear(); + seqsToIds.clear(); + } + void initSeqRefs() + { + if (seqsToIds==null) + { + seqsToIds = new IdentityHashMap(); + } + if (seqRefIds==null) + { + seqRefIds = new Hashtable(); + } + } + java.util.IdentityHashMap seqsToIds = null; // SequenceI->key resolution + java.util.Hashtable seqRefIds = null; // key->SequenceI resolution + + Vector frefedSequence = null; + boolean raiseGUI = true; // whether errors are raised in dialog boxes or not + public Jalview2XML() + { + } + public Jalview2XML(boolean raiseGUI) + { + this.raiseGUI = raiseGUI; + } + + public void resolveFrefedSequences() + { + if (frefedSequence.size() > 0) + { + int r = 0, rSize = frefedSequence.size(); + while (r < rSize) + { + Object[] ref = (Object[]) frefedSequence.elementAt(r); + if (ref != null) + { + String sref = (String) ref[0]; + if (seqRefIds.containsKey(sref)) + { + if (ref[1] instanceof jalview.datamodel.Mapping) + { + SequenceI seq = (SequenceI) seqRefIds.get(sref); + while (seq.getDatasetSequence() != null) + { + seq = seq.getDatasetSequence(); + } + ((jalview.datamodel.Mapping) ref[1]).setTo(seq); + } + else + { + System.err + .println("IMPLEMENTATION ERROR: Unimplemented forward sequence references for " + + ref[1].getClass() + " type objects."); + } + frefedSequence.remove(r); + rSize--; + } + else + { + r++; + } + } + else + { + frefedSequence.remove(r); + rSize--; + } + } + } + } /** * This maintains a list of viewports, the key being the @@ -56,1636 +150,2367 @@ public class Jalview2XML */ Hashtable viewportsAdded; + Hashtable annotationIds = new Hashtable(); + String uniqueSetSuffix = ""; + /** + * List of pdbfiles added to Jar + */ + Vector pdbfiles = null; - // SAVES SEVERAL ALIGNMENT WINDOWS TO SAME JARFILE - public void SaveState(File statefile) - { - JInternalFrame[] frames = Desktop.desktop.getAllFrames(); + // SAVES SEVERAL ALIGNMENT WINDOWS TO SAME JARFILE + public void SaveState(File statefile) + { + JInternalFrame[] frames = Desktop.desktop.getAllFrames(); - if (frames == null) - { - return; - } + if (frames == null) + { + return; + } - try - { - FileOutputStream fos = new FileOutputStream(statefile); - JarOutputStream jout = new JarOutputStream(fos); + try + { + FileOutputStream fos = new FileOutputStream(statefile); + JarOutputStream jout = new JarOutputStream(fos); - //NOTE UTF-8 MUST BE USED FOR WRITING UNICODE CHARS - //////////////////////////////////////////////////// - PrintWriter out = new PrintWriter(new OutputStreamWriter(jout, - "UTF-8")); + //NOTE UTF-8 MUST BE USED FOR WRITING UNICODE CHARS + //////////////////////////////////////////////////// + //NOTE ALSO new PrintWriter must be used for each new JarEntry + PrintWriter out = null; - Vector shortNames = new Vector(); + Vector shortNames = new Vector(); - //REVERSE ORDER - for (int i = frames.length - 1; i > -1; i--) - { - if (frames[i] instanceof AlignFrame) - { - AlignFrame af = (AlignFrame) frames[i]; + //REVERSE ORDER + for (int i = frames.length - 1; i > -1; i--) + { + if (frames[i] instanceof AlignFrame) + { + AlignFrame af = (AlignFrame) frames[i]; - String shortName = af.getTitle(); + String shortName = af.getTitle(); - if (shortName.indexOf(File.separatorChar) > -1) - { - shortName = shortName.substring(shortName.lastIndexOf( - File.separatorChar) + 1); - } + if (shortName.indexOf(File.separatorChar) > -1) + { + shortName = shortName.substring(shortName + .lastIndexOf(File.separatorChar) + 1); + } - int count = 1; + int count = 1; - while (shortNames.contains(shortName)) - { - if (shortName.endsWith("_" + (count - 1))) - { - shortName = shortName.substring(0, - shortName.lastIndexOf("_")); - } - - shortName = shortName.concat("_" + count); - count++; - } + while (shortNames.contains(shortName)) + { + if (shortName.endsWith("_" + (count - 1))) + { + shortName = shortName + .substring(0, shortName.lastIndexOf("_")); + } - shortNames.addElement(shortName); + shortName = shortName.concat("_" + count); + count++; + } - if (!shortName.endsWith(".xml")) - { - shortName = shortName + ".xml"; - } + shortNames.addElement(shortName); - int ap, apSize= af.alignPanels.size(); - for (ap = 0; ap < apSize; ap++) - { - AlignmentPanel apanel = (AlignmentPanel) af.alignPanels. - elementAt(ap); + if (!shortName.endsWith(".xml")) + { + shortName = shortName + ".xml"; + } - SaveState(apanel, - apSize == 1 ? shortName : ap+shortName, - jout, out); - } - } + int ap, apSize = af.alignPanels.size(); + for (ap = 0; ap < apSize; ap++) + { + AlignmentPanel apanel = (AlignmentPanel) af.alignPanels + .elementAt(ap); + String fileName = apSize == 1 ? shortName : ap + shortName; + if (!fileName.endsWith(".xml")) + { + fileName = fileName + ".xml"; } - out.close(); - jout.close(); - } - catch (Exception ex) - { - ex.printStackTrace(); + SaveState(apanel, fileName, jout); + } } + } + try { jout.flush(); } catch (Exception foo) {}; + jout.close(); + } catch (Exception ex) + { + //TODO: inform user of the problem - they need to know if their data was not saved ! + ex.printStackTrace(); } + } - // USE THIS METHOD TO SAVE A SINGLE ALIGNMENT WINDOW - public boolean SaveAlignment(AlignFrame af, String jarFile, - String fileName) + // USE THIS METHOD TO SAVE A SINGLE ALIGNMENT WINDOW + public boolean SaveAlignment(AlignFrame af, String jarFile, + String fileName) + { + try { - try - { - int ap, apSize= af.alignPanels.size(); - FileOutputStream fos = new FileOutputStream(jarFile); - JarOutputStream jout = new JarOutputStream(fos); - PrintWriter out = new PrintWriter(new OutputStreamWriter(jout, - "UTF-8")); - for( ap=0; ap -1; f--) + { + if (frames[f] instanceof AppJmol) { - jseq.setHidden(av.alignment.getHiddenSequences().isHidden(jds)); - - if(av.hiddenRepSequences!=null - && av.hiddenRepSequences.containsKey(jal.getSequenceAt(i))) + jmol = (AppJmol) frames[f]; + if (!jmol.pdbentry.getId().equals(entry.getId())) + continue; + + StructureState state = new StructureState(); + state.setVisible(true); + state.setXpos(jmol.getX()); + state.setYpos(jmol.getY()); + state.setWidth(jmol.getWidth()); + state.setHeight(jmol.getHeight()); + + String statestring = jmol.viewer.getStateInfo(); + if (state != null) { - jalview.datamodel.SequenceI[] reps = - ( (jalview.datamodel.SequenceGroup) - av.hiddenRepSequences.get( - jal.getSequenceAt(i))).getSequencesInOrder(jal); - - for(int h=0; h -1) { - if (reps[h] != jal.getSequenceAt(i)) - jseq.addHiddenSequences( - jal.findIndex(reps[h]) - ); + pdb.addStructureState(state); } } } + } - - if(jds.getDatasetSequence().getSequenceFeatures()!=null) + if (entry.getFile() != null) + { + pdb.setFile(entry.getFile()); + if (pdbfiles == null) { - jalview.datamodel.SequenceFeature[] sf - = jds.getDatasetSequence().getSequenceFeatures(); - int index = 0; - while(index < sf.length) - { - Features features = new Features(); - - features.setBegin(sf[index].getBegin()); - features.setEnd(sf[index].getEnd()); - features.setDescription(sf[index].getDescription()); - features.setType(sf[index].getType()); - features.setFeatureGroup(sf[index].getFeatureGroup()); - features.setScore(sf[index].getScore()); - if(sf[index].links!=null) - { - for(int l=0; l 0) + { + jalview.datamodel.AlignedCodonFrame[] jac = jal.getCodonFrames(); + for (int i = 0; i < jac.length; i++) + { + AlcodonFrame alc = new AlcodonFrame(); + vamsasSet.addAlcodonFrame(alc); + for (int p = 0; p < jac[i].aaWidth; p++) + { + Alcodon cmap = new Alcodon(); + cmap.setPos1(jac[i].codons[p][0]); + cmap.setPos2(jac[i].codons[p][1]); + cmap.setPos3(jac[i].codons[p][2]); + alc.addAlcodon(cmap); + } + if (jac[i].getProtMappings() != null + && jac[i].getProtMappings().length > 0) + { + SequenceI[] dnas = jac[i].getdnaSeqs(); + jalview.datamodel.Mapping[] pmaps = jac[i].getProtMappings(); + for (int m = 0; m < pmaps.length; m++) + { + AlcodMap alcmap = new AlcodMap(); + alcmap.setDnasq("" + dnas[m].hashCode()); + alcmap.setMapping(createVamsasMapping(pmaps[m], dnas[m], null, + false)); + alc.addAlcodMap(alcmap); + } + } + } + } + //SAVE TREES + /////////////////////////////////// + if (av.currentTree != null) + { + // FIND ANY ASSOCIATED TREES + // NOT IMPLEMENTED FOR HEADLESS STATE AT PRESENT + if (Desktop.desktop != null) + { + JInternalFrame[] frames = Desktop.desktop.getAllFrames(); - //SAVE TREES - /////////////////////////////////// - if (av.currentTree != null) + for (int t = 0; t < frames.length; t++) { - // FIND ANY ASSOCIATED TREES - // NOT IMPLEMENTED FOR HEADLESS STATE AT PRESENT - if (Desktop.desktop != null) + if (frames[t] instanceof TreePanel) { - JInternalFrame[] frames = Desktop.desktop.getAllFrames(); + TreePanel tp = (TreePanel) frames[t]; - for (int t = 0; t < frames.length; t++) + if (tp.treeCanvas.av.alignment == jal) { - if (frames[t] instanceof TreePanel) - { - TreePanel tp = (TreePanel) frames[t]; - - if (tp.treeCanvas.av.alignment == jal) - { - Tree tree = new Tree(); - tree.setTitle(tp.getTitle()); - tree.setCurrentTree( (av.currentTree == tp.getTree())); - tree.setNewick(tp.getTree().toString()); - tree.setThreshold(tp.treeCanvas.threshold); - - tree.setFitToWindow(tp.fitToWindow.getState()); - tree.setFontName(tp.getTreeFont().getName()); - tree.setFontSize(tp.getTreeFont().getSize()); - tree.setFontStyle(tp.getTreeFont().getStyle()); - tree.setMarkUnlinked(tp.placeholdersMenu.getState()); - - tree.setShowBootstrap(tp.bootstrapMenu.getState()); - tree.setShowDistances(tp.distanceMenu.getState()); - - tree.setHeight(tp.getHeight()); - tree.setWidth(tp.getWidth()); - tree.setXpos(tp.getX()); - tree.setYpos(tp.getY()); - - jms.addTree(tree); - } - } + Tree tree = new Tree(); + tree.setTitle(tp.getTitle()); + tree.setCurrentTree((av.currentTree == tp.getTree())); + tree.setNewick(tp.getTree().toString()); + tree.setThreshold(tp.treeCanvas.threshold); + + tree.setFitToWindow(tp.fitToWindow.getState()); + tree.setFontName(tp.getTreeFont().getName()); + tree.setFontSize(tp.getTreeFont().getSize()); + tree.setFontStyle(tp.getTreeFont().getStyle()); + tree.setMarkUnlinked(tp.placeholdersMenu.getState()); + + tree.setShowBootstrap(tp.bootstrapMenu.getState()); + tree.setShowDistances(tp.distanceMenu.getState()); + + tree.setHeight(tp.getHeight()); + tree.setWidth(tp.getWidth()); + tree.setXpos(tp.getX()); + tree.setYpos(tp.getY()); + + jms.addTree(tree); } } } + } + } - //SAVE ANNOTATIONS - if (jal.getAlignmentAnnotation() != null) - { - jalview.datamodel.AlignmentAnnotation[] aa = jal.getAlignmentAnnotation(); + //SAVE ANNOTATIONS + if (jal.getAlignmentAnnotation() != null) + { + jalview.datamodel.AlignmentAnnotation[] aa = jal + .getAlignmentAnnotation(); - for (int i = 0; i < aa.length; i++) - { - Annotation an = new Annotation(); + for (int i = 0; i < aa.length; i++) + { + Annotation an = new Annotation(); - if (aa[i].label.equals("Quality") || - aa[i].label.equals("Conservation") || - aa[i].label.equals("Consensus")) - { - an.setLabel(aa[i].label); - an.setGraph(true); - vamsasSet.addAnnotation(an); - continue; - } + if (aa[i].annotationId != null) + { + annotationIds.put(aa[i].annotationId, aa[i]); + } + an.setId(aa[i].annotationId); - an.setDescription(aa[i].description); + if (aa[i] == av.quality || aa[i] == av.conservation + || aa[i] == av.consensus) + { + an.setLabel(aa[i].label); + an.setGraph(true); + vamsasSet.addAnnotation(an); + continue; + } - if(aa[i].sequenceRef!=null) - { - an.setSequenceRef(aa[i].sequenceRef.getName()); - } + an.setVisible(aa[i].visible); - if(aa[i].graph>0) - { - an.setGraph(true); - an.setGraphType(aa[i].graph); - an.setGraphGroup(aa[i].graphGroup); - if(aa[i].getThreshold()!=null) - { - ThresholdLine line = new ThresholdLine(); - line.setLabel(aa[i].getThreshold().label); - line.setValue(aa[i].getThreshold().value); - line.setColour(aa[i].getThreshold().colour.getRGB()); - an.setThresholdLine(line); - } - } - else - an.setGraph(false); + an.setDescription(aa[i].description); - an.setLabel(aa[i].label); + if (aa[i].sequenceRef != null) + { + an.setSequenceRef(aa[i].sequenceRef.getName()); + } - AnnotationElement ae; + if (aa[i].graph > 0) + { + an.setGraph(true); + an.setGraphType(aa[i].graph); + an.setGraphGroup(aa[i].graphGroup); + if (aa[i].getThreshold() != null) + { + ThresholdLine line = new ThresholdLine(); + line.setLabel(aa[i].getThreshold().label); + line.setValue(aa[i].getThreshold().value); + line.setColour(aa[i].getThreshold().colour.getRGB()); + an.setThresholdLine(line); + } + } + else + { + an.setGraph(false); + } - for (int a = 0; a < aa[i].annotations.length; a++) - { - if ((aa[i] == null) || (aa[i].annotations[a] == null)) - { - continue; - } + an.setLabel(aa[i].label); + if (aa[i].hasScore()) + { + an.setScore(aa[i].getScore()); + } + AnnotationElement ae; + if (aa[i].annotations != null) + { + an.setScoreOnly(false); + for (int a = 0; a < aa[i].annotations.length; a++) + { + if ((aa[i] == null) || (aa[i].annotations[a] == null)) + { + continue; + } - ae = new AnnotationElement(); - ae.setDescription(aa[i].annotations[a].description); - ae.setDisplayCharacter(aa[i].annotations[a].displayCharacter); - ae.setValue(aa[i].annotations[a].value); - ae.setPosition(a); - ae.setSecondaryStructure(aa[i].annotations[a].secondaryStructure + - ""); + ae = new AnnotationElement(); + if (aa[i].annotations[a].description != null) + ae.setDescription(aa[i].annotations[a].description); + if (aa[i].annotations[a].displayCharacter != null) + ae.setDisplayCharacter(aa[i].annotations[a].displayCharacter); - if(aa[i].annotations[a].colour!=java.awt.Color.black) - ae.setColour(aa[i].annotations[a].colour.getRGB()); + if (!Float.isNaN(aa[i].annotations[a].value)) + ae.setValue(aa[i].annotations[a].value); - an.addAnnotationElement(ae); - } + ae.setPosition(a); + if (aa[i].annotations[a].secondaryStructure != ' ' + && aa[i].annotations[a].secondaryStructure != '\0') + ae + .setSecondaryStructure(aa[i].annotations[a].secondaryStructure + + ""); - vamsasSet.addAnnotation(an); + if (aa[i].annotations[a].colour != null + && aa[i].annotations[a].colour != java.awt.Color.black) + { + ae.setColour(aa[i].annotations[a].colour.getRGB()); } + + an.addAnnotationElement(ae); + } + } + else + { + an.setScoreOnly(true); } + vamsasSet.addAnnotation(an); + } + } + + //SAVE GROUPS + if (jal.getGroups() != null) + { + JGroup[] groups = new JGroup[jal.getGroups().size()]; - //SAVE GROUPS - if (jal.getGroups() != null) + for (int i = 0; i < groups.length; i++) + { + groups[i] = new JGroup(); + + jalview.datamodel.SequenceGroup sg = (jalview.datamodel.SequenceGroup) jal + .getGroups().elementAt(i); + groups[i].setStart(sg.getStartRes()); + groups[i].setEnd(sg.getEndRes()); + groups[i].setName(sg.getName()); + if (sg.cs != null) { - JGroup[] groups = new JGroup[jal.getGroups().size()]; + if (sg.cs.conservationApplied()) + { + groups[i].setConsThreshold(sg.cs.getConservationInc()); - for (int i = 0; i < groups.length; i++) + if (sg.cs instanceof jalview.schemes.UserColourScheme) { - groups[i] = new JGroup(); - - jalview.datamodel.SequenceGroup sg = (jalview.datamodel.SequenceGroup) jal.getGroups() - .elementAt(i); - groups[i].setStart(sg.getStartRes()); - groups[i].setEnd(sg.getEndRes()); - groups[i].setName(sg.getName()); - if(sg.cs!=null) - { - if (sg.cs.conservationApplied()) - { - groups[i].setConsThreshold(sg.cs.getConservationInc()); + groups[i].setColour(SetUserColourScheme(sg.cs, userColours, + jms)); + } + else + { + groups[i] + .setColour(ColourSchemeProperty.getColourName(sg.cs)); + } + } + else if (sg.cs instanceof jalview.schemes.AnnotationColourGradient) + { + groups[i] + .setColour(ColourSchemeProperty + .getColourName(((jalview.schemes.AnnotationColourGradient) sg.cs) + .getBaseColour())); + } + else if (sg.cs instanceof jalview.schemes.UserColourScheme) + { + groups[i] + .setColour(SetUserColourScheme(sg.cs, userColours, jms)); + } + else + { + groups[i].setColour(ColourSchemeProperty.getColourName(sg.cs)); + } - if (sg.cs instanceof jalview.schemes.UserColourScheme) - { - groups[i].setColour(SetUserColourScheme(sg.cs, - userColours, - jms)); - } - else - { - groups[i].setColour(ColourSchemeProperty.getColourName(sg. - cs)); - } - } - else if(sg.cs instanceof jalview.schemes.AnnotationColourGradient) - { - groups[i].setColour( - ColourSchemeProperty.getColourName( - ( (jalview.schemes.AnnotationColourGradient) sg.cs).getBaseColour())); - } - else if (sg.cs instanceof jalview.schemes.UserColourScheme) - { - groups[i].setColour(SetUserColourScheme(sg.cs, userColours, - jms)); - } - else - { - groups[i].setColour(ColourSchemeProperty.getColourName( - sg.cs)); - } + groups[i].setPidThreshold(sg.cs.getThreshold()); + } - groups[i].setPidThreshold(sg.cs.getThreshold()); - } + groups[i].setOutlineColour(sg.getOutlineColour().getRGB()); + groups[i].setDisplayBoxes(sg.getDisplayBoxes()); + groups[i].setDisplayText(sg.getDisplayText()); + groups[i].setColourText(sg.getColourText()); + groups[i].setTextCol1(sg.textColour.getRGB()); + groups[i].setTextCol2(sg.textColour2.getRGB()); + groups[i].setTextColThreshold(sg.thresholdTextColour); - groups[i].setOutlineColour(sg.getOutlineColour().getRGB()); - groups[i].setDisplayBoxes(sg.getDisplayBoxes()); - groups[i].setDisplayText(sg.getDisplayText()); - groups[i].setColourText(sg.getColourText()); - groups[i].setTextCol1(sg.textColour.getRGB()); - groups[i].setTextCol2(sg.textColour2.getRGB()); - groups[i].setTextColThreshold(sg.thresholdTextColour); + for (int s = 0; s < sg.getSize(); s++) + { + jalview.datamodel.Sequence seq = (jalview.datamodel.Sequence) sg + .getSequenceAt(s); + groups[i].addSeq(seqHash(seq)); + } + } - for (int s = 0; s < sg.getSize(); s++) - { - jalview.datamodel.Sequence seq = - (jalview.datamodel.Sequence) sg.getSequenceAt(s); - groups[i].addSeq(seq.hashCode()); - } - } + jms.setJGroup(groups); + } - jms.setJGroup(groups); - } + ///////////SAVE VIEWPORT + Viewport view = new Viewport(); + view.setTitle(ap.alignFrame.getTitle()); + view.setSequenceSetId(av.getSequenceSetId()); + view.setViewName(av.viewName); + view.setGatheredViews(av.gatherViewsHere); + if (ap.av.explodedPosition != null) + { + view.setXpos(av.explodedPosition.x); + view.setYpos(av.explodedPosition.y); + view.setWidth(av.explodedPosition.width); + view.setHeight(av.explodedPosition.height); + } + else + { + view.setXpos(ap.alignFrame.getBounds().x); + view.setYpos(ap.alignFrame.getBounds().y); + view.setWidth(ap.alignFrame.getBounds().width); + view.setHeight(ap.alignFrame.getBounds().height); + } - ///////////SAVE VIEWPORT - Viewport view = new Viewport(); - view.setTitle(ap.alignFrame.getTitle()); - view.setSequenceSetId(av.getSequenceSetId()); - view.setViewName(av.viewName); - view.setGatheredViews(av.gatherViewsHere); + view.setStartRes(av.startRes); + view.setStartSeq(av.startSeq); + if (av.getGlobalColourScheme() instanceof jalview.schemes.UserColourScheme) + { + view.setBgColour(SetUserColourScheme(av.getGlobalColourScheme(), + userColours, jms)); + } + else if (av.getGlobalColourScheme() instanceof jalview.schemes.AnnotationColourGradient) + { + jalview.schemes.AnnotationColourGradient acg = (jalview.schemes.AnnotationColourGradient) av + .getGlobalColourScheme(); + + AnnotationColours ac = new AnnotationColours(); + ac.setAboveThreshold(acg.getAboveThreshold()); + ac.setThreshold(acg.getAnnotationThreshold()); + ac.setAnnotation(acg.getAnnotation()); + if (acg.getBaseColour() instanceof jalview.schemes.UserColourScheme) + { + ac.setColourScheme(SetUserColourScheme(acg.getBaseColour(), + userColours, jms)); + } + else + { + ac.setColourScheme(ColourSchemeProperty.getColourName(acg + .getBaseColour())); + } - if (ap.av.explodedPosition != null) - { - view.setXpos(av.explodedPosition.x); - view.setYpos(av.explodedPosition.y); - view.setWidth(av.explodedPosition.width); - view.setHeight(av.explodedPosition.height); - } - else - { - view.setXpos(ap.alignFrame.getBounds().x); - view.setYpos(ap.alignFrame.getBounds().y); - view.setWidth(ap.alignFrame.getBounds().width); - view.setHeight(ap.alignFrame.getBounds().height); - } + ac.setMaxColour(acg.getMaxColour().getRGB()); + ac.setMinColour(acg.getMinColour().getRGB()); + view.setAnnotationColours(ac); + view.setBgColour("AnnotationColourGradient"); + } + else + { + view.setBgColour(ColourSchemeProperty.getColourName(av + .getGlobalColourScheme())); + } - view.setStartRes(av.startRes); - view.setStartSeq(av.startSeq); + ColourSchemeI cs = av.getGlobalColourScheme(); - if (av.getGlobalColourScheme() instanceof jalview.schemes.UserColourScheme) + if (cs != null) + { + if (cs.conservationApplied()) + { + view.setConsThreshold(cs.getConservationInc()); + if (cs instanceof jalview.schemes.UserColourScheme) { - view.setBgColour(SetUserColourScheme(av.getGlobalColourScheme(), - userColours, jms)); + view.setBgColour(SetUserColourScheme(cs, userColours, jms)); } - else if(av.getGlobalColourScheme() instanceof jalview.schemes.AnnotationColourGradient) - { - jalview.schemes.AnnotationColourGradient acg - = (jalview.schemes.AnnotationColourGradient)av.getGlobalColourScheme(); + } - AnnotationColours ac = new AnnotationColours(); - ac.setAboveThreshold(acg.getAboveThreshold()); - ac.setThreshold(acg.getAnnotationThreshold()); - ac.setAnnotation(acg.getAnnotation()); - if(acg.getBaseColour() instanceof jalview.schemes.UserColourScheme) - ac.setColourScheme(SetUserColourScheme(acg.getBaseColour(), - userColours, jms)); - else - ac.setColourScheme(ColourSchemeProperty.getColourName(acg.getBaseColour())); + if (cs instanceof ResidueColourScheme) + { + view.setPidThreshold(cs.getThreshold()); + } + } - ac.setMaxColour(acg.getMaxColour().getRGB()); - ac.setMinColour(acg.getMinColour().getRGB()); - view.setAnnotationColours(ac); - view.setBgColour("AnnotationColourGradient"); - } - else - { - view.setBgColour(ColourSchemeProperty.getColourName( - av.getGlobalColourScheme())); - } + view.setConservationSelected(av.getConservationSelected()); + view.setPidSelected(av.getAbovePIDThreshold()); + view.setFontName(av.font.getName()); + view.setFontSize(av.font.getSize()); + view.setFontStyle(av.font.getStyle()); + view.setRenderGaps(av.renderGaps); + view.setShowAnnotation(av.getShowAnnotation()); + view.setShowBoxes(av.getShowBoxes()); + view.setShowColourText(av.getColourText()); + view.setShowFullId(av.getShowJVSuffix()); + view.setRightAlignIds(av.rightAlignIds); + view.setShowSequenceFeatures(av.showSequenceFeatures); + view.setShowText(av.getShowText()); + view.setWrapAlignment(av.getWrapAlignment()); + view.setTextCol1(av.textColour.getRGB()); + view.setTextCol2(av.textColour2.getRGB()); + view.setTextColThreshold(av.thresholdTextColour); + + if (av.featuresDisplayed != null) + { + jalview.schemabinding.version2.FeatureSettings fs = new jalview.schemabinding.version2.FeatureSettings(); - ColourSchemeI cs = av.getGlobalColourScheme(); + String[] renderOrder = ap.seqPanel.seqCanvas.getFeatureRenderer().renderOrder; - if(cs!=null) + Vector settingsAdded = new Vector(); + for (int ro = 0; ro < renderOrder.length; ro++) + { + Setting setting = new Setting(); + setting.setType(renderOrder[ro]); + setting.setColour(ap.seqPanel.seqCanvas.getFeatureRenderer() + .getColour(renderOrder[ro]).getRGB()); + + setting.setDisplay(av.featuresDisplayed + .containsKey(renderOrder[ro])); + float rorder = ap.seqPanel.seqCanvas.getFeatureRenderer().getOrder( + renderOrder[ro]); + if (rorder > -1) { - if (cs.conservationApplied()) - { - view.setConsThreshold(cs.getConservationInc()); - if (cs instanceof jalview.schemes.UserColourScheme) - view.setBgColour(SetUserColourScheme(cs, userColours, jms)); - } - - if (cs instanceof ResidueColourScheme) - { - view.setPidThreshold(cs.getThreshold()); - } + setting.setOrder(rorder); } + fs.addSetting(setting); + settingsAdded.addElement(renderOrder[ro]); + } - view.setConservationSelected(av.getConservationSelected()); - view.setPidSelected(av.getAbovePIDThreshold()); - view.setFontName(av.font.getName()); - view.setFontSize(av.font.getSize()); - view.setFontStyle(av.font.getStyle()); - view.setRenderGaps(av.renderGaps); - view.setShowAnnotation(av.getShowAnnotation()); - view.setShowBoxes(av.getShowBoxes()); - view.setShowColourText(av.getColourText()); - view.setShowFullId(av.getShowJVSuffix()); - view.setRightAlignIds(av.rightAlignIds); - view.setShowSequenceFeatures(av.showSequenceFeatures); - view.setShowText(av.getShowText()); - view.setWrapAlignment(av.getWrapAlignment()); - view.setTextCol1(av.textColour.getRGB()); - view.setTextCol2(av.textColour2.getRGB()); - view.setTextColThreshold(av.thresholdTextColour); - - - if(av.featuresDisplayed!=null) + //Make sure we save none displayed feature settings + Enumeration en = ap.seqPanel.seqCanvas.getFeatureRenderer().featureColours + .keys(); + while (en.hasMoreElements()) + { + String key = en.nextElement().toString(); + if (settingsAdded.contains(key)) { - jalview.schemabinding.version2.FeatureSettings fs - = new jalview.schemabinding.version2.FeatureSettings(); - - String [] renderOrder = - ap.seqPanel.seqCanvas.getFeatureRenderer().renderOrder; - - Vector settingsAdded = new Vector(); - for(int ro=0; ro -1) + { + setting.setOrder(rorder); + } + fs.addSetting(setting); + settingsAdded.addElement(key); + } + en = ap.seqPanel.seqCanvas.getFeatureRenderer().featureGroups.keys(); + Vector groupsAdded = new Vector(); + while (en.hasMoreElements()) + { + String grp = en.nextElement().toString(); + if (groupsAdded.contains(grp)) + { + continue; + } + Group g = new Group(); + g.setName(grp); + g + .setDisplay(((Boolean) ap.seqPanel.seqCanvas + .getFeatureRenderer().featureGroups.get(grp)) + .booleanValue()); + fs.addGroup(g); + groupsAdded.addElement(grp); + } + jms.setFeatureSettings(fs); - Setting setting = new Setting(); - setting.setType(key); - setting.setColour( - ap.seqPanel.seqCanvas.getFeatureRenderer().getColour(key).getRGB() - ); + } - setting.setDisplay(false); + if (av.hasHiddenColumns) + { + for (int c = 0; c < av.getColumnSelection().getHiddenColumns().size(); c++) + { + int[] region = (int[]) av.getColumnSelection().getHiddenColumns() + .elementAt(c); + HiddenColumns hc = new HiddenColumns(); + hc.setStart(region[0]); + hc.setEnd(region[1]); + view.addHiddenColumns(hc); + } + } - fs.addSetting(setting); - settingsAdded.addElement(key); - } + jms.addViewport(view); - jms.setFeatureSettings(fs); + object.setJalviewModelSequence(jms); + object.getVamsasModel().addSequenceSet(vamsasSet); + if (jout!=null && fileName!=null) + { + //We may not want to write the object to disk, + //eg we can copy the alignViewport to a new view object + //using save and then load + try + { + JarEntry entry = new JarEntry(fileName); + jout.putNextEntry(entry); + PrintWriter pout = new PrintWriter(new OutputStreamWriter(jout, + "UTF-8")); + org.exolab.castor.xml.Marshaller marshaller = new org.exolab.castor.xml.Marshaller(pout); + marshaller.marshal(object); + pout.flush(); + jout.closeEntry(); + } catch (Exception ex) + { + // TODO: raise error in GUI if marshalling failed. + ex.printStackTrace(); + } + } + return object; + } + + private Sequence createVamsasSequence(String id, SequenceI jds) + { + return createVamsasSequence(true, id, jds, null); + } + + private Sequence createVamsasSequence(boolean recurse, String id, + SequenceI jds, SequenceI parentseq) + { + Sequence vamsasSeq = new Sequence(); + vamsasSeq.setId(id); + vamsasSeq.setName(jds.getName()); + vamsasSeq.setSequence(jds.getSequenceAsString()); + vamsasSeq.setDescription(jds.getDescription()); + jalview.datamodel.DBRefEntry[] dbrefs = null; + if (jds.getDatasetSequence() != null) + { + vamsasSeq.setDsseqid(seqHash(jds.getDatasetSequence())); + if (jds.getDatasetSequence().getDBRef() != null) + { + dbrefs = jds.getDatasetSequence().getDBRef(); + } + } + else + { + vamsasSeq.setDsseqid(id); // so we can tell which sequences really are dataset sequences only + dbrefs = jds.getDBRef(); + } + if (dbrefs != null) + { + for (int d = 0; d < dbrefs.length; d++) + { + DBRef dbref = new DBRef(); + dbref.setSource(dbrefs[d].getSource()); + dbref.setVersion(dbrefs[d].getVersion()); + dbref.setAccessionId(dbrefs[d].getAccessionId()); + if (dbrefs[d].hasMap()) + { + Mapping mp = createVamsasMapping(dbrefs[d].getMap(), parentseq, + jds, recurse); + dbref.setMapping(mp); } + vamsasSeq.addDBRef(dbref); + } + } + return vamsasSeq; + } + + private Mapping createVamsasMapping(jalview.datamodel.Mapping jmp, + SequenceI parentseq, SequenceI jds, boolean recurse) + { + Mapping mp = null; + if (jmp.getMap() != null) + { + mp = new Mapping(); - if(av.hasHiddenColumns) + jalview.util.MapList mlst = jmp.getMap(); + int r[] = mlst.getFromRanges(); + for (int s = 0; s < r.length; s += 2) + { + MapListFrom mfrom = new MapListFrom(); + mfrom.setStart(r[s]); + mfrom.setEnd(r[s + 1]); + mp.addMapListFrom(mfrom); + } + r = mlst.getToRanges(); + for (int s = 0; s < r.length; s += 2) + { + MapListTo mto = new MapListTo(); + mto.setStart(r[s]); + mto.setEnd(r[s + 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())) { - for(int c=0; c 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); - } + for (int i = 0; i < uc.length; i++) + { + if (uc[i].getId().equals(id)) + { + colours = uc[i]; - return ucs; + break; + } } + java.awt.Color[] newColours = new java.awt.Color[24]; - /** - * DOCUMENT ME! - * - * @param file DOCUMENT ME! - */ - public AlignFrame LoadJalviewAlign(final String file) + for (int i = 0; i < 24; i++) { - uniqueSetSuffix = System.currentTimeMillis()%100000 +""; + newColours[i] = new java.awt.Color(Integer.parseInt(colours + .getUserColourScheme().getColour(i).getRGB(), 16)); + } - jalview.gui.AlignFrame af = null; + jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme( + newColours); - seqRefIds = new Hashtable(); - viewportsAdded = new Hashtable(); + 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); + } - Vector gatherToThisFrame= new Vector(); + return ucs; + } - try - { - //UNMARSHALLER SEEMS TO CLOSE JARINPUTSTREAM, MOST ANNOYING - URL url = null; + /** + * DOCUMENT ME! + * + * @param file DOCUMENT ME! + */ + public AlignFrame LoadJalviewAlign(final String file) + { + uniqueSetSuffix = System.currentTimeMillis() % 100000 + ""; - if (file.startsWith("http://")) - { - url = new URL(file); - } + jalview.gui.AlignFrame af = null; - JarInputStream jin = null; - JarEntry jarentry = null; - int entryCount = 1; + seqRefIds = new Hashtable(); + viewportsAdded = new Hashtable(); + frefedSequence = new Vector(); + Hashtable gatherToThisFrame = new Hashtable(); - do - { - if (url != null) - { - jin = new JarInputStream(url.openStream()); - } - else - { - jin = new JarInputStream(new FileInputStream(file)); - } + String errorMessage = null; - for (int i = 0; i < entryCount; i++) - { - jarentry = jin.getNextJarEntry(); - } + try + { + //UNMARSHALLER SEEMS TO CLOSE JARINPUTSTREAM, MOST ANNOYING + URL url = null; - if (jarentry != null && jarentry.getName().endsWith(".xml")) - { - InputStreamReader in = new InputStreamReader(jin, "UTF-8"); - JalviewModel object = new JalviewModel(); + if (file.startsWith("http://")) + { + url = new URL(file); + } - Unmarshaller unmar = new Unmarshaller(object); - unmar.setValidation(false); - object = (JalviewModel) unmar.unmarshal( in ); + JarInputStream jin = null; + JarEntry jarentry = null; + int entryCount = 1; - af = LoadFromObject(object, file, true); - if(af.viewport.gatherViewsHere) - { - gatherToThisFrame.add(af); - } - entryCount++; - } - else if (jarentry != null) - { - //Some other file here. - entryCount++; - } - } - while (jarentry != null); + do + { + if (url != null) + { + jin = new JarInputStream(url.openStream()); } - catch(java.net.UnknownHostException ex) + else { - ex.printStackTrace(); - System.err.println("Couldn't locate Jalview XML file : " + - ex + "\n"); + jin = new JarInputStream(new FileInputStream(file)); + } - javax.swing.SwingUtilities.invokeLater(new Runnable() - { - public void run() - { - JOptionPane.showInternalMessageDialog(Desktop.desktop, - "Couldn't locate " + file, - "URL not found", - JOptionPane.WARNING_MESSAGE); - } - }); + for (int i = 0; i < entryCount; i++) + { + jarentry = jin.getNextJarEntry(); } - catch (Exception ex) + + if (jarentry != null && jarentry.getName().endsWith(".xml")) { - //Is Version 1 Jar file? - af = new Jalview2XML_V1().LoadJalviewAlign(file); + 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(af!=null) + af = LoadFromObject(object, file, true); + if (af.viewport.gatherViewsHere) { - System.out.println("Successfully loaded archive file"); - return af; + gatherToThisFrame.put(af.viewport.getSequenceSetId(), af); } - - System.err.println("Exception whilst loading jalview XML file : " + - ex + "\n"); - javax.swing.SwingUtilities.invokeLater(new Runnable() - { - public void run() - { - - JOptionPane.showInternalMessageDialog(Desktop.desktop, - "Error loading " + file, - "Error loading Jalview file", - JOptionPane.WARNING_MESSAGE); - }}); + entryCount++; } - - if (Desktop.instance != null) - Desktop.instance.stopLoading(); - - for (int i = 0; i < gatherToThisFrame.size(); i++) + else if (jarentry != null) { - Desktop.instance.gatherViews( - (AlignFrame) gatherToThisFrame.elementAt(i)); + //Some other file here. + entryCount++; } + } while (jarentry != null); + resolveFrefedSequences(); + } catch (java.io.FileNotFoundException ex) + { + ex.printStackTrace(); + errorMessage = "Couldn't locate Jalview XML file : " + file; + System.err.println("Exception whilst loading jalview XML file : " + + ex + "\n"); + } catch (java.net.UnknownHostException 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); + + //Is Version 1 Jar file? + try { + af = new Jalview2XML_V1(raiseGUI).LoadJalviewAlign(file); + } 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"); } - String loadPDBFile(String file, String pdbId) + if (Desktop.instance != null) { - System.out.println(file +" "+pdbId); - try - { - JarInputStream jin = null; + Desktop.instance.stopLoading(); + } + + Enumeration en = gatherToThisFrame.elements(); + while (en.hasMoreElements()) + { + Desktop.instance.gatherViews((AlignFrame) en.nextElement()); + } - if (file.startsWith("http://")) + if (errorMessage != null) + { + final String finalErrorMessage = errorMessage; + if (raiseGUI) { - jin = new JarInputStream(new URL(file).openStream()); - } - else + javax.swing.SwingUtilities.invokeLater(new Runnable() { - jin = new JarInputStream(new FileInputStream(file)); - } - - JarEntry entry = null; - do + public void run() { - entry = jin.getNextJarEntry(); + JOptionPane.showInternalMessageDialog(Desktop.desktop, + finalErrorMessage, "Error loading Jalview file", + JOptionPane.WARNING_MESSAGE); } - while (!entry.getName().equals(pdbId)); + }); + } else { + System.err.println("Problem loading Jalview file: "+errorMessage); + } + } - BufferedReader in = new BufferedReader(new InputStreamReader(jin)); - File outFile = File.createTempFile("jalview_pdb", ".txt"); - outFile.deleteOnExit(); - PrintWriter out = new PrintWriter(new FileOutputStream(outFile)); - String data; + return af; + } - while ( (data = in.readLine()) != null) - { - out.println(data); - } - out.close(); - return outFile.getAbsolutePath(); + Hashtable alreadyLoadedPDB; + + String loadPDBFile(String file, String pdbId) + { + if (alreadyLoadedPDB == null) + alreadyLoadedPDB = new Hashtable(); + + if (alreadyLoadedPDB.containsKey(pdbId)) + return alreadyLoadedPDB.get(pdbId).toString(); + try + { + JarInputStream jin = null; + + if (file.startsWith("http://")) + { + jin = new JarInputStream(new URL(file).openStream()); } - catch (Exception ex) + else { - ex.printStackTrace(); + jin = new JarInputStream(new FileInputStream(file)); } - return null; - } - + JarEntry entry = null; + do + { + entry = jin.getNextJarEntry(); + } while (!entry.getName().equals(pdbId)); - AlignFrame LoadFromObject(JalviewModel object, - String file, - boolean loadTrees ) - { - SequenceSet vamsasSet = object.getVamsasModel().getSequenceSet(0); - Sequence[] vamsasSeq = vamsasSet.getSequence(); + BufferedReader in = new BufferedReader(new InputStreamReader(jin)); + File outFile = File.createTempFile("jalview_pdb", ".txt"); + outFile.deleteOnExit(); + PrintWriter out = new PrintWriter(new FileOutputStream(outFile)); + String data; - JalviewModelSequence jms = object.getJalviewModelSequence(); + while ((data = in.readLine()) != null) + { + out.println(data); + } + try { out.flush(); } catch (Exception foo) {}; + out.close(); - Viewport view = jms.getViewport(0); + alreadyLoadedPDB.put(pdbId, outFile.getAbsolutePath()); + return outFile.getAbsolutePath(); - ////////////////////////////////// - //LOAD SEQUENCES + } catch (Exception ex) + { + ex.printStackTrace(); + } - Vector hiddenSeqs = null; - jalview.datamodel.Sequence jseq; + return null; + } - ArrayList tmpseqs = new ArrayList(); + AlignFrame LoadFromObject(JalviewModel object, String file, + boolean loadTreesAndStructures) + { + SequenceSet vamsasSet = object.getVamsasModel().getSequenceSet(0); + Sequence[] vamsasSeq = vamsasSet.getSequence(); - boolean multipleView = false; + JalviewModelSequence jms = object.getJalviewModelSequence(); - JSeq[] JSEQ = object.getJalviewModelSequence().getJSeq(); - for (int i = 0; i < JSEQ.length; i++) - { - String seqId = JSEQ[i].getId() + ""; + Viewport view = jms.getViewport(0); - if (seqRefIds.get(seqId) != null) - { - tmpseqs.add( (jalview.datamodel.Sequence) seqRefIds.get(seqId)); - multipleView = true; - } - else - { - jseq = new jalview.datamodel.Sequence(vamsasSeq[i].getName(), - vamsasSeq[i].getSequence()); - jseq.setDescription(vamsasSeq[i].getDescription()); - jseq.setStart(JSEQ[i].getStart()); - jseq.setEnd(JSEQ[i].getEnd()); - seqRefIds.put(vamsasSeq[i].getId(), jseq); - tmpseqs.add( jseq ); - } + ////////////////////////////////// + //LOAD SEQUENCES + Vector hiddenSeqs = null; + jalview.datamodel.Sequence jseq; + ArrayList tmpseqs = new ArrayList(); - if (JSEQ[i].getHidden()) - { - if (hiddenSeqs == null) - hiddenSeqs = new Vector(); + boolean multipleView = false; + JSeq[] JSEQ = object.getJalviewModelSequence().getJSeq(); + int vi=0; // counter in vamsasSeq array + for (int i = 0; i < JSEQ.length; i++) + { + String seqId = JSEQ[i].getId() + ""; - hiddenSeqs.addElement( - (jalview.datamodel.Sequence) seqRefIds.get(seqId)); - } + if (seqRefIds.get(seqId) != null) + { + tmpseqs.add((jalview.datamodel.Sequence) seqRefIds.get(seqId)); + multipleView = true; + } + else + { + jseq = new jalview.datamodel.Sequence(vamsasSeq[vi].getName(), + vamsasSeq[vi].getSequence()); + jseq.setDescription(vamsasSeq[vi].getDescription()); + jseq.setStart(JSEQ[i].getStart()); + jseq.setEnd(JSEQ[i].getEnd()); + jseq.setVamsasId(uniqueSetSuffix + seqId); + seqRefIds.put(vamsasSeq[vi].getId()+"", jseq); + tmpseqs.add(jseq); + vi++; + } + if (JSEQ[i].getHidden()) + { + if (hiddenSeqs == null) + { + hiddenSeqs = new Vector(); } - ///SequenceFeatures are added to the DatasetSequence, - // so we must create the dataset before loading features - ///////////////////////////////// - + hiddenSeqs.addElement((jalview.datamodel.Sequence) seqRefIds + .get(seqId)); + } - jalview.datamodel.Sequence[] orderedSeqs = new jalview.datamodel.Sequence[ - tmpseqs.size()]; + } - tmpseqs.toArray(orderedSeqs) ; + /// + // Create the alignment object from the sequence set + ///////////////////////////////// + jalview.datamodel.Sequence[] orderedSeqs = new jalview.datamodel.Sequence[tmpseqs + .size()]; + tmpseqs.toArray(orderedSeqs); - jalview.datamodel.Alignment al = - new jalview.datamodel.Alignment(orderedSeqs); + jalview.datamodel.Alignment al = new jalview.datamodel.Alignment( + orderedSeqs); - al.setDataset(null); - ///////////////////////////////// + /// 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 + { + recoverDatasetFor(vamsasSet, al); + } + ///////////////////////////////// - Hashtable pdbloaded = new Hashtable(); - if(!multipleView) + Hashtable pdbloaded = new Hashtable(); + if (!multipleView) + { + for (int i = 0; i < vamsasSeq.length; i++) + { + if (JSEQ[i].getFeaturesCount() > 0) { - for (int i = 0; i < vamsasSeq.length; i++) + Features[] features = JSEQ[i].getFeatures(); + for (int f = 0; f < features.length; f++) { - if (JSEQ[i].getFeaturesCount() > 0) + 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()); + for (int od = 0; od < features[f].getOtherDataCount(); od++) { - Features[] features = JSEQ[i].getFeatures(); - for (int f = 0; f < features.length; f++) + OtherData keyValue = features[f].getOtherData(od); + if (keyValue.getKey().startsWith("LINK")) { - 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()); - for (int od = 0; od < features[f].getOtherDataCount(); od++) - { - OtherData keyValue = features[f].getOtherData(od); - if (keyValue.getKey().startsWith("LINK")) - sf.addLink(keyValue.getValue()); - else - sf.setValue(keyValue.getKey(), keyValue.getValue()); - - } - - al.getSequenceAt(i).getDatasetSequence().addSequenceFeature(sf); + sf.addLink(keyValue.getValue()); + } + else + { + sf.setValue(keyValue.getKey(), keyValue.getValue()); } + } - if (JSEQ[i].getPdbidsCount() > 0) + + al.getSequenceAt(i).getDatasetSequence().addSequenceFeature(sf); + } + } + if (vamsasSeq[i].getDBRefCount() > 0) + { + addDBRefs(al.getSequenceAt(i).getDatasetSequence(), vamsasSeq[i]); + } + if (JSEQ[i].getPdbidsCount() > 0) + { + Pdbids[] ids = JSEQ[i].getPdbids(); + for (int p = 0; p < ids.length; p++) + { + jalview.datamodel.PDBEntry entry = new jalview.datamodel.PDBEntry(); + entry.setId(ids[p].getId()); + entry.setType(ids[p].getType()); + if (ids[p].getFile() != null) { - Pdbids[] ids = JSEQ[i].getPdbids(); - for (int p = 0; p < ids.length; p++) + if (!pdbloaded.containsKey(ids[p].getFile())) { - jalview.datamodel.PDBEntry entry = new jalview.datamodel. - PDBEntry(); - entry.setId(ids[p].getId()); - entry.setType(ids[p].getType()); - if (ids[p].getFile() != null) - { - if (!pdbloaded.containsKey(ids[p].getFile())) - { - String tmppdb = loadPDBFile(file, ids[p].getId()); - entry.setFile(tmppdb); - pdbloaded.put(ids[p].getId(), tmppdb); - } - else - entry.setFile(pdbloaded.get(ids[p].getId()).toString()); - } - - al.getSequenceAt(i).getDatasetSequence().addPDBId(entry); + entry.setFile(loadPDBFile(file, ids[p].getId())); + } + else + { + entry.setFile(pdbloaded.get(ids[p].getId()).toString()); } } - if (vamsasSeq[i].getDBRefCount() > 0) + + al.getSequenceAt(i).getDatasetSequence().addPDBId(entry); + } + } + } + } + + ///////////////////////////////// + // LOAD SEQUENCE MAPPINGS + if (vamsasSet.getAlcodonFrameCount() > 0) + { + AlcodonFrame[] alc = vamsasSet.getAlcodonFrame(); + for (int i = 0; i < alc.length; i++) + { + jalview.datamodel.AlignedCodonFrame cf = new jalview.datamodel.AlignedCodonFrame( + alc[i].getAlcodonCount()); + if (alc[i].getAlcodonCount() > 0) + { + Alcodon[] alcods = alc[i].getAlcodon(); + for (int p = 0; p < cf.codons.length; p++) + { + cf.codons[p] = new int[3]; + cf.codons[p][0] = (int) alcods[p].getPos1(); + cf.codons[p][1] = (int) alcods[p].getPos2(); + cf.codons[p][2] = (int) alcods[p].getPos3(); + } + } + if (alc[i].getAlcodMapCount() > 0) + { + AlcodMap[] maps = alc[i].getAlcodMap(); + for (int m = 0; m < maps.length; m++) + { + SequenceI dnaseq = (SequenceI) seqRefIds + .get(maps[m].getDnasq()); + // Load Mapping + // attach to dna sequence reference. + if (dnaseq != null) { - for (int d = 0; d < vamsasSeq[i].getDBRefCount(); d++) + if (maps[m].getMapping() != null) { - jalview.datamodel.DBRefEntry entry = - new jalview.datamodel.DBRefEntry( - vamsasSeq[i].getDBRef(d).getSource(), - vamsasSeq[i].getDBRef(d).getVersion(), - vamsasSeq[i].getDBRef(d).getAccessionId() - ); - al.getSequenceAt(i).getDatasetSequence().addDBRef(entry); + jalview.datamodel.Mapping mapping = addMapping(maps[m] + .getMapping()); + cf.addMap(dnaseq, mapping.getTo(), mapping.getMap()); } - } } } + al.addCodonFrame(cf); + } + + } + ////////////////////////////////// + //LOAD ANNOTATIONS + boolean hideQuality = true, hideConservation = true, hideConsensus = true; - ///////////////////////////////// - ////////////////////////////////// - //LOAD ANNOTATIONS - boolean hideQuality = true, - hideConservation = true, - hideConsensus = true; + if (vamsasSet.getAnnotationCount() > 0) + { + Annotation[] an = vamsasSet.getAnnotation(); + + for (int i = 0; i < an.length; i++) + { + if (an[i].getLabel().equals("Quality")) + { + hideQuality = false; + continue; + } + else if (an[i].getLabel().equals("Conservation")) + { + hideConservation = false; + continue; + } + else if (an[i].getLabel().equals("Consensus")) + { + hideConsensus = false; + continue; + } - if (vamsasSet.getAnnotationCount()>0) + if (an[i].getId() != null + && annotationIds.containsKey(an[i].getId())) { - Annotation[] an = vamsasSet.getAnnotation(); + jalview.datamodel.AlignmentAnnotation jda = (jalview.datamodel.AlignmentAnnotation) annotationIds + .get(an[i].getId()); + if (an[i].hasVisible()) + jda.visible = an[i].getVisible(); - for (int i = 0; i < an.length; i++) - { - if (an[i].getLabel().equals("Quality")) - { - hideQuality = false; - continue; - } - else if(an[i].getLabel().equals("Conservation")) - { - hideConservation = false; - continue; - } - else if(an[i].getLabel().equals("Consensus")) - { - hideConsensus = false; - continue; - } - - AnnotationElement[] ae = an[i].getAnnotationElement(); - jalview.datamodel.Annotation[] anot = new jalview.datamodel.Annotation[al.getWidth()]; - - for (int aa = 0; aa < ae.length; aa++) - { - anot[ae[aa].getPosition()] = new jalview.datamodel.Annotation(ae[aa].getDisplayCharacter(), - ae[aa].getDescription(), - ae[aa].getSecondaryStructure().length()==0?' ':ae[aa].getSecondaryStructure().charAt(0), - ae[aa].getValue()); - anot[ae[aa].getPosition()].colour = new java.awt.Color( ae[aa].getColour() ); - } + al.addAnnotation(jda); - jalview.datamodel.AlignmentAnnotation jaa = null; + continue; + } - if (an[i].getGraph()) - { - jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(), - an[i].getDescription(), anot, 0, 0, - an[i].getGraphType()); + AnnotationElement[] ae = an[i].getAnnotationElement(); + jalview.datamodel.Annotation[] anot = null; - jaa.graphGroup = an[i].getGraphGroup(); + if (!an[i].getScoreOnly()) + { + anot = new jalview.datamodel.Annotation[al.getWidth()]; - if (an[i].getThresholdLine() != null) - { - jaa.setThreshold(new jalview.datamodel.GraphLine( - an[i].getThresholdLine().getValue(), - an[i].getThresholdLine().getLabel(), - new java.awt.Color(an[i].getThresholdLine().getColour())) - ); + for (int aa = 0; aa < ae.length && aa < anot.length; aa++) + { + if (ae[aa].getPosition() >= anot.length) + continue; - } + anot[ae[aa].getPosition()] = new jalview.datamodel.Annotation( + + ae[aa].getDisplayCharacter(), ae[aa].getDescription(), (ae[aa] + .getSecondaryStructure() == null || ae[aa] + .getSecondaryStructure().length() == 0) ? ' ' : ae[aa] + .getSecondaryStructure().charAt(0), ae[aa].getValue() + + ); + // JBPNote: Consider verifying dataflow for IO of secondary structure annotation read from Stockholm files + // this was added to try to ensure that + //if (anot[ae[aa].getPosition()].secondaryStructure>' ') + //{ + // anot[ae[aa].getPosition()].displayCharacter = ""; + //} + anot[ae[aa].getPosition()].colour = new java.awt.Color(ae[aa] + .getColour()); + } + } + jalview.datamodel.AlignmentAnnotation jaa = null; - } - else - { - jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(), - an[i].getDescription(), anot); - } + if (an[i].getGraph()) + { + jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(), + an[i].getDescription(), anot, 0, 0, an[i].getGraphType()); - if(an[i].getSequenceRef()!=null) - { - jaa.createSequenceMapping( - al.findName(an[i].getSequenceRef()), 1, true - ); - al.findName(an[i].getSequenceRef()).addAlignmentAnnotation(jaa); - } + jaa.graphGroup = an[i].getGraphGroup(); - al.addAnnotation(jaa); - } + if (an[i].getThresholdLine() != null) + { + jaa.setThreshold(new jalview.datamodel.GraphLine(an[i] + .getThresholdLine().getValue(), an[i] + .getThresholdLine().getLabel(), new java.awt.Color( + an[i].getThresholdLine().getColour()))); + + } + + } + else + { + jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(), + an[i].getDescription(), anot); } - ///////////////////////// - //LOAD GROUPS - if (jms.getJGroupCount() > 0) + if (an[i].getId() != null) { - JGroup[] groups = jms.getJGroup(); + annotationIds.put(an[i].getId(), jaa); + jaa.annotationId = an[i].getId(); + } - for (int i = 0; i < groups.length; i++) - { - ColourSchemeI cs = null; + if (an[i].getSequenceRef() != null) + { + if (al.findName(an[i].getSequenceRef()) != null) + { + jaa.createSequenceMapping(al.findName(an[i].getSequenceRef()), + 1, true); + al.findName(an[i].getSequenceRef()).addAlignmentAnnotation(jaa); + } + } + if (an[i].hasScore()) + { + jaa.setScore(an[i].getScore()); + } - if (groups[i].getColour() != null) - { - if (groups[i].getColour().startsWith("ucs")) - { - cs = GetUserColourScheme(jms, groups[i].getColour()); - } - else - { - cs = ColourSchemeProperty.getColour(al, - groups[i].getColour()); - } + if (an[i].hasVisible()) + jaa.visible = an[i].getVisible(); - if(cs!=null) - cs.setThreshold(groups[i].getPidThreshold(), true); - } + al.addAnnotation(jaa); + } + } - Vector seqs = new Vector(); + ///////////////////////// + //LOAD GROUPS + if (jms.getJGroupCount() > 0) + { + JGroup[] groups = jms.getJGroup(); - for (int s = 0; s < groups[i].getSeqCount(); s++) - { - String seqId = groups[i].getSeq(s)+""; - seqs.addElement((jalview.datamodel.SequenceI) seqRefIds.get(seqId)); - } + for (int i = 0; i < groups.length; i++) + { + ColourSchemeI cs = null; - jalview.datamodel.SequenceGroup sg = new jalview.datamodel.SequenceGroup(seqs, - groups[i].getName(), cs, groups[i].getDisplayBoxes(), - groups[i].getDisplayText(), groups[i].getColourText(), - groups[i].getStart(), groups[i].getEnd()); + if (groups[i].getColour() != null) + { + if (groups[i].getColour().startsWith("ucs")) + { + cs = GetUserColourScheme(jms, groups[i].getColour()); + } + else + { + cs = ColourSchemeProperty.getColour(al, groups[i].getColour()); + } - sg.setOutlineColour(new java.awt.Color( - groups[i].getOutlineColour())); + if (cs != null) + { + cs.setThreshold(groups[i].getPidThreshold(), true); + } + } - sg.textColour = new java.awt.Color(groups[i].getTextCol1()); - sg.textColour2 = new java.awt.Color(groups[i].getTextCol2()); - sg.thresholdTextColour = groups[i].getTextColThreshold(); + Vector seqs = new Vector(); - if (groups[i].getConsThreshold() != 0) - { - jalview.analysis.Conservation c = new jalview.analysis.Conservation("All", - ResidueProperties.propHash, 3, sg.getSequences(null), 0, - sg.getWidth() - 1); - c.calculate(); - c.verdict(false, 25); - sg.cs.setConservation(c); - } + for (int s = 0; s < groups[i].getSeqCount(); s++) + { + String seqId = groups[i].getSeq(s) + ""; + jalview.datamodel.SequenceI ts = (jalview.datamodel.SequenceI) seqRefIds + .get(seqId); - al.addGroup(sg); - } + if (ts != null) + { + seqs.addElement(ts); + } } + if (seqs.size() < 1) + { + continue; + } - ///////////////////////////////// - // LOAD VIEWPORT + jalview.datamodel.SequenceGroup sg = new jalview.datamodel.SequenceGroup( + seqs, groups[i].getName(), cs, groups[i].getDisplayBoxes(), + groups[i].getDisplayText(), groups[i].getColourText(), + groups[i].getStart(), groups[i].getEnd()); - AlignFrame af = new AlignFrame(al, - view.getWidth(), - view.getHeight() ); + sg + .setOutlineColour(new java.awt.Color(groups[i] + .getOutlineColour())); - af.setFileName(file, "Jalview"); + sg.textColour = new java.awt.Color(groups[i].getTextCol1()); + sg.textColour2 = new java.awt.Color(groups[i].getTextCol2()); + sg.thresholdTextColour = groups[i].getTextColThreshold(); - for (int i = 0; i < JSEQ.length; i++) + if (groups[i].getConsThreshold() != 0) { - af.viewport.setSequenceColour( - af.viewport.alignment.getSequenceAt(i), - new java.awt.Color( - JSEQ[i].getColour())); + jalview.analysis.Conservation c = new jalview.analysis.Conservation( + "All", ResidueProperties.propHash, 3, sg + .getSequences(null), 0, sg.getWidth() - 1); + c.calculate(); + c.verdict(false, 25); + sg.cs.setConservation(c); } - //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; - - af.viewport.gatherViewsHere = view.getGatheredViews(); + al.addGroup(sg); + } + } - if (view.getSequenceSetId() != null) - { - jalview.gui.AlignViewport av = - (jalview.gui.AlignViewport) - viewportsAdded.get(uniqueSeqSetId); + ///////////////////////////////// + // LOAD VIEWPORT - af.viewport.sequenceSetID = uniqueSeqSetId; - if(av!=null) - { + AlignFrame af = new AlignFrame(al, view.getWidth(), view.getHeight()); - af.viewport.historyList = av.historyList; - af.viewport.redoList = av.redoList; - } - else - { - viewportsAdded.put(uniqueSeqSetId, af.viewport); - } + af.setFileName(file, "Jalview"); - PaintRefresher.Register(af.alignPanel, uniqueSeqSetId); - } - if(hiddenSeqs!=null) - { - for(int s=0; s