X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FJalview2XML.java;h=ebf7aba0e1addc316c183227aac793c79b4e8c4d;hb=60f2d6c034560415fd0139c8bc7df0c19cae1186;hp=f1f7db19d93985d2172c77d728052518f351e99b;hpb=451619e33c0a90c8130c7d79ffa38161af1c6e0f;p=jalview.git diff --git a/src/jalview/gui/Jalview2XML.java b/src/jalview/gui/Jalview2XML.java index f1f7db1..ebf7aba 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,155 @@ */ 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 + { + if (ref[1] instanceof jalview.datamodel.AlignedCodonFrame) + { + SequenceI seq = (SequenceI) seqRefIds.get(sref); + while (seq.getDatasetSequence() != null) + { + seq = seq.getDatasetSequence(); + } + if (ref[2] != null + && ref[2] instanceof jalview.datamodel.Mapping) + { + jalview.datamodel.Mapping mp = (jalview.datamodel.Mapping) ref[2]; + ((jalview.datamodel.AlignedCodonFrame) ref[1]).addMap( + seq, mp.getTo(), mp.getMap()); + } + else + { + System.err + .println("IMPLEMENTATION ERROR: Unimplemented forward sequence references for AlcodonFrames involving " + + ref[2].getClass() + " type objects."); + } + } + else + { + System.err + .println("IMPLEMENTATION ERROR: Unimplemented forward sequence references for " + + ref[1].getClass() + " type objects."); + } + frefedSequence.remove(r); + rSize--; + } + } + else + { + System.err.println("IMPLEMENTATION WARNING: Unresolved forward reference for hash string "+ref[0]+" with objecttype "+ref[1].getClass()); + r++; + } + } + else + { + frefedSequence.remove(r); + rSize--; + } + } + } + } /** * This maintains a list of viewports, the key being the @@ -56,1627 +175,2372 @@ 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) + if (frames == null) { - JInternalFrame[] frames = Desktop.desktop.getAllFrames(); - - if (frames == null) - { - return; - } + 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) + { + 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 = - jal.getSequenceAt(i).getHiddenSequences().getSequencesInOrder(jal); - - for(int h=0; h -1) { - 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); - } - } - - //SAVE GROUPS - if (jal.getGroups() != null) - { - JGroup[] groups = new JGroup[jal.getGroups().size()]; - - for (int i = 0; i < groups.length; i++) + if (aa[i].annotations[a].colour != null + && aa[i].annotations[a].colour != java.awt.Color.black) { - 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()); - - 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].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(false); s++) - { - jalview.datamodel.Sequence seq = - (jalview.datamodel.Sequence) sg.getSequenceAt(s); - groups[i].addSeq(seq.hashCode()); - } + ae.setColour(aa[i].annotations[a].colour.getRGB()); } - 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); + an.addAnnotationElement(ae); + } } 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); + an.setScoreOnly(true); } + vamsasSet.addAnnotation(an); + } + } - view.setStartRes(av.startRes); - view.setStartSeq(av.startSeq); + //SAVE GROUPS + if (jal.getGroups() != null) + { + JGroup[] groups = new JGroup[jal.getGroups().size()]; - if (av.getGlobalColourScheme() instanceof jalview.schemes.UserColourScheme) - { - view.setBgColour(SetUserColourScheme(av.getGlobalColourScheme(), - userColours, jms)); - } - else if(av.getGlobalColourScheme() instanceof jalview.schemes.AnnotationColourGradient) + 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) { - jalview.schemes.AnnotationColourGradient acg - = (jalview.schemes.AnnotationColourGradient)av.getGlobalColourScheme(); + if (sg.cs.conservationApplied()) + { + groups[i].setConsThreshold(sg.cs.getConservationInc()); - 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)); + if (sg.cs instanceof jalview.schemes.UserColourScheme) + { + groups[i].setColour(SetUserColourScheme(sg.cs, userColours, + jms)); + } else - ac.setColourScheme(ColourSchemeProperty.getColourName(acg.getBaseColour())); - - ac.setMaxColour(acg.getMaxColour().getRGB()); - ac.setMinColour(acg.getMinColour().getRGB()); - view.setAnnotationColours(ac); - view.setBgColour("AnnotationColourGradient"); - } - else - { - view.setBgColour(ColourSchemeProperty.getColourName( - av.getGlobalColourScheme())); - } - - ColourSchemeI cs = av.getGlobalColourScheme(); - - if(cs!=null) - { - if (cs.conservationApplied()) + { + groups[i] + .setColour(ColourSchemeProperty.getColourName(sg.cs)); + } + } + else if (sg.cs instanceof jalview.schemes.AnnotationColourGradient) { - view.setConsThreshold(cs.getConservationInc()); - if (cs instanceof jalview.schemes.UserColourScheme) - view.setBgColour(SetUserColourScheme(cs, userColours, jms)); + groups[i] + .setColour(ColourSchemeProperty + .getColourName(((jalview.schemes.AnnotationColourGradient) sg.cs) + .getBaseColour())); } - - if (cs instanceof ResidueColourScheme) + else if (sg.cs instanceof jalview.schemes.UserColourScheme) { - view.setPidThreshold(cs.getThreshold()); + groups[i] + .setColour(SetUserColourScheme(sg.cs, userColours, jms)); + } + else + { + groups[i].setColour(ColourSchemeProperty.getColourName(sg.cs)); } - } - 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); + 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); - if(av.featuresDisplayed!=null) + for (int s = 0; s < sg.getSize(); s++) { - 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) { - //We may not want to right the object to disk, - //eg we can copy the alignViewport to a new view object - //using save and then load - try - { - if (!fileName.endsWith(".xml")) - { - fileName = fileName + ".xml"; - } - - JarEntry entry = new JarEntry(fileName); - jout.putNextEntry(entry); - - object.marshal(out); - } - catch (Exception ex) - { - ex.printStackTrace(); - } + setting.setOrder(rorder); } - return object; - } - - String SetUserColourScheme(jalview.schemes.ColourSchemeI cs, - Vector userColours, JalviewModelSequence jms) - { - String id = null; - jalview.schemes.UserColourScheme ucs = (jalview.schemes.UserColourScheme) cs; + fs.addSetting(setting); + settingsAdded.addElement(renderOrder[ro]); + } - if (!userColours.contains(ucs)) + //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)) { - userColours.add(ucs); - - 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(); + continue; + } - 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); - } - } + Setting setting = new Setting(); + setting.setType(key); + setting.setColour(ap.seqPanel.seqCanvas.getFeatureRenderer() + .getColour(key).getRGB()); - id = "ucs" + userColours.indexOf(ucs); - uc.setId(id); - uc.setUserColourScheme(jbucs); - jms.addUserColours(uc); + setting.setDisplay(false); + float rorder = ap.seqPanel.seqCanvas.getFeatureRenderer().getOrder( + key); + if (rorder > -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); - return id; } - jalview.schemes.UserColourScheme GetUserColourScheme( - JalviewModelSequence jms, String id) + if (av.hasHiddenColumns) { - UserColours[] uc = jms.getUserColours(); - UserColours colours = null; - - for (int i = 0; i < uc.length; i++) - { - if (uc[i].getId().equals(id)) - { - colours = uc[i]; + 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); + } + } - break; - } - } + jms.addViewport(view); - java.awt.Color[] newColours = new java.awt.Color[24]; + object.setJalviewModelSequence(jms); + object.getVamsasModel().addSequenceSet(vamsasSet); - for (int i = 0; i < 24; i++) + 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()) { - newColours[i] = new java.awt.Color(Integer.parseInt( - colours.getUserColourScheme().getColour(i).getRGB(), 16)); + 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(); - jalview.schemes.UserColourScheme ucs = - new jalview.schemes.UserColourScheme(newColours); - - if (colours.getUserColourScheme().getColourCount() > 24) + 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())) + { + mpc.setSequence(createVamsasSequence(false, seqHash(jmp.getTo()) + , jmp.getTo(), jds)); + } + else { - newColours = new java.awt.Color[23]; - for (int i = 0; i < 23; i++) + String jmpid = ""; + SequenceI ps = null; + if (parentseq != jmp.getTo() + && parentseq.getDatasetSequence() != jmp.getTo()) { - newColours[i] = new java.awt.Color(Integer.parseInt( - colours.getUserColourScheme().getColour(i+24).getRGB(), 16)); + // 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"); } - ucs.setLowerCaseColours(newColours); } - - return ucs; + mp.setMappingChoice(mpc); + } } + return mp; + } + String SetUserColourScheme(jalview.schemes.ColourSchemeI cs, + Vector userColours, JalviewModelSequence jms) + { + String id = null; + jalview.schemes.UserColourScheme ucs = (jalview.schemes.UserColourScheme) cs; - /** - * DOCUMENT ME! - * - * @param file DOCUMENT ME! - */ - public AlignFrame LoadJalviewAlign(final String file) + if (!userColours.contains(ucs)) { - uniqueSetSuffix = System.currentTimeMillis()%100000 +""; - - jalview.gui.AlignFrame af = null; - - seqRefIds = new Hashtable(); - viewportsAdded = new Hashtable(); + userColours.add(ucs); - Vector gatherToThisFrame= new Vector(); + 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(); - try + 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++) { - //UNMARSHALLER SEEMS TO CLOSE JARINPUTSTREAM, MOST ANNOYING - URL url = null; + 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); + } + } - if (file.startsWith("http://")) - { - url = new URL(file); - } + id = "ucs" + userColours.indexOf(ucs); + uc.setId(id); + uc.setUserColourScheme(jbucs); + jms.addUserColours(uc); + } - JarInputStream jin = null; - JarEntry jarentry = null; - int entryCount = 1; + return id; + } - do - { - if (url != null) - { - jin = new JarInputStream(url.openStream()); - } - else - { - jin = new JarInputStream(new FileInputStream(file)); - } + jalview.schemes.UserColourScheme GetUserColourScheme( + JalviewModelSequence jms, String id) + { + UserColours[] uc = jms.getUserColours(); + UserColours colours = null; - for (int i = 0; i < entryCount; i++) - { - jarentry = jin.getNextJarEntry(); - } + for (int i = 0; i < uc.length; i++) + { + if (uc[i].getId().equals(id)) + { + colours = uc[i]; - if (jarentry != null && jarentry.getName().endsWith(".xml")) - { - InputStreamReader in = new InputStreamReader(jin, "UTF-8"); - JalviewModel object = new JalviewModel(); + break; + } + } - Unmarshaller unmar = new Unmarshaller(object); - unmar.setValidation(false); - object = (JalviewModel) unmar.unmarshal( in ); + java.awt.Color[] newColours = new java.awt.Color[24]; - 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); - } - catch(java.net.UnknownHostException ex) - { - ex.printStackTrace(); - System.err.println("Couldn't locate Jalview XML file : " + - ex + "\n"); + for (int i = 0; i < 24; i++) + { + newColours[i] = new java.awt.Color(Integer.parseInt(colours + .getUserColourScheme().getColour(i).getRGB(), 16)); + } - javax.swing.SwingUtilities.invokeLater(new Runnable() - { - public void run() - { - JOptionPane.showInternalMessageDialog(Desktop.desktop, - "Couldn't locate " + file, - "URL not found", - JOptionPane.WARNING_MESSAGE); - } - }); - } - catch (Exception ex) - { - //Is Version 1 Jar file? - af = new Jalview2XML_V1().LoadJalviewAlign(file); + jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme( + newColours); - if(af!=null) - { - System.out.println("Successfully loaded archive file"); - return af; - } + 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); + } - System.err.println("Exception whilst loading jalview XML file : " + - ex + "\n"); - javax.swing.SwingUtilities.invokeLater(new Runnable() - { - public void run() - { + return ucs; + } - JOptionPane.showInternalMessageDialog(Desktop.desktop, - "Error loading " + file, - "Error loading Jalview file", - JOptionPane.WARNING_MESSAGE); - }}); - } + /** + * DOCUMENT ME! + * + * @param file DOCUMENT ME! + */ + public AlignFrame LoadJalviewAlign(final String file) + { + uniqueSetSuffix = System.currentTimeMillis() % 100000 + ""; - if (Desktop.instance != null) - Desktop.instance.stopLoading(); + jalview.gui.AlignFrame af = null; - for (int i = 0; i < gatherToThisFrame.size(); i++) - { - Desktop.instance.gatherViews( - (AlignFrame) gatherToThisFrame.elementAt(i)); - } + seqRefIds = new Hashtable(); + viewportsAdded = new Hashtable(); + frefedSequence = new Vector(); + Hashtable gatherToThisFrame = new Hashtable(); - return af; - } + String errorMessage = null; - String loadPDBFile(String file, String pdbId) + try { - System.out.println(file +" "+pdbId); - try + //UNMARSHALLER SEEMS TO CLOSE JARINPUTSTREAM, MOST ANNOYING + URL url = null; + + if (file.startsWith("http://")) { - JarInputStream jin = null; + url = new URL(file); + } + + JarInputStream jin = null; + JarEntry jarentry = null; + int entryCount = 1; - if (file.startsWith("http://")) + do + { + if (url != null) { - jin = new JarInputStream(new URL(file).openStream()); + jin = new JarInputStream(url.openStream()); } else { jin = new JarInputStream(new FileInputStream(file)); } - JarEntry entry = null; - do + for (int i = 0; i < entryCount; i++) { - entry = jin.getNextJarEntry(); + jarentry = jin.getNextJarEntry(); } - while (!entry.getName().equals(pdbId)); - 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; + 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); - while ( (data = in.readLine()) != null) + af = LoadFromObject(object, file, true); + if (af.viewport.gatherViewsHere) + { + gatherToThisFrame.put(af.viewport.getSequenceSetId(), af); + } + entryCount++; + } + else if (jarentry != null) { - out.println(data); + //Some other file here. + entryCount++; } - out.close(); - return outFile.getAbsolutePath(); + } 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(); } - catch (Exception ex) + if (af != null) { - ex.printStackTrace(); + System.out.println("Successfully loaded archive file"); + return af; } + ex.printStackTrace(); - return null; + System.err.println("Exception whilst loading jalview XML file : " + + ex + "\n"); } - - AlignFrame LoadFromObject(JalviewModel object, - String file, - boolean loadTrees ) + if (Desktop.instance != null) { - SequenceSet vamsasSet = object.getVamsasModel().getSequenceSet(0); - Sequence[] vamsasSeq = vamsasSet.getSequence(); + Desktop.instance.stopLoading(); + } - JalviewModelSequence jms = object.getJalviewModelSequence(); + Enumeration en = gatherToThisFrame.elements(); + while (en.hasMoreElements()) + { + Desktop.instance.gatherViews((AlignFrame) en.nextElement()); + } - Viewport view = jms.getViewport(0); + if (errorMessage != null) + { + final String finalErrorMessage = errorMessage; + if (raiseGUI) + { + javax.swing.SwingUtilities.invokeLater(new Runnable() + { + public void run() + { + JOptionPane.showInternalMessageDialog(Desktop.desktop, + finalErrorMessage, "Error loading Jalview file", + JOptionPane.WARNING_MESSAGE); + } + }); + } else { + System.err.println("Problem loading Jalview file: "+errorMessage); + } + } - ////////////////////////////////// - //LOAD SEQUENCES + return af; + } - Vector hiddenSeqs = null; - jalview.datamodel.Sequence jseq; + Hashtable alreadyLoadedPDB; - ArrayList tmpseqs = new ArrayList(); + String loadPDBFile(String file, String pdbId) + { + if (alreadyLoadedPDB == null) + alreadyLoadedPDB = new Hashtable(); - boolean multipleView = false; + if (alreadyLoadedPDB.containsKey(pdbId)) + return alreadyLoadedPDB.get(pdbId).toString(); - JSeq[] JSEQ = object.getJalviewModelSequence().getJSeq(); - for (int i = 0; i < JSEQ.length; i++) - { - String seqId = JSEQ[i].getId() + ""; + try + { + JarInputStream jin = null; - 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 ); - } + if (file.startsWith("http://")) + { + jin = new JarInputStream(new URL(file).openStream()); + } + else + { + jin = new JarInputStream(new FileInputStream(file)); + } + JarEntry entry = null; + do + { + entry = jin.getNextJarEntry(); + } while (!entry.getName().equals(pdbId)); + 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; - if (JSEQ[i].getHidden()) - { - if (hiddenSeqs == null) - hiddenSeqs = new Vector(); + while ((data = in.readLine()) != null) + { + out.println(data); + } + try { out.flush(); } catch (Exception foo) {}; + out.close(); + alreadyLoadedPDB.put(pdbId, outFile.getAbsolutePath()); + return outFile.getAbsolutePath(); - hiddenSeqs.addElement( - (jalview.datamodel.Sequence) seqRefIds.get(seqId)); - } + } catch (Exception ex) + { + ex.printStackTrace(); + } - } + return null; + } + + AlignFrame LoadFromObject(JalviewModel object, String file, + boolean loadTreesAndStructures) + { + SequenceSet vamsasSet = object.getVamsasModel().getSequenceSet(0); + Sequence[] vamsasSeq = vamsasSet.getSequence(); + + JalviewModelSequence jms = object.getJalviewModelSequence(); + + Viewport view = jms.getViewport(0); + + ////////////////////////////////// + //LOAD SEQUENCES + + Vector hiddenSeqs = null; + jalview.datamodel.Sequence jseq; + + ArrayList tmpseqs = new ArrayList(); + + boolean multipleView = false; - ///SequenceFeatures are added to the DatasetSequence, - // so we must create the dataset before loading features - ///////////////////////////////// + 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() + ""; + 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++; + } - jalview.datamodel.Sequence[] orderedSeqs = new jalview.datamodel.Sequence[ - tmpseqs.size()]; + if (JSEQ[i].getHidden()) + { + if (hiddenSeqs == null) + { + hiddenSeqs = new Vector(); + } - tmpseqs.toArray(orderedSeqs) ; + hiddenSeqs.addElement((jalview.datamodel.Sequence) seqRefIds + .get(seqId)); + } + } - jalview.datamodel.Alignment al = - new jalview.datamodel.Alignment(orderedSeqs); + /// + // Create the alignment object from the sequence set + ///////////////////////////////// + jalview.datamodel.Sequence[] orderedSeqs = new jalview.datamodel.Sequence[tmpseqs + .size()]; - al.setDataset(null); - ///////////////////////////////// + tmpseqs.toArray(orderedSeqs); + jalview.datamodel.Alignment al = new jalview.datamodel.Alignment( + orderedSeqs); - Hashtable pdbloaded = new Hashtable(); - if(!multipleView) + /// 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) + { + 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()); } - } - if (JSEQ[i].getPdbidsCount() > 0) - { - Pdbids[] ids = JSEQ[i].getPdbids(); - for (int p = 0; p < ids.length; p++) + else { - 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); + sf.setValue(keyValue.getKey(), keyValue.getValue()); } + } - if (vamsasSeq[i].getDBRefCount() > 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) { - for (int d = 0; d < vamsasSeq[i].getDBRefCount(); d++) + if (!pdbloaded.containsKey(ids[p].getFile())) { - 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); + entry.setFile(loadPDBFile(file, ids[p].getId())); } + else + { + entry.setFile(pdbloaded.get(ids[p].getId()).toString()); + } + } + 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 + jalview.datamodel.Mapping mapping = null; + // attach to dna sequence reference. + if (maps[m].getMapping() != null) + { + mapping = addMapping(maps[m] + .getMapping()); + } + if (dnaseq != null) + { + cf.addMap(dnaseq, mapping.getTo(), mapping.getMap()); + } else { + // defer to later + frefedSequence.add(new Object[] { maps[m].getDnasq(), cf, mapping}); } } } + 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) + 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")) { - Annotation[] an = vamsasSet.getAnnotation(); + hideConsensus = false; + continue; + } - 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() ); - } + if (an[i].getId() != null + && annotationIds.containsKey(an[i].getId())) + { + jalview.datamodel.AlignmentAnnotation jda = (jalview.datamodel.AlignmentAnnotation) annotationIds + .get(an[i].getId()); + if (an[i].hasVisible()) + jda.visible = an[i].getVisible(); - jalview.datamodel.AlignmentAnnotation jaa = null; + al.addAnnotation(jda); - if (an[i].getGraph()) - { - jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(), - an[i].getDescription(), anot, 0, 0, - an[i].getGraphType()); + continue; + } - jaa.graphGroup = an[i].getGraphGroup(); + AnnotationElement[] ae = an[i].getAnnotationElement(); + jalview.datamodel.Annotation[] anot = null; - 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())) - ); + if (!an[i].getScoreOnly()) + { + anot = new jalview.datamodel.Annotation[al.getWidth()]; - } + for (int aa = 0; aa < ae.length && aa < anot.length; aa++) + { + if (ae[aa].getPosition() >= anot.length) + continue; - } - else - { - jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(), - an[i].getDescription(), anot); - } + 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; - if(an[i].getSequenceRef()!=null) - { - jaa.createSequenceMapping( - al.findName(an[i].getSequenceRef()), 1, true - ); - al.findName(an[i].getSequenceRef()).addAlignmentAnnotation(jaa); - } + if (an[i].getGraph()) + { + jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(), + an[i].getDescription(), anot, 0, 0, an[i].getGraphType()); - al.addAnnotation(jaa); - } + jaa.graphGroup = an[i].getGraphGroup(); + + 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(false), 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; + al.addGroup(sg); + } + } - af.viewport.gatherViewsHere = view.getGatheredViews(); + ///////////////////////////////// + // LOAD VIEWPORT - if (view.getSequenceSetId() != null) - { - jalview.gui.AlignViewport av = - (jalview.gui.AlignViewport) - viewportsAdded.get(uniqueSeqSetId); + AlignFrame af = new AlignFrame(al, view.getWidth(), view.getHeight()); - af.viewport.sequenceSetID = uniqueSeqSetId; - if(av!=null) - { + af.setFileName(file, "Jalview"); - af.viewport.historyList = av.historyList; - af.viewport.redoList = av.redoList; - } - else - { - viewportsAdded.put(uniqueSeqSetId, af.viewport); - } + for (int i = 0; i < JSEQ.length; i++) + { + af.viewport.setSequenceColour(af.viewport.alignment.getSequenceAt(i), + new java.awt.Color(JSEQ[i].getColour())); + } - PaintRefresher.Register(af.alignPanel, uniqueSeqSetId); - } - if(hiddenSeqs!=null) - { - for(int s=0; s