visibility setting
[jalview.git] / src / jalview / gui / Jalview2XML.java
index b6e6041..061dfca 100755 (executable)
@@ -51,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,8 +209,20 @@ public class Jalview2XML
 
     vamsasSet.setGapChar(jal.getGapCharacter() + "");
 
+    if(jal.getProperties()!=null)
+    {
+      Enumeration en = jal.getProperties().keys();
+      while(en.hasMoreElements())
+      {
+        String key = en.nextElement().toString();
+        SequenceSetProperties ssp = new SequenceSetProperties();
+        ssp.setKey(key);
+        ssp.setValue(jal.getProperties().get(key).toString());
+        vamsasSet.addSequenceSetProperties(ssp);
+      }
+    }
+
     JSeq jseq;
-    Vector pdbfiles = null;
 
     //SAVE SEQUENCES
     int id = 0;
@@ -335,17 +351,18 @@ public class Jalview2XML
           pdb.setId(entry.getId());
           pdb.setType(entry.getType());
 
-          AppJMol jmol;
+          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)
+            if (frames[f] instanceof AppJmol)
             {
-              jmol = (AppJMol) frames[f];
+              jmol = (AppJmol) frames[f];
               if (!jmol.pdbentry.getId().equals(entry.getId()))
                 continue;
 
+
               StructureState state = new StructureState();
               state.setVisible(true);
               state.setXpos(jmol.getX());
@@ -353,8 +370,11 @@ public class Jalview2XML
               state.setWidth(jmol.getWidth());
               state.setHeight(jmol.getHeight());
 
-              state.setContent(jmol.viewer.getStateInfo().replaceAll("\n", ""));
-
+              String statestring = jmol.viewer.getStateInfo();
+              if(state!=null)
+              {
+                state.setContent(statestring.replaceAll("\n", ""));
+              }
               for (int s = 0; s < jmol.sequence.length; s++)
               {
                 if (jal.findIndex(jmol.sequence[s]) > -1)
@@ -500,6 +520,8 @@ public class Jalview2XML
           continue;
         }
 
+        an.setVisible(aa[i].visible);
+
         an.setDescription(aa[i].description);
 
         if (aa[i].sequenceRef != null)
@@ -527,32 +549,47 @@ 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();
+            if (aa[i].annotations[a].description != null)
+              ae.setDescription(aa[i].annotations[a].description);
+            if(aa[i].annotations[a].displayCharacter!=null)
+              ae.setDisplayCharacter(aa[i].annotations[a].displayCharacter);
 
-          if (aa[i].annotations[a].colour != java.awt.Color.black)
-          {
-            ae.setColour(aa[i].annotations[a].colour.getRGB());
-          }
+            if (!Float.isNaN(aa[i].annotations[a].value))
+              ae.setValue(aa[i].annotations[a].value);
 
-          an.addAnnotationElement(ae);
-        }
+            ae.setPosition(a);
+            if (aa[i].annotations[a].secondaryStructure != ' '
+                && aa[i].annotations[a].secondaryStructure != '\0')
+              ae.setSecondaryStructure(aa[i].annotations[a].secondaryStructure
+                                       + "");
+
+            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 {
+          an.setScoreOnly(true);
+        }
         vamsasSet.addAnnotation(an);
       }
     }
@@ -758,7 +795,7 @@ public class Jalview2XML
         fs.addSetting(setting);
         settingsAdded.addElement(renderOrder[ro]);
       }
-      
+
       //Make sure we save none displayed feature settings
       Enumeration en =
           ap.seqPanel.seqCanvas.getFeatureRenderer().featureColours.keys();
@@ -778,7 +815,7 @@ public class Jalview2XML
 
         setting.setDisplay(false);
         float rorder = ap.seqPanel.seqCanvas.getFeatureRenderer().getOrder(key);
-        if (rorder>-1) 
+        if (rorder>-1)
         {
           setting.setOrder(rorder);
         }
@@ -786,11 +823,11 @@ 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();
-        if (groupsAdded.contains(grp)) 
+        if (groupsAdded.contains(grp))
         {
           continue;
         }
@@ -951,6 +988,8 @@ public class Jalview2XML
 
     Hashtable gatherToThisFrame = new Hashtable();
 
+    String errorMessage = null;
+
     try
     {
       //UNMARSHALLER SEEMS TO CLOSE JARINPUTSTREAM, MOST ANNOYING
@@ -1005,22 +1044,19 @@ public class Jalview2XML
       }
       while (jarentry != null);
     }
+    catch(java.io.FileNotFoundException ex)
+    {
+      ex.printStackTrace();
+      errorMessage = "Couldn't locate Jalview XML file : "+file;
+      System.err.println("Exception whilst loading jalview XML file : " +
+                         ex + "\n");
+    }
     catch (java.net.UnknownHostException ex)
     {
       ex.printStackTrace();
-      System.err.println("Couldn't locate Jalview XML file : " +
+      errorMessage = "Couldn't locate Jalview XML file : " +file;
+      System.err.println("Exception whilst loading jalview XML file : " +
                          ex + "\n");
-
-      javax.swing.SwingUtilities.invokeLater(new Runnable()
-      {
-        public void run()
-        {
-          JOptionPane.showInternalMessageDialog(Desktop.desktop,
-                                                "Couldn't locate " + file,
-                                                "URL not found",
-                                                JOptionPane.WARNING_MESSAGE);
-        }
-      });
     }
     catch (Exception ex)
     {
@@ -1036,17 +1072,6 @@ public class Jalview2XML
 
       System.err.println("Exception whilst loading jalview XML file : " +
                          ex + "\n");
-      javax.swing.SwingUtilities.invokeLater(new Runnable()
-      {
-        public void run()
-        {
-
-          JOptionPane.showInternalMessageDialog(Desktop.desktop,
-                                                "Error loading  " + file,
-                                                "Error loading Jalview file",
-                                                JOptionPane.WARNING_MESSAGE);
-        }
-      });
     }
 
     if (Desktop.instance != null)
@@ -1061,6 +1086,21 @@ public class Jalview2XML
           (AlignFrame) en.nextElement());
     }
 
+    if(errorMessage!=null)
+    {
+      final String finalErrorMessage = errorMessage;
+      javax.swing.SwingUtilities.invokeLater(new Runnable()
+      {
+        public void run()
+        {
+          JOptionPane.showInternalMessageDialog(Desktop.desktop,
+                                                finalErrorMessage,
+                                                "Error loading Jalview file",
+                                                JOptionPane.WARNING_MESSAGE);
+        }
+      });
+    }
+
     return af;
   }
 
@@ -1185,6 +1225,13 @@ public class Jalview2XML
     jalview.datamodel.Alignment al =
         new jalview.datamodel.Alignment(orderedSeqs);
 
+    for(int i=0; i<vamsasSet.getSequenceSetPropertiesCount(); i++ )
+    {
+      SequenceSetProperties ssp = vamsasSet.getSequenceSetProperties(i);
+      al.setProperty(ssp.getKey(), ssp.getValue());
+    }
+
+
     al.setDataset(null);
     /////////////////////////////////
 
@@ -1298,30 +1345,45 @@ public class Jalview2XML
         if (an[i].getId() != null
             && annotationIds.containsKey(an[i].getId()))
         {
-          al.addAnnotation(
+          jalview.datamodel.AlignmentAnnotation jda =
               (jalview.datamodel.AlignmentAnnotation) annotationIds.get(an[i].
-              getId())
-              );
+              getId());
+          if (an[i].hasVisible())
+            jda.visible = an[i].getVisible();
+
+          al.addAnnotation(jda);
 
           continue;
         }
 
         AnnotationElement[] ae = an[i].getAnnotationElement();
-        jalview.datamodel.Annotation[] anot = new jalview.datamodel.Annotation[
-            al.getWidth()];
+        jalview.datamodel.Annotation[] anot = null;
 
-        for (int aa = 0; aa < ae.length && aa < anot.length; aa++)
+        if (!an[i].getScoreOnly())
         {
-          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());
-        }
+          anot = new jalview.datamodel.Annotation[
+                    al.getWidth()];
 
+          for (int aa = 0; aa < ae.length && aa < anot.length; aa++)
+          {
+            if(ae[aa].getPosition()>=anot.length)
+              continue;
+
+            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()
+
+                            );
+
+
+              anot[ae[aa].getPosition()].colour = new java.awt.Color(ae[aa].
+                  getColour());
+          }
+        }
         jalview.datamodel.AlignmentAnnotation jaa = null;
 
         if (an[i].getGraph())
@@ -1357,12 +1419,22 @@ public class Jalview2XML
 
         if (an[i].getSequenceRef() != null)
         {
-          jaa.createSequenceMapping(
-              al.findName(an[i].getSequenceRef()), 1, true
-              );
-          al.findName(an[i].getSequenceRef()).addAlignmentAnnotation(jaa);
+          if (al.findName(an[i].getSequenceRef()) != null)
+          {
+            jaa.createSequenceMapping(
+                al.findName(an[i].getSequenceRef()), 1, true
+                );
+            al.findName(an[i].getSequenceRef()).addAlignmentAnnotation(jaa);
+          }
+        }
+        if (an[i].hasScore())
+        {
+          jaa.setScore(an[i].getScore());
         }
 
+        if(an[i].hasVisible())
+          jaa.visible =  an[i].getVisible();
+
         al.addAnnotation(jaa);
       }
     }
@@ -1734,13 +1806,13 @@ public class Jalview2XML
           renderOrder;
       Hashtable fgtable;
       af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().featureGroups = fgtable=new Hashtable();
-      for (int gs=0;gs<jms.getFeatureSettings().getGroupCount(); gs++) 
+      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++)
@@ -1830,16 +1902,36 @@ public class Jalview2XML
               int width = ids[p].getStructureState(s).getWidth();
               int height = ids[p].getStructureState(s).getHeight();
 
-              java.awt.Component comp = Desktop.desktop.getComponentAt(x, y);
+              java.awt.Component comp = null;
+
+              JInternalFrame [] frames = Desktop.desktop.getAllFrames();
+              for(int f=0; f<frames.length; f++)
+              {
+                if(frames[f] instanceof AppJmol)
+                {
+                  if (frames[f].getX() == x && frames[f].getY() == y
+                      && frames[f].getHeight() == height
+                      && frames[f].getWidth() == width)
+                  {
+                    comp = frames[f];
+                    break;
+                  }
+                }
+              }
+
+
+              Desktop.desktop.getComponentAt(x, y);
 
               String pdbFile = loadPDBFile(file, ids[p].getId());
 
-              jalview.datamodel.SequenceI[] seq = new jalview.datamodel.SequenceI[]
+              jalview.datamodel.SequenceI[] seq = new jalview.datamodel.
+                  SequenceI[]
                   {
-                  al.getSequenceAt(i)};
+                   (jalview.datamodel.SequenceI)
+                  seqRefIds.get(JSEQ[i].getId()+"")};
+
 
-              if (comp == null ||
-                  (comp.getWidth() != width && comp.getHeight() != height))
+              if (comp == null)
               {
                 String state = ids[p].getStructureState(s).getContent();
 
@@ -1850,7 +1942,7 @@ public class Jalview2XML
                 newFileLoc.append(state.substring(
                     state.indexOf("\"", state.indexOf("load \"") + 6)));
 
-                new AppJMol(pdbFile,
+                new AppJmol(pdbFile,
                             ids[p].getId(),
                             seq,
                             af.alignPanel,
@@ -1861,10 +1953,10 @@ public class Jalview2XML
               else if(comp!=null)
               {
                 StructureSelectionManager.getStructureSelectionManager()
-                    .setMapping(seq, pdbFile,
+                    .setMapping(seq, null, pdbFile,
                                 jalview.io.AppletFormatAdapter.FILE);
 
-                ( (AppJMol) comp).addSequence(seq);
+                ( (AppJmol) comp).addSequence(seq);
               }
             }
           }