3253-omnibus save
[jalview.git] / test / jalview / ext / ensembl / EnsemblSeqProxyTest.java
index 10ecfe0..a4d6d92 100644 (file)
@@ -1,25 +1,54 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ 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.ext.ensembl;
 
-import jalview.datamodel.Alignment;
+import static org.testng.AssertJUnit.assertEquals;
+
 import jalview.datamodel.AlignmentI;
+import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
-import jalview.io.AppletFormatAdapter;
+import jalview.gui.JvOptionPane;
+import jalview.io.DataSourceType;
 import jalview.io.FastaFile;
-import jalview.io.FileParse;
+import jalview.io.gff.SequenceOntologyFactory;
+import jalview.io.gff.SequenceOntologyLite;
 
 import java.lang.reflect.Method;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.util.Arrays;
-import java.util.List;
 
 import org.testng.Assert;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 
-
 public class EnsemblSeqProxyTest
 {
+
+  @BeforeClass(alwaysRun = true)
+  public void setUpJvOptionPane()
+  {
+    JvOptionPane.setInteractiveMode(false);
+    JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
+  }
+
   private static final Object[][] allSeqs = new Object[][] {
       {
           new EnsemblProtein(),
@@ -91,7 +120,11 @@ public class EnsemblSeqProxyTest
                   + "LKKALMMRGLIPECCAVYRIQDGEKKPIGWDTDISWLTGEELHVEVLENVPLTTHNFVRK\n"
                   + "TFFTLAFCDFCRKLLFQGFRCQTCGYKFHQRCSTEVPLMCVNYDQLDLLFVSKFFEHHPI\n"
                   + "PQEEASLAETALTSGSSPSAPASDSIGPQILTSPSPSKSIPIPQPFRPADEDHRNQFGQR\n"
-                  + "DRSSSAPNVHINTIEPVNIDDLIRDQGFRGDGGSTTGLSATPPASLPGSLTNVKALQKSP\n"
+                  + "DRSSSAPNVHINTIEPVNIDDLIRDQGFRGDG\n"
+                  // ? insertion added in ENSP00000288602.11, not in P15056
+                  + "APLNQLMRCLRKYQSRTPSPLLHSVPSEIVFDFEPGPVFR\n"
+                  // end insertion
+                  + "GSTTGLSATPPASLPGSLTNVKALQKSP\n"
                   + "GPQRERKSSSSSEDRNRMKTLGRRDSSDDWEIPDGQITVGQRIGSGSFGTVYKGKWHGDV\n"
                   + "AVKMLNVTAPTPQQLQAFKNEVGVLRKTRHVNILLFMGYSTKPQLAIVTQWCEGSSLYHH\n"
                   + "LHIIETKFEMIKLIDIARQTAQGMDYLHAKSIIHRDLKSNNIFLHEDLTVKIGDFGLATV\n"
@@ -99,20 +132,16 @@ public class EnsemblSeqProxyTest
                   + "NRDQIIFMVGRGYLSPDLSKVRSNCPKAMKRLMAECLKKKRDERPLFPQILASIELLARS\n"
                   + "LPKIHRSASEPSLNRAGFQTEDFSLYACASPKTPIQAGGYGAFPVH" } };
 
-  @DataProvider(name = "queries")
-  public Object[][] createQueryData(Method m)
+  @BeforeClass(alwaysRun = true)
+  public void setUp()
   {
-    return new Object[][] { { "CCDS5863.1" }, { "ENSP00000288602" } };
+    SequenceOntologyFactory.setSequenceOntology(new SequenceOntologyLite());
   }
 
-  @Test(dataProvider = "queries")
-  public void testIsValidReference(String query) throws Exception
+  @AfterClass(alwaysRun = true)
+  public void tearDown()
   {
-    EnsemblSequenceFetcher esq = new EnsemblProtein();
-    Assert.assertTrue(esq.isValidReference(query),
-            "Expected reference string " + query
-                    + " to be valid for regex "
-                    + esq.getAccessionValidator().toString());
+    SequenceOntologyFactory.setSequenceOntology(null);
   }
 
   @DataProvider(name = "ens_seqs")
@@ -123,23 +152,21 @@ public class EnsemblSeqProxyTest
   }
 
   @Test(dataProvider = "ens_seqs", suiteName = "live")
-  public void testGetOneSeqs(EnsemblRestClient proxy, String sq, String fastasq)
-          throws Exception
+  public void testGetSequenceRecords(EnsemblSeqProxy proxy, String sq,
+          String fastasq) throws Exception
   {
-    FileParse fp = proxy.getSequenceReader(Arrays
-            .asList(new String[]
-    { sq }));
-    SequenceI[] sqs = new FastaFile(fp).getSeqsAsArray();
-    FastaFile trueRes = new FastaFile(fastasq, AppletFormatAdapter.PASTE);
-    SequenceI[] trueSqs = trueRes.getSeqsAsArray();
-    Assert.assertEquals(sqs.length, trueSqs.length,
+    FastaFile trueRes = new FastaFile(fastasq, DataSourceType.PASTE);
+    SequenceI[] expected = trueRes.getSeqsAsArray();
+    AlignmentI retrieved = proxy.getSequenceRecords(sq);
+
+    Assert.assertEquals(retrieved.getHeight(), expected.length,
             "Different number of sequences retrieved for query " + sq);
-    Alignment ral = new Alignment(sqs);
-    for (SequenceI tr : trueSqs)
+
+    for (SequenceI tr : expected)
     {
       SequenceI[] rseq;
       Assert.assertNotNull(
-              rseq = ral.findSequenceMatch(tr.getName()),
+              rseq = retrieved.findSequenceMatch(tr.getName()),
               "Couldn't find sequences matching expected sequence "
                       + tr.getName());
       Assert.assertEquals(rseq.length, 1,
@@ -150,63 +177,54 @@ public class EnsemblSeqProxyTest
               "Sequences differ for " + tr.getName() + "\n" + "Exp:"
                       + tr.getSequenceAsString() + "\n" + "Got:"
                       + rseq[0].getSequenceAsString());
-  
     }
   }
 
-  @Test(suiteName = "live")
-  public void testLiveCheckEnsembl()
+  @Test(groups = "Functional")
+  public void getGenomicRangesFromFeatures()
   {
-    EnsemblRestClient sf = new EnsemblRestClient()
-    {
-
-      @Override
-      public String getDbName()
-      {
-        // TODO Auto-generated method stub
-        return null;
-      }
 
-      @Override
-      public AlignmentI getSequenceRecords(String queries) throws Exception
-      {
-        // TODO Auto-generated method stub
-        return null;
-      }
-
-      @Override
-      protected URL getUrl(List<String> ids) throws MalformedURLException
-      {
-        // TODO Auto-generated method stub
-        return null;
-      }
-
-      @Override
-      protected boolean useGetRequest()
-      {
-        // TODO Auto-generated method stub
-        return false;
-      }
+  }
 
-      @Override
-      protected String getRequestMimeType(boolean b)
-      {
-        // TODO Auto-generated method stub
-        return null;
-      }
+  /**
+   * Test the method that appends a single allele's reverse complement to a
+   * string buffer
+   */
+  @Test(groups = "Functional")
+  public void testReverseComplementAllele()
+  {
+    StringBuilder sb = new StringBuilder();
+    EnsemblSeqProxy.reverseComplementAllele(sb, "G"); // comp=C
+    EnsemblSeqProxy.reverseComplementAllele(sb, "g"); // comp=c
+    EnsemblSeqProxy.reverseComplementAllele(sb, "C"); // comp=G
+    EnsemblSeqProxy.reverseComplementAllele(sb, "T"); // comp=A
+    EnsemblSeqProxy.reverseComplementAllele(sb, "A"); // comp=T
+    assertEquals("C,c,G,A,T", sb.toString());
+
+    sb = new StringBuilder();
+    EnsemblSeqProxy.reverseComplementAllele(sb, "-GATt"); // revcomp=aATC-
+    EnsemblSeqProxy.reverseComplementAllele(sb, "hgmd_mutation");
+    EnsemblSeqProxy.reverseComplementAllele(sb, "PhenCode_variation");
+    assertEquals("aATC-,hgmd_mutation,PhenCode_variation", sb.toString());
+  }
 
-      @Override
-      protected String getResponseMimeType()
-      {
-        // TODO Auto-generated method stub
-        return null;
-      }
-
-    };
-    boolean isAvailable = sf.isEnsemblAvailable();
-    System.out.println("Ensembl is "
-            + (isAvailable ? "UP!"
-                    : "DOWN or unreachable ******************* BAD!"));
+  /**
+   * Test the method that computes the reverse complement of the alleles in a
+   * sequence_variant feature
+   */
+  @Test(groups = "Functional")
+  public void testReverseComplementAlleles()
+  {
+    String alleles = "C,G,-TAC,HGMD_MUTATION,gac";
+    SequenceFeature sf = new SequenceFeature("sequence_variant", alleles,
+            1, 2, 0f, null);
+    sf.setValue("alleles", alleles);
+
+    EnsemblSeqProxy.reverseComplementAlleles(sf);
+    String revcomp = "G,C,GTA-,HGMD_MUTATION,gtc";
+    // verify description is updated with reverse complement
+    assertEquals(revcomp, sf.getDescription());
+    // verify alleles attribute is updated with reverse complement
+    assertEquals(revcomp, sf.getValue("alleles"));
   }
-  // todo lots of tests
-}
\ No newline at end of file
+}