X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FJalview2XML.java;h=f54383e19e902da5b5a38d7a99158ff1749c7706;hb=506d60f0e188723ddc91c26824b41ac7034df3fe;hp=aa1abb38b74654ffda078e52aa5c29789de591c2;hpb=836c80393a86ef51b1d2c6f763c3bc50274e7c96;p=jalview.git diff --git a/src/jalview/gui/Jalview2XML.java b/src/jalview/gui/Jalview2XML.java index aa1abb3..f54383e 100755 --- a/src/jalview/gui/Jalview2XML.java +++ b/src/jalview/gui/Jalview2XML.java @@ -1,17 +1,17 @@ /* - * Jalview - A Sequence Alignment Editor and Viewer - * Copyright (C) 2007 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 @@ -26,31 +26,171 @@ 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 { + /** + * 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() + { + } - Hashtable seqRefIds; + 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 - * seqSetId. Important to set historyItem and redoList - * for multiple views + * This maintains a list of viewports, the key being the seqSetId. Important + * to set historyItem and redoList for multiple views */ Hashtable viewportsAdded; Hashtable annotationIds = new Hashtable(); String uniqueSetSuffix = ""; + /** * List of pdbfiles added to Jar */ @@ -71,14 +211,14 @@ public class Jalview2XML 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(); - //REVERSE ORDER + // REVERSE ORDER for (int i = frames.length - 1; i > -1; i--) { if (frames[i] instanceof AlignFrame) @@ -89,8 +229,8 @@ public class Jalview2XML if (shortName.indexOf(File.separatorChar) > -1) { - shortName = shortName.substring(shortName.lastIndexOf( - File.separatorChar) + 1); + shortName = shortName.substring(shortName + .lastIndexOf(File.separatorChar) + 1); } int count = 1; @@ -99,8 +239,8 @@ public class Jalview2XML { if (shortName.endsWith("_" + (count - 1))) { - shortName = shortName.substring(0, - shortName.lastIndexOf("_")); + shortName = shortName + .substring(0, shortName.lastIndexOf("_")); } shortName = shortName.concat("_" + count); @@ -117,50 +257,65 @@ public class Jalview2XML int ap, apSize = af.alignPanels.size(); for (ap = 0; ap < apSize; ap++) { - AlignmentPanel apanel = (AlignmentPanel) af.alignPanels. - elementAt(ap); + AlignmentPanel apanel = (AlignmentPanel) af.alignPanels + .elementAt(ap); + String fileName = apSize == 1 ? shortName : ap + shortName; + if (!fileName.endsWith(".xml")) + { + fileName = fileName + ".xml"; + } - SaveState(apanel, - apSize == 1 ? shortName : ap + shortName, - jout, out); + SaveState(apanel, fileName, jout); } } } - - out.close(); + try + { + jout.flush(); + } catch (Exception foo) + { + } + ; jout.close(); - } - catch (Exception ex) + } 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) + 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 < apSize; ap++) { - AlignmentPanel apanel = (AlignmentPanel) af.alignPanels.elementAt(ap); - - SaveState(apanel, - apSize == 1 ? fileName : fileName + ap, - jout, out); + 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); } - out.close(); + try + { + jout.flush(); + } catch (Exception foo) + { + } + ; jout.close(); return true; - } - catch (Exception ex) + } catch (Exception ex) { ex.printStackTrace(); return false; @@ -168,23 +323,22 @@ public class Jalview2XML } /** - * DOCUMENT ME! - * - * @param af DOCUMENT ME! - * @param timeStamp DOCUMENT ME! - * @param fileName DOCUMENT ME! - * @param jout DOCUMENT ME! - * @param out DOCUMENT ME! + * 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, - PrintWriter out) + public JalviewModel SaveState(AlignmentPanel ap, String fileName, + JarOutputStream jout) { - if (seqRefIds == null) - { - seqRefIds = new Hashtable(); - } + initSeqRefs(); Vector userColours = new Vector(); @@ -209,10 +363,15 @@ public class Jalview2XML vamsasSet.setGapChar(jal.getGapCharacter() + ""); - if(jal.getProperties()!=null) + if (jal.getDataset() != null) + { + // dataset id is the dataset's hashcode + vamsasSet.setDatasetId(jal.getDataset().hashCode() + ""); + } + if (jal.getProperties() != null) { Enumeration en = jal.getProperties().keys(); - while(en.hasMoreElements()) + while (en.hasMoreElements()) { String key = en.nextElement().toString(); SequenceSetProperties ssp = new SequenceSetProperties(); @@ -224,43 +383,35 @@ public class Jalview2XML JSeq jseq; - //SAVE SEQUENCES - int id = 0; + // SAVE SEQUENCES + String id = ""; jalview.datamodel.SequenceI jds; for (int i = 0; i < jal.getHeight(); i++) { jds = jal.getSequenceAt(i); - id = jds.hashCode(); + id = seqHash(jds); - if (seqRefIds.get(id + "") != null) + if (seqRefIds.get(id) != null) { - + // This happens for two reasons: 1. multiple views are being serialised. + // 2. the hashCode has collided with another sequence's code. This DOES + // HAPPEN! (PF00072.15.stk does this) + // JBPNote: Uncomment to debug writing out of files that do not read + // back in due to ArrayOutOfBoundExceptions. + // System.err.println("vamsasSeq backref: "+id+""); + // System.err.println(jds.getName()+" + // "+jds.getStart()+"-"+jds.getEnd()+" "+jds.getSequenceAsString()); + // System.err.println("Hashcode: "+seqHash(jds)); + // SequenceI rsq = (SequenceI) seqRefIds.get(id + ""); + // System.err.println(rsq.getName()+" + // "+rsq.getStart()+"-"+rsq.getEnd()+" "+rsq.getSequenceAsString()); + // System.err.println("Hashcode: "+seqHash(rsq)); } 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(); - - 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); - } - } - + vamsasSeq = createVamsasSequence(id, jds); vamsasSet.addSequence(vamsasSeq); - seqRefIds.put(id + "", jal.getSequenceAt(i)); + seqRefIds.put(id, jds); } jseq = new JSeq(); @@ -268,27 +419,23 @@ public class Jalview2XML jseq.setEnd(jds.getEnd()); jseq.setColour(av.getSequenceColour(jds).getRGB()); - jseq.setId(id); + jseq.setId(id); // jseq id should be a string not a number if (av.hasHiddenRows) { jseq.setHidden(av.alignment.getHiddenSequences().isHidden(jds)); if (av.hiddenRepSequences != null - && av.hiddenRepSequences.containsKey(jal.getSequenceAt(i))) + && av.hiddenRepSequences.containsKey(jal.getSequenceAt(i))) { - jalview.datamodel.SequenceI[] reps = - ( (jalview.datamodel.SequenceGroup) - av.hiddenRepSequences.get( - jal.getSequenceAt(i))).getSequencesInOrder(jal); + jalview.datamodel.SequenceI[] reps = ((jalview.datamodel.SequenceGroup) av.hiddenRepSequences + .get(jal.getSequenceAt(i))).getSequencesInOrder(jal); for (int h = 0; h < reps.length; h++) { if (reps[h] != jal.getSequenceAt(i)) { - jseq.addHiddenSequences( - jal.findIndex(reps[h]) - ); + jseq.addHiddenSequences(jal.findIndex(reps[h])); } } } @@ -296,8 +443,8 @@ public class Jalview2XML if (jds.getDatasetSequence().getSequenceFeatures() != null) { - jalview.datamodel.SequenceFeature[] sf - = jds.getDatasetSequence().getSequenceFeatures(); + jalview.datamodel.SequenceFeature[] sf = jds.getDatasetSequence() + .getSequenceFeatures(); int index = 0; while (index < sf.length) { @@ -328,8 +475,7 @@ public class Jalview2XML key = keys.nextElement().toString(); OtherData keyValue = new OtherData(); keyValue.setKey(key); - keyValue.setValue( - sf[index].otherDetails.get(key).toString()); + keyValue.setValue(sf[index].otherDetails.get(key).toString()); features.addOtherData(keyValue); } } @@ -345,14 +491,14 @@ public class Jalview2XML while (en.hasMoreElements()) { Pdbids pdb = new Pdbids(); - jalview.datamodel.PDBEntry entry - = (jalview.datamodel.PDBEntry) en.nextElement(); + jalview.datamodel.PDBEntry entry = (jalview.datamodel.PDBEntry) en + .nextElement(); pdb.setId(entry.getId()); pdb.setType(entry.getType()); AppJmol jmol; - //This must have been loaded, is it still visible? + // This must have been loaded, is it still visible? JInternalFrame[] frames = Desktop.desktop.getAllFrames(); for (int f = frames.length - 1; f > -1; f--) { @@ -362,7 +508,6 @@ public class Jalview2XML if (!jmol.pdbentry.getId().equals(entry.getId())) continue; - StructureState state = new StructureState(); state.setVisible(true); state.setXpos(jmol.getX()); @@ -371,7 +516,7 @@ public class Jalview2XML state.setHeight(jmol.getHeight()); String statestring = jmol.viewer.getStateInfo(); - if(state!=null) + if (state != null) { state.setContent(statestring.replaceAll("\n", "")); } @@ -385,7 +530,6 @@ public class Jalview2XML } } - if (entry.getFile() != null) { pdb.setFile(entry.getFile()); @@ -402,20 +546,20 @@ public class Jalview2XML File file = new File(entry.getFile()); if (file.exists() && jout != null) { - byte[] data = new byte[ (int) file.length()]; + byte[] data = new byte[(int) file.length()]; jout.putNextEntry(new JarEntry(entry.getId())); - DataInputStream dis = new DataInputStream(new - FileInputStream(file)); + 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) + } catch (Exception ex) { - ex.printStackTrace(); + ex.printStackTrace(); } } @@ -448,9 +592,41 @@ public class Jalview2XML { 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++) + { + 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 - /////////////////////////////////// + // SAVE TREES + // ///////////////////////////////// if (av.currentTree != null) { // FIND ANY ASSOCIATED TREES @@ -469,7 +645,7 @@ public class Jalview2XML { Tree tree = new Tree(); tree.setTitle(tp.getTitle()); - tree.setCurrentTree( (av.currentTree == tp.getTree())); + tree.setCurrentTree((av.currentTree == tp.getTree())); tree.setNewick(tp.getTree().toString()); tree.setThreshold(tp.treeCanvas.threshold); @@ -494,10 +670,11 @@ public class Jalview2XML } } - //SAVE ANNOTATIONS + // SAVE ANNOTATIONS if (jal.getAlignmentAnnotation() != null) { - jalview.datamodel.AlignmentAnnotation[] aa = jal.getAlignmentAnnotation(); + jalview.datamodel.AlignmentAnnotation[] aa = jal + .getAlignmentAnnotation(); for (int i = 0; i < aa.length; i++) { @@ -510,9 +687,8 @@ public class Jalview2XML an.setId(aa[i].annotationId); - if (aa[i] == av.quality || - aa[i] == av.conservation || - aa[i] == av.consensus) + if (aa[i] == av.quality || aa[i] == av.conservation + || aa[i] == av.consensus) { an.setLabel(aa[i].label); an.setGraph(true); @@ -554,7 +730,7 @@ public class Jalview2XML an.setScore(aa[i].getScore()); } AnnotationElement ae; - if (aa[i].annotations!=null) + if (aa[i].annotations != null) { an.setScoreOnly(false); for (int a = 0; a < aa[i].annotations.length; a++) @@ -567,7 +743,7 @@ public class Jalview2XML ae = new AnnotationElement(); if (aa[i].annotations[a].description != null) ae.setDescription(aa[i].annotations[a].description); - if(aa[i].annotations[a].displayCharacter!=null) + if (aa[i].annotations[a].displayCharacter != null) ae.setDisplayCharacter(aa[i].annotations[a].displayCharacter); if (!Float.isNaN(aa[i].annotations[a].value)) @@ -575,26 +751,29 @@ public class Jalview2XML ae.setPosition(a); if (aa[i].annotations[a].secondaryStructure != ' ' - && aa[i].annotations[a].secondaryStructure != '\0') - ae.setSecondaryStructure(aa[i].annotations[a].secondaryStructure - + ""); + && aa[i].annotations[a].secondaryStructure != '\0') + ae + .setSecondaryStructure(aa[i].annotations[a].secondaryStructure + + ""); - if (aa[i].annotations[a].colour!=null - && aa[i].annotations[a].colour != java.awt.Color.black) + if (aa[i].annotations[a].colour != null + && aa[i].annotations[a].colour != java.awt.Color.black) { ae.setColour(aa[i].annotations[a].colour.getRGB()); } an.addAnnotationElement(ae); } - } else { + } + else + { an.setScoreOnly(true); } vamsasSet.addAnnotation(an); } } - //SAVE GROUPS + // SAVE GROUPS if (jal.getGroups() != null) { JGroup[] groups = new JGroup[jal.getGroups().size()]; @@ -603,9 +782,8 @@ public class Jalview2XML { groups[i] = new JGroup(); - jalview.datamodel.SequenceGroup sg = (jalview.datamodel.SequenceGroup) - jal.getGroups() - .elementAt(i); + 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()); @@ -617,32 +795,30 @@ public class Jalview2XML if (sg.cs instanceof jalview.schemes.UserColourScheme) { - groups[i].setColour(SetUserColourScheme(sg.cs, - userColours, - jms)); + groups[i].setColour(SetUserColourScheme(sg.cs, userColours, + jms)); } else { - groups[i].setColour(ColourSchemeProperty.getColourName(sg. - cs)); + 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())); + 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)); + groups[i] + .setColour(SetUserColourScheme(sg.cs, userColours, jms)); } else { - groups[i].setColour(ColourSchemeProperty.getColourName( - sg.cs)); + groups[i].setColour(ColourSchemeProperty.getColourName(sg.cs)); } groups[i].setPidThreshold(sg.cs.getThreshold()); @@ -658,24 +834,22 @@ public class Jalview2XML for (int s = 0; s < sg.getSize(); s++) { - jalview.datamodel.Sequence seq = - (jalview.datamodel.Sequence) sg.getSequenceAt(s); - groups[i].addSeq(seq.hashCode()); + jalview.datamodel.Sequence seq = (jalview.datamodel.Sequence) sg + .getSequenceAt(s); + groups[i].addSeq(seqHash(seq)); } } jms.setJGroup(groups); } - ///////////SAVE VIEWPORT + // /////////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); @@ -697,13 +871,12 @@ public class Jalview2XML if (av.getGlobalColourScheme() instanceof jalview.schemes.UserColourScheme) { view.setBgColour(SetUserColourScheme(av.getGlobalColourScheme(), - userColours, jms)); + userColours, jms)); } - else if (av.getGlobalColourScheme() instanceof jalview.schemes. - AnnotationColourGradient) + else if (av.getGlobalColourScheme() instanceof jalview.schemes.AnnotationColourGradient) { - jalview.schemes.AnnotationColourGradient acg - = (jalview.schemes.AnnotationColourGradient) av.getGlobalColourScheme(); + jalview.schemes.AnnotationColourGradient acg = (jalview.schemes.AnnotationColourGradient) av + .getGlobalColourScheme(); AnnotationColours ac = new AnnotationColours(); ac.setAboveThreshold(acg.getAboveThreshold()); @@ -712,11 +885,12 @@ public class Jalview2XML if (acg.getBaseColour() instanceof jalview.schemes.UserColourScheme) { ac.setColourScheme(SetUserColourScheme(acg.getBaseColour(), - userColours, jms)); + userColours, jms)); } else { - ac.setColourScheme(ColourSchemeProperty.getColourName(acg.getBaseColour())); + ac.setColourScheme(ColourSchemeProperty.getColourName(acg + .getBaseColour())); } ac.setMaxColour(acg.getMaxColour().getRGB()); @@ -726,8 +900,8 @@ public class Jalview2XML } else { - view.setBgColour(ColourSchemeProperty.getColourName( - av.getGlobalColourScheme())); + view.setBgColour(ColourSchemeProperty.getColourName(av + .getGlobalColourScheme())); } ColourSchemeI cs = av.getGlobalColourScheme(); @@ -769,36 +943,33 @@ public class Jalview2XML if (av.featuresDisplayed != null) { - jalview.schemabinding.version2.FeatureSettings fs - = new jalview.schemabinding.version2.FeatureSettings(); + jalview.schemabinding.version2.FeatureSettings fs = new jalview.schemabinding.version2.FeatureSettings(); - String[] renderOrder = - ap.seqPanel.seqCanvas.getFeatureRenderer().renderOrder; + String[] renderOrder = ap.seqPanel.seqCanvas.getFeatureRenderer().renderOrder; Vector settingsAdded = new Vector(); for (int ro = 0; ro < renderOrder.length; ro++) { Setting setting = new Setting(); setting.setType(renderOrder[ro]); - setting.setColour( - ap.seqPanel.seqCanvas.getFeatureRenderer().getColour(renderOrder[ro]). - getRGB() - ); - - setting.setDisplay( - av.featuresDisplayed.containsKey(renderOrder[ro]) - ); - float rorder=ap.seqPanel.seqCanvas.getFeatureRenderer().getOrder(renderOrder[ro]); - if (rorder>-1) { + setting.setColour(ap.seqPanel.seqCanvas.getFeatureRenderer() + .getColour(renderOrder[ro]).getRGB()); + + setting.setDisplay(av.featuresDisplayed + .containsKey(renderOrder[ro])); + float rorder = ap.seqPanel.seqCanvas.getFeatureRenderer().getOrder( + renderOrder[ro]); + if (rorder > -1) + { setting.setOrder(rorder); } fs.addSetting(setting); settingsAdded.addElement(renderOrder[ro]); } - //Make sure we save none displayed feature settings - Enumeration en = - ap.seqPanel.seqCanvas.getFeatureRenderer().featureColours.keys(); + // Make sure we save none displayed feature settings + Enumeration en = ap.seqPanel.seqCanvas.getFeatureRenderer().featureColours + .keys(); while (en.hasMoreElements()) { String key = en.nextElement().toString(); @@ -809,13 +980,13 @@ public class Jalview2XML Setting setting = new Setting(); setting.setType(key); - setting.setColour( - ap.seqPanel.seqCanvas.getFeatureRenderer().getColour(key).getRGB() - ); + setting.setColour(ap.seqPanel.seqCanvas.getFeatureRenderer() + .getColour(key).getRGB()); setting.setDisplay(false); - float rorder = ap.seqPanel.seqCanvas.getFeatureRenderer().getOrder(key); - if (rorder>-1) + float rorder = ap.seqPanel.seqCanvas.getFeatureRenderer().getOrder( + key); + if (rorder > -1) { setting.setOrder(rorder); } @@ -823,7 +994,7 @@ public class Jalview2XML settingsAdded.addElement(key); } en = ap.seqPanel.seqCanvas.getFeatureRenderer().featureGroups.keys(); - Vector groupsAdded=new Vector(); + Vector groupsAdded = new Vector(); while (en.hasMoreElements()) { String grp = en.nextElement().toString(); @@ -833,7 +1004,10 @@ public class Jalview2XML } Group g = new Group(); g.setName(grp); - g.setDisplay(((Boolean)ap.seqPanel.seqCanvas.getFeatureRenderer().featureGroups.get(grp)).booleanValue()); + g + .setDisplay(((Boolean) ap.seqPanel.seqCanvas + .getFeatureRenderer().featureGroups.get(grp)) + .booleanValue()); fs.addGroup(g); groupsAdded.addElement(grp); } @@ -845,8 +1019,8 @@ public class Jalview2XML { for (int c = 0; c < av.getColumnSelection().getHiddenColumns().size(); c++) { - int[] region = (int[]) av.getColumnSelection().getHiddenColumns(). - elementAt(c); + int[] region = (int[]) av.getColumnSelection().getHiddenColumns() + .elementAt(c); HiddenColumns hc = new HiddenColumns(); hc.setStart(region[0]); hc.setEnd(region[1]); @@ -859,52 +1033,164 @@ public class Jalview2XML object.setJalviewModelSequence(jms); object.getVamsasModel().addSequenceSet(vamsasSet); - if (out != null) + if (jout != null && fileName != null) { - //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 + // 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 { - if (!fileName.endsWith(".xml")) + 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()) { - fileName = fileName + ".xml"; + Mapping mp = createVamsasMapping(dbrefs[d].getMap(), parentseq, + jds, recurse); + dbref.setMapping(mp); } + vamsasSeq.addDBRef(dbref); + } + } + return vamsasSeq; + } - JarEntry entry = new JarEntry(fileName); - jout.putNextEntry(entry); + private Mapping createVamsasMapping(jalview.datamodel.Mapping jmp, + SequenceI parentseq, SequenceI jds, boolean recurse) + { + Mapping mp = null; + if (jmp.getMap() != null) + { + mp = new Mapping(); - object.marshal(out); + 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); } - catch (Exception ex) + r = mlst.getToRanges(); + for (int s = 0; s < r.length; s += 2) { - ex.printStackTrace(); + 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 + { + String jmpid = ""; + SequenceI ps = null; + if (parentseq != jmp.getTo() + && parentseq.getDatasetSequence() != jmp.getTo()) + { + // chaining dbref rather than a handshaking one + jmpid = seqHash(ps = jmp.getTo()); + } + else + { + jmpid = seqHash(ps = parentseq); + } + mpc.setDseqFor(jmpid); + if (!seqRefIds.containsKey(mpc.getDseqFor())) + { + jalview.bin.Cache.log.debug("creatign new DseqFor ID"); + seqRefIds.put(mpc.getDseqFor(), ps); + } + else + { + jalview.bin.Cache.log.debug("reusing DseqFor ID"); + } + } + mp.setMappingChoice(mpc); } } - return object; + return mp; } String SetUserColourScheme(jalview.schemes.ColourSchemeI cs, - Vector userColours, JalviewModelSequence jms) + Vector userColours, JalviewModelSequence jms) { String id = null; - jalview.schemes.UserColourScheme ucs = (jalview.schemes.UserColourScheme) - cs; + jalview.schemes.UserColourScheme ucs = (jalview.schemes.UserColourScheme) cs; if (!userColours.contains(ucs)) { 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(); + jalview.schemabinding.version2.UserColours uc = new jalview.schemabinding.version2.UserColours(); + jalview.schemabinding.version2.UserColourScheme jbucs = new jalview.schemabinding.version2.UserColourScheme(); for (int i = 0; i < colours.length; i++) { - jalview.schemabinding.version2.Colour col = new jalview.schemabinding. - version2.Colour(); + 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); @@ -914,8 +1200,7 @@ public class Jalview2XML colours = ucs.getLowerCaseColours(); for (int i = 0; i < colours.length; i++) { - jalview.schemabinding.version2.Colour col = new jalview.schemabinding. - version2.Colour(); + 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); @@ -932,7 +1217,7 @@ public class Jalview2XML } jalview.schemes.UserColourScheme GetUserColourScheme( - JalviewModelSequence jms, String id) + JalviewModelSequence jms, String id) { UserColours[] uc = jms.getUserColours(); UserColours colours = null; @@ -951,20 +1236,20 @@ public class Jalview2XML for (int i = 0; i < 24; i++) { - newColours[i] = new java.awt.Color(Integer.parseInt( - colours.getUserColourScheme().getColour(i).getRGB(), 16)); + newColours[i] = new java.awt.Color(Integer.parseInt(colours + .getUserColourScheme().getColour(i).getRGB(), 16)); } - jalview.schemes.UserColourScheme ucs = - new jalview.schemes.UserColourScheme(newColours); + jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme( + newColours); if (colours.getUserColourScheme().getColourCount() > 24) { newColours = new java.awt.Color[23]; for (int i = 0; i < 23; i++) { - newColours[i] = new java.awt.Color(Integer.parseInt( - colours.getUserColourScheme().getColour(i + 24).getRGB(), 16)); + newColours[i] = new java.awt.Color(Integer.parseInt(colours + .getUserColourScheme().getColour(i + 24).getRGB(), 16)); } ucs.setLowerCaseColours(newColours); } @@ -974,8 +1259,9 @@ public class Jalview2XML /** * DOCUMENT ME! - * - * @param file DOCUMENT ME! + * + * @param file + * DOCUMENT ME! */ public AlignFrame LoadJalviewAlign(final String file) { @@ -985,14 +1271,14 @@ public class Jalview2XML seqRefIds = new Hashtable(); viewportsAdded = new Hashtable(); - + frefedSequence = new Vector(); Hashtable gatherToThisFrame = new Hashtable(); String errorMessage = null; try { - //UNMARSHALLER SEEMS TO CLOSE JARINPUTSTREAM, MOST ANNOYING + // UNMARSHALLER SEEMS TO CLOSE JARINPUTSTREAM, MOST ANNOYING URL url = null; if (file.startsWith("http://")) @@ -1038,31 +1324,43 @@ public class Jalview2XML } else if (jarentry != null) { - //Some other file here. + // Some other file here. entryCount++; } - } - while (jarentry != null); - } - catch(java.io.FileNotFoundException ex) + } 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) + 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) + errorMessage = "Couldn't locate Jalview XML file : " + file; + System.err.println("Exception whilst loading jalview XML file : " + + ex + "\n"); + } catch (Exception ex) { - //Is Version 1 Jar file? - af = new Jalview2XML_V1().LoadJalviewAlign(file); + System.err.println("Parsing as Jalview Version 2 file failed."); + ex.printStackTrace(System.err); + // Is Version 1 Jar file? + try + { + af = new Jalview2XML_V1(raiseGUI).LoadJalviewAlign(file); + } catch (Exception ex2) + { + System.err.println("Exception whilst loading as jalviewXMLV1:"); + ex2.printStackTrace(); + af = null; + } + + if (Desktop.instance != null) + { + Desktop.instance.stopLoading(); + } if (af != null) { System.out.println("Successfully loaded archive file"); @@ -1070,8 +1368,8 @@ public class Jalview2XML } ex.printStackTrace(); - System.err.println("Exception whilst loading jalview XML file : " + - ex + "\n"); + System.err.println("Exception whilst loading jalview XML file : " + + ex + "\n"); } if (Desktop.instance != null) @@ -1080,31 +1378,37 @@ public class Jalview2XML } Enumeration en = gatherToThisFrame.elements(); - while(en.hasMoreElements()) + while (en.hasMoreElements()) { - Desktop.instance.gatherViews( - (AlignFrame) en.nextElement()); + Desktop.instance.gatherViews((AlignFrame) en.nextElement()); } - if(errorMessage!=null) + if (errorMessage != null) { final String finalErrorMessage = errorMessage; - javax.swing.SwingUtilities.invokeLater(new Runnable() + if (raiseGUI) { - public void run() + javax.swing.SwingUtilities.invokeLater(new Runnable() { - JOptionPane.showInternalMessageDialog(Desktop.desktop, - finalErrorMessage, - "Error loading Jalview file", - JOptionPane.WARNING_MESSAGE); - } - }); + public void run() + { + 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; + String loadPDBFile(String file, String pdbId) { if (alreadyLoadedPDB == null) @@ -1130,8 +1434,7 @@ public class Jalview2XML do { entry = jin.getNextJarEntry(); - } - while (!entry.getName().equals(pdbId)); + } while (!entry.getName().equals(pdbId)); BufferedReader in = new BufferedReader(new InputStreamReader(jin)); File outFile = File.createTempFile("jalview_pdb", ".txt"); @@ -1139,17 +1442,23 @@ public class Jalview2XML PrintWriter out = new PrintWriter(new FileOutputStream(outFile)); String data; - while ( (data = in.readLine()) != null) + 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) + } catch (Exception ex) { ex.printStackTrace(); } @@ -1157,9 +1466,8 @@ public class Jalview2XML return null; } - AlignFrame LoadFromObject(JalviewModel object, - String file, - boolean loadTreesAndStructures) + AlignFrame LoadFromObject(JalviewModel object, String file, + boolean loadTreesAndStructures) { SequenceSet vamsasSet = object.getVamsasModel().getSequenceSet(0); Sequence[] vamsasSeq = vamsasSet.getSequence(); @@ -1168,8 +1476,8 @@ public class Jalview2XML Viewport view = jms.getViewport(0); - ////////////////////////////////// - //LOAD SEQUENCES + // //////////////////////////////// + // LOAD SEQUENCES Vector hiddenSeqs = null; jalview.datamodel.Sequence jseq; @@ -1179,24 +1487,27 @@ public class Jalview2XML boolean multipleView = false; JSeq[] JSEQ = object.getJalviewModelSequence().getJSeq(); + int vi = 0; // counter in vamsasSeq array for (int i = 0; i < JSEQ.length; i++) { String seqId = JSEQ[i].getId() + ""; if (seqRefIds.get(seqId) != null) { - tmpseqs.add( (jalview.datamodel.Sequence) seqRefIds.get(seqId)); + 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 = 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()); - seqRefIds.put(vamsasSeq[i].getId(), jseq); + jseq.setVamsasId(uniqueSetSuffix + seqId); + seqRefIds.put(vamsasSeq[vi].getId() + "", jseq); tmpseqs.add(jseq); + vi++; } if (JSEQ[i].getHidden()) @@ -1206,38 +1517,46 @@ public class Jalview2XML hiddenSeqs = new Vector(); } - hiddenSeqs.addElement( - (jalview.datamodel.Sequence) seqRefIds.get(seqId)); + hiddenSeqs.addElement((jalview.datamodel.Sequence) seqRefIds + .get(seqId)); } } - ///SequenceFeatures are added to the DatasetSequence, - // so we must create the dataset before loading features - ///////////////////////////////// - - - jalview.datamodel.Sequence[] orderedSeqs = new jalview.datamodel.Sequence[ - tmpseqs.size()]; + // / + // Create the alignment object from the sequence set + // /////////////////////////////// + jalview.datamodel.Sequence[] orderedSeqs = new jalview.datamodel.Sequence[tmpseqs + .size()]; tmpseqs.toArray(orderedSeqs); - jalview.datamodel.Alignment al = - new jalview.datamodel.Alignment(orderedSeqs); + jalview.datamodel.Alignment al = new jalview.datamodel.Alignment( + orderedSeqs); - for(int i=0; i 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(); + jalview.datamodel.PDBEntry entry = new jalview.datamodel.PDBEntry(); entry.setId(ids[p].getId()); entry.setType(ids[p].getType()); if (ids[p].getFile() != null) @@ -1296,29 +1616,63 @@ public class Jalview2XML al.getSequenceAt(i).getDatasetSequence().addPDBId(entry); } } - if (vamsasSeq[i].getDBRefCount() > 0) + } + } + + // /////////////////////////////// + // 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) { - for (int d = 0; d < vamsasSeq[i].getDBRefCount(); d++) + Alcodon[] alcods = alc[i].getAlcodon(); + for (int p = 0; p < cf.codons.length; p++) { - 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); + 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) { @@ -1343,12 +1697,14 @@ public class Jalview2XML } if (an[i].getId() != null - && annotationIds.containsKey(an[i].getId())) + && annotationIds.containsKey(an[i].getId())) { - al.addAnnotation( - (jalview.datamodel.AlignmentAnnotation) annotationIds.get(an[i]. - getId()) - ); + jalview.datamodel.AlignmentAnnotation jda = (jalview.datamodel.AlignmentAnnotation) annotationIds + .get(an[i].getId()); + if (an[i].hasVisible()) + jda.visible = an[i].getVisible(); + + al.addAnnotation(jda); continue; } @@ -1358,27 +1714,30 @@ public class Jalview2XML if (!an[i].getScoreOnly()) { - anot = new jalview.datamodel.Annotation[ - al.getWidth()]; + anot = new jalview.datamodel.Annotation[al.getWidth()]; for (int aa = 0; aa < ae.length && aa < anot.length; aa++) { - if(ae[aa].getPosition()>=anot.length) + if (ae[aa].getPosition() >= anot.length) continue; anot[ae[aa].getPosition()] = new jalview.datamodel.Annotation( - ae[aa].getDisplayCharacter(), - ae[aa].getDescription(), - (ae[aa].getSecondaryStructure()==null || ae[aa].getSecondaryStructure().length() == 0) ? ' ' : - ae[aa].getSecondaryStructure().charAt(0), - ae[aa].getValue() - - ); + ae[aa].getDisplayCharacter(), ae[aa].getDescription(), (ae[aa] + .getSecondaryStructure() == null || 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()); + ); + // 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; @@ -1386,18 +1745,16 @@ public class Jalview2XML if (an[i].getGraph()) { jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(), - an[i].getDescription(), anot, 0, 0, - an[i].getGraphType()); + an[i].getDescription(), anot, 0, 0, an[i].getGraphType()); 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())) - ); + jaa.setThreshold(new jalview.datamodel.GraphLine(an[i] + .getThresholdLine().getValue(), an[i] + .getThresholdLine().getLabel(), new java.awt.Color( + an[i].getThresholdLine().getColour()))); } @@ -1405,7 +1762,7 @@ public class Jalview2XML else { jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(), - an[i].getDescription(), anot); + an[i].getDescription(), anot); } if (an[i].getId() != null) @@ -1418,9 +1775,8 @@ public class Jalview2XML { if (al.findName(an[i].getSequenceRef()) != null) { - jaa.createSequenceMapping( - al.findName(an[i].getSequenceRef()), 1, true - ); + jaa.createSequenceMapping(al.findName(an[i].getSequenceRef()), + 1, true); al.findName(an[i].getSequenceRef()).addAlignmentAnnotation(jaa); } } @@ -1429,15 +1785,15 @@ public class Jalview2XML jaa.setScore(an[i].getScore()); } - if(an[i].hasVisible()) - jaa.visible = an[i].getVisible(); + if (an[i].hasVisible()) + jaa.visible = an[i].getVisible(); al.addAnnotation(jaa); } } - ///////////////////////// - //LOAD GROUPS + // /////////////////////// + // LOAD GROUPS if (jms.getJGroupCount() > 0) { JGroup[] groups = jms.getJGroup(); @@ -1454,8 +1810,7 @@ public class Jalview2XML } else { - cs = ColourSchemeProperty.getColour(al, - groups[i].getColour()); + cs = ColourSchemeProperty.getColour(al, groups[i].getColour()); } if (cs != null) @@ -1469,8 +1824,8 @@ public class Jalview2XML 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); + jalview.datamodel.SequenceI ts = (jalview.datamodel.SequenceI) seqRefIds + .get(seqId); if (ts != null) { @@ -1483,14 +1838,14 @@ public class Jalview2XML continue; } - 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()); + 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()); - sg.setOutlineColour(new java.awt.Color( - groups[i].getOutlineColour())); + sg + .setOutlineColour(new java.awt.Color(groups[i] + .getOutlineColour())); sg.textColour = new java.awt.Color(groups[i].getTextCol1()); sg.textColour2 = new java.awt.Color(groups[i].getTextCol2()); @@ -1499,9 +1854,8 @@ public class Jalview2XML if (groups[i].getConsThreshold() != 0) { jalview.analysis.Conservation c = new jalview.analysis.Conservation( - "All", - ResidueProperties.propHash, 3, sg.getSequences(null), 0, - sg.getWidth() - 1); + "All", ResidueProperties.propHash, 3, sg + .getSequences(null), 0, sg.getWidth() - 1); c.calculate(); c.verdict(false, 25); sg.cs.setConservation(c); @@ -1511,36 +1865,31 @@ public class Jalview2XML } } - ///////////////////////////////// + // /////////////////////////////// // LOAD VIEWPORT - AlignFrame af = new AlignFrame(al, - view.getWidth(), - view.getHeight()); + AlignFrame af = new AlignFrame(al, view.getWidth(), view.getHeight()); af.setFileName(file, "Jalview"); for (int i = 0; i < JSEQ.length; i++) { - af.viewport.setSequenceColour( - af.viewport.alignment.getSequenceAt(i), - new java.awt.Color( - JSEQ[i].getColour())); + af.viewport.setSequenceColour(af.viewport.alignment.getSequenceAt(i), + new java.awt.Color(JSEQ[i].getColour())); } - //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 + // If we just load in the same jar file again, the sequenceSetId + // will be the same, and we end up with multiple references + // to the same sequenceSet. We must modify this id on load + // so that each load of the file gives a unique id String uniqueSeqSetId = view.getSequenceSetId() + uniqueSetSuffix; af.viewport.gatherViewsHere = view.getGatheredViews(); if (view.getSequenceSetId() != null) { - jalview.gui.AlignViewport av = - (jalview.gui.AlignViewport) - viewportsAdded.get(uniqueSeqSetId); + jalview.gui.AlignViewport av = (jalview.gui.AlignViewport) viewportsAdded + .get(uniqueSeqSetId); af.viewport.sequenceSetID = uniqueSeqSetId; if (av != null) @@ -1560,21 +1909,18 @@ public class Jalview2XML { for (int s = 0; s < JSEQ.length; s++) { - jalview.datamodel.SequenceGroup hidden = - new jalview.datamodel.SequenceGroup(); + jalview.datamodel.SequenceGroup hidden = new jalview.datamodel.SequenceGroup(); for (int r = 0; r < JSEQ[s].getHiddenSequencesCount(); r++) { - hidden.addSequence( - al.getSequenceAt(JSEQ[s].getHiddenSequences(r)) - , false - ); + hidden.addSequence(al + .getSequenceAt(JSEQ[s].getHiddenSequences(r)), false); } af.viewport.hideRepSequences(al.getSequenceAt(s), hidden); } - jalview.datamodel.SequenceI[] hseqs = new - jalview.datamodel.SequenceI[hiddenSeqs.size()]; + jalview.datamodel.SequenceI[] hseqs = new jalview.datamodel.SequenceI[hiddenSeqs + .size()]; for (int s = 0; s < hiddenSeqs.size(); s++) { @@ -1585,21 +1931,18 @@ public class Jalview2XML } - if ( (hideConsensus || hideQuality || hideConservation) - && al.getAlignmentAnnotation() != null) + if ((hideConsensus || hideQuality || hideConservation) + && al.getAlignmentAnnotation() != null) { int hSize = al.getAlignmentAnnotation().length; for (int h = 0; h < hSize; h++) { - if ( - (hideConsensus && - al.getAlignmentAnnotation()[h].label.equals("Consensus")) - || - (hideQuality && - al.getAlignmentAnnotation()[h].label.equals("Quality")) - || - (hideConservation && - al.getAlignmentAnnotation()[h].label.equals("Conservation"))) + if ((hideConsensus && al.getAlignmentAnnotation()[h].label + .equals("Consensus")) + || (hideQuality && al.getAlignmentAnnotation()[h].label + .equals("Quality")) + || (hideConservation && al.getAlignmentAnnotation()[h].label + .equals("Conservation"))) { al.deleteAnnotation(al.getAlignmentAnnotation()[h]); hSize--; @@ -1614,8 +1957,8 @@ public class Jalview2XML af.viewport.viewName = view.getViewName(); af.setInitialTabVisible(); } - af.setBounds(view.getXpos(), view.getYpos(), view.getWidth(), - view.getHeight()); + af.setBounds(view.getXpos(), view.getYpos(), view.getWidth(), view + .getHeight()); af.viewport.setShowAnnotation(view.getShowAnnotation()); af.viewport.setAbovePIDThreshold(view.getPidSelected()); @@ -1625,8 +1968,8 @@ public class Jalview2XML af.viewport.setConservationSelected(view.getConservationSelected()); af.viewport.setShowJVSuffix(view.getShowFullId()); af.viewport.rightAlignIds = view.getRightAlignIds(); - af.viewport.setFont(new java.awt.Font(view.getFontName(), - view.getFontStyle(), view.getFontSize())); + af.viewport.setFont(new java.awt.Font(view.getFontName(), view + .getFontStyle(), view.getFontSize())); af.alignPanel.fontChanged(); af.viewport.setRenderGaps(view.getRenderGaps()); af.viewport.setWrapAlignment(view.getWrapAlignment()); @@ -1655,55 +1998,48 @@ public class Jalview2XML } else if (view.getBgColour().startsWith("Annotation")) { - //int find annotation - for (int i = 0; - i < af.viewport.alignment.getAlignmentAnnotation().length; i++) + // int find annotation + for (int i = 0; i < af.viewport.alignment.getAlignmentAnnotation().length; i++) { - if (af.viewport.alignment.getAlignmentAnnotation()[i].label. - equals(view.getAnnotationColours().getAnnotation())) + if (af.viewport.alignment.getAlignmentAnnotation()[i].label + .equals(view.getAnnotationColours().getAnnotation())) { - if (af.viewport.alignment.getAlignmentAnnotation()[i]. - getThreshold() == null) + if (af.viewport.alignment.getAlignmentAnnotation()[i] + .getThreshold() == null) { - af.viewport.alignment.getAlignmentAnnotation()[i]. - setThreshold( - new jalview.datamodel.GraphLine( - view.getAnnotationColours().getThreshold(), - "Threshold", java.awt.Color.black) + af.viewport.alignment.getAlignmentAnnotation()[i] + .setThreshold(new jalview.datamodel.GraphLine(view + .getAnnotationColours().getThreshold(), + "Threshold", java.awt.Color.black) - ); + ); } - if (view.getAnnotationColours().getColourScheme().equals( - "None")) + if (view.getAnnotationColours().getColourScheme() + .equals("None")) { - cs = new AnnotationColourGradient( - af.viewport.alignment.getAlignmentAnnotation()[i], - new java.awt.Color(view.getAnnotationColours(). - getMinColour()), - new java.awt.Color(view.getAnnotationColours(). - getMaxColour()), - view.getAnnotationColours().getAboveThreshold()); + cs = new AnnotationColourGradient(af.viewport.alignment + .getAlignmentAnnotation()[i], new java.awt.Color(view + .getAnnotationColours().getMinColour()), + new java.awt.Color(view.getAnnotationColours() + .getMaxColour()), view.getAnnotationColours() + .getAboveThreshold()); } - else if (view.getAnnotationColours().getColourScheme(). - startsWith("ucs")) + else if (view.getAnnotationColours().getColourScheme() + .startsWith("ucs")) { - cs = new AnnotationColourGradient( - af.viewport.alignment.getAlignmentAnnotation()[i], - GetUserColourScheme(jms, view.getAnnotationColours(). - getColourScheme()), - view.getAnnotationColours().getAboveThreshold() - ); + cs = new AnnotationColourGradient(af.viewport.alignment + .getAlignmentAnnotation()[i], GetUserColourScheme( + jms, view.getAnnotationColours().getColourScheme()), + view.getAnnotationColours().getAboveThreshold()); } else { - cs = new AnnotationColourGradient( - af.viewport.alignment.getAlignmentAnnotation()[i], - ColourSchemeProperty.getColour(al, - view.getAnnotationColours(). - getColourScheme()), - view.getAnnotationColours().getAboveThreshold() - ); + cs = new AnnotationColourGradient(af.viewport.alignment + .getAlignmentAnnotation()[i], ColourSchemeProperty + .getColour(al, view.getAnnotationColours() + .getColourScheme()), view + .getAnnotationColours().getAboveThreshold()); } // Also use these settings for all the groups @@ -1711,32 +2047,29 @@ public class Jalview2XML { for (int g = 0; g < al.getGroups().size(); g++) { - jalview.datamodel.SequenceGroup sg - = (jalview.datamodel.SequenceGroup) al.getGroups(). - elementAt(g); + jalview.datamodel.SequenceGroup sg = (jalview.datamodel.SequenceGroup) al + .getGroups().elementAt(g); if (sg.cs == null) { continue; } - /* if (view.getAnnotationColours().getColourScheme().equals("None")) - { - sg.cs = new AnnotationColourGradient( - af.viewport.alignment.getAlignmentAnnotation()[i], - new java.awt.Color(view.getAnnotationColours(). - getMinColour()), - new java.awt.Color(view.getAnnotationColours(). - getMaxColour()), - view.getAnnotationColours().getAboveThreshold()); - } - else*/ + /* + * if + * (view.getAnnotationColours().getColourScheme().equals("None")) { + * sg.cs = new AnnotationColourGradient( + * af.viewport.alignment.getAlignmentAnnotation()[i], new + * java.awt.Color(view.getAnnotationColours(). getMinColour()), + * new java.awt.Color(view.getAnnotationColours(). + * getMaxColour()), + * view.getAnnotationColours().getAboveThreshold()); } else + */ { sg.cs = new AnnotationColourGradient( - af.viewport.alignment.getAlignmentAnnotation()[i], - sg.cs, - view.getAnnotationColours().getAboveThreshold() - ); + af.viewport.alignment.getAlignmentAnnotation()[i], + sg.cs, view.getAnnotationColours() + .getAboveThreshold()); } } @@ -1779,31 +2112,32 @@ public class Jalview2XML if (jms.getFeatureSettings() != null) { af.viewport.featuresDisplayed = new Hashtable(); - String[] renderOrder = new String[jms.getFeatureSettings(). - getSettingCount()]; + String[] renderOrder = new String[jms.getFeatureSettings() + .getSettingCount()]; for (int fs = 0; fs < jms.getFeatureSettings().getSettingCount(); fs++) { Setting setting = jms.getFeatureSettings().getSetting(fs); - af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().setColour(setting. - getType(), - new java.awt.Color(setting.getColour())); + af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().setColour( + setting.getType(), new java.awt.Color(setting.getColour())); renderOrder[fs] = setting.getType(); if (setting.hasOrder()) - af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().setOrder(setting.getType(), setting.getOrder()); + af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().setOrder( + setting.getType(), setting.getOrder()); else - af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().setOrder(setting.getType(), fs/jms.getFeatureSettings().getSettingCount()); + af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().setOrder( + setting.getType(), + fs / jms.getFeatureSettings().getSettingCount()); if (setting.getDisplay()) { - af.viewport.featuresDisplayed.put( - setting.getType(), new Integer(setting.getColour())); + af.viewport.featuresDisplayed.put(setting.getType(), new Integer( + setting.getColour())); } } - af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().renderOrder = - renderOrder; + af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().renderOrder = renderOrder; Hashtable fgtable; - af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().featureGroups = fgtable=new Hashtable(); - for (int gs=0;gs 0) { try @@ -1837,25 +2170,22 @@ public class Jalview2XML Tree tree = jms.getTree(t); - TreePanel tp = af.ShowNewickTree(new jalview.io.NewickFile( - tree.getNewick()), tree.getTitle(), - tree.getWidth(), tree.getHeight(), - tree.getXpos(), tree.getYpos()); + TreePanel tp = af.ShowNewickTree(new jalview.io.NewickFile(tree + .getNewick()), tree.getTitle(), tree.getWidth(), tree + .getHeight(), tree.getXpos(), tree.getYpos()); tp.fitToWindow.setState(tree.getFitToWindow()); tp.fitToWindow_actionPerformed(null); if (tree.getFontName() != null) { - tp.setTreeFont(new java.awt.Font(tree.getFontName(), - tree.getFontStyle(), - tree.getFontSize())); + tp.setTreeFont(new java.awt.Font(tree.getFontName(), tree + .getFontStyle(), tree.getFontSize())); } else { - tp.setTreeFont(new java.awt.Font(view.getFontName(), - view.getFontStyle(), - tree.getFontSize())); + tp.setTreeFont(new java.awt.Font(view.getFontName(), view + .getFontStyle(), tree.getFontSize())); } tp.showPlaceholders(tree.getMarkUnlinked()); @@ -1870,15 +2200,14 @@ public class Jalview2XML } } - } - catch (Exception ex) + } catch (Exception ex) { ex.printStackTrace(); } } - ////LOAD STRUCTURES - if(loadTreesAndStructures) + // //LOAD STRUCTURES + if (loadTreesAndStructures) { for (int i = 0; i < JSEQ.length; i++) { @@ -1901,14 +2230,32 @@ public class Jalview2XML java.awt.Component comp = null; - JInternalFrame [] frames = Desktop.desktop.getAllFrames(); - for(int f=0; f dsq.getEnd(); + // if (pre || post) + if (sq != dsq) + { + StringBuffer sb = new StringBuffer(); + String newres = jalview.analysis.AlignSeq.extractGaps( + jalview.util.Comparison.GapChars, sq.getSequenceAsString()); + if (!newres.equalsIgnoreCase(dsq.getSequenceAsString()) + && newres.length() > dsq.getLength()) + { + // Update with the longer sequence. + synchronized (dsq) + { + /* + * if (pre) { sb.insert(0, newres .substring(0, dsq.getStart() - + * sq.getStart())); dsq.setStart(sq.getStart()); } if (post) { + * sb.append(newres.substring(newres.length() - sq.getEnd() - + * dsq.getEnd())); dsq.setEnd(sq.getEnd()); } + */ + dsq.setSequence(sb.toString()); + } + // TODO: merges will never happen if we 'know' we have the real dataset + // sequence - this should be detected when id==dssid + System.err.println("DEBUG Notice: Merged dataset sequence"); // (" + // + (pre ? "prepended" : "") + " " + // + (post ? "appended" : "")); + } + } + } + + java.util.Hashtable datasetIds = null; + + private Alignment getDatasetFor(String datasetId) + { + if (datasetIds == null) + { + datasetIds = new Hashtable(); + return null; + } + if (datasetIds.containsKey(datasetId)) + { + return (Alignment) datasetIds.get(datasetId); + } + return null; + } + + private void addDatasetRef(String datasetId, Alignment dataset) + { + if (datasetIds == null) + { + datasetIds = new Hashtable(); + } + datasetIds.put(datasetId, dataset); + } + + private void addDBRefs(SequenceI datasetSequence, Sequence sequence) + { + for (int d = 0; d < sequence.getDBRefCount(); d++) + { + DBRef dr = sequence.getDBRef(d); + jalview.datamodel.DBRefEntry entry = new jalview.datamodel.DBRefEntry( + sequence.getDBRef(d).getSource(), sequence.getDBRef(d) + .getVersion(), sequence.getDBRef(d).getAccessionId()); + if (dr.getMapping() != null) + { + entry.setMap(addMapping(dr.getMapping())); + } + datasetSequence.addDBRef(entry); + } + } + + private jalview.datamodel.Mapping addMapping(Mapping m) + { + SequenceI dsto = null; + // Mapping m = dr.getMapping(); + int fr[] = new int[m.getMapListFromCount() * 2]; + Enumeration f = m.enumerateMapListFrom(); + for (int _i = 0; f.hasMoreElements(); _i += 2) + { + MapListFrom mf = (MapListFrom) f.nextElement(); + fr[_i] = mf.getStart(); + fr[_i + 1] = mf.getEnd(); + } + int fto[] = new int[m.getMapListToCount() * 2]; + f = m.enumerateMapListTo(); + for (int _i = 0; f.hasMoreElements(); _i += 2) + { + MapListTo mf = (MapListTo) f.nextElement(); + fto[_i] = mf.getStart(); + fto[_i + 1] = mf.getEnd(); + } + jalview.datamodel.Mapping jmap = new jalview.datamodel.Mapping(dsto, + fr, fto, (int) m.getMapFromUnit(), (int) m.getMapToUnit()); + if (m.getMappingChoice() != null) + { + MappingChoice mc = m.getMappingChoice(); + if (mc.getDseqFor() != null) + { + if (seqRefIds.containsKey(mc.getDseqFor())) + { + /** + * recover from hash + */ + jmap.setTo((SequenceI) seqRefIds.get(mc.getDseqFor())); + } + else + { + frefedSequence.add(new Object[] + { mc.getDseqFor(), jmap }); + } + } + else + { + /** + * local sequence definition + */ + Sequence ms = mc.getSequence(); + jalview.datamodel.Sequence djs = null; + String sqid = ms.getDsseqid(); + if (sqid != null && sqid.length() > 0) + { + /* + * recover dataset sequence + */ + djs = (jalview.datamodel.Sequence) seqRefIds.get(sqid); + } + else + { + System.err + .println("Warning - making up dataset sequence id for DbRef sequence map reference"); + sqid = ((Object) ms).toString(); // make up a new hascode for + // undefined dataset sequence hash + // (unlikely to happen) + } + + if (djs == null) + { + /** + * make a new dataset sequence and add it to refIds hash + */ + djs = new jalview.datamodel.Sequence(ms.getName(), ms + .getSequence()); + djs.setStart(jmap.getMap().getToLowest()); + djs.setEnd(jmap.getMap().getToHighest()); + djs.setVamsasId(uniqueSetSuffix + sqid); + jmap.setTo(djs); + seqRefIds.put(sqid, djs); + + } + jalview.bin.Cache.log.debug("about to recurse on addDBRefs."); + addDBRefs(djs, ms); + + } + } + return (jmap); + + } + public jalview.gui.AlignmentPanel copyAlignPanel(AlignmentPanel ap, - boolean keepSeqRefs) + boolean keepSeqRefs) { - jalview.schemabinding.version2.JalviewModel jm - = SaveState(ap, null, null, null); + jalview.schemabinding.version2.JalviewModel jm = SaveState(ap, null, + null); if (!keepSeqRefs) { - seqRefIds.clear(); + clearSeqRefs(); jm.getJalviewModelSequence().getViewport(0).setSequenceSetId(null); } else @@ -1986,18 +2579,28 @@ public class Jalview2XML af.alignPanels.clear(); af.closeMenuItem_actionPerformed(true); - /* if(ap.av.alignment.getAlignmentAnnotation()!=null) - { - for(int i=0; i