X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjalview%2Fgui%2FJalview2XML.java;h=88bb5f2f077d286219e8760ef45058e9e95f9517;hb=a6243461e1c57048d9b1f33a1df1f1a3980d5cb0;hp=7d6d519dca2cbbd6c0514e417851997092e9e9b0;hpb=fbd0380f6ba13c335225197177c416009e786a5b;p=jalview.git diff --git a/src/jalview/gui/Jalview2XML.java b/src/jalview/gui/Jalview2XML.java index 7d6d519..88bb5f2 100755 --- a/src/jalview/gui/Jalview2XML.java +++ b/src/jalview/gui/Jalview2XML.java @@ -1,1736 +1,2606 @@ /* - * Jalview - A Sequence Alignment Editor and Viewer - * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle - * + * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4) + * Copyright (C) 2008 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 * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ 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; - /** - * This maintains a list of viewports, the key being the - * seqSetId. Important to set historyItem and redoList - * for multiple views + * create/return unique hash string for sq + * + * @param sq + * @return new or existing unique string for sq */ - Hashtable viewportsAdded; + 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; + } + } - Hashtable annotationIds = new Hashtable(); + void clearSeqRefs() + { + seqRefIds.clear(); + seqsToIds.clear(); + } - String uniqueSetSuffix = ""; + void initSeqRefs() + { + if (seqsToIds == null) + { + seqsToIds = new IdentityHashMap(); + } + if (seqRefIds == null) + { + seqRefIds = new Hashtable(); + } + } + java.util.IdentityHashMap seqsToIds = null; // SequenceI->key resolution - // SAVES SEVERAL ALIGNMENT WINDOWS TO SAME JARFILE - public void SaveState(File statefile) - { - JInternalFrame[] frames = Desktop.desktop.getAllFrames(); + java.util.Hashtable seqRefIds = null; // key->SequenceI resolution - if (frames == null) - { - return; - } + Vector frefedSequence = null; - try - { - FileOutputStream fos = new FileOutputStream(statefile); - JarOutputStream jout = new JarOutputStream(fos); + boolean raiseGUI = true; // whether errors are raised in dialog boxes or not - //NOTE UTF-8 MUST BE USED FOR WRITING UNICODE CHARS - //////////////////////////////////////////////////// - PrintWriter out = new PrintWriter(new OutputStreamWriter(jout, - "UTF-8")); + public Jalview2XML() + { + } - Vector shortNames = new Vector(); + public Jalview2XML(boolean raiseGUI) + { + this.raiseGUI = raiseGUI; + } - //REVERSE ORDER - for (int i = frames.length - 1; i > -1; i--) + 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 (frames[i] instanceof AlignFrame) + if (ref[1] instanceof jalview.datamodel.AlignedCodonFrame) + { + SequenceI seq = (SequenceI) seqRefIds.get(sref); + while (seq.getDatasetSequence() != null) { - AlignFrame af = (AlignFrame) frames[i]; - - String shortName = af.getTitle(); - - if (shortName.indexOf(File.separatorChar) > -1) - { - shortName = shortName.substring(shortName.lastIndexOf( - File.separatorChar) + 1); - } - - int count = 1; - - while (shortNames.contains(shortName)) - { - if (shortName.endsWith("_" + (count - 1))) - { - shortName = shortName.substring(0, - shortName.lastIndexOf("_")); - } - - shortName = shortName.concat("_" + count); - count++; - } - - shortNames.addElement(shortName); - - if (!shortName.endsWith(".xml")) - { - shortName = shortName + ".xml"; - } - - int ap, apSize= af.alignPanels.size(); - for (ap = 0; ap < apSize; ap++) - { - AlignmentPanel apanel = (AlignmentPanel) af.alignPanels. - elementAt(ap); - - SaveState(apanel, - apSize == 1 ? shortName : ap+shortName, - jout, out); - } + 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--; } - - out.close(); - jout.close(); + } + else + { + System.err + .println("IMPLEMENTATION WARNING: Unresolved forward reference for hash string " + + ref[0] + + " with objecttype " + + ref[1].getClass()); + r++; + } } - catch (Exception ex) + else { - ex.printStackTrace(); + frefedSequence.remove(r); + rSize--; } + } } + } - // USE THIS METHOD TO SAVE A SINGLE ALIGNMENT WINDOW - public boolean SaveAlignment(AlignFrame af, String jarFile, - String fileName) - { - 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; i--) + { + if (frames[i] instanceof AlignFrame) { - jal = jal.getHiddenSequences().getFullAlignment(); - } + AlignFrame af = (AlignFrame) frames[i]; - SequenceSet vamsasSet = new SequenceSet(); - Sequence vamsasSeq; - JalviewModelSequence jms = new JalviewModelSequence(); + String shortName = af.getTitle(); - vamsasSet.setGapChar(jal.getGapCharacter() + ""); - - JSeq jseq; - Vector pdbfiles = null; + if (shortName.indexOf(File.separatorChar) > -1) + { + shortName = shortName.substring(shortName + .lastIndexOf(File.separatorChar) + 1); + } - //SAVE SEQUENCES - int id = 0; - jalview.datamodel.SequenceI jds; - for (int i = 0; i < jal.getHeight(); i++) - { - jds = jal.getSequenceAt(i); - id = jds.hashCode(); + int count = 1; - if(seqRefIds.get(id+"")!=null) + while (shortNames.contains(shortName)) + { + if (shortName.endsWith("_" + (count - 1))) { - + shortName = shortName + .substring(0, shortName.lastIndexOf("_")); } - else - { - vamsasSeq = new Sequence(); - vamsasSeq.setId(id + ""); - vamsasSeq.setName(jds.getName()); - vamsasSeq.setSequence(jds.getSequenceAsString()); - vamsasSeq.setDescription(jds.getDescription()); - if (jds.getDatasetSequence().getDBRef() != null) - { - jalview.datamodel.DBRefEntry[] dbrefs = - jds.getDatasetSequence().getDBRef(); + shortName = shortName.concat("_" + count); + count++; + } - 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()); - vamsasSeq.addDBRef(dbref); - } - } + shortNames.addElement(shortName); + + if (!shortName.endsWith(".xml")) + { + shortName = shortName + ".xml"; + } - vamsasSet.addSequence(vamsasSeq); - seqRefIds.put(id+"", jal.getSequenceAt(i)); + 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"; } - jseq = new JSeq(); - jseq.setStart(jds.getStart()); - jseq.setEnd(jds.getEnd()); - jseq.setColour( av.getSequenceColour(jds).getRGB()); + 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(); + } + } - jseq.setId(id); + // USE THIS METHOD TO SAVE A SINGLE ALIGNMENT WINDOW + public boolean SaveAlignment(AlignFrame af, String jarFile, + String fileName) + { + try + { + int ap, apSize = af.alignPanels.size(); + FileOutputStream fos = new FileOutputStream(jarFile); + JarOutputStream jout = new JarOutputStream(fos); + for (ap = 0; ap < apSize; ap++) + { + AlignmentPanel apanel = (AlignmentPanel) af.alignPanels + .elementAt(ap); + String jfileName = apSize == 1 ? fileName : fileName + ap; + if (!jfileName.endsWith(".xml")) + { + jfileName = jfileName + ".xml"; + } + SaveState(apanel, jfileName, jout); + } - if (av.hasHiddenRows) - { - jseq.setHidden(av.alignment.getHiddenSequences().isHidden(jds)); + try + { + jout.flush(); + } catch (Exception foo) + { + } + ; + jout.close(); + return true; + } catch (Exception ex) + { + ex.printStackTrace(); + return false; + } + } - if(av.hiddenRepSequences!=null - && av.hiddenRepSequences.containsKey(jal.getSequenceAt(i))) - { - jalview.datamodel.SequenceI[] reps = - ( (jalview.datamodel.SequenceGroup) - av.hiddenRepSequences.get( - jal.getSequenceAt(i))).getSequencesInOrder(jal); + /** + * create a JalviewModel from an algnment view and marshall it to a + * JarOutputStream + * + * @param ap + * panel to create jalview model for + * @param fileName + * name of alignment panel written to output stream + * @param jout + * jar output stream + * @param out + * jar entry name + */ + public JalviewModel SaveState(AlignmentPanel ap, String fileName, + JarOutputStream jout) + { + initSeqRefs(); - for(int h=0; h0) - { - 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); + jseq.addFeatures(features); + index++; + } + } - an.setLabel(aa[i].label); + if (jds.getDatasetSequence().getPDBId() != null) + { + Enumeration en = jds.getDatasetSequence().getPDBId().elements(); + while (en.hasMoreElements()) + { + Pdbids pdb = new Pdbids(); + jalview.datamodel.PDBEntry entry = (jalview.datamodel.PDBEntry) en + .nextElement(); - AnnotationElement ae; + pdb.setId(entry.getId()); + pdb.setType(entry.getType()); - for (int a = 0; a < aa[i].annotations.length; a++) + AppJmol jmol; + // This must have been loaded, is it still visible? + JInternalFrame[] frames = Desktop.desktop.getAllFrames(); + for (int f = frames.length - 1; f > -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) + { + state.setContent(statestring.replaceAll("\n", "")); + } + for (int s = 0; s < jmol.sequence.length; s++) + { + if (jal.findIndex(jmol.sequence[s]) > -1) { - 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 + - ""); - - if(aa[i].annotations[a].colour!=java.awt.Color.black) - ae.setColour(aa[i].annotations[a].colour.getRGB()); - - an.addAnnotationElement(ae); + pdb.addStructureState(state); } - - vamsasSet.addAnnotation(an); + } } - } + } - //SAVE GROUPS - if (jal.getGroups() != null) - { - JGroup[] groups = new JGroup[jal.getGroups().size()]; + if (entry.getFile() != null) + { + pdb.setFile(entry.getFile()); + if (pdbfiles == null) + { + pdbfiles = new Vector(); + } - for (int i = 0; i < groups.length; i++) + if (!pdbfiles.contains(entry.getId())) { - 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) + pdbfiles.addElement(entry.getId()); + try + { + File file = new File(entry.getFile()); + if (file.exists() && jout != 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()); + byte[] data = new byte[(int) file.length()]; + jout.putNextEntry(new JarEntry(entry.getId())); + DataInputStream dis = new DataInputStream( + new FileInputStream(file)); + dis.readFully(data); + + DataOutputStream dout = new DataOutputStream(jout); + dout.write(data, 0, data.length); + dout.flush(); + jout.closeEntry(); } + } catch (Exception ex) + { + ex.printStackTrace(); + } - 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(seq.hashCode()); - } + if (entry.getProperty() != null) + { + PdbentryItem item = new PdbentryItem(); + Hashtable properties = entry.getProperty(); + Enumeration en2 = properties.keys(); + while (en2.hasMoreElements()) + { + Property prop = new Property(); + String key = en2.nextElement().toString(); + prop.setName(key); + prop.setValue(properties.get(key).toString()); + item.addProperty(prop); } + pdb.addPdbentryItem(item); + } - jms.setJGroup(groups); + jseq.addPdbids(pdb); } + } + jms.addJSeq(jseq); + } - ///////////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) + if (av.hasHiddenRows) + { + jal = av.alignment; + } + // SAVE MAPPINGS + if (jal.getCodonFrames() != null && jal.getCodonFrames().length > 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++) { - view.setXpos(av.explodedPosition.x); - view.setYpos(av.explodedPosition.y); - view.setWidth(av.explodedPosition.width); - view.setHeight(av.explodedPosition.height); + 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); } - else + if (jac[i].getProtMappings() != null + && jac[i].getProtMappings().length > 0) { - view.setXpos(ap.alignFrame.getBounds().x); - view.setYpos(ap.alignFrame.getBounds().y); - view.setWidth(ap.alignFrame.getBounds().width); - view.setHeight(ap.alignFrame.getBounds().height); + 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("" + seqHash(dnas[m])); + alcmap.setMapping(createVamsasMapping(pmaps[m], dnas[m], null, + false)); + alc.addAlcodMap(alcmap); + } } + } + } - view.setStartRes(av.startRes); - view.setStartSeq(av.startSeq); + // 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(); - if (av.getGlobalColourScheme() instanceof jalview.schemes.UserColourScheme) - { - view.setBgColour(SetUserColourScheme(av.getGlobalColourScheme(), - userColours, jms)); - } - else if(av.getGlobalColourScheme() instanceof jalview.schemes.AnnotationColourGradient) + for (int t = 0; t < frames.length; t++) { - 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 (frames[t] instanceof TreePanel) + { + TreePanel tp = (TreePanel) frames[t]; - ac.setMaxColour(acg.getMaxColour().getRGB()); - ac.setMinColour(acg.getMinColour().getRGB()); - view.setAnnotationColours(ac); - view.setBgColour("AnnotationColourGradient"); + 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); + } + } } - else + } + } + + // SAVE ANNOTATIONS + if (jal.getAlignmentAnnotation() != null) + { + jalview.datamodel.AlignmentAnnotation[] aa = jal + .getAlignmentAnnotation(); + + for (int i = 0; i < aa.length; i++) + { + Annotation an = new Annotation(); + + if (aa[i].annotationId != null) { - view.setBgColour(ColourSchemeProperty.getColourName( - av.getGlobalColourScheme())); + annotationIds.put(aa[i].annotationId, aa[i]); } - ColourSchemeI cs = av.getGlobalColourScheme(); + an.setId(aa[i].annotationId); - if(cs!=null) + if (aa[i] == av.quality || aa[i] == av.conservation + || aa[i] == av.consensus) { - 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()); - } + an.setLabel(aa[i].label); + an.setGraph(true); + vamsasSet.addAnnotation(an); + continue; } - 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); + an.setVisible(aa[i].visible); + an.setDescription(aa[i].description); - if(av.featuresDisplayed!=null) + if (aa[i].sequenceRef != null) { - jalview.schemabinding.version2.FeatureSettings fs - = new jalview.schemabinding.version2.FeatureSettings(); - - String [] renderOrder = - ap.seqPanel.seqCanvas.getFeatureRenderer().renderOrder; + an.setSequenceRef(aa[i].sequenceRef.getName()); + } - Vector settingsAdded = new Vector(); - for(int ro=0; ro 0) + { + an.setGraph(true); + an.setGraphType(aa[i].graph); + an.setGraphGroup(aa[i].graphGroup); + if (aa[i].getThreshold() != null) { - 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]) - ); - - fs.addSetting(setting); - settingsAdded.addElement(renderOrder[ro]); + 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); + } - //Make sure we save none displayed feature settings - Enumeration en = - ap.seqPanel.seqCanvas.getFeatureRenderer().featureColours.keys(); - while(en.hasMoreElements()) + 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++) { - String key = en.nextElement().toString(); - if(settingsAdded.contains(key)) + if ((aa[i] == null) || (aa[i].annotations[a] == null)) + { continue; + } - Setting setting = new Setting(); - setting.setType(key); - setting.setColour( - ap.seqPanel.seqCanvas.getFeatureRenderer().getColour(key).getRGB() - ); + 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); - setting.setDisplay(false); + if (!Float.isNaN(aa[i].annotations[a].value)) + ae.setValue(aa[i].annotations[a].value); - fs.addSetting(setting); - settingsAdded.addElement(key); - } + ae.setPosition(a); + if (aa[i].annotations[a].secondaryStructure != ' ' + && aa[i].annotations[a].secondaryStructure != '\0') + ae + .setSecondaryStructure(aa[i].annotations[a].secondaryStructure + + ""); - jms.setFeatureSettings(fs); + 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); + } } - - if(av.hasHiddenColumns) + else { - for(int c=0; c -1) { - if (uc[i].getId().equals(id)) - { - colours = uc[i]; + setting.setOrder(rorder); + } + fs.addSetting(setting); + settingsAdded.addElement(renderOrder[ro]); + } - break; - } + // 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)) + { + continue; } - java.awt.Color[] newColours = new java.awt.Color[24]; + Setting setting = new Setting(); + setting.setType(key); + setting.setColour(ap.seqPanel.seqCanvas.getFeatureRenderer() + .getColour(key).getRGB()); - for (int i = 0; i < 24; i++) + 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)) { - newColours[i] = new java.awt.Color(Integer.parseInt( - colours.getUserColourScheme().getColour(i).getRGB(), 16)); + 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); + + } + + 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); + } + } + + jms.addViewport(view); + + object.setJalviewModelSequence(jms); + object.getVamsasModel().addSequenceSet(vamsasSet); - jalview.schemes.UserColourScheme ucs = - new jalview.schemes.UserColourScheme(newColours); + 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 (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()) + { + // 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 { - newColours[i] = new java.awt.Color(Integer.parseInt( - colours.getUserColourScheme().getColour(i+24).getRGB(), 16)); + 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 +""; + userColours.add(ucs); - jalview.gui.AlignFrame af = null; + 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(); - seqRefIds = new Hashtable(); - viewportsAdded = new Hashtable(); + 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); + } + } - Vector gatherToThisFrame= new Vector(); + id = "ucs" + userColours.indexOf(ucs); + uc.setId(id); + uc.setUserColourScheme(jbucs); + jms.addUserColours(uc); + } - try - { - //UNMARSHALLER SEEMS TO CLOSE JARINPUTSTREAM, MOST ANNOYING - URL url = null; + return id; + } - if (file.startsWith("http://")) - { - url = new URL(file); - } + jalview.schemes.UserColourScheme GetUserColourScheme( + JalviewModelSequence jms, String id) + { + UserColours[] uc = jms.getUserColours(); + UserColours colours = null; - JarInputStream jin = null; - JarEntry jarentry = null; - int entryCount = 1; + for (int i = 0; i < uc.length; i++) + { + if (uc[i].getId().equals(id)) + { + colours = uc[i]; - do - { - if (url != null) - { - jin = new JarInputStream(url.openStream()); - } - else - { - jin = new JarInputStream(new FileInputStream(file)); - } + break; + } + } - for (int i = 0; i < entryCount; i++) - { - jarentry = jin.getNextJarEntry(); - } + java.awt.Color[] newColours = new java.awt.Color[24]; - 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 ); - - 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; - } - ex.printStackTrace(); + 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(); - - JalviewModelSequence jms = object.getJalviewModelSequence(); - - Viewport view = jms.getViewport(0); - - ////////////////////////////////// - //LOAD SEQUENCES - - Vector hiddenSeqs = null; - jalview.datamodel.Sequence jseq; - - ArrayList tmpseqs = new ArrayList(); + Desktop.instance.stopLoading(); + } - boolean multipleView = false; + Enumeration en = gatherToThisFrame.elements(); + while (en.hasMoreElements()) + { + Desktop.instance.gatherViews((AlignFrame) en.nextElement()); + } - JSeq[] JSEQ = object.getJalviewModelSequence().getJSeq(); - for (int i = 0; i < JSEQ.length; i++) + if (errorMessage != null) + { + final String finalErrorMessage = errorMessage; + if (raiseGUI) + { + javax.swing.SwingUtilities.invokeLater(new Runnable() { - String seqId = JSEQ[i].getId() + ""; - - if (seqRefIds.get(seqId) != null) - { - tmpseqs.add( (jalview.datamodel.Sequence) seqRefIds.get(seqId)); - multipleView = true; - } - else + public void run() { - 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 ); + JOptionPane.showInternalMessageDialog(Desktop.desktop, + finalErrorMessage, "Error loading Jalview file", + JOptionPane.WARNING_MESSAGE); } + }); + } + else + { + System.err.println("Problem loading Jalview file: " + errorMessage); + } + } + return af; + } + Hashtable alreadyLoadedPDB; - if (JSEQ[i].getHidden()) - { - if (hiddenSeqs == null) - hiddenSeqs = new Vector(); - + String loadPDBFile(String file, String pdbId) + { + if (alreadyLoadedPDB == null) + alreadyLoadedPDB = new Hashtable(); - hiddenSeqs.addElement( - (jalview.datamodel.Sequence) seqRefIds.get(seqId)); - } + if (alreadyLoadedPDB.containsKey(pdbId)) + return alreadyLoadedPDB.get(pdbId).toString(); - } + try + { + JarInputStream jin = null; - ///SequenceFeatures are added to the DatasetSequence, - // so we must create the dataset before loading features - ///////////////////////////////// + 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; + + while ((data = in.readLine()) != null) + { + out.println(data); + } + try + { + out.flush(); + } catch (Exception foo) + { + } + ; + out.close(); + + alreadyLoadedPDB.put(pdbId, outFile.getAbsolutePath()); + return outFile.getAbsolutePath(); + + } 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; - jalview.datamodel.Sequence[] orderedSeqs = new jalview.datamodel.Sequence[ - tmpseqs.size()]; + 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++; + } + + if (JSEQ[i].getHidden()) + { + if (hiddenSeqs == null) + { + hiddenSeqs = new Vector(); + } + + hiddenSeqs.addElement((jalview.datamodel.Sequence) seqRefIds + .get(seqId)); + } - tmpseqs.toArray(orderedSeqs) ; + } + // / + // Create the alignment object from the sequence set + // /////////////////////////////// + jalview.datamodel.Sequence[] orderedSeqs = new jalview.datamodel.Sequence[tmpseqs + .size()]; - jalview.datamodel.Alignment al = - new jalview.datamodel.Alignment(orderedSeqs); + tmpseqs.toArray(orderedSeqs); - al.setDataset(null); - ///////////////////////////////// + jalview.datamodel.Alignment al = new jalview.datamodel.Alignment( + orderedSeqs); + // / Add the alignment properties + for (int i = 0; i < vamsasSet.getSequenceSetPropertiesCount(); i++) + { + SequenceSetProperties ssp = vamsasSet.getSequenceSetProperties(i); + al.setProperty(ssp.getKey(), ssp.getValue()); + } - Hashtable pdbloaded = new Hashtable(); - if(!multipleView) + // / + // 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) + { + Annotation[] an = vamsasSet.getAnnotation(); - if (vamsasSet.getAnnotationCount()>0) + for (int i = 0; i < an.length; i++) + { + if (an[i].getLabel().equals("Quality")) + { + hideQuality = false; + continue; + } + else if (an[i].getLabel().equals("Conservation")) { - Annotation[] an = vamsasSet.getAnnotation(); + hideConservation = false; + continue; + } + else if (an[i].getLabel().equals("Consensus")) + { + 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; - } - - if (an[i].getId() != null - && annotationIds.containsKey(an[i].getId())) - { - al.addAnnotation( - (jalview.datamodel.AlignmentAnnotation)annotationIds.get(an[i].getId()) - ); - - continue; - } + 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(); - AnnotationElement[] ae = an[i].getAnnotationElement(); - jalview.datamodel.Annotation[] anot = new jalview.datamodel.Annotation[al.getWidth()]; + al.addAnnotation(jda); - for (int aa = 0; aa < ae.length && aa= anot.length) + continue; - 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())) - ); + 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].getGraph()) + { + jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(), + an[i].getDescription(), anot, 0, 0, an[i].getGraphType()); - } - else - { - jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(), - an[i].getDescription(), anot); - } + jaa.graphGroup = an[i].getGraphGroup(); - if(an[i].getId()!=null) - { - annotationIds.put(an[i].getId(), jaa); - jaa.annotationId = an[i].getId(); - } + 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].getSequenceRef()!=null) - { - jaa.createSequenceMapping( - al.findName(an[i].getSequenceRef()), 1, true - ); - al.findName(an[i].getSequenceRef()).addAlignmentAnnotation(jaa); - } + } - al.addAnnotation(jaa); - } } - - ///////////////////////// - //LOAD GROUPS - if (jms.getJGroupCount() > 0) + else { - JGroup[] groups = jms.getJGroup(); + jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(), + an[i].getDescription(), anot); + } - for (int i = 0; i < groups.length; i++) - { - ColourSchemeI cs = null; + if (an[i].getId() != null) + { + annotationIds.put(an[i].getId(), jaa); + jaa.annotationId = an[i].getId(); + } - 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(cs!=null) - cs.setThreshold(groups[i].getPidThreshold(), true); - } + 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()); + } - Vector seqs = new Vector(); + if (an[i].hasVisible()) + jaa.visible = an[i].getVisible(); - 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.addAnnotation(jaa); + } + } - if (ts != null) - seqs.addElement(ts); - } + // /////////////////////// + // LOAD GROUPS + if (jms.getJGroupCount() > 0) + { + JGroup[] groups = jms.getJGroup(); - if(seqs.size()<1) - continue; + 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; + 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); - } - - PaintRefresher.Register(af.alignPanel, uniqueSeqSetId); - } - if(hiddenSeqs!=null) - { - for(int s=0; s