JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / test / jalview / io / Jalview2xmlTests.java
index fbfff74..924dd37 100644 (file)
@@ -1,19 +1,46 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
+ * Copyright (C) 2015 The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview 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 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview 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 Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.io;
 
-import static org.junit.Assert.*;
+import static org.testng.AssertJUnit.assertTrue;
+
+import jalview.api.AlignmentViewPanel;
+import jalview.api.ViewStyleI;
+import jalview.bin.Cache;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
 import jalview.gui.AlignFrame;
 import jalview.gui.Desktop;
+import jalview.gui.Jalview2XML;
 import jalview.schemes.AnnotationColourGradient;
 import jalview.schemes.ColourSchemeI;
 
 import java.io.File;
 
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
+import org.testng.Assert;
+import org.testng.AssertJUnit;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
 
 public class Jalview2xmlTests
 {
@@ -21,10 +48,11 @@ public class Jalview2xmlTests
   /**
    * @throws java.lang.Exception
    */
-  @BeforeClass
+  @BeforeClass(alwaysRun = true)
   public static void setUpBeforeClass() throws Exception
   {
-    jalview.bin.Jalview.main(new String[] {"-props","test/src/jalview/io/testProps.jvprops"});
+    jalview.bin.Jalview.main(new String[] { "-props",
+        "test/jalview/io/testProps.jvprops" });
   }
 
   /**
@@ -34,12 +62,14 @@ public class Jalview2xmlTests
   public static void tearDownAfterClass() throws Exception
   {
     jalview.gui.Desktop.instance.closeAll_actionPerformed(null);
-    
+
   }
+
   public int countDsAnn(jalview.viewmodel.AlignmentViewport avp)
   {
-    int numdsann=0;
-    for (SequenceI sq: avp.getAlignment().getDataset().getSequences()) {
+    int numdsann = 0;
+    for (SequenceI sq : avp.getAlignment().getDataset().getSequences())
+    {
       if (sq.getAnnotation() != null)
       {
         for (AlignmentAnnotation dssa : sq.getAnnotation())
@@ -53,35 +83,52 @@ public class Jalview2xmlTests
     }
     return numdsann;
   }
-  @Test
+
+  @Test(groups = { "Functional" })
   public void testRNAStructureRecovery() throws Exception
   {
     String inFile = "examples/RF00031_folded.stk";
-    String tfile = File.createTempFile("JalviewTest", ".jvp").getAbsolutePath();
-    AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(inFile, FormatAdapter.FILE);
-    assertTrue("Didn't read input file "+inFile, af!=null);
-    int olddsann=countDsAnn(af.getViewport());
-    assertTrue("Didn't find any dataset annotations",olddsann>0);
+    String tfile = File.createTempFile("JalviewTest", ".jvp")
+            .getAbsolutePath();
+    AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
+            inFile, FormatAdapter.FILE);
+    assertTrue("Didn't read input file " + inFile, af != null);
+    int olddsann = countDsAnn(af.getViewport());
+    assertTrue("Didn't find any dataset annotations", olddsann > 0);
     af.rnahelicesColour_actionPerformed(null);
-    assertTrue("Couldn't apply RNA helices colourscheme",af.getViewport().getGlobalColourScheme() instanceof jalview.schemes.RNAHelicesColour);
-    assertTrue("Failed to store as a project.",af.saveAlignment(tfile, "Jalview"));
+    assertTrue(
+            "Couldn't apply RNA helices colourscheme",
+            af.getViewport().getGlobalColourScheme() instanceof jalview.schemes.RNAHelicesColour);
+    assertTrue("Failed to store as a project.",
+            af.saveAlignment(tfile, "Jalview"));
     af.closeMenuItem_actionPerformed(true);
-    af=null;
-    af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(tfile, FormatAdapter.FILE);
-    assertTrue("Failed to import new project", af!=null);
-    int newdsann=countDsAnn(af.getViewport());
-    assertTrue("Differing numbers of dataset sequence annotation\nOriginally "+olddsann+" and now "+newdsann,olddsann==newdsann);
-    System.out.println("Read in same number of annotations as originally present ("+olddsann+")");
-    assertTrue("RNA helices colourscheme was not applied on import.",af.getViewport().getGlobalColourScheme() instanceof jalview.schemes.RNAHelicesColour);
+    af = null;
+    af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(tfile,
+            FormatAdapter.FILE);
+    assertTrue("Failed to import new project", af != null);
+    int newdsann = countDsAnn(af.getViewport());
+    assertTrue(
+            "Differing numbers of dataset sequence annotation\nOriginally "
+                    + olddsann + " and now " + newdsann,
+            olddsann == newdsann);
+    System.out
+            .println("Read in same number of annotations as originally present ("
+                    + olddsann + ")");
+    assertTrue(
+            "RNA helices colourscheme was not applied on import.",
+            af.getViewport().getGlobalColourScheme() instanceof jalview.schemes.RNAHelicesColour);
   }
-  @Test
+
+  @Test(groups = { "Functional" })
   public void testTCoffeeScores() throws Exception
   {
-    String inFile = "examples/uniref50.fa",inAnnot="examples/uniref50.score_ascii";
-    String tfile = File.createTempFile("JalviewTest", ".jvp").getAbsolutePath();
-    AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(inFile, FormatAdapter.FILE);
-    assertTrue("Didn't read input file "+inFile, af!=null);
-    af.loadJalviewDataFile(inAnnot, FormatAdapter.FILE, null,null);
+    String inFile = "examples/uniref50.fa", inAnnot = "examples/uniref50.score_ascii";
+    String tfile = File.createTempFile("JalviewTest", ".jvp")
+            .getAbsolutePath();
+    AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
+            inFile, FormatAdapter.FILE);
+    assertTrue("Didn't read input file " + inFile, af != null);
+    af.loadJalviewDataFile(inAnnot, FormatAdapter.FILE, null, null);
     assertTrue(
             "Didn't set T-coffee colourscheme",
             af.getViewport().getGlobalColourScheme().getClass()
@@ -93,26 +140,40 @@ public class Jalview2xmlTests
                     jalview.schemes.ColourSchemeProperty.getColourName(af
                             .getViewport().getGlobalColourScheme())) != null);
 
-    assertTrue("Failed to store as a project.",af.saveAlignment(tfile, "Jalview"));
+    assertTrue("Failed to store as a project.",
+            af.saveAlignment(tfile, "Jalview"));
     af.closeMenuItem_actionPerformed(true);
-    af=null;
-    af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(tfile, FormatAdapter.FILE);
-    assertTrue("Failed to import new project", af!=null);
-    assertTrue("Didn't set T-coffee colourscheme for imported project.",af.getViewport().getGlobalColourScheme().getClass().equals(jalview.schemes.TCoffeeColourScheme.class));
-    System.out.println("T-Coffee score shading successfully recovered from project.");
+    af = null;
+    af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(tfile,
+            FormatAdapter.FILE);
+    assertTrue("Failed to import new project", af != null);
+    assertTrue(
+            "Didn't set T-coffee colourscheme for imported project.",
+            af.getViewport().getGlobalColourScheme().getClass()
+                    .equals(jalview.schemes.TCoffeeColourScheme.class));
+    System.out
+            .println("T-Coffee score shading successfully recovered from project.");
   }
-  @Test
+
+  @Test(groups = { "Functional" })
   public void testColourByAnnotScores() throws Exception
   {
-    String inFile = "examples/uniref50.fa",inAnnot="examples/testdata/uniref50_iupred.jva";
-    String tfile = File.createTempFile("JalviewTest", ".jvp").getAbsolutePath();
-    AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(inFile, FormatAdapter.FILE);
-    assertTrue("Didn't read input file "+inFile, af!=null);
-    af.loadJalviewDataFile(inAnnot, FormatAdapter.FILE, null,null);
-    AlignmentAnnotation[] aa = af.getViewport().getAlignment().getSequenceAt(0).getAnnotation("IUPredWS (Short)");
-    assertTrue("Didn't find any IUPred annotation to use to shade alignment.",aa!=null && aa.length>0);
-    AnnotationColourGradient cs = new jalview.schemes.AnnotationColourGradient(aa[0], null, AnnotationColourGradient.ABOVE_THRESHOLD);
-    AnnotationColourGradient gcs = new jalview.schemes.AnnotationColourGradient(aa[0], null, AnnotationColourGradient.BELOW_THRESHOLD);
+    String inFile = "examples/uniref50.fa", inAnnot = "examples/testdata/uniref50_iupred.jva";
+    String tfile = File.createTempFile("JalviewTest", ".jvp")
+            .getAbsolutePath();
+    AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
+            inFile, FormatAdapter.FILE);
+    assertTrue("Didn't read input file " + inFile, af != null);
+    af.loadJalviewDataFile(inAnnot, FormatAdapter.FILE, null, null);
+    AlignmentAnnotation[] aa = af.getViewport().getAlignment()
+            .getSequenceAt(0).getAnnotation("IUPredWS (Short)");
+    assertTrue(
+            "Didn't find any IUPred annotation to use to shade alignment.",
+            aa != null && aa.length > 0);
+    AnnotationColourGradient cs = new jalview.schemes.AnnotationColourGradient(
+            aa[0], null, AnnotationColourGradient.ABOVE_THRESHOLD);
+    AnnotationColourGradient gcs = new jalview.schemes.AnnotationColourGradient(
+            aa[0], null, AnnotationColourGradient.BELOW_THRESHOLD);
     cs.setSeqAssociated(true);
     gcs.setSeqAssociated(true);
     af.changeColour(cs);
@@ -124,56 +185,231 @@ public class Jalview2xmlTests
     sg.addSequence(af.getViewport().getAlignment().getSequenceAt(1), false);
     sg.addSequence(af.getViewport().getAlignment().getSequenceAt(2), true);
     af.alignPanel.alignmentChanged();
-    assertTrue("Failed to store as a project.",af.saveAlignment(tfile, "Jalview"));
+    assertTrue("Failed to store as a project.",
+            af.saveAlignment(tfile, "Jalview"));
     af.closeMenuItem_actionPerformed(true);
-    af=null;
-    af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(tfile, FormatAdapter.FILE);
-    assertTrue("Failed to import new project", af!=null);
-    
+    af = null;
+    af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(tfile,
+            FormatAdapter.FILE);
+    assertTrue("Failed to import new project", af != null);
+
     // check for group and alignment colourschemes
-    
-    ColourSchemeI _rcs=af.getViewport().getGlobalColourScheme();
-    ColourSchemeI _rgcs=af.getViewport().getAlignment().getGroups().get(0).cs;
-    assertTrue("Didn't recover global colourscheme",_rcs!=null); 
-    assertTrue("Didn't recover annotation colour global scheme",_rcs instanceof AnnotationColourGradient);
+
+    ColourSchemeI _rcs = af.getViewport().getGlobalColourScheme();
+    ColourSchemeI _rgcs = af.getViewport().getAlignment().getGroups()
+            .get(0).cs;
+    assertTrue("Didn't recover global colourscheme", _rcs != null);
+    assertTrue("Didn't recover annotation colour global scheme",
+            _rcs instanceof AnnotationColourGradient);
     AnnotationColourGradient __rcs = (AnnotationColourGradient) _rcs;
-    assertTrue("Annotation colourscheme wasn't sequence associated",__rcs.isSeqAssociated());
+    assertTrue("Annotation colourscheme wasn't sequence associated",
+            __rcs.isSeqAssociated());
 
-    
-    boolean diffseqcols=false,diffgseqcols=false;
-    SequenceI[] sqs=af.getViewport().getAlignment().getSequencesArray();
-    for (int p=0,pSize=af.getViewport().getAlignment().getWidth();p<pSize && (!diffseqcols || !diffgseqcols);p++)
+    boolean diffseqcols = false, diffgseqcols = false;
+    SequenceI[] sqs = af.getViewport().getAlignment().getSequencesArray();
+    for (int p = 0, pSize = af.getViewport().getAlignment().getWidth(); p < pSize
+            && (!diffseqcols || !diffgseqcols); p++)
     {
-      if (_rcs.findColour(sqs[0].getCharAt(p), p, sqs[0])!=_rcs.findColour(sqs[5].getCharAt(p), p, sqs[5]))
+      if (_rcs.findColour(sqs[0].getCharAt(p), p, sqs[0]) != _rcs
+              .findColour(sqs[5].getCharAt(p), p, sqs[5]))
       {
         diffseqcols = true;
       }
     }
     assertTrue("Got Different sequence colours", diffseqcols);
-    System.out.println("Per sequence colourscheme (Background) successfully applied and recovered.");
-    
-    assertTrue("Didn't recover group colourscheme",_rgcs!=null); 
-    assertTrue("Didn't recover annotation colour group colourscheme",_rgcs instanceof AnnotationColourGradient);
+    System.out
+            .println("Per sequence colourscheme (Background) successfully applied and recovered.");
+
+    assertTrue("Didn't recover group colourscheme", _rgcs != null);
+    assertTrue("Didn't recover annotation colour group colourscheme",
+            _rgcs instanceof AnnotationColourGradient);
     __rcs = (AnnotationColourGradient) _rgcs;
-    assertTrue("Group Annotation colourscheme wasn't sequence associated",__rcs.isSeqAssociated());
-    
-    for (int p=0,pSize=af.getViewport().getAlignment().getWidth();p<pSize && (!diffseqcols || !diffgseqcols);p++)
+    assertTrue("Group Annotation colourscheme wasn't sequence associated",
+            __rcs.isSeqAssociated());
+
+    for (int p = 0, pSize = af.getViewport().getAlignment().getWidth(); p < pSize
+            && (!diffseqcols || !diffgseqcols); p++)
     {
-    if (_rgcs.findColour(sqs[1].getCharAt(p), p, sqs[1])!=_rgcs.findColour(sqs[2].getCharAt(p), p, sqs[2]))
+      if (_rgcs.findColour(sqs[1].getCharAt(p), p, sqs[1]) != _rgcs
+              .findColour(sqs[2].getCharAt(p), p, sqs[2]))
       {
         diffgseqcols = true;
       }
     }
     assertTrue("Got Different group sequence colours", diffgseqcols);
-    System.out.println("Per sequence (Group) colourscheme successfully applied and recovered.");
+    System.out
+            .println("Per sequence (Group) colourscheme successfully applied and recovered.");
   }
-  @Test
+
+  @Test(groups = { "Functional" })
   public void gatherViewsHere() throws Exception
   {
-    int origCount = Desktop.getAlignframes().length;
-    AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded("examples/exampleFile_2_7.jar", FormatAdapter.FILE);
-    assertTrue("Didn't read in the example file correctly.",af!=null);
-    assertTrue("Didn't gather the views in the example file.", Desktop.getAlignframes().length==1+origCount);
-    
+    int origCount = Desktop.getAlignFrames() == null ? 0 : Desktop
+            .getAlignFrames().length;
+    AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
+            "examples/exampleFile_2_7.jar", FormatAdapter.FILE);
+    assertTrue("Didn't read in the example file correctly.", af != null);
+    assertTrue("Didn't gather the views in the example file.",
+            Desktop.getAlignFrames().length == 1 + origCount);
+
+  }
+
+  @Test(groups = { "Functional" })
+  public void viewRefPdbAnnotation() throws Exception
+  {
+    Cache.applicationProperties.setProperty("STRUCT_FROM_PDB",
+            Boolean.TRUE.toString());
+    Cache.applicationProperties.setProperty("ADD_SS_ANN",
+            Boolean.TRUE.toString());
+    AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
+            "examples/exampleFile_2_7.jar", FormatAdapter.FILE);
+    assertTrue("Didn't read in the example file correctly.", af != null);
+    AlignmentViewPanel sps = null;
+    for (AlignmentViewPanel ap : af.alignPanel.alignFrame.getAlignPanels())
+    {
+      if ("Spinach Feredoxin Structure".equals(ap.getViewName()))
+      {
+        sps = ap;
+        break;
+      }
+    }
+    assertTrue("Couldn't find the structure view", sps != null);
+    SequenceI sq = sps.getAlignment().findName("1A70|");
+    AlignmentAnnotation refan = null;
+    for (AlignmentAnnotation ra : sps.getAlignment()
+            .getAlignmentAnnotation())
+    {
+      if (ra.graph != 0)
+      {
+        refan = ra;
+        break;
+      }
+    }
+    assertTrue("Annotation secondary structure not found.", refan != null);
+    assertTrue("Couldn't find 1a70 null chain", sq != null);
+    // compare the manually added temperature factor annotation
+    // to the track automatically transferred from the pdb structure on load
+    for (AlignmentAnnotation ala : sq.getDatasetSequence().getAnnotation())
+    {
+      AlignmentAnnotation alaa;
+      sq.addAlignmentAnnotation(alaa = new AlignmentAnnotation(ala));
+      alaa.adjustForAlignment();
+      if (ala.graph == refan.graph)
+      {
+        for (int p = 0; p < ala.annotations.length; p++)
+        {
+          sq.findPosition(p);
+          try
+          {
+            assertTrue(
+                    "Mismatch at alignment position " + p,
+                    (alaa.annotations[p] == null && refan.annotations[p] == null)
+                            || alaa.annotations[p].value == refan.annotations[p].value);
+          } catch (NullPointerException q)
+          {
+            Assert.fail("Mismatch of alignment annotations at position "
+                    + p + " Ref seq ann: " + refan.annotations[p]
+                    + " alignment " + alaa.annotations[p]);
+          }
+        }
+      }
+    }
+
+  }
+
+  @Test(groups = { "Functional" })
+  public void testCopyViewSettings() throws Exception
+  {
+    AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
+            "examples/exampleFile_2_7.jar", FormatAdapter.FILE);
+    assertTrue("Didn't read in the example file correctly.", af != null);
+    AlignmentViewPanel sps = null, groups = null;
+    for (AlignmentViewPanel ap : af.alignPanel.alignFrame.getAlignPanels())
+    {
+      if ("Spinach Feredoxin Structure".equals(ap.getViewName()))
+      {
+        sps = ap;
+      }
+      if (ap.getViewName().contains("MAFFT"))
+      {
+        groups = ap;
+      }
+    }
+    assertTrue("Couldn't find the structure view", sps != null);
+    assertTrue("Couldn't find the MAFFT view", groups != null);
+
+    ViewStyleI structureStyle = sps.getAlignViewport().getViewStyle();
+    ViewStyleI groupStyle = groups.getAlignViewport().getViewStyle();
+    AssertJUnit.assertFalse(structureStyle.sameStyle(groupStyle));
+
+    groups.getAlignViewport().setViewStyle(structureStyle);
+    AssertJUnit.assertFalse(groupStyle.sameStyle(groups.getAlignViewport()
+            .getViewStyle()));
+    Assert.assertTrue(structureStyle.sameStyle(groups.getAlignViewport()
+            .getViewStyle()));
+
   }
+
+  /**
+   * test store and recovery of expanded views - currently this is disabled
+   * since the Desktop.explodeViews method doesn't seem to result in the views
+   * being expanded to distinct align frames when executed programmatically.
+   * 
+   * @throws Exception
+   */
+  @Test(groups = { "Functional" }, enabled = false)
+  public void testStoreAndRecoverExpandedviews() throws Exception
+  {
+    AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
+            "examples/exampleFile_2_7.jar", FormatAdapter.FILE);
+    assertTrue("Didn't read in the example file correctly.", af != null);
+    String afid = af.getViewport().getSequenceSetId();
+    {
+      final AlignFrame xaf = af;
+      af = null;
+      new Thread(new Runnable()
+      {
+        @Override
+        public void run()
+        {
+          Desktop.instance.explodeViews(xaf);
+        }
+      }).start();
+      Thread.sleep(1000);
+    }
+    // int times = 0;
+    // while (++times < 5 && Desktop.getAlignFrames().length < )
+    // {
+    // Thread.sleep(300);
+    // }
+    int oldviews = Desktop.getAlignFrames().length;
+    Assert.assertEquals(Desktop.getAlignFrames().length,
+            Desktop.getAlignmentPanels(afid).length);
+    File tfile = File.createTempFile("testStoreAndRecoverExpanded", ".jvp");
+    try
+    {
+      new Jalview2XML(false).saveState(tfile);
+    } catch (Error e)
+    {
+      Assert.fail("Didn't save the expanded view state", e);
+    } catch (Exception e)
+    {
+      Assert.fail("Didn't save the expanded view state", e);
+    }
+    Desktop.instance.closeAll_actionPerformed(null);
+    if (Desktop.getAlignFrames() != null)
+    {
+      Assert.assertEquals(Desktop.getAlignFrames().length, 0);
+    }
+    af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
+            tfile.getAbsolutePath(), FormatAdapter.FILE);
+    Assert.assertNotNull(af);
+    Assert.assertEquals(
+            Desktop.getAlignFrames().length,
+            Desktop.getAlignmentPanels(af.getViewport().getSequenceSetId()).length);
+    Assert.assertEquals(
+            oldviews,
+            Desktop.getAlignmentPanels(af.getViewport().getSequenceSetId()).length);
+  }
+
 }