JAL-2110 fixes and tests for adding dbrefs to EMBL sequence and products
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Wed, 6 Jul 2016 09:10:53 +0000 (10:10 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Wed, 6 Jul 2016 09:10:53 +0000 (10:10 +0100)
src/jalview/datamodel/xdb/embl/EmblEntry.java
test/jalview/datamodel/xdb/embl/EmblEntryTest.java

index 67d7059..56b1325 100644 (file)
@@ -187,12 +187,12 @@ public class EmblEntry
    */
   public SequenceI getSequence(String sourceDb, List<SequenceI> peptides)
   {
-    SequenceI dna = new Sequence(sourceDb + "|" + accession,
-            sequence.getSequence());
+    SequenceI dna = makeSequence(sourceDb);
     dna.setDescription(description);
     DBRefEntry retrievedref = new DBRefEntry(sourceDb,
             getSequenceVersion(), accession);
     dna.addDBRef(retrievedref);
+    dna.setSourceDBRef(retrievedref);
     // add map to indicate the sequence is a valid coordinate frame for the
     // dbref
     retrievedref.setMap(new Mapping(null, new int[] { 1, dna.getLength() },
@@ -235,6 +235,17 @@ public class EmblEntry
   }
 
   /**
+   * @param sourceDb
+   * @return
+   */
+  SequenceI makeSequence(String sourceDb)
+  {
+    SequenceI dna = new Sequence(sourceDb + "|" + accession,
+            sequence.getSequence());
+    return dna;
+  }
+
+  /**
    * Extracts coding region and product from a CDS feature and properly decorate
    * it with annotations.
    * 
@@ -322,13 +333,15 @@ public class EmblEntry
     Mapping dnaToProteinMapping = null;
     if (translation != null && proteinName != null && proteinId != null)
     {
+      int translationLength = translation.length();
+
       /*
        * look for product in peptides list, if not found, add it
        */
       product = matcher.findIdMatch(proteinId);
       if (product == null)
       {
-        product = new Sequence(proteinId, translation, 1, translation.length());
+        product = new Sequence(proteinId, translation, 1, translationLength);
         product.setDescription(((proteinName.length() == 0) ? "Protein Product from "
                 + sourceDb
                 : proteinName));
@@ -340,30 +353,32 @@ public class EmblEntry
       // sequence
       if (exons == null || exons.length == 0)
       {
+        /*
+         * workaround until we handle dna location for CDS sequence
+         * e.g. location="X53828.1:60..1058" correctly
+         */
         System.err
                 .println("Implementation Notice: EMBLCDS records not properly supported yet - Making up the CDNA region of this sequence... may be incorrect ("
                         + sourceDb + ":" + getAccession() + ")");
-        if (translation.length() * 3 == (1 - codonStart + dna.getSequence().length))
+        if (translationLength * 3 == (1 - codonStart + dna.getSequence().length))
         {
           System.err
                   .println("Not allowing for additional stop codon at end of cDNA fragment... !");
-          // this might occur for CDS sequences where no features are
-          // marked.
+          // this might occur for CDS sequences where no features are marked
           exons = new int[] { dna.getStart() + (codonStart - 1),
               dna.getEnd() };
           dnaToProteinMapping = new Mapping(product, exons, new int[] { 1,
-              translation.length() },
-                  3, 1);
+              translationLength }, 3, 1);
         }
-        if ((translation.length() + 1) * 3 == (1 - codonStart + dna.getSequence().length))
+        if ((translationLength + 1) * 3 == (1 - codonStart + dna
+                .getSequence().length))
         {
           System.err
                   .println("Allowing for additional stop codon at end of cDNA fragment... will probably cause an error in VAMSAs!");
           exons = new int[] { dna.getStart() + (codonStart - 1),
               dna.getEnd() - 3 };
           dnaToProteinMapping = new Mapping(product, exons, new int[] { 1,
-              translation.length() },
-                  3, 1);
+              translationLength }, 3, 1);
         }
       }
       else
@@ -382,28 +397,37 @@ public class EmblEntry
         else
         {
           // final product length truncation check
-          int[] cdsRanges = adjustForProteinLength(translation.length(), exons);
-          dnaToProteinMapping = new Mapping(product, cdsRanges, new int[] { 1,
-              translation.length() }, 3, 1);
+          int[] cdsRanges = adjustForProteinLength(translationLength, exons);
+          dnaToProteinMapping = new Mapping(product, cdsRanges, new int[] {
+              1, translationLength }, 3, 1);
           if (product != null)
           {
             /*
+             * make xref with mapping from protein to EMBL dna
+             */
+            DBRefEntry proteinToEmblRef = new DBRefEntry(DBRefSource.EMBL,
+                    getSequenceVersion(), proteinId, new Mapping(
+                            dnaToProteinMapping.getMap().getInverse()));
+            product.addDBRef(proteinToEmblRef);
+
+            /*
              * make xref from protein to EMBLCDS; we assume here that the 
              * CDS sequence version is same as dna sequence (?!)
              */
             MapList proteinToCdsMapList = new MapList(new int[] { 1,
-                translation.length() }, new int[] { 1 + (codonStart - 1),
-                (codonStart - 1) + 3 * translation.length() }, 1, 3);
+                translationLength }, new int[] { 1 + (codonStart - 1),
+                (codonStart - 1) + 3 * translationLength }, 1, 3);
             DBRefEntry proteinToEmblCdsRef = new DBRefEntry(
                     DBRefSource.EMBLCDS, getSequenceVersion(), proteinId,
                     new Mapping(proteinToCdsMapList));
             product.addDBRef(proteinToEmblCdsRef);
 
             /*
-             * make xref from protein to EMBLCDSPROTEIN
+             * make 'direct' xref from protein to EMBLCDSPROTEIN
              */
             proteinToEmblProteinRef = new DBRefEntry(proteinToEmblCdsRef);
             proteinToEmblProteinRef.setSource(DBRefSource.EMBLCDSProduct);
+            proteinToEmblProteinRef.setMap(null);
             product.addDBRef(proteinToEmblProteinRef);
           }
         }
@@ -414,8 +438,8 @@ public class EmblEntry
        */
       for (int xint = 0; exons != null && xint < exons.length; xint += 2)
       {
-        SequenceFeature sf = makeCdsFeature(exons, xint, proteinName, proteinId, vals,
-                codonStart);
+        SequenceFeature sf = makeCdsFeature(exons, xint, proteinName,
+                proteinId, vals, codonStart);
         sf.setType(feature.getName()); // "CDS"
         sf.setEnaLocation(feature.getLocation());
         sf.setFeatureGroup(sourceDb);
@@ -437,7 +461,7 @@ public class EmblEntry
          */
         String source = DBRefUtils.getCanonicalName(ref.getSource());
         ref.setSource(source);
-        DBRefEntry proteinToDnaRef = new DBRefEntry(ref.getSource(), ref.getVersion(), ref
+        DBRefEntry proteinDbRef = new DBRefEntry(ref.getSource(), ref.getVersion(), ref
                 .getAccessionId());
         if (source.equals(DBRefSource.UNIPROT))
         {
@@ -469,7 +493,9 @@ public class EmblEntry
               peptides.add(proteinSeq);
             }
             dnaToProteinMapping.setTo(proteinSeq);
-            proteinSeq.addDBRef(proteinToDnaRef);
+            dnaToProteinMapping.setMappedFromId(proteinId);
+            proteinSeq.addDBRef(proteinDbRef);
+            proteinSeq.setSourceDBRef(proteinDbRef);
             ref.setMap(dnaToProteinMapping);
           }
           hasUniprotDbref = true;
@@ -479,7 +505,7 @@ public class EmblEntry
           /*
            * copy feature dbref to our protein product
            */
-          DBRefEntry pref = proteinToDnaRef;
+          DBRefEntry pref = proteinDbRef;
           pref.setMap(null); // reference is direct
           product.addDBRef(pref);
           // Add converse mapping reference
@@ -514,6 +540,7 @@ public class EmblEntry
                 DBRefSource.EMBLCDSProduct, getSequenceVersion(), proteinId);
       }
       product.addDBRef(proteinToEmblProteinRef);
+      product.setSourceDBRef(proteinToEmblProteinRef);
 
       if (dnaToProteinMapping != null
               && dnaToProteinMapping.getTo() != null)
@@ -521,6 +548,7 @@ public class EmblEntry
         DBRefEntry dnaToEmblProteinRef = new DBRefEntry(
                 DBRefSource.EMBLCDSProduct, getSequenceVersion(), proteinId);
         dnaToEmblProteinRef.setMap(dnaToProteinMapping);
+        dnaToProteinMapping.setMappedFromId(proteinId);
         dna.addDBRef(dnaToEmblProteinRef);
       }
     }
index d987e53..4b71417 100644 (file)
@@ -1,12 +1,14 @@
 package jalview.datamodel.xdb.embl;
 
 import static org.testng.AssertJUnit.assertEquals;
+import static org.testng.AssertJUnit.assertNull;
 import static org.testng.AssertJUnit.assertSame;
 
 import jalview.analysis.SequenceIdMatcher;
 import jalview.datamodel.DBRefEntry;
-import jalview.datamodel.Sequence;
+import jalview.datamodel.DBRefSource;
 import jalview.datamodel.SequenceI;
+import jalview.util.MapList;
 
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -22,7 +24,7 @@ public class EmblEntryTest
     EmblEntry testee = new EmblEntry();
 
     /*
-     * Make a (CDS) Feature with 4 locations
+     * Make a (CDS) Feature with 5 locations
      */
     EmblFeature cds = new EmblFeature();
     cds.setLocation("join(10..20,complement(30..40),50..60,70..80,complement(110..120))");
@@ -36,20 +38,22 @@ public class EmblEntryTest
   public void testParseCodingFeature()
   {
     // not the whole sequence but enough for this test...
-    SequenceI dna = new Sequence("J03321", "GGATCCGTAAGTTAGACGAAATT");
     List<SequenceI> peptides = new ArrayList<SequenceI>();
     SequenceIdMatcher matcher = new SequenceIdMatcher(peptides);
     EmblFile ef = EmblTestHelper.getEmblFile();
+    assertEquals(1, ef.getEntries().size());
+    EmblEntry testee = ef.getEntries().get(0);
+    String sourceDb = "EMBL";
+    SequenceI dna = testee.makeSequence(sourceDb);
 
     /*
      * parse three CDS features, with two/one/no Uniprot cross-refs
      */
-    EmblEntry testee = new EmblEntry();
     for (EmblFeature feature : ef.getEntries().get(0).getFeatures())
     {
       if ("CDS".equals(feature.getName()))
       {
-        testee.parseCodingFeature(feature, "EMBL", dna, peptides, matcher);
+        testee.parseCodingFeature(feature, sourceDb, dna, peptides, matcher);
       }
     }
 
@@ -57,7 +61,7 @@ public class EmblEntryTest
      * peptides should now have five entries:
      * EMBL product and two Uniprot accessions for the first CDS / translation
      * EMBL product and one Uniprot accession for the second CDS / "
-     * EMBL product and synthesized EMBLCDSPROTEINaccession for the third
+     * EMBL product only for the third
      */
     assertEquals(6, peptides.size());
     assertEquals("CAA30420.1", peptides.get(0).getName());
@@ -74,63 +78,128 @@ public class EmblEntryTest
     assertEquals("MSS", peptides.get(5).getSequenceAsString());
 
     /*
-     * verify dna sequence has dbrefs with mappings to the peptide 'products'
+     * verify dna sequence has dbrefs with CDS mappings to the peptide 'products'
      */
+    MapList cds1Map = new MapList(new int[] { 57, 46 }, new int[] { 1, 4 },
+            3, 1);
+    MapList cds2Map = new MapList(new int[] { 4, 15 }, new int[] { 1, 4 },
+            3, 1);
+    MapList cds3Map = new MapList(new int[] { 4, 6, 10, 15 }, new int[] {
+        1, 3 }, 3, 1);
     DBRefEntry[] dbrefs = dna.getDBRefs();
     assertEquals(4, dbrefs.length);
     DBRefEntry dbRefEntry = dbrefs[0];
     assertEquals("UNIPROT", dbRefEntry.getSource());
     assertEquals("B0BCM4", dbRefEntry.getAccessionId());
     assertSame(peptides.get(1), dbRefEntry.getMap().getTo());
-    List<int[]> fromRanges = dbRefEntry.getMap().getMap().getFromRanges();
-    assertEquals(1, fromRanges.size());
-    assertEquals(57, fromRanges.get(0)[0]);
-    assertEquals(46, fromRanges.get(0)[1]);
-    List<int[]> toRanges = dbRefEntry.getMap().getMap().getToRanges();
-    assertEquals(1, toRanges.size());
-    assertEquals(1, toRanges.get(0)[0]);
-    assertEquals(4, toRanges.get(0)[1]);
+    assertEquals(cds1Map, dbRefEntry.getMap().getMap());
 
     dbRefEntry = dbrefs[1];
     assertEquals("UNIPROT", dbRefEntry.getSource());
     assertEquals("P0CE20", dbRefEntry.getAccessionId());
     assertSame(peptides.get(2), dbRefEntry.getMap().getTo());
-    fromRanges = dbRefEntry.getMap().getMap().getFromRanges();
-    assertEquals(1, fromRanges.size());
-    assertEquals(57, fromRanges.get(0)[0]);
-    assertEquals(46, fromRanges.get(0)[1]);
-    toRanges = dbRefEntry.getMap().getMap().getToRanges();
-    assertEquals(1, toRanges.size());
-    assertEquals(1, toRanges.get(0)[0]);
-    assertEquals(4, toRanges.get(0)[1]);
+    assertEquals(cds1Map, dbRefEntry.getMap().getMap());
 
     dbRefEntry = dbrefs[2];
     assertEquals("UNIPROT", dbRefEntry.getSource());
     assertEquals("B0BCM3", dbRefEntry.getAccessionId());
     assertSame(peptides.get(4), dbRefEntry.getMap().getTo());
-    fromRanges = dbRefEntry.getMap().getMap().getFromRanges();
-    assertEquals(1, fromRanges.size());
-    assertEquals(4, fromRanges.get(0)[0]);
-    assertEquals(15, fromRanges.get(0)[1]);
-    toRanges = dbRefEntry.getMap().getMap().getToRanges();
-    assertEquals(1, toRanges.size());
-    assertEquals(1, toRanges.get(0)[0]);
-    assertEquals(4, toRanges.get(0)[1]);
+    assertEquals(cds2Map, dbRefEntry.getMap().getMap());
 
     dbRefEntry = dbrefs[3];
     assertEquals("EMBLCDSPROTEIN", dbRefEntry.getSource());
     assertEquals("CAA12345.6", dbRefEntry.getAccessionId());
     assertSame(peptides.get(5), dbRefEntry.getMap().getTo());
-    fromRanges = dbRefEntry.getMap().getMap().getFromRanges();
-    assertEquals(2, fromRanges.size());
-    assertEquals(4, fromRanges.get(0)[0]);
-    assertEquals(6, fromRanges.get(0)[1]);
-    assertEquals(10, fromRanges.get(1)[0]);
-    assertEquals(15, fromRanges.get(1)[1]);
-    toRanges = dbRefEntry.getMap().getMap().getToRanges();
-    assertEquals(1, toRanges.size());
-    assertEquals(1, toRanges.get(0)[0]);
-    assertEquals(3, toRanges.get(0)[1]);
+    assertEquals(cds3Map, dbRefEntry.getMap().getMap());
+
+    /*
+     * verify peptides have dbrefs
+     * - to EMBL sequence (with inverse 1:3 cds mapping)
+     * - to EMBLCDS (with 1:3 mapping)
+     * - direct (no mapping) to other protein accessions
+     */
+    MapList proteinToCdsMap1 = new MapList(new int[] { 1, 4 }, new int[] {
+        1, 12 }, 1, 3);
+    MapList proteinToCdsMap2 = new MapList(new int[] { 1, 3 }, new int[] {
+        1, 9 }, 1, 3);
+
+    // dbrefs for first CDS EMBL product CAA30420.1
+    dbrefs = peptides.get(0).getDBRefs();
+    assertEquals(5, dbrefs.length);
+    assertEquals(DBRefSource.EMBL, dbrefs[0].getSource());
+    assertEquals("CAA30420.1", dbrefs[0].getAccessionId());
+    assertEquals(cds1Map.getInverse(), dbrefs[0].getMap().getMap());
+    assertEquals(DBRefSource.EMBLCDS, dbrefs[1].getSource());
+    assertEquals("CAA30420.1", dbrefs[1].getAccessionId());
+    assertEquals(proteinToCdsMap1, dbrefs[1].getMap().getMap());
+    assertEquals(DBRefSource.EMBLCDSProduct, dbrefs[2].getSource());
+    assertEquals("CAA30420.1", dbrefs[2].getAccessionId());
+    assertNull(dbrefs[2].getMap());
+    assertEquals(new DBRefEntry(DBRefSource.UNIPROT, "2.1", "B0BCM4"),
+            dbrefs[3]);
+    assertNull(dbrefs[3].getMap());
+    assertEquals(new DBRefEntry(DBRefSource.UNIPROT, "0", "P0CE20"),
+            dbrefs[4]);
+    assertNull(dbrefs[4].getMap());
+
+    // dbrefs for first CDS first Uniprot xref
+    dbrefs = peptides.get(1).getDBRefs();
+    assertEquals(2, dbrefs.length);
+    assertEquals(new DBRefEntry(DBRefSource.UNIPROT, "2.1", "B0BCM4"),
+            dbrefs[0]);
+    assertNull(dbrefs[0].getMap());
+    assertEquals(DBRefSource.EMBL, dbrefs[1].getSource());
+    assertEquals("X07547", dbrefs[1].getAccessionId());
+    assertEquals(cds1Map.getInverse(), dbrefs[1].getMap().getMap());
+
+    // dbrefs for first CDS second Uniprot xref
+    dbrefs = peptides.get(2).getDBRefs();
+    assertEquals(2, dbrefs.length);
+    assertEquals(new DBRefEntry(DBRefSource.UNIPROT, "0", "P0CE20"),
+            dbrefs[0]);
+    assertNull(dbrefs[0].getMap());
+    assertEquals(DBRefSource.EMBL, dbrefs[1].getSource());
+    assertEquals("X07547", dbrefs[1].getAccessionId());
+    assertEquals(cds1Map.getInverse(), dbrefs[1].getMap().getMap());
+
+    // dbrefs for second CDS EMBL product CAA30421.1
+    dbrefs = peptides.get(3).getDBRefs();
+    assertEquals(4, dbrefs.length);
+    assertEquals(DBRefSource.EMBL, dbrefs[0].getSource());
+    assertEquals("CAA30421.1", dbrefs[0].getAccessionId());
+    assertEquals(cds2Map.getInverse(), dbrefs[0].getMap().getMap());
+    assertEquals(DBRefSource.EMBLCDS, dbrefs[1].getSource());
+    assertEquals("CAA30421.1", dbrefs[1].getAccessionId());
+    assertEquals(proteinToCdsMap1, dbrefs[1].getMap().getMap());
+    assertEquals(DBRefSource.EMBLCDSProduct, dbrefs[2].getSource());
+    assertEquals("CAA30421.1", dbrefs[2].getAccessionId());
+    assertNull(dbrefs[2].getMap());
+    assertEquals(new DBRefEntry(DBRefSource.UNIPROT, "0", "B0BCM3"),
+            dbrefs[3]);
+    assertNull(dbrefs[3].getMap());
+
+    // dbrefs for second CDS second Uniprot xref
+    dbrefs = peptides.get(4).getDBRefs();
+    assertEquals(2, dbrefs.length);
+    assertEquals(new DBRefEntry(DBRefSource.UNIPROT, "0", "B0BCM3"),
+            dbrefs[0]);
+    assertNull(dbrefs[0].getMap());
+    assertEquals(DBRefSource.EMBL, dbrefs[1].getSource());
+    assertEquals("X07547", dbrefs[1].getAccessionId());
+    assertEquals(cds2Map.getInverse(), dbrefs[1].getMap().getMap());
+
+    // dbrefs for third CDS inferred EMBL product CAA12345.6
+    dbrefs = peptides.get(5).getDBRefs();
+    assertEquals(3, dbrefs.length);
+    assertEquals(DBRefSource.EMBL, dbrefs[0].getSource());
+    assertEquals("CAA12345.6", dbrefs[0].getAccessionId());
+    assertEquals(cds3Map.getInverse(), dbrefs[0].getMap().getMap());
+    assertEquals(DBRefSource.EMBLCDS, dbrefs[1].getSource());
+    assertEquals("CAA12345.6", dbrefs[1].getAccessionId());
+    assertEquals(proteinToCdsMap2, dbrefs[1].getMap().getMap());
+    assertEquals(DBRefSource.EMBLCDSProduct, dbrefs[2].getSource());
+    assertEquals("CAA12345.6", dbrefs[2].getAccessionId());
+    assertNull(dbrefs[2].getMap());
   }
 
   @Test(groups = "Functional")
@@ -161,6 +230,5 @@ public class EmblEntryTest
     // what if exons are too short for protein?
     truncated = EmblEntry.adjustForProteinLength(7, exons);
     assertSame(exons, truncated);
-    // assertEquals("[11, 15, 21, 27]", Arrays.toString(truncated));
   }
 }