AlignmentAnnotation: added annotation score attribute and allowed for annotation...
[jalview.git] / src / jalview / gui / Jalview2XML.java
index da807d4..f0551e8 100755 (executable)
@@ -28,6 +28,7 @@ import javax.swing.*;
 import org.exolab.castor.xml.*;
 import jalview.schemabinding.version2.*;
 import jalview.schemes.*;
+import jalview.structure.StructureSelectionManager;
 
 /**
  * DOCUMENT ME!
@@ -50,6 +51,10 @@ public class Jalview2XML
   Hashtable annotationIds = new Hashtable();
 
   String uniqueSetSuffix = "";
+  /**
+   * List of pdbfiles added to Jar
+   */
+  Vector pdbfiles = null;
 
   // SAVES SEVERAL ALIGNMENT WINDOWS TO SAME JARFILE
   public void SaveState(File statefile)
@@ -205,7 +210,6 @@ public class Jalview2XML
     vamsasSet.setGapChar(jal.getGapCharacter() + "");
 
     JSeq jseq;
-    Vector pdbfiles = null;
 
     //SAVE SEQUENCES
     int id = 0;
@@ -334,6 +338,37 @@ public class Jalview2XML
           pdb.setId(entry.getId());
           pdb.setType(entry.getType());
 
+          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());
+
+              state.setContent(jmol.viewer.getStateInfo().replaceAll("\n", ""));
+
+              for (int s = 0; s < jmol.sequence.length; s++)
+              {
+                if (jal.findIndex(jmol.sequence[s]) > -1)
+                {
+                  pdb.addStructureState(state);
+                }
+              }
+            }
+          }
+
+
           if (entry.getFile() != null)
           {
             pdb.setFile(entry.getFile());
@@ -363,8 +398,9 @@ public class Jalview2XML
               }
               catch (Exception ex)
               {
-                ex.printStackTrace();
+                  ex.printStackTrace();
               }
+
             }
           }
 
@@ -494,32 +530,40 @@ public class Jalview2XML
         }
 
         an.setLabel(aa[i].label);
-
+        if (aa[i].hasScore())
+        {
+          an.setScore(aa[i].getScore());
+        }
         AnnotationElement ae;
-
-        for (int a = 0; a < aa[i].annotations.length; a++)
+        if (aa[i].annotations!=null)
         {
-          if ( (aa[i] == null) || (aa[i].annotations[a] == null))
+          an.setScoreOnly(false);
+          for (int a = 0; a < aa[i].annotations.length; a++)
           {
-            continue;
-          }
+            if ((aa[i] == null) || (aa[i].annotations[a] == null))
+            {
+              continue;
+            }
 
-          ae = new AnnotationElement();
-          ae.setDescription(aa[i].annotations[a].description);
-          ae.setDisplayCharacter(aa[i].annotations[a].displayCharacter);
-          ae.setValue(aa[i].annotations[a].value);
-          ae.setPosition(a);
-          ae.setSecondaryStructure(aa[i].annotations[a].secondaryStructure +
-                                   "");
+            ae = new AnnotationElement();
+            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());
-          }
+            if (aa[i].annotations[a].colour != java.awt.Color.black)
+            {
+              ae.setColour(aa[i].annotations[a].colour.getRGB());
+            }
 
-          an.addAnnotationElement(ae);
+            an.addAnnotationElement(ae);
+          }
+        } else {
+          an.setScoreOnly(true);
         }
-
         vamsasSet.addAnnotation(an);
       }
     }
@@ -604,6 +648,8 @@ public class Jalview2XML
     view.setViewName(av.viewName);
     view.setGatheredViews(av.gatherViewsHere);
 
+
+
     if (ap.av.explodedPosition != null)
     {
       view.setXpos(av.explodedPosition.x);
@@ -716,11 +762,14 @@ public class Jalview2XML
         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();
@@ -739,11 +788,29 @@ public class Jalview2XML
             );
 
         setting.setDisplay(false);
-
+        float rorder = ap.seqPanel.seqCanvas.getFeatureRenderer().getOrder(key);
+        if (rorder>-1) 
+        {
+          setting.setOrder(rorder);
+        }
         fs.addSetting(setting);
         settingsAdded.addElement(key);
       }
-
+      en = ap.seqPanel.seqCanvas.getFeatureRenderer().featureGroups.keys();
+      Vector groupsAdded=new Vector(); 
+      while (en.hasMoreElements())
+      {
+        String grp = en.nextElement().toString();
+        if (groupsAdded.contains(grp)) 
+        {
+          continue;
+        }
+        Group g = new Group();
+        g.setName(grp);
+        g.setDisplay(((Boolean)ap.seqPanel.seqCanvas.getFeatureRenderer().featureGroups.get(grp)).booleanValue());
+        fs.addGroup(g);
+        groupsAdded.addElement(grp);
+      }
       jms.setFeatureSettings(fs);
 
     }
@@ -893,7 +960,7 @@ public class Jalview2XML
     seqRefIds = new Hashtable();
     viewportsAdded = new Hashtable();
 
-    Vector gatherToThisFrame = new Vector();
+    Hashtable gatherToThisFrame = new Hashtable();
 
     try
     {
@@ -937,7 +1004,7 @@ public class Jalview2XML
           af = LoadFromObject(object, file, true);
           if (af.viewport.gatherViewsHere)
           {
-            gatherToThisFrame.add(af);
+            gatherToThisFrame.put(af.viewport.getSequenceSetId(), af);
           }
           entryCount++;
         }
@@ -998,10 +1065,11 @@ public class Jalview2XML
       Desktop.instance.stopLoading();
     }
 
-    for (int i = 0; i < gatherToThisFrame.size(); i++)
+    Enumeration en = gatherToThisFrame.elements();
+    while(en.hasMoreElements())
     {
       Desktop.instance.gatherViews(
-          (AlignFrame) gatherToThisFrame.elementAt(i));
+          (AlignFrame) en.nextElement());
     }
 
     return af;
@@ -1062,7 +1130,7 @@ public class Jalview2XML
 
   AlignFrame LoadFromObject(JalviewModel object,
                             String file,
-                            boolean loadTrees)
+                            boolean loadTreesAndStructures)
   {
     SequenceSet vamsasSet = object.getVamsasModel().getSequenceSet(0);
     Sequence[] vamsasSeq = vamsasSet.getSequence();
@@ -1133,6 +1201,7 @@ public class Jalview2XML
 
 
     Hashtable pdbloaded = new Hashtable();
+
     if (!multipleView)
     {
       for (int i = 0; i < vamsasSeq.length; i++)
@@ -1166,6 +1235,7 @@ public class Jalview2XML
             al.getSequenceAt(i).getDatasetSequence().addSequenceFeature(sf);
           }
         }
+
         if (JSEQ[i].getPdbidsCount() > 0)
         {
           Pdbids[] ids = JSEQ[i].getPdbids();
@@ -1179,9 +1249,7 @@ public class Jalview2XML
             {
               if (!pdbloaded.containsKey(ids[p].getFile()))
               {
-                String tmppdb = loadPDBFile(file, ids[p].getId());
-                entry.setFile(tmppdb);
-                pdbloaded.put(ids[p].getId(), tmppdb);
+                entry.setFile(loadPDBFile(file, ids[p].getId()));
               }
               else
               {
@@ -1250,21 +1318,24 @@ public class Jalview2XML
         }
 
         AnnotationElement[] ae = an[i].getAnnotationElement();
-        jalview.datamodel.Annotation[] anot = new jalview.datamodel.Annotation[
-            al.getWidth()];
+        jalview.datamodel.Annotation[] anot = null;
+        if (!an[i].getScoreOnly())
+        { 
+          anot = new jalview.datamodel.Annotation[
+                    al.getWidth()];
 
-        for (int aa = 0; aa < ae.length && aa < anot.length; aa++)
-        {
-          anot[ae[aa].getPosition()] = new jalview.datamodel.Annotation(ae[aa].
-              getDisplayCharacter(),
-              ae[aa].getDescription(),
-              ae[aa].getSecondaryStructure().length() == 0 ? ' ' :
-              ae[aa].getSecondaryStructure().charAt(0),
-                   ae[aa].getValue());
-          anot[ae[aa].getPosition()].colour = new java.awt.Color(ae[aa].
-              getColour());
+          for (int aa = 0; aa < ae.length && aa < anot.length; aa++)
+          {
+            anot[ae[aa].getPosition()] = new jalview.datamodel.Annotation(ae[aa].
+                    getDisplayCharacter(),
+                    ae[aa].getDescription(),
+                    ae[aa].getSecondaryStructure().length() == 0 ? ' ' :
+                      ae[aa].getSecondaryStructure().charAt(0),
+                      ae[aa].getValue());
+            anot[ae[aa].getPosition()].colour = new java.awt.Color(ae[aa].
+                    getColour());
+          }
         }
-
         jalview.datamodel.AlignmentAnnotation jaa = null;
 
         if (an[i].getGraph())
@@ -1305,7 +1376,10 @@ public class Jalview2XML
               );
           al.findName(an[i].getSequenceRef()).addAlignmentAnnotation(jaa);
         }
-
+        if (an[i].hasScore())
+        {
+          jaa.setScore(an[i].getScore());
+        }
         al.addAnnotation(jaa);
       }
     }
@@ -1662,9 +1736,11 @@ public class Jalview2XML
         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());
+        else
+          af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().setOrder(setting.getType(), fs/jms.getFeatureSettings().getSettingCount());
         if (setting.getDisplay())
         {
           af.viewport.featuresDisplayed.put(
@@ -1673,8 +1749,15 @@ public class Jalview2XML
       }
       af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().renderOrder =
           renderOrder;
+      Hashtable fgtable;
+      af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().featureGroups = fgtable=new Hashtable();
+      for (int gs=0;gs<jms.getFeatureSettings().getGroupCount(); gs++) 
+      {
+        Group grp = jms.getFeatureSettings().getGroup(gs);
+        fgtable.put(grp.getName(), new Boolean(grp.getDisplay()));
+      }
     }
-
+    
     if (view.getHiddenColumnsCount() > 0)
     {
       for (int c = 0; c < view.getHiddenColumnsCount(); c++)
@@ -1693,7 +1776,7 @@ public class Jalview2XML
 
     //LOAD TREES
     ///////////////////////////////////////
-    if (loadTrees && jms.getTreeCount() > 0)
+    if (loadTreesAndStructures && jms.getTreeCount() > 0)
     {
       try
       {
@@ -1740,7 +1823,70 @@ public class Jalview2XML
       {
         ex.printStackTrace();
       }
+    }
 
+    ////LOAD STRUCTURES
+    if(loadTreesAndStructures)
+    {
+      for (int i = 0; i < JSEQ.length; i++)
+      {
+        if (JSEQ[i].getPdbidsCount() > 0)
+        {
+          Pdbids[] ids = JSEQ[i].getPdbids();
+          for (int p = 0; p < ids.length; p++)
+          {
+            for (int s = 0; s < ids[p].getStructureStateCount(); s++)
+            {
+              jalview.datamodel.PDBEntry jpdb = new jalview.datamodel.PDBEntry();
+
+              jpdb.setFile(loadPDBFile(ids[p].getFile(), ids[p].getId()));
+              jpdb.setId(ids[p].getId());
+
+              int x = ids[p].getStructureState(s).getXpos();
+              int y = ids[p].getStructureState(s).getYpos();
+              int width = ids[p].getStructureState(s).getWidth();
+              int height = ids[p].getStructureState(s).getHeight();
+
+              java.awt.Component comp = Desktop.desktop.getComponentAt(x, y);
+
+              String pdbFile = loadPDBFile(file, ids[p].getId());
+
+              jalview.datamodel.SequenceI[] seq = new jalview.datamodel.SequenceI[]
+                  {
+                  al.getSequenceAt(i)};
+
+              if (comp == null ||
+                  (comp.getWidth() != width && comp.getHeight() != height))
+              {
+                String state = ids[p].getStructureState(s).getContent();
+
+                StringBuffer newFileLoc = new StringBuffer(state.substring(0,
+                    state.indexOf("\"", state.indexOf("load")) + 1));
+
+                newFileLoc.append(jpdb.getFile());
+                newFileLoc.append(state.substring(
+                    state.indexOf("\"", state.indexOf("load \"") + 6)));
+
+                new AppJMol(pdbFile,
+                            ids[p].getId(),
+                            seq,
+                            af.alignPanel,
+                            newFileLoc.toString(),
+                            new java.awt.Rectangle(x, y, width, height));
+
+              }
+              else if(comp!=null)
+              {
+                StructureSelectionManager.getStructureSelectionManager()
+                    .setMapping(seq, pdbFile,
+                                jalview.io.AppletFormatAdapter.FILE);
+
+                ( (AppJMol) comp).addSequence(seq);
+              }
+            }
+          }
+        }
+      }
     }
 
     return af;