new class
authoramwaterhouse <Andrew Waterhouse>
Thu, 19 May 2005 17:51:18 +0000 (17:51 +0000)
committeramwaterhouse <Andrew Waterhouse>
Thu, 19 May 2005 17:51:18 +0000 (17:51 +0000)
src/jalview/gui/Jalview2XML.java [new file with mode: 0755]

diff --git a/src/jalview/gui/Jalview2XML.java b/src/jalview/gui/Jalview2XML.java
new file mode 100755 (executable)
index 0000000..008be21
--- /dev/null
@@ -0,0 +1,466 @@
+package jalview.gui;\r
+\r
+import java.io.*;\r
+import java.util.*;\r
+import java.util.jar.*;\r
+\r
+import jalview.binding.*;\r
+import jalview.schemes.*;\r
+import javax.swing.JInternalFrame;\r
+\r
+\r
+public class Jalview2XML\r
+{\r
+\r
+  public static void SaveState(File statefile)\r
+  {\r
+      long creation = System.currentTimeMillis();\r
+      JInternalFrame[] frames = Desktop.desktop.getAllFrames();\r
+      try{\r
+        JalviewState state = new JalviewState();\r
+\r
+        Vector shortNames = new Vector();\r
+        for (int i = 0; i < frames.length; i++)\r
+        {\r
+          if (frames[i] instanceof AlignFrame)\r
+          {\r
+            AlignFrame af = (AlignFrame) frames[i];\r
+\r
+            String shortName = af.getTitle();\r
+\r
+            shortName = shortName.replace('/', '_');\r
+            shortName = shortName.replace('\\', '_');\r
+            String jarFile = statefile.getParent()+File.separatorChar+shortName+".jar";\r
+\r
+            int count=1;\r
+            while( shortNames.contains(shortName) )\r
+            {\r
+              if(shortName.endsWith("_"+(count-1)))\r
+                shortName = shortName.substring(0, shortName.lastIndexOf("_"));\r
+\r
+              shortName = shortName.concat("_"+count);\r
+              count++;\r
+            }\r
+            shortNames.addElement(shortName);\r
+\r
+            if(!shortName.endsWith(".xml"))\r
+              shortName = shortName+".xml";\r
+\r
+            jarFile = statefile.getParent()+File.separatorChar+shortName+".jar";\r
+\r
+            SaveState( af, creation, shortName, jarFile);\r
+            state.addFile(jarFile);\r
+          }\r
+        }\r
+\r
+        PrintWriter out = new PrintWriter(new FileWriter(statefile));\r
+        state.marshal(out);\r
+        out.close();\r
+      }\r
+      catch(Exception ex){ex.printStackTrace();}\r
+  }\r
+\r
+\r
+  public static void SaveState( AlignFrame af, long timeStamp, String fileName, String jarFile)\r
+  {\r
+\r
+    Vector seqids = new Vector();\r
+\r
+    AlignViewport av = af.viewport;\r
+\r
+    JalviewModel object = new JalviewModel();\r
+    object.setVamsasModel( new VamsasModel() );\r
+\r
+    object.setCreationDate( new java.util.Date(timeStamp) );\r
+\r
+    jalview.datamodel.AlignmentI jal = af.viewport.alignment;\r
+\r
+    SequenceSet vamsasSet = new SequenceSet();\r
+    Sequence vamsasSeq;\r
+    JalviewModelSequence jms = new JalviewModelSequence();\r
+\r
+    vamsasSet.setGapChar(jal.getGapCharacter()+"");\r
+\r
+    JSeq jseq;\r
+\r
+\r
+    //SAVE SEQUENCES\r
+    int id = 0;\r
+    for(int i=0; i<jal.getHeight(); i++)\r
+    {\r
+      seqids.add(jal.getSequenceAt(i));\r
+\r
+      vamsasSeq = new Sequence( );\r
+      vamsasSeq.setId(id+"");\r
+      vamsasSeq.setName(jal.getSequenceAt(i).getName());\r
+      vamsasSeq.setSequence(jal.getSequenceAt(i).getSequence());\r
+\r
+      jseq = new JSeq();\r
+      jseq.setStart(jal.getSequenceAt(i).getStart());\r
+      jseq.setEnd(jal.getSequenceAt(i).getEnd());\r
+\r
+      jseq.setColour(jal.getSequenceAt(i).getColor().getRGB());\r
+\r
+      jseq.setId( id );\r
+\r
+      jms.addJSeq(jseq);\r
+      vamsasSet.addSequence(vamsasSeq);\r
+      id++;\r
+    }\r
+\r
+    //SAVE TREES\r
+    if(af.viewport.currentTree!=null)\r
+    {\r
+      object.getVamsasModel().addTree(af.viewport.currentTree.toString());\r
+    }\r
+\r
+    //SAVE ANNOTATIONS\r
+    if(jal.getAlignmentAnnotation()!=null)\r
+    {\r
+\r
+      jalview.datamodel.AlignmentAnnotation [] aa = jal.getAlignmentAnnotation();\r
+      for(int i=0; i<aa.length; i++)\r
+      {\r
+        if(aa[i].label.equals("Quality")\r
+        || aa[i].label.equals("Conservation")\r
+        || aa[i].label.equals("Consensus"))\r
+       {    continue;}\r
+\r
+        Annotation an = new Annotation();\r
+        an.setDescription(aa[i].description);\r
+        an.setGraph(aa[i].isGraph);\r
+        an.setLabel(aa[i].label);\r
+\r
+        AnnotationElement ae;\r
+\r
+        for(int a=0; a<aa[i].annotations.length; a++)\r
+        {\r
+          if(aa[i]==null || aa[i].annotations[a]==null)\r
+            continue;\r
+\r
+          ae = new AnnotationElement();\r
+          ae.setDescription(aa[i].annotations[a].description);\r
+          ae.setDisplayCharacter(aa[i].annotations[a].displayCharacter);\r
+          ae.setValue(aa[i].annotations[a].value);\r
+          ae.setPosition(a);\r
+          ae.setSecondaryStructure(aa[i].annotations[a].secondaryStructure+"");\r
+          an.addAnnotationElement(ae);\r
+        }\r
+\r
+        vamsasSet.addAnnotation(an);\r
+      }\r
+\r
+    }\r
+\r
+    //SAVE GROUPS\r
+    if(jal.getGroups()!=null)\r
+    {\r
+      JGroup [] groups = new JGroup[jal.getGroups().size()];\r
+      for(int i=0; i<groups.length; i++)\r
+      {\r
+        groups[i] = new JGroup();\r
+        jalview.datamodel.SequenceGroup sg\r
+            = (jalview.datamodel.SequenceGroup)jal.getGroups().elementAt(i);\r
+        groups[i].setStart( sg.getStartRes() );\r
+        groups[i].setEnd( sg.getEndRes() );\r
+        groups[i].setName( sg.getName());\r
+\r
+        if(sg.cs instanceof ConservationColourScheme)\r
+          groups[i].setConsThreshold( ((ConservationColourScheme)sg.cs).inc);\r
+\r
+        if (sg.cs instanceof ResidueColourScheme)\r
+          groups[i].setPidThreshold( ( (ResidueColourScheme) sg.cs).getThreshold());\r
+       else if (sg.cs instanceof ScoreColourScheme)\r
+         groups[i].setPidThreshold( ( (ScoreColourScheme) sg.cs).getThreshold());\r
+\r
+\r
+        groups[i].setColour( ColourSchemeProperty.getColourName(sg.cs) );\r
+        groups[i].setOutlineColour(sg.getOutlineColour().getRGB());\r
+        groups[i].setDisplayBoxes(sg.getDisplayBoxes());\r
+        groups[i].setDisplayText(sg.getDisplayText());\r
+        groups[i].setColourText(sg.getColourText());\r
+\r
+        for(int s=0; s<sg.getSize(); s++)\r
+        {\r
+          jalview.datamodel.Sequence seq = (jalview.datamodel.Sequence)sg.getSequenceAt(s);\r
+          int index = seqids.indexOf(seq);\r
+          groups[i].addSeq(index);\r
+        }\r
+\r
+      }\r
+\r
+      jms.setJGroup(groups);\r
+\r
+    }\r
+\r
+    ///////////SAVE VIEWPORT\r
+    Viewport view = new Viewport();\r
+   view.setTitle(af.getTitle());\r
+   view.setXpos(af.getX());\r
+   view.setYpos(af.getY());\r
+   view.setWidth(af.getWidth());\r
+   view.setHeight(af.getHeight());\r
+   view.setStartRes(av.startRes);\r
+   view.setStartSeq(av.startSeq);\r
+   view.setBgColour( ColourSchemeProperty.getColourName\r
+                     ( av.getGlobalColourScheme()));\r
+\r
+   ColourSchemeI cs = av.getGlobalColourScheme();\r
+   if(cs instanceof ConservationColourScheme)\r
+     view.setConsThreshold( ((ConservationColourScheme)cs).inc);\r
+\r
+   if (cs instanceof ResidueColourScheme)\r
+     view.setPidThreshold( ( (ResidueColourScheme) cs).getThreshold());\r
+  else if (cs instanceof ScoreColourScheme)\r
+     view.setPidThreshold( ( (ScoreColourScheme) cs).getThreshold());\r
+\r
+\r
+   view.setConservationSelected(av.getConservationSelected());\r
+   view.setPidSelected(av.getAbovePIDThreshold());\r
+   view.setFontName(av.font.getFontName());\r
+   view.setFontSize(av.font.getSize());\r
+   view.setFontStyle(av.font.getStyle());\r
+   view.setRenderGaps(av.renderGaps);\r
+   view.setShowAnnotation(av.getShowAnnotation());\r
+   view.setShowBoxes(av.getShowBoxes());\r
+   view.setShowColourText(av.getColourText());\r
+   view.setShowConservation(av.showConservation);\r
+   view.setShowFullId(av.getShowFullId());\r
+   view.setShowIdentity(av.showIdentity);\r
+   view.setShowQuality(av.showQuality);\r
+   view.setShowSequenceFeatures(av.showSequenceFeatures);\r
+   view.setShowText(av.getShowText());\r
+   view.setWrapAlignment(av.getWrapAlignment());\r
+\r
+   jms.addViewport( view );\r
+\r
+    object.setJalviewModelSequence(jms);\r
+    object.getVamsasModel().addSequenceSet(vamsasSet);\r
+\r
+\r
+    try\r
+    {\r
+    // NAME OF FILE\r
+     FileOutputStream fos = new FileOutputStream(jarFile);\r
+\r
+     //PUT IT IN THIS JARFILE\r
+     JarOutputStream jout = new JarOutputStream(fos);\r
+\r
+     //NOTE UTF-8 MUST BE USED FOR WRITING UNICODE CHARS\r
+     ////////////////////////////////////////////////////\r
+     PrintWriter out = new PrintWriter(new OutputStreamWriter(jout, "UTF-8"));\r
+\r
+     if(!fileName.endsWith(".xml"))\r
+              fileName = fileName+".xml";\r
+\r
+     JarEntry entry = new JarEntry(fileName);\r
+     jout.putNextEntry(entry);\r
+\r
+      object.marshal(out);\r
+      out.close();\r
+      fos.close();\r
+      jout.close();\r
+    }\r
+    catch (Exception ex)\r
+    {\r
+      ex.printStackTrace();\r
+    }\r
+  }\r
+\r
+  public static void LoadState(String file)\r
+  {\r
+    try{\r
+       JalviewState state = new JalviewState();\r
+       BufferedReader in = new BufferedReader(new FileReader(file));\r
+       state = (JalviewState)state.unmarshal(in);\r
+       Enumeration en = state.enumerateFile();\r
+       while( en.hasMoreElements() )\r
+       {\r
+         LoadJalviewAlign( en.nextElement().toString());\r
+       }\r
+    }\r
+    catch(Exception ex)\r
+    {   ex.printStackTrace(); return; }\r
+  }\r
+\r
+  public static void LoadJalviewAlign(String file)\r
+  {\r
+    JalviewModel object = new JalviewModel();\r
+    Vector seqids = new Vector();\r
+    try{\r
+      FileInputStream fis = new FileInputStream(file);\r
+      JarInputStream jin = new JarInputStream(fis);\r
+      jin.getNextJarEntry();\r
+      InputStreamReader in = new InputStreamReader(jin, "UTF-8");\r
+      object = (JalviewModel) object.unmarshal(in);\r
+    }\r
+    catch(Exception ex)\r
+    {   System.out.println(ex); return; }\r
+\r
+\r
+     SequenceSet vamsasSet = object.getVamsasModel().getSequenceSet(0);\r
+     Sequence [] vamsasSeq = vamsasSet.getSequence();\r
+\r
+     JalviewModelSequence jms = object.getJalviewModelSequence();\r
+     //////////////////////////////////\r
+     //LOAD SEQUENCES\r
+     jalview.datamodel.Sequence [] jseqs = new jalview.datamodel.Sequence[vamsasSeq.length];\r
+     JSeq [] JSEQ = object.getJalviewModelSequence().getJSeq();\r
+     for(int i=0; i<vamsasSeq.length; i++)\r
+     {\r
+       jseqs[i] = new jalview.datamodel.Sequence(vamsasSeq[i].getName(),\r
+                                                 vamsasSeq[i].getSequence());\r
+\r
+       jseqs[i].setStart( JSEQ[i].getStart());\r
+       jseqs[i].setEnd( JSEQ[i].getEnd());\r
+       jseqs[i].setColor( new java.awt.Color(JSEQ[i].getColour()) );\r
+       seqids.add(jseqs[i]);\r
+     }\r
+\r
+     /////////////////////////////////\r
+     jalview.datamodel.Alignment al = new jalview.datamodel.Alignment(jseqs);\r
+     /////////////////////////////////\r
+\r
+     //////////////////////////////////\r
+     //LOAD ANNOTATIONS\r
+     if(vamsasSet.getAnnotation()!=null)\r
+     {\r
+       Annotation[] an = vamsasSet.getAnnotation();\r
+       for (int i = 0; i < an.length; i++)\r
+       {\r
+         AnnotationElement[] ae = an[i].getAnnotationElement();\r
+         jalview.datamodel.Annotation anot[]\r
+             = new jalview.datamodel.Annotation[al.getWidth()];\r
+         for (int aa = 0; aa < ae.length; aa++)\r
+         {\r
+           anot[ae[aa].getPosition()]\r
+               = new jalview.datamodel.Annotation(\r
+                   ae[aa].getDisplayCharacter(),\r
+                   ae[aa].getDescription(),\r
+                   ae[aa].getSecondaryStructure().charAt(0),\r
+                   ae[aa].getValue()\r
+               );\r
+         }\r
+\r
+\r
+          jalview.datamodel.AlignmentAnnotation jaa = null;\r
+          if(an[i].getGraph())\r
+            jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(),\r
+                                        an[i].getDescription(), anot,\r
+                                        0,0,1);\r
+          else\r
+            jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(),\r
+                                        an[i].getDescription(), anot);\r
+          al.addAnnotation(jaa);\r
+       }\r
+     }\r
+\r
+      /////////////////////////////////\r
+      // LOAD VIEWPORT\r
+      Viewport[] views = jms.getViewport();\r
+      Viewport view = views[0]; // DEAL WITH MULTIPLE VIEWPORTS LATER\r
+\r
+      AlignFrame af = new AlignFrame(al);\r
+    //  af.changeColour() );\r
+\r
+    /////////////////////////\r
+    //LOAD GROUPS\r
+    if (jms.getJGroupCount()>0)\r
+    {\r
+      JGroup[] groups = jms.getJGroup();\r
+      for (int i = 0; i < groups.length; i++)\r
+      {\r
+       ColourSchemeI cs = ColourSchemeProperty.getColour(al, groups[i].getColour() );\r
+       if (cs instanceof ResidueColourScheme)\r
+         ( (ResidueColourScheme) cs).setThreshold(groups[i].getPidThreshold());\r
+       else if (cs instanceof ScoreColourScheme)\r
+      ( (ScoreColourScheme) cs).setThreshold(groups[i].getPidThreshold());\r
+\r
+        jalview.datamodel.SequenceGroup sg = new jalview.datamodel.SequenceGroup\r
+                (groups[i].getName(),\r
+                 cs,\r
+                 groups[i].getDisplayBoxes(),\r
+                 groups[i].getDisplayText(),\r
+                 groups[i].getColourText(),\r
+                 groups[i].getStart(),\r
+                 groups[i].getEnd()) ;\r
+        sg.setOutlineColour( new java.awt.Color(groups[i].getOutlineColour()));\r
+        int [] ids = groups[i].getSeq();\r
+        for(int s=0; s<ids.length; s++)\r
+            sg.addSequence( (jalview.datamodel.SequenceI)seqids.elementAt( ids[s] ));\r
+\r
+        if(groups[i].getConsThreshold()!=0)\r
+        {\r
+          jalview.analysis.Conservation c = new jalview.analysis.Conservation("All",\r
+              ResidueProperties.propHash, 3,\r
+              sg.sequences, 0,\r
+              sg.getWidth() - 1);\r
+          c.calculate();\r
+          c.verdict(false, 25);\r
+          cs = new ConservationColourScheme(c, cs);\r
+          sg.cs = cs;\r
+        }\r
+\r
+\r
+        al.addGroup(sg);\r
+      }\r
+    }\r
+\r
+\r
+    Desktop.addInternalFrame(af, view.getTitle(), AlignFrame.NEW_WINDOW_WIDTH, AlignFrame.NEW_WINDOW_HEIGHT);\r
+    af.setBounds(view.getXpos(), view.getYpos(), view.getWidth(), view.getHeight());\r
+    af.viewport.setStartRes( view.getStartRes() );\r
+    af.viewport.setStartSeq( view.getStartSeq() );\r
+    af.viewport.setAbovePIDThreshold( view.getPidSelected() );\r
+    af.viewport.setColourText( view.getShowColourText() );\r
+    af.viewport.setConservationSelected( view.getConservationSelected());\r
+    af.viewport.setShowFullId( view.getShowFullId());\r
+    af.viewport.setFont( new java.awt.Font( view.getFontName(),\r
+                                            view.getFontStyle(),\r
+                                            view.getFontSize()));\r
+    af.alignPanel.fontChanged();\r
+    af.viewport.setRenderGaps( view.getRenderGaps() );\r
+\r
+    af.viewport.setWrapAlignment( view.getWrapAlignment() );\r
+    if(view.getWrapAlignment())\r
+    {\r
+      af.alignPanel.setWrapAlignment(view.getWrapAlignment());\r
+    }\r
+    else\r
+    {\r
+      af.viewport.setShowAnnotation(view.getShowAnnotation());\r
+      af.alignPanel.setAnnotationVisible(view.getShowAnnotation());\r
+    }\r
+\r
+    af.viewport.setShowBoxes( view.getShowBoxes() );\r
+    af.viewport.setShowText( view.getShowText() );\r
+\r
+    af.viewport.setGlobalColourScheme( ColourSchemeProperty.getColour(al, view.getBgColour()));\r
+    af.viewport.setColourAppliesToAllGroups(false);\r
+    af.changeColour( af.viewport.getGlobalColourScheme() );\r
+    af.viewport.setColourAppliesToAllGroups(true);\r
+\r
+    ColourSchemeI cs = af.viewport.getGlobalColourScheme();\r
+    if(cs instanceof ConservationColourScheme)\r
+      ( (ConservationColourScheme) cs).inc = view.getConsThreshold();\r
+\r
+    if (cs instanceof ResidueColourScheme)\r
+      ( (ResidueColourScheme) cs).setThreshold(view.getPidThreshold());\r
+    else if (cs instanceof ScoreColourScheme)\r
+      ( (ScoreColourScheme) cs).setThreshold(view.getPidThreshold());\r
+\r
+    //LOAD TREES - THIS RELEASE ONLY ALLOWS 1 TREE\r
+    if(object.getVamsasModel().getTreeCount()>0)\r
+    {\r
+      try{\r
+        af.ShowNewickTree(new jalview.io.NewickFile(\r
+            (String)object.getVamsasModel().getTree(0)), "Tree");\r
+      }catch(Exception ex){ex.printStackTrace();}\r
+\r
+    }\r
+\r
+  }\r
+}\r
+\r
+\r
+\r