JAL-2018 export graduated feature colours correctly
[jalview.git] / test / jalview / io / FeaturesFileTest.java
index 506ee91..81d5b05 100644 (file)
@@ -24,14 +24,11 @@ import static org.testng.AssertJUnit.assertEquals;
 import static org.testng.AssertJUnit.assertFalse;
 import static org.testng.AssertJUnit.assertNotNull;
 import static org.testng.AssertJUnit.assertNull;
-import static org.testng.AssertJUnit.assertSame;
 import static org.testng.AssertJUnit.assertTrue;
-import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals;
 
-import jalview.datamodel.AlignedCodonFrame;
+import jalview.api.FeatureRenderer;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentI;
-import jalview.datamodel.Mapping;
 import jalview.datamodel.SequenceDummy;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
@@ -42,9 +39,7 @@ import jalview.schemes.GraduatedColor;
 import java.awt.Color;
 import java.io.File;
 import java.io.IOException;
-import java.util.Iterator;
 import java.util.Map;
-import java.util.Set;
 
 import org.testng.annotations.Test;
 
@@ -167,7 +162,7 @@ public class FeaturesFileTest
             .getSequenceFeatures();
     assertEquals(1, sfs.length);
     SequenceFeature sf = sfs[0];
-    assertEquals("Iron-sulfur; 2Fe-2S", sf.description);
+    assertEquals("Iron-sulfur,2Fe-2S", sf.description);
     assertEquals(44, sf.begin);
     assertEquals(45, sf.end);
     assertEquals("uniprot", sf.featureGroup);
@@ -256,7 +251,7 @@ public class FeaturesFileTest
     String gffData = "##gff-version 3\n"
             + "FER_CAPAA\tuniprot\tMETAL\t39\t39\t0.0\t.\t.\t"
             + "Note=Iron-sulfur (2Fe-2S);Note=another note;evidence=ECO:0000255|PROSITE-ProRule:PRU00465\n"
-            + "FER1_SOLLC\tuniprot\tPfam\t55\t130\t3.0\t.\t.";
+            + "FER1_SOLLC\tuniprot\tPfam\t55\t130\t3.0\t.\t.\tID=$23";
     FeaturesFile featuresFile = new FeaturesFile(gffData,
             FormatAdapter.PASTE);
     assertTrue("Failed to parse features file",
@@ -268,7 +263,7 @@ public class FeaturesFileTest
     assertEquals(1, sfs.length);
     SequenceFeature sf = sfs[0];
     // description parsed from Note attribute
-    assertEquals("Iron-sulfur (2Fe-2S); another note", sf.description);
+    assertEquals("Iron-sulfur (2Fe-2S),another note", sf.description);
     assertEquals(39, sf.begin);
     assertEquals(39, sf.end);
     assertEquals("uniprot", sf.featureGroup);
@@ -281,7 +276,8 @@ public class FeaturesFileTest
     sfs = al.getSequenceAt(2).getDatasetSequence().getSequenceFeatures();
     assertEquals(1, sfs.length);
     sf = sfs[0];
-    assertEquals("uniprot", sf.description);
+    // ID used for description if available
+    assertEquals("$23", sf.description);
     assertEquals(55, sf.begin);
     assertEquals(130, sf.end);
     assertEquals("uniprot", sf.featureGroup);
@@ -339,8 +335,8 @@ public class FeaturesFileTest
     assertEquals("no sequences extracted from GFF3 file", 2,
             dataset.getHeight());
   
-    SequenceI seq1 = dataset.findName("seq1"), seq2 = dataset
-            .findName("seq2");
+    SequenceI seq1 = dataset.findName("seq1");
+    SequenceI seq2 = dataset.findName("seq2");
     assertNotNull(seq1);
     assertNotNull(seq2);
     assertFalse(
@@ -402,8 +398,7 @@ public class FeaturesFileTest
     assertTrue(
             "Didn't read the alignment into an alignframe from Gff3 File",
             af != null);
-    // FIXME codon mappings are on the alignment but not on the dataset
-    checkDatasetfromSimpleGff3(af.getViewport().getAlignment()/* .getDataset() */);
+    checkDatasetfromSimpleGff3(af.getViewport().getAlignment());
   }
 
   @Test(groups = { "Functional" })
@@ -419,63 +414,72 @@ public class FeaturesFileTest
     checkDatasetfromSimpleGff3(dataset);
   }
 
-  /**
-   * Tests loading exonerate GFF2 output, including 'similarity' alignment
-   * feature, on to sequences
-   */
   @Test(groups = { "Functional" })
-  public void testExonerateImport()
+  public void testPrintJalviewFormat() throws Exception
   {
-    FileLoader loader = new FileLoader(false);
-    AlignFrame af = loader.LoadFileWaitTillLoaded(
-            "examples/testdata/exonerateseqs.fa",
-            FormatAdapter.FILE);
-  
-    af.loadJalviewDataFile("examples/testdata/exonerateoutput.gff",
-            FormatAdapter.FILE, null, null);
-  
+    File f = new File("examples/uniref50.fa");
+    AlignmentI al = readAlignmentFile(f);
+    AlignFrame af = new AlignFrame(al, 500, 500);
+    Map<String, Object> colours = af.getFeatureRenderer()
+            .getFeatureColours();
+    String features = "METAL\tcc9900\n"
+            + "GAMMA-TURN\tred|0,255,255|20.0|95.0|below|66.0\n"
+            + "Pfam\tred\n"
+            + "STARTGROUP\tuniprot\n"
+            + "Iron\tFER_CAPAA\t-1\t39\t39\tMETAL\n"
+            + "Turn\tFER_CAPAA\t-1\t36\t38\tGAMMA-TURN\n"
+            + "<html>Pfam domain<a href=\"http://pfam.sanger.ac.uk/family/PF00111\">Pfam_3_4</a></html>\tFER_CAPAA\t-1\t20\t20\tPfam\n"
+            + "ENDGROUP\tuniprot\n";
+    FeaturesFile featuresFile = new FeaturesFile(features,
+            FormatAdapter.PASTE);
+    featuresFile.parse(al.getDataset(), colours, false);
+
+    /*
+     * first with no features displayed
+     */
+    FeatureRenderer fr = af.alignPanel.getFeatureRenderer();
+    Map<String, Object> visible = fr
+            .getDisplayedFeatureCols();
+    String exported = featuresFile.printJalviewFormat(
+            al.getSequencesArray(), visible);
+    String expected = "No Features Visible";
+    assertEquals(expected, exported);
+
+    /*
+     * set METAL (in uniprot group) and GAMMA-TURN visible, but not Pfam
+     */
+    fr.setVisible("METAL");
+    fr.setVisible("GAMMA-TURN");
+    visible = fr.getDisplayedFeatureCols();
+    exported = featuresFile.printJalviewFormat(al.getSequencesArray(),
+            visible);
+    expected = "METAL\tcc9900\n"
+            + "GAMMA-TURN\tff0000|00ffff|20.0|95.0|below|66.0\n"
+            + "\nSTARTGROUP\tuniprot\n"
+            + "Iron\tFER_CAPAA\t-1\t39\t39\tMETAL\t0.0\n"
+            + "Turn\tFER_CAPAA\t-1\t36\t38\tGAMMA-TURN\t0.0\n"
+            + "ENDGROUP\tuniprot\n";
+    assertEquals(expected, exported);
+
+    /*
+     * now set Pfam visible
+     */
+    fr.setVisible("Pfam");
+    visible = fr.getDisplayedFeatureCols();
+    exported = featuresFile.printJalviewFormat(al.getSequencesArray(),
+            visible);
     /*
-     * verify one mapping to a dummy sequence, one to a real one
+     * note the order of feature types is uncontrolled - derives from
+     * FeaturesDisplayed.featuresDisplayed which is a HashSet
      */
-    Set<AlignedCodonFrame> mappings = af
-            .getViewport().getAlignment().getDataset().getCodonFrames();
-    assertEquals(2, mappings.size());
-    Iterator<AlignedCodonFrame> iter = mappings.iterator();
-
-    // first mapping is to dummy sequence
-    AlignedCodonFrame mapping = iter.next();
-    Mapping[] mapList = mapping.getProtMappings();
-    assertEquals(1, mapList.length);
-    assertTrue(mapList[0].getTo() instanceof SequenceDummy);
-    assertEquals("DDB_G0269124", mapList[0].getTo().getName());
-
-    // second mapping is to a sequence in the alignment
-    mapping = iter.next();
-    mapList = mapping.getProtMappings();
-    assertEquals(1, mapList.length);
-    SequenceI proteinSeq = af.getViewport().getAlignment()
-            .findName("DDB_G0280897");
-    assertSame(proteinSeq.getDatasetSequence(), mapList[0].getTo());
-    assertEquals(1, mapping.getdnaToProt().length);
-
-    // 143 in protein should map to codon [11270, 11269, 11268] in dna
-    int[] mappedRegion = mapList[0].getMap().locateInFrom(143, 143);
-    assertArrayEquals(new int[] { 11270, 11268 }, mappedRegion);
-
-    // 182 in protein should map to codon [11153, 11152, 11151] in dna
-    mappedRegion = mapList[0].getMap().locateInFrom(182, 182);
-    assertArrayEquals(new int[] { 11153, 11151 }, mappedRegion);
-
-    // and the reverse mapping:
-    mappedRegion = mapList[0].getMap().locateInTo(11151, 11153);
-    assertArrayEquals(new int[] { 182, 182 }, mappedRegion);
-
-    // 11150 in dna should _not_ map to protein
-    mappedRegion = mapList[0].getMap().locateInTo(11150, 11150);
-    assertNull(mappedRegion);
-
-    // similarly 183 in protein should _not_ map to dna
-    mappedRegion = mapList[0].getMap().locateInFrom(183, 183);
-    assertNull(mappedRegion);
+    expected = "METAL\tcc9900\n"
+            + "Pfam\tff0000\n"
+            + "GAMMA-TURN\tff0000|00ffff|20.0|95.0|below|66.0\n"
+            + "\nSTARTGROUP\tuniprot\n"
+            + "Iron\tFER_CAPAA\t-1\t39\t39\tMETAL\t0.0\n"
+            + "Turn\tFER_CAPAA\t-1\t36\t38\tGAMMA-TURN\t0.0\n"
+            + "<html>Pfam domain<a href=\"http://pfam.sanger.ac.uk/family/PF00111\">Pfam_3_4</a></html>\tFER_CAPAA\t-1\t20\t20\tPfam\t0.0\n"
+            + "ENDGROUP\tuniprot\n";
+    assertEquals(expected, exported);
   }
 }